4 * Define the main (emu) GTK window, along with its menubars,
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <sys/types.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <X11/Xutil.h>
34 #include "dreamcast.h"
36 #include "gdrom/gdrom.h"
37 #include "gtkui/gtkui.h"
40 struct main_window_info {
46 GtkActionGroup *actions;
49 int32_t mouse_x, mouse_y;
53 /******************** Video window **************************/
55 #if !(GTK_CHECK_VERSION(2,8,0))
56 void gdk_display_warp_pointer (GdkDisplay *display,
63 * Adjust the mouse pointer so that it appears in the center of the video
64 * window. Mainly used for when we have the mouse grab
66 void video_window_center_pointer( main_window_t win )
68 GdkDisplay *display = gtk_widget_get_display(win->video);
69 GdkScreen *screen = gtk_widget_get_screen(win->video);
73 gdk_window_get_origin(win->video->window, &x, &y);
74 gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
78 gdk_display_warp_pointer( display, screen, x, y );
79 win->mouse_x = width/2;
80 win->mouse_y = height/2;
84 * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
85 * moved to the centre of the window.
87 * @param win The window receiving the grab
88 * @return TRUE if the grab was successful, FALSE on failure.
90 gboolean video_window_grab_display( main_window_t win )
92 GdkWindow *gdkwin = win->video->window;
93 GdkColor color = { 0,0,0,0 };
94 char bytes[32]; /* 16 * 16 / 8 */
96 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bytes, 16, 16);
97 GdkCursor *cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 16, 16);
98 gdk_pixmap_unref(pixmap);
101 gdk_pointer_grab( gdkwin, FALSE,
102 GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK,
103 gdkwin, cursor, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
104 gdk_cursor_unref(cursor);
106 success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
108 gdk_pointer_ungrab(GDK_CURRENT_TIME);
111 win->is_grabbed = success;
112 main_window_set_running(win, dreamcast_is_running());
117 * Release the display grab.
119 void video_window_ungrab_display( main_window_t win )
121 gdk_pointer_ungrab(GDK_CURRENT_TIME);
122 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
123 win->is_grabbed = FALSE;
124 main_window_set_running(win, dreamcast_is_running());
127 static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
130 main_window_t win = (main_window_t)user_data;
131 int32_t x = (int32_t)event->x;
132 int32_t y = (int32_t)event->y;
133 if( win->is_grabbed &&
134 (x != win->mouse_x || y != win->mouse_y) ) {
135 input_event_mousemove( x - win->mouse_x, y - win->mouse_y, FALSE );
136 video_window_center_pointer(win);
139 gdk_drawable_get_size(GDK_DRAWABLE(widget->window), &width, &height);
140 input_event_mousemove( x * DISPLAY_WIDTH /width , y * DISPLAY_HEIGHT / height, TRUE );
145 static gboolean on_video_window_mouse_exited( GtkWidget *widget, GdkEventCrossing *event,
148 main_window_t win = (main_window_t)user_data;
149 if( !win->is_grabbed ) {
150 input_event_mousemove( -1, -1, TRUE );
155 static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
158 main_window_t win = (main_window_t)user_data;
159 if( win->is_grabbed ) {
160 input_event_mousedown( event->button-1, 0, 0, FALSE );
163 gdk_drawable_get_size(GDK_DRAWABLE(widget->window), &width, &height);
164 input_event_mousedown( event->button-1, (int32_t)event->x * DISPLAY_WIDTH / width,
165 (int32_t)event->y * DISPLAY_HEIGHT / height, TRUE );
170 static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
173 main_window_t win = (main_window_t)user_data;
174 if( win->is_grabbed ) {
175 input_event_mouseup( event->button-1, 0, 0, FALSE );
176 } else if( win->use_grab) {
177 video_window_grab_display(win);
180 gdk_drawable_get_size(GDK_DRAWABLE(widget->window), &width, &height);
181 input_event_mouseup( event->button-1, (int32_t)event->x * DISPLAY_WIDTH / width,
182 (int32_t)event->y * DISPLAY_HEIGHT / height, TRUE );
187 static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
190 main_window_t win = (main_window_t)user_data;
191 if( win->is_grabbed ) {
193 /* On OSX, use the command key rather than ctrl-alt. Mainly because GTK/OSX
194 * doesn't seem to be able to get ctrl-alt reliably
196 if( event->keyval == GDK_Meta_L || event->keyval == GDK_Meta_R ) {
197 video_window_ungrab_display(win);
201 /* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
202 * a singly-modified keypress rather than a double-modified 'null' press,
203 * so we have to do a little more work.
204 * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
207 int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
208 int state = event->state & gtk_accelerator_get_default_mod_mask();
209 if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
210 (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
211 video_window_ungrab_display(win);
212 // Consume the keypress, DC doesn't get it.
217 input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
221 static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
224 input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
228 static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
231 display_set_focused(event->in);
235 /*************************** Main window (frame) ******************************/
237 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
239 dreamcast_shutdown();
243 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state,
246 main_window_t win = (main_window_t)userdata;
247 if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
248 gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
249 GtkWidget *frame = gtk_widget_get_parent(win->video);
250 if( frame->style == NULL ) {
251 gtk_widget_set_style( frame, gtk_style_new() );
254 gtk_widget_hide( win->menubar );
255 gtk_widget_hide( win->toolbar );
256 gtk_widget_hide( win->statusbar );
258 frame->style->xthickness = 0;
259 frame->style->ythickness = 0;
261 frame->style->xthickness = 2;
262 frame->style->ythickness = 2;
263 gtk_widget_show( win->menubar );
264 gtk_widget_show( win->toolbar );
265 gtk_widget_show( win->statusbar );
267 gtk_widget_queue_draw( win->window );
271 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
272 GtkAccelGroup *accel_group )
276 main_window_t win = g_malloc0( sizeof(struct main_window_info) );
278 win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
279 win->menubar = menubar;
280 win->toolbar = toolbar;
281 win->use_grab = FALSE;
282 win->is_grabbed = FALSE;
283 gtk_window_set_title( GTK_WINDOW(win->window), title );
284 gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
286 gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
288 win->video = video_gtk_create_drawable();
289 gtk_widget_set_size_request( win->video, 640, 480 );
290 gtk_widget_set_double_buffered( win->video, FALSE );
291 frame = gtk_frame_new(NULL);
292 gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
293 gtk_container_add( GTK_CONTAINER(frame), win->video );
295 win->statusbar = gtk_statusbar_new();
297 vbox = gtk_vbox_new(FALSE, 0);
298 gtk_container_add( GTK_CONTAINER(win->window), vbox );
299 gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
300 gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
301 gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
302 gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
303 gtk_widget_show_all( win->window );
304 gtk_widget_grab_focus( win->video );
306 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
308 g_signal_connect( win->window, "delete_event",
309 G_CALLBACK(on_main_window_deleted), win );
310 g_signal_connect( win->window, "window-state-event",
311 G_CALLBACK(on_main_window_state_changed), win );
313 g_signal_connect( win->video, "key-press-event",
314 G_CALLBACK(on_video_window_key_pressed), win );
315 g_signal_connect( win->video, "key-release-event",
316 G_CALLBACK(on_video_window_key_released), win );
317 g_signal_connect( win->video, "motion-notify-event",
318 G_CALLBACK(on_video_window_mouse_motion), win );
319 g_signal_connect( win->video, "leave-notify-event",
320 G_CALLBACK(on_video_window_mouse_exited), win );
321 g_signal_connect( win->video, "button-press-event",
322 G_CALLBACK(on_video_window_mouse_pressed), win );
323 g_signal_connect( win->video, "button-release-event",
324 G_CALLBACK(on_video_window_mouse_released), win );
325 g_signal_connect( win->video, "focus-in-event",
326 G_CALLBACK(on_video_window_focus_changed), win);
327 g_signal_connect( win->video, "focus-out-event",
328 G_CALLBACK(on_video_window_focus_changed), win);
330 gtk_widget_add_events( win->video,
331 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
332 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
333 GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK |
334 GDK_LEAVE_NOTIFY_MASK );
339 void main_window_set_status_text( main_window_t win, char *text )
341 gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
342 if( win->is_grabbed ) {
345 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <command> to release grab)") );
347 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
349 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
351 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
355 void main_window_set_running( main_window_t win, gboolean running )
357 char *text = running ? _("Running") : _("Stopped");
358 gtk_gui_enable_action( "Pause", running );
359 gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
360 main_window_set_status_text( win, text );
363 void main_window_set_framerate( main_window_t win, float rate )
369 void main_window_set_speed( main_window_t win, double speed )
373 snprintf( buf, 32, "Running (%2.4f%%)", speed );
374 main_window_set_status_text( win, buf );
377 GtkWidget *main_window_get_renderarea( main_window_t win )
382 GtkWindow *main_window_get_frame( main_window_t win )
384 return GTK_WINDOW(win->window);
387 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
390 gtk_window_fullscreen( GTK_WINDOW(win->window) );
392 gtk_window_unfullscreen( GTK_WINDOW(win->window) );
396 void main_window_set_use_grab( main_window_t win, gboolean use_grab )
398 if( use_grab != win->use_grab ) {
400 GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
401 gdk_window_set_cursor( win->video->window, cursor );
402 gdk_cursor_unref( cursor );
404 gdk_window_set_cursor( win->video->window, NULL );
405 if( gdk_pointer_is_grabbed() ) {
406 video_window_ungrab_display(win);
409 win->use_grab = use_grab;
413 void main_window_update_title( main_window_t win )
415 const char *disc = gdrom_get_current_disc_title();
418 gtk_window_set_title( GTK_WINDOW(win->window), lxdream_package_name );
421 snprintf( buf, sizeof(buf), "%s - %s", lxdream_package_name, disc );
422 gtk_window_set_title( GTK_WINDOW(win->window), buf );
.