Search
lxdream.org :: lxdream/src/gtkui/gtk_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtk_win.c
changeset 849:bbe26d798fc2
prev843:7936c1162ce9
next850:28782ebbd01d
author nkeynes
date Mon Sep 08 05:13:51 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor mouse event management - button events are now usable for controllers
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 "dreamcast.h"
    35 #include "display.h"
    36 #include "gdrom/gdrom.h"
    37 #include "gtkui/gtkui.h"
    40 struct main_window_info {
    41     GtkWidget *window;
    42     GtkWidget *video;
    43     GtkWidget *menubar;
    44     GtkWidget *toolbar;
    45     GtkWidget *statusbar;
    46     GtkActionGroup *actions;
    47     gboolean use_grab;
    48     gboolean is_grabbed;
    49     int32_t mouse_x, mouse_y;
    50 };
    53 /******************** Video window **************************/
    55 #if !(GTK_CHECK_VERSION(2,8,0))
    56 void gdk_display_warp_pointer (GdkDisplay *display,
    57                                GdkScreen  *screen,
    58                                gint        x,
    59                                gint        y);
    60 #endif
    62 /**
    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
    65  */
    66 void video_window_center_pointer( main_window_t win )
    67 {
    68     GdkDisplay *display = gtk_widget_get_display(win->video);
    69     GdkScreen *screen = gtk_widget_get_screen(win->video);
    70     int x,y;
    71     int width, height;
    73     gdk_window_get_origin(win->video->window, &x, &y);
    74     gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
    75     x += width / 2;
    76     y += height / 2;
    78     gdk_display_warp_pointer( display, screen, x, y );
    79     win->mouse_x = width/2;
    80     win->mouse_y = height/2;
    81 }
    83 /**
    84  * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
    85  * moved to the centre of the window.
    86  *
    87  * @param win The window receiving the grab
    88  * @return TRUE if the grab was successful, FALSE on failure.
    89  */
    90 gboolean video_window_grab_display( main_window_t win )
    91 {
    92     GdkWindow *gdkwin = win->video->window;
    93     GdkColor color = { 0,0,0,0 };
    94     char bytes[32]; /* 16 * 16 / 8 */
    95     memset(bytes, 0, 32);
    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);
   100     gboolean success =
   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);
   105     if( success ) {
   106         success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
   107         if( !success ) {
   108             gdk_pointer_ungrab(GDK_CURRENT_TIME);
   109         }
   110     }
   111     win->is_grabbed = success;
   112     main_window_set_running(win, dreamcast_is_running());
   113     return success;
   114 }
   116 /**
   117  * Release the display grab.
   118  */
   119 void video_window_ungrab_display( main_window_t win )
   120 {
   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());
   125 }
   127 static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
   128                                               gpointer user_data )
   129 {
   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);
   137     } else {
   138         input_event_mousemove( x, y, TRUE );
   139     }
   140     return TRUE;
   141 }
   143 static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
   144                                                gpointer user_data )
   145 {
   146     main_window_t win = (main_window_t)user_data;
   147     if( win->is_grabbed ) {
   148         input_event_mousedown( event->button-1, 0, 0, FALSE );
   149     } else {
   150         input_event_mousedown( event->button-1, (int32_t)event->x, (int32_t)event->y, TRUE );
   151     }
   152     return TRUE;
   153 }
   155 static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
   156                                                 gpointer user_data )
   157 {
   158     main_window_t win = (main_window_t)user_data;
   159     if( win->is_grabbed ) {
   160         input_event_mouseup( event->button-1, 0, 0, FALSE );
   161     } else if( win->use_grab) {
   162         video_window_grab_display(win);
   163     } else {
   164         input_event_mouseup( event->button-1, (int32_t)event->x, (int32_t)event->y, TRUE );
   165     }        
   166     return TRUE;
   167 }
   169 static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
   170                                              gpointer user_data )
   171 {
   172     main_window_t win = (main_window_t)user_data;
   173     if( win->is_grabbed ) {
   174 #ifdef HAVE_GTK_OSX
   175         /* On OSX, use the command key rather than ctrl-alt. Mainly because GTK/OSX 
   176          * doesn't seem to be able to get ctrl-alt reliably 
   177          **/
   178         if( event->keyval == GDK_Meta_L || event->keyval == GDK_Meta_R ) {
   179             video_window_ungrab_display(win);
   180             return TRUE;
   181         }
   182 #else    	
   183         /* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
   184          * a singly-modified keypress rather than a double-modified 'null' press, 
   185          * so we have to do a little more work.
   186          * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
   187          * mouse buttons/etc
   188          */
   189         int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
   190         int state = event->state & gtk_accelerator_get_default_mod_mask();
   191         if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
   192                 (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
   193             video_window_ungrab_display(win);
   194             // Consume the keypress, DC doesn't get it.
   195             return TRUE;
   196         }
   197 #endif
   198     }
   199     input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
   200     return TRUE;
   201 }
   203 static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
   204                                               gpointer user_data )
   205 {
   206     input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
   207     return TRUE;
   208 }
   210 static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
   211                                                gpointer user_data )
   212 {
   213     display_set_focused(event->in);
   214     return TRUE;
   215 }
   217 /*************************** Main window (frame) ******************************/
   219 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
   220 {
   221     dreamcast_shutdown();
   222     exit(0);
   223 }
   225 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state, 
   226                                           gpointer userdata )
   227 {
   228     main_window_t win = (main_window_t)userdata;
   229     if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
   230         gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
   231         GtkWidget *frame = gtk_widget_get_parent(win->video);
   232         if( frame->style == NULL ) {
   233             gtk_widget_set_style( frame, gtk_style_new() );
   234         }
   235         if( fs ) {
   236             gtk_widget_hide( win->menubar );
   237             gtk_widget_hide( win->toolbar );
   238             gtk_widget_hide( win->statusbar );
   240             frame->style->xthickness = 0;
   241             frame->style->ythickness = 0;
   242         } else {
   243             frame->style->xthickness = 2;
   244             frame->style->ythickness = 2;
   245             gtk_widget_show( win->menubar );
   246             gtk_widget_show( win->toolbar );
   247             gtk_widget_show( win->statusbar );
   248         }
   249         gtk_widget_queue_draw( win->window );
   250     }
   251 }
   253 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
   254                                GtkAccelGroup *accel_group )
   255 {
   256     GtkWidget *vbox;
   257     GtkWidget *frame;
   258     main_window_t win = g_malloc0( sizeof(struct main_window_info) );
   260     win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   261     win->menubar = menubar;
   262     win->toolbar = toolbar;
   263     win->use_grab = FALSE;
   264     win->is_grabbed = FALSE;
   265     gtk_window_set_title( GTK_WINDOW(win->window), title );
   266     gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
   268     gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
   270     win->video = video_gtk_create_drawable();
   271     gtk_widget_set_size_request( win->video, 640, 480 ); 
   272     gtk_widget_set_double_buffered( win->video, FALSE );
   273     frame = gtk_frame_new(NULL);
   274     gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
   275     gtk_container_add( GTK_CONTAINER(frame), win->video );
   277     win->statusbar = gtk_statusbar_new();
   279     vbox = gtk_vbox_new(FALSE, 0);
   280     gtk_container_add( GTK_CONTAINER(win->window), vbox );
   281     gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
   282     gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
   283     gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
   284     gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
   285     gtk_widget_show_all( win->window );
   286     gtk_widget_grab_focus( win->video );
   288     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
   290     g_signal_connect( win->window, "delete_event", 
   291                       G_CALLBACK(on_main_window_deleted), win );
   292     g_signal_connect( win->window, "window-state-event",
   293                       G_CALLBACK(on_main_window_state_changed), win );
   295     g_signal_connect( win->video, "key-press-event",
   296                       G_CALLBACK(on_video_window_key_pressed), win );
   297     g_signal_connect( win->video, "key-release-event",
   298                       G_CALLBACK(on_video_window_key_released), win );
   299     g_signal_connect( win->video, "motion-notify-event",
   300                       G_CALLBACK(on_video_window_mouse_motion), win );
   301     g_signal_connect( win->video, "button-press-event",
   302                       G_CALLBACK(on_video_window_mouse_pressed), win );
   303     g_signal_connect( win->video, "button-release-event", 
   304                       G_CALLBACK(on_video_window_mouse_released), win );
   305     g_signal_connect( win->video, "focus-in-event",
   306                       G_CALLBACK(on_video_window_focus_changed), win);
   307     g_signal_connect( win->video, "focus-out-event",
   308                       G_CALLBACK(on_video_window_focus_changed), win);
   310     gtk_widget_add_events( win->video, 
   311                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
   312                            GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
   313                            GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK );
   315     return win;
   316 }
   318 void main_window_set_status_text( main_window_t win, char *text )
   319 {
   320     gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
   321     if( win->is_grabbed ) {
   322         char buf[128];
   323 #ifdef HAVE_GTK_OSX
   324         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <command> to release grab)") );
   325 #else	
   326         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   327 #endif
   328         gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
   329     } else {
   330         gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
   331     }
   332 }
   334 void main_window_set_running( main_window_t win, gboolean running )
   335 {
   336     char *text = running ? _("Running") : _("Stopped");
   337     gtk_gui_enable_action( "Pause", running );
   338     gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
   339     main_window_set_status_text( win, text );
   340 }
   342 void main_window_set_framerate( main_window_t win, float rate )
   343 {
   346 }
   348 void main_window_set_speed( main_window_t win, double speed )
   349 {
   350     char buf[32];
   352     snprintf( buf, 32, "Running (%2.4f%%)", speed );
   353     main_window_set_status_text( win, buf );
   354 }
   356 GtkWidget *main_window_get_renderarea( main_window_t win )
   357 {
   358     return win->video;
   359 }
   361 GtkWindow *main_window_get_frame( main_window_t win )
   362 {
   363     return GTK_WINDOW(win->window);
   364 }
   366 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
   367 {
   368     if( fullscreen ) {
   369         gtk_window_fullscreen( GTK_WINDOW(win->window) );
   370     } else {
   371         gtk_window_unfullscreen( GTK_WINDOW(win->window) );
   372     }
   373 }
   375 void main_window_set_use_grab( main_window_t win, gboolean use_grab )
   376 {
   377     if( use_grab != win->use_grab ) {
   378         if( use_grab ) {
   379             GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
   380             gdk_window_set_cursor( win->video->window, cursor );
   381             gdk_cursor_unref( cursor );
   382         } else {
   383             gdk_window_set_cursor( win->video->window, NULL );
   384             if( gdk_pointer_is_grabbed() ) {
   385                 video_window_ungrab_display(win);
   386             }
   387         }
   388         win->use_grab = use_grab;
   389     }
   390 }
   392 void main_window_update_title( main_window_t win )
   393 {
   394     const char *disc = gdrom_get_current_disc_title();
   396     if( disc == NULL ) {
   397         gtk_window_set_title( GTK_WINDOW(win->window), lxdream_package_name );
   398     } else {
   399         char buf[256];
   400         snprintf( buf, sizeof(buf), "%s - %s", lxdream_package_name, disc );
   401         gtk_window_set_title( GTK_WINDOW(win->window), buf );
   402     }
   403 }
.