Search
lxdream.org :: lxdream/src/gtkui/main_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/main_win.c
changeset 659:6b1dff1575b3
prev658:f5926310bfbe
next668:c62dff190859
author nkeynes
date Wed Apr 16 22:54:40 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Move drawable creation to video_gtk.c - removes the last platform dependency from gtkui
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Define the main (emu) GTK window, along with its menubars,
     5  * toolbars, etc.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #include <assert.h>
    21 #include <sys/types.h>
    22 #include <sys/stat.h>
    23 #include <unistd.h>
    24 #include <string.h>
    25 #include <stdio.h>
    26 #include <stdlib.h>
    28 #include <gtk/gtk.h>
    29 #include <gdk/gdk.h>
    30 #include <gdk/gdkkeysyms.h>
    31 #include <X11/Xutil.h>
    33 #include "lxdream.h"
    34 #include "gtkui/gtkui.h"
    37 struct main_window_info {
    38     GtkWidget *window;
    39     GtkWidget *video;
    40     GtkWidget *menubar;
    41     GtkWidget *toolbar;
    42     GtkWidget *statusbar;
    43     GtkActionGroup *actions;
    44     gboolean use_grab;
    45     gboolean is_grabbed;
    46     int32_t mouse_x, mouse_y;
    47 };
    50 /******************** Video window **************************/
    52 /**
    53  * Adjust the mouse pointer so that it appears in the center of the video
    54  * window. Mainly used for when we have the mouse grab
    55  */
    56 void video_window_center_pointer( main_window_t win )
    57 {
    58     GdkDisplay *display = gtk_widget_get_display(win->video);
    59     GdkScreen *screen = gtk_widget_get_screen(win->video);
    60     int x,y;
    61     int width, height;
    63     gdk_window_get_origin(win->video->window, &x, &y);
    64     gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
    65     x += width / 2;
    66     y += height / 2;
    68     gdk_display_warp_pointer( display, screen, x, y );
    69     win->mouse_x = width/2;
    70     win->mouse_y = height/2;
    71 }
    73 /**
    74  * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
    75  * moved to the centre of the window.
    76  *
    77  * @param win The window receiving the grab
    78  * @return TRUE if the grab was successful, FALSE on failure.
    79  */
    80 gboolean video_window_grab_display( main_window_t win )
    81 {
    82     GdkWindow *gdkwin = win->video->window;
    83     GdkColor color = { 0,0,0,0 };
    84     char bytes[32]; /* 16 * 16 / 8 */
    85     memset(bytes, 0, 32);
    86     GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bytes, 16, 16);
    87     GdkCursor *cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 16, 16);
    88     gdk_pixmap_unref(pixmap);
    90     gboolean success =
    91 	gdk_pointer_grab( gdkwin, FALSE, 
    92 			  GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK, 
    93 			  gdkwin, cursor, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
    94     gdk_cursor_unref(cursor);
    95     if( success ) {
    96 	success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
    97 	if( !success ) {
    98 	    gdk_pointer_ungrab(GDK_CURRENT_TIME);
    99 	}
   100     }
   101     win->is_grabbed = success;
   102     main_window_set_running(win, dreamcast_is_running());
   103     return success;
   104 }
   106 /**
   107  * Release the display grab.
   108  */
   109 void video_window_ungrab_display( main_window_t win )
   110 {
   111     gdk_pointer_ungrab(GDK_CURRENT_TIME);
   112     gdk_keyboard_ungrab(GDK_CURRENT_TIME);
   113     win->is_grabbed = FALSE;
   114     main_window_set_running(win, dreamcast_is_running());
   115 }
   117 static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
   118 					      gpointer user_data )
   119 {
   120     main_window_t win = (main_window_t)user_data;
   121     int32_t x = (int32_t)event->x;
   122     int32_t y = (int32_t)event->y;
   123     if( win->is_grabbed && 
   124 	(x != win->mouse_x || y != win->mouse_y) ) {
   125 	uint32_t buttons = (event->state >> 8)&0x1F;
   126 	input_event_mouse( buttons, x - win->mouse_x, y - win->mouse_y );
   127 	video_window_center_pointer(win);
   128     }
   129     return TRUE;
   130 }
   132 static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
   133 					       gpointer user_data )
   134 {
   135     main_window_t win = (main_window_t)user_data;
   136     if( win->is_grabbed ) {
   137 	// Get the buttons from the event state, and remove the released button
   138 	uint32_t buttons = ((event->state >> 8) & 0x1F) | (1<<(event->button-1));
   139 	input_event_mouse( buttons, 0, 0 );
   140     }
   141     return TRUE;
   142 }
   144 static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
   145 					      gpointer user_data )
   146 {
   147     main_window_t win = (main_window_t)user_data;
   148     if( win->is_grabbed ) {
   149 	// Get the buttons from the event state, and remove the released button
   150 	uint32_t buttons = ((event->state >> 8) & 0x1F) & (~(1<<(event->button-1)));
   151 	input_event_mouse( buttons, 0, 0 );
   152     } else if( win->use_grab) {
   153 	video_window_grab_display(win);
   154     }
   155     return TRUE;
   156 }
   158 static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
   159 					    gpointer user_data )
   160 {
   161     main_window_t win = (main_window_t)user_data;
   162     if( win->is_grabbed ) {
   163 	/* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
   164 	 * a singly-modified keypress rather than a double-modified 'null' press, 
   165 	 * so we have to do a little more work.
   166 	 * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
   167 	 * mouse buttons/etc
   168 	 */
   169         int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
   170 	int state = event->state & gtk_accelerator_get_default_mod_mask();
   171 	if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
   172 	    (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
   173 	    video_window_ungrab_display(win);
   174 	    // Consume the keypress, DC doesn't get it.
   175 	    return TRUE;
   176 	}
   177     }
   178     input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
   179     return TRUE;
   180 }
   182 static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
   183 					     gpointer user_data )
   184 {
   185     main_window_t win = (main_window_t)user_data;
   186     input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
   187     return TRUE;
   188 }
   190 static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
   191 					       gpointer user_data )
   192 {
   193     display_set_focused(event->in);
   194 }
   196 /*************************** Main window (frame) ******************************/
   198 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
   199 {
   200     exit(0);
   201 }
   203 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state, 
   204 					  gpointer userdata )
   205 {
   206     main_window_t win = (main_window_t)userdata;
   207     if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
   208 	gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
   209 	GtkWidget *frame = gtk_widget_get_parent(win->video);
   210 	if( frame->style == NULL ) {
   211 	    gtk_widget_set_style( frame, gtk_style_new() );
   212 	}
   213 	if( fs ) {
   214 	    gtk_widget_hide( win->menubar );
   215 	    gtk_widget_hide( win->toolbar );
   216 	    gtk_widget_hide( win->statusbar );
   218 	    frame->style->xthickness = 0;
   219 	    frame->style->ythickness = 0;
   220 	} else {
   221 	    frame->style->xthickness = 2;
   222 	    frame->style->ythickness = 2;
   223 	    gtk_widget_show( win->menubar );
   224 	    gtk_widget_show( win->toolbar );
   225 	    gtk_widget_show( win->statusbar );
   226 	}
   227 	gtk_widget_queue_draw( win->window );
   228     }
   229 }
   231 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
   232 			       GtkAccelGroup *accel_group )
   233 {
   234     GtkWidget *vbox;
   235     GtkWidget *frame;
   236     main_window_t win = g_malloc0( sizeof(struct main_window_info) );
   238     win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   239     win->menubar = menubar;
   240     win->toolbar = toolbar;
   241     win->use_grab = FALSE;
   242     win->is_grabbed = FALSE;
   243     gtk_window_set_title( GTK_WINDOW(win->window), title );
   244     gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
   246     gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
   248     win->video = video_gtk_create_drawable();
   249     gtk_widget_set_size_request( win->video, 640, 480 ); 
   250     gtk_widget_set_double_buffered( win->video, FALSE );
   251     frame = gtk_frame_new(NULL);
   252     gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
   253     gtk_container_add( GTK_CONTAINER(frame), win->video );
   255     win->statusbar = gtk_statusbar_new();
   257     vbox = gtk_vbox_new(FALSE, 0);
   258     gtk_container_add( GTK_CONTAINER(win->window), vbox );
   259     gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
   260     gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
   261     gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
   262     gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
   263     gtk_widget_show_all( win->window );
   264     gtk_widget_grab_focus( win->video );
   266     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
   268     g_signal_connect( win->window, "delete_event", 
   269 		      G_CALLBACK(on_main_window_deleted), win );
   270     g_signal_connect( win->window, "window-state-event",
   271 		      G_CALLBACK(on_main_window_state_changed), win );
   273     g_signal_connect( win->video, "key-press-event",
   274 		      G_CALLBACK(on_video_window_key_pressed), win );
   275     g_signal_connect( win->video, "key-release-event",
   276 		      G_CALLBACK(on_video_window_key_released), win );
   277     g_signal_connect( win->video, "motion-notify-event",
   278 		      G_CALLBACK(on_video_window_mouse_motion), win );
   279     g_signal_connect( win->video, "button-press-event",
   280 		      G_CALLBACK(on_video_window_mouse_pressed), win );
   281     g_signal_connect( win->video, "button-release-event", 
   282 		      G_CALLBACK(on_video_window_mouse_released), win );
   283     g_signal_connect( win->video, "focus-in-event",
   284 		      G_CALLBACK(on_video_window_focus_changed), win);
   285     g_signal_connect( win->video, "focus-out-event",
   286 		      G_CALLBACK(on_video_window_focus_changed), win);
   288     gtk_widget_add_events( win->video, 
   289 			   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
   290 			   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
   291 			   GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK );
   293     return win;
   294 }
   296 void main_window_set_status_text( main_window_t win, char *text )
   297 {
   298     gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
   299     if( win->is_grabbed ) {
   300 	char buf[128];
   301 	snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   302 	gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
   303     } else {
   304 	gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
   305     }
   306 }
   308 void main_window_set_running( main_window_t win, gboolean running )
   309 {
   310     char *text = running ? _("Running") : _("Stopped");
   311     gtk_gui_enable_action( "Pause", running );
   312     gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
   313     main_window_set_status_text( win, text );
   314 }
   316 void main_window_set_framerate( main_window_t win, float rate )
   317 {
   320 }
   322 void main_window_set_speed( main_window_t win, double speed )
   323 {
   324     char buf[32];
   326     snprintf( buf, 32, "Running (%2.4f%%)", speed );
   327     main_window_set_status_text( win, buf );
   328 }
   330 GtkWidget *main_window_get_renderarea( main_window_t win )
   331 {
   332     return win->video;
   333 }
   335 GtkWindow *main_window_get_frame( main_window_t win )
   336 {
   337     return GTK_WINDOW(win->window);
   338 }
   340 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
   341 {
   342     if( fullscreen ) {
   343 	gtk_window_fullscreen( GTK_WINDOW(win->window) );
   344     } else {
   345 	gtk_window_unfullscreen( GTK_WINDOW(win->window) );
   346     }
   347 }
   349 void main_window_set_use_grab( main_window_t win, gboolean use_grab )
   350 {
   351     if( use_grab != win->use_grab ) {
   352 	if( use_grab ) {
   353 	    GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
   354 	    gdk_window_set_cursor( win->video->window, cursor );
   355 	    gdk_cursor_unref( cursor );
   356 	} else {
   357 	    gdk_window_set_cursor( win->video->window, NULL );
   358 	    if( gdk_pointer_is_grabbed() ) {
   359 		video_window_ungrab_display(win);
   360 	    }
   361 	}
   362 	win->use_grab = use_grab;
   363     }
   364 }
.