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 "gtkui/gtkui.h"
39 struct main_window_info {
45 GtkActionGroup *actions;
48 int32_t mouse_x, mouse_y;
52 /******************** Video window **************************/
55 * Adjust the mouse pointer so that it appears in the center of the video
56 * window. Mainly used for when we have the mouse grab
58 void video_window_center_pointer( main_window_t win )
60 GdkDisplay *display = gtk_widget_get_display(win->video);
61 GdkScreen *screen = gtk_widget_get_screen(win->video);
65 gdk_window_get_origin(win->video->window, &x, &y);
66 gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
70 gdk_display_warp_pointer( display, screen, x, y );
71 win->mouse_x = width/2;
72 win->mouse_y = height/2;
76 * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
77 * moved to the centre of the window.
79 * @param win The window receiving the grab
80 * @return TRUE if the grab was successful, FALSE on failure.
82 gboolean video_window_grab_display( main_window_t win )
84 GdkWindow *gdkwin = win->video->window;
85 GdkColor color = { 0,0,0,0 };
86 char bytes[32]; /* 16 * 16 / 8 */
88 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bytes, 16, 16);
89 GdkCursor *cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 16, 16);
90 gdk_pixmap_unref(pixmap);
93 gdk_pointer_grab( gdkwin, FALSE,
94 GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK,
95 gdkwin, cursor, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
96 gdk_cursor_unref(cursor);
98 success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
100 gdk_pointer_ungrab(GDK_CURRENT_TIME);
103 win->is_grabbed = success;
104 main_window_set_running(win, dreamcast_is_running());
109 * Release the display grab.
111 void video_window_ungrab_display( main_window_t win )
113 gdk_pointer_ungrab(GDK_CURRENT_TIME);
114 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
115 win->is_grabbed = FALSE;
116 main_window_set_running(win, dreamcast_is_running());
119 static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
122 main_window_t win = (main_window_t)user_data;
123 int32_t x = (int32_t)event->x;
124 int32_t y = (int32_t)event->y;
125 if( win->is_grabbed &&
126 (x != win->mouse_x || y != win->mouse_y) ) {
127 uint32_t buttons = (event->state >> 8)&0x1F;
128 input_event_mouse( buttons, x - win->mouse_x, y - win->mouse_y );
129 video_window_center_pointer(win);
134 static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
137 main_window_t win = (main_window_t)user_data;
138 if( win->is_grabbed ) {
139 // Get the buttons from the event state, and remove the released button
140 uint32_t buttons = ((event->state >> 8) & 0x1F) | (1<<(event->button-1));
141 input_event_mouse( buttons, 0, 0 );
146 static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
149 main_window_t win = (main_window_t)user_data;
150 if( win->is_grabbed ) {
151 // Get the buttons from the event state, and remove the released button
152 uint32_t buttons = ((event->state >> 8) & 0x1F) & (~(1<<(event->button-1)));
153 input_event_mouse( buttons, 0, 0 );
154 } else if( win->use_grab) {
155 video_window_grab_display(win);
160 static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
163 main_window_t win = (main_window_t)user_data;
164 if( win->is_grabbed ) {
166 /* On OSX, use the command key rather than ctrl-alt. Mainly because GTK/OSX
167 * doesn't seem to be able to get ctrl-alt reliably
169 if( event->keyval == GDK_Meta_L || event->keyval == GDK_Meta_R ) {
170 video_window_ungrab_display(win);
174 /* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
175 * a singly-modified keypress rather than a double-modified 'null' press,
176 * so we have to do a little more work.
177 * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
180 int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
181 int state = event->state & gtk_accelerator_get_default_mod_mask();
182 if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
183 (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
184 video_window_ungrab_display(win);
185 // Consume the keypress, DC doesn't get it.
190 input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
194 static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
197 input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
201 static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
204 display_set_focused(event->in);
208 /*************************** Main window (frame) ******************************/
210 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
212 dreamcast_shutdown();
216 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state,
219 main_window_t win = (main_window_t)userdata;
220 if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
221 gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
222 GtkWidget *frame = gtk_widget_get_parent(win->video);
223 if( frame->style == NULL ) {
224 gtk_widget_set_style( frame, gtk_style_new() );
227 gtk_widget_hide( win->menubar );
228 gtk_widget_hide( win->toolbar );
229 gtk_widget_hide( win->statusbar );
231 frame->style->xthickness = 0;
232 frame->style->ythickness = 0;
234 frame->style->xthickness = 2;
235 frame->style->ythickness = 2;
236 gtk_widget_show( win->menubar );
237 gtk_widget_show( win->toolbar );
238 gtk_widget_show( win->statusbar );
240 gtk_widget_queue_draw( win->window );
244 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
245 GtkAccelGroup *accel_group )
249 main_window_t win = g_malloc0( sizeof(struct main_window_info) );
251 win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
252 win->menubar = menubar;
253 win->toolbar = toolbar;
254 win->use_grab = FALSE;
255 win->is_grabbed = FALSE;
256 gtk_window_set_title( GTK_WINDOW(win->window), title );
257 gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
259 gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
261 win->video = video_gtk_create_drawable();
262 gtk_widget_set_size_request( win->video, 640, 480 );
263 gtk_widget_set_double_buffered( win->video, FALSE );
264 frame = gtk_frame_new(NULL);
265 gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
266 gtk_container_add( GTK_CONTAINER(frame), win->video );
268 win->statusbar = gtk_statusbar_new();
270 vbox = gtk_vbox_new(FALSE, 0);
271 gtk_container_add( GTK_CONTAINER(win->window), vbox );
272 gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
273 gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
274 gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
275 gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
276 gtk_widget_show_all( win->window );
277 gtk_widget_grab_focus( win->video );
279 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
281 g_signal_connect( win->window, "delete_event",
282 G_CALLBACK(on_main_window_deleted), win );
283 g_signal_connect( win->window, "window-state-event",
284 G_CALLBACK(on_main_window_state_changed), win );
286 g_signal_connect( win->video, "key-press-event",
287 G_CALLBACK(on_video_window_key_pressed), win );
288 g_signal_connect( win->video, "key-release-event",
289 G_CALLBACK(on_video_window_key_released), win );
290 g_signal_connect( win->video, "motion-notify-event",
291 G_CALLBACK(on_video_window_mouse_motion), win );
292 g_signal_connect( win->video, "button-press-event",
293 G_CALLBACK(on_video_window_mouse_pressed), win );
294 g_signal_connect( win->video, "button-release-event",
295 G_CALLBACK(on_video_window_mouse_released), win );
296 g_signal_connect( win->video, "focus-in-event",
297 G_CALLBACK(on_video_window_focus_changed), win);
298 g_signal_connect( win->video, "focus-out-event",
299 G_CALLBACK(on_video_window_focus_changed), win);
301 gtk_widget_add_events( win->video,
302 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
303 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
304 GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK );
309 void main_window_set_status_text( main_window_t win, char *text )
311 gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
312 if( win->is_grabbed ) {
315 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <command> to release grab)") );
317 snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
319 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
321 gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
325 void main_window_set_running( main_window_t win, gboolean running )
327 char *text = running ? _("Running") : _("Stopped");
328 gtk_gui_enable_action( "Pause", running );
329 gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
330 main_window_set_status_text( win, text );
333 void main_window_set_framerate( main_window_t win, float rate )
339 void main_window_set_speed( main_window_t win, double speed )
343 snprintf( buf, 32, "Running (%2.4f%%)", speed );
344 main_window_set_status_text( win, buf );
347 GtkWidget *main_window_get_renderarea( main_window_t win )
352 GtkWindow *main_window_get_frame( main_window_t win )
354 return GTK_WINDOW(win->window);
357 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
360 gtk_window_fullscreen( GTK_WINDOW(win->window) );
362 gtk_window_unfullscreen( GTK_WINDOW(win->window) );
366 void main_window_set_use_grab( main_window_t win, gboolean use_grab )
368 if( use_grab != win->use_grab ) {
370 GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
371 gdk_window_set_cursor( win->video->window, cursor );
372 gdk_cursor_unref( cursor );
374 gdk_window_set_cursor( win->video->window, NULL );
375 if( gdk_pointer_is_grabbed() ) {
376 video_window_ungrab_display(win);
379 win->use_grab = use_grab;
.