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 887:8b3ee741c9d7
prev854:130928a3cdcb
next889:5baaea6d9722
author nkeynes
date Mon Oct 20 06:11:10 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix const-correctness
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"
    38 #include "drivers/video_gl.h"
    41 struct main_window_info {
    42     GtkWidget *window;
    43     GtkWidget *video;
    44     GtkWidget *menubar;
    45     GtkWidget *toolbar;
    46     GtkWidget *statusbar;
    47     GtkActionGroup *actions;
    48     gboolean use_grab;
    49     gboolean is_grabbed;
    50     int32_t mouse_x, mouse_y;
    51 };
    54 /******************** Video window **************************/
    56 #if !(GTK_CHECK_VERSION(2,8,0))
    57 void gdk_display_warp_pointer (GdkDisplay *display,
    58                                GdkScreen  *screen,
    59                                gint        x,
    60                                gint        y);
    61 #endif
    63 /**
    64  * Adjust the mouse pointer so that it appears in the center of the video
    65  * window. Mainly used for when we have the mouse grab
    66  */
    67 void video_window_center_pointer( main_window_t win )
    68 {
    69     GdkDisplay *display = gtk_widget_get_display(win->video);
    70     GdkScreen *screen = gtk_widget_get_screen(win->video);
    71     int x,y;
    72     int width, height;
    74     gdk_window_get_origin(win->video->window, &x, &y);
    75     gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
    76     x += width / 2;
    77     y += height / 2;
    79     gdk_display_warp_pointer( display, screen, x, y );
    80     win->mouse_x = width/2;
    81     win->mouse_y = height/2;
    82 }
    84 /**
    85  * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
    86  * moved to the centre of the window.
    87  *
    88  * @param win The window receiving the grab
    89  * @return TRUE if the grab was successful, FALSE on failure.
    90  */
    91 gboolean video_window_grab_display( main_window_t win )
    92 {
    93     GdkWindow *gdkwin = win->video->window;
    94     GdkColor color = { 0,0,0,0 };
    95     char bytes[32]; /* 16 * 16 / 8 */
    96     memset(bytes, 0, 32);
    97     GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bytes, 16, 16);
    98     GdkCursor *cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 16, 16);
    99     gdk_pixmap_unref(pixmap);
   101     gboolean success =
   102         gdk_pointer_grab( gdkwin, FALSE, 
   103                 GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK, 
   104                 gdkwin, cursor, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
   105     gdk_cursor_unref(cursor);
   106     if( success ) {
   107         success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
   108         if( !success ) {
   109             gdk_pointer_ungrab(GDK_CURRENT_TIME);
   110         }
   111     }
   112     win->is_grabbed = success;
   113     main_window_set_running(win, dreamcast_is_running());
   114     return success;
   115 }
   117 /**
   118  * Release the display grab.
   119  */
   120 void video_window_ungrab_display( main_window_t win )
   121 {
   122     gdk_pointer_ungrab(GDK_CURRENT_TIME);
   123     gdk_keyboard_ungrab(GDK_CURRENT_TIME);
   124     win->is_grabbed = FALSE;
   125     main_window_set_running(win, dreamcast_is_running());
   126 }
   128 static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
   129                                               gpointer user_data )
   130 {
   131     main_window_t win = (main_window_t)user_data;
   132     int x = (int)event->x;
   133     int y = (int)event->y;
   134     if( win->is_grabbed && 
   135             (x != win->mouse_x || y != win->mouse_y) ) {
   136         input_event_mousemove( x - win->mouse_x, y - win->mouse_y, FALSE );
   137         video_window_center_pointer(win);
   138     } else {
   139         int width, height;
   140         gl_window_to_system_coords( &x, &y );
   141         input_event_mousemove( x, y, TRUE );
   142     }
   143     return TRUE;
   144 }
   146 static gboolean on_video_window_mouse_exited( GtkWidget *widget, GdkEventCrossing *event,
   147                                               gpointer user_data )
   148 {
   149     main_window_t win = (main_window_t)user_data;
   150     if( !win->is_grabbed ) {
   151         input_event_mousemove( -1, -1, TRUE );
   152     }
   153     return TRUE;
   154 }    
   156 static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
   157                                                gpointer user_data )
   158 {
   159     main_window_t win = (main_window_t)user_data;
   160     if( win->is_grabbed ) {
   161         input_event_mousedown( event->button-1, 0, 0, FALSE );
   162     } else {
   163         int x = (int)event->x;
   164         int y = (int)event->y;
   165         gl_window_to_system_coords( &x, &y );
   166         input_event_mousedown( event->button-1, x, y, TRUE ); 
   167     }
   168     return TRUE;
   169 }
   171 static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
   172                                                 gpointer user_data )
   173 {
   174     main_window_t win = (main_window_t)user_data;
   175     if( win->is_grabbed ) {
   176         input_event_mouseup( event->button-1, 0, 0, FALSE );
   177     } else if( win->use_grab) {
   178         video_window_grab_display(win);
   179     } else {
   180         int x = (int)event->x;
   181         int y = (int)event->y;
   182         gl_window_to_system_coords( &x, &y );
   183         input_event_mouseup( event->button-1, x, y, TRUE ); 
   184     }        
   185     return TRUE;
   186 }
   188 static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
   189                                              gpointer user_data )
   190 {
   191     main_window_t win = (main_window_t)user_data;
   192     if( win->is_grabbed ) {
   193 #ifdef HAVE_GTK_OSX
   194         /* On OSX, use the command key rather than ctrl-alt. Mainly because GTK/OSX 
   195          * doesn't seem to be able to get ctrl-alt reliably 
   196          **/
   197         if( event->keyval == GDK_Meta_L || event->keyval == GDK_Meta_R ) {
   198             video_window_ungrab_display(win);
   199             return TRUE;
   200         }
   201 #else    	
   202         /* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
   203          * a singly-modified keypress rather than a double-modified 'null' press, 
   204          * so we have to do a little more work.
   205          * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
   206          * mouse buttons/etc
   207          */
   208         int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
   209         int state = event->state & gtk_accelerator_get_default_mod_mask();
   210         if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
   211                 (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
   212             video_window_ungrab_display(win);
   213             // Consume the keypress, DC doesn't get it.
   214             return TRUE;
   215         }
   216 #endif
   217     }
   218     input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
   219     return TRUE;
   220 }
   222 static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
   223                                               gpointer user_data )
   224 {
   225     input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
   226     return TRUE;
   227 }
   229 static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
   230                                                gpointer user_data )
   231 {
   232     display_set_focused(event->in);
   233     return TRUE;
   234 }
   236 /*************************** Main window (frame) ******************************/
   238 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
   239 {
   240     dreamcast_shutdown();
   241     exit(0);
   242 }
   244 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state, 
   245                                           gpointer userdata )
   246 {
   247     main_window_t win = (main_window_t)userdata;
   248     if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
   249         gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
   250         GtkWidget *frame = gtk_widget_get_parent(win->video);
   251         if( frame->style == NULL ) {
   252             gtk_widget_set_style( frame, gtk_style_new() );
   253         }
   254         if( fs ) {
   255             gtk_widget_hide( win->menubar );
   256             gtk_widget_hide( win->toolbar );
   257             gtk_widget_hide( win->statusbar );
   259             frame->style->xthickness = 0;
   260             frame->style->ythickness = 0;
   261         } else {
   262             frame->style->xthickness = 2;
   263             frame->style->ythickness = 2;
   264             gtk_widget_show( win->menubar );
   265             gtk_widget_show( win->toolbar );
   266             gtk_widget_show( win->statusbar );
   267         }
   268         gtk_widget_queue_draw( win->window );
   269     }
   270 }
   272 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
   273                                GtkAccelGroup *accel_group )
   274 {
   275     GtkWidget *vbox;
   276     GtkWidget *frame;
   277     main_window_t win = g_malloc0( sizeof(struct main_window_info) );
   279     win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   280     win->menubar = menubar;
   281     win->toolbar = toolbar;
   282     win->use_grab = FALSE;
   283     win->is_grabbed = FALSE;
   284     gtk_window_set_title( GTK_WINDOW(win->window), title );
   285     gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
   287     gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
   289     win->video = video_gtk_create_drawable();
   290     gtk_widget_set_size_request( win->video, 640, 480 ); 
   291     gtk_widget_set_double_buffered( win->video, FALSE );
   292     frame = gtk_frame_new(NULL);
   293     gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
   294     gtk_container_add( GTK_CONTAINER(frame), win->video );
   296     win->statusbar = gtk_statusbar_new();
   298     vbox = gtk_vbox_new(FALSE, 0);
   299     gtk_container_add( GTK_CONTAINER(win->window), vbox );
   300     gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
   301     gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
   302     gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
   303     gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
   304     gtk_widget_show_all( win->window );
   305     gtk_widget_grab_focus( win->video );
   307     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
   309     g_signal_connect( win->window, "delete_event", 
   310                       G_CALLBACK(on_main_window_deleted), win );
   311     g_signal_connect( win->window, "window-state-event",
   312                       G_CALLBACK(on_main_window_state_changed), win );
   314     g_signal_connect( win->video, "key-press-event",
   315                       G_CALLBACK(on_video_window_key_pressed), win );
   316     g_signal_connect( win->video, "key-release-event",
   317                       G_CALLBACK(on_video_window_key_released), win );
   318     g_signal_connect( win->video, "motion-notify-event",
   319                       G_CALLBACK(on_video_window_mouse_motion), win );
   320     g_signal_connect( win->video, "leave-notify-event",
   321                       G_CALLBACK(on_video_window_mouse_exited), win );
   322     g_signal_connect( win->video, "button-press-event",
   323                       G_CALLBACK(on_video_window_mouse_pressed), win );
   324     g_signal_connect( win->video, "button-release-event", 
   325                       G_CALLBACK(on_video_window_mouse_released), win );
   326     g_signal_connect( win->video, "focus-in-event",
   327                       G_CALLBACK(on_video_window_focus_changed), win);
   328     g_signal_connect( win->video, "focus-out-event",
   329                       G_CALLBACK(on_video_window_focus_changed), win);
   331     gtk_widget_add_events( win->video, 
   332                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
   333                            GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
   334                            GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK |
   335                            GDK_LEAVE_NOTIFY_MASK );
   337     return win;
   338 }
   340 void main_window_set_status_text( main_window_t win, const char *text )
   341 {
   342     gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
   343     if( win->is_grabbed ) {
   344         char buf[128];
   345 #ifdef HAVE_GTK_OSX
   346         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <command> to release grab)") );
   347 #else	
   348         snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
   349 #endif
   350         gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
   351     } else {
   352         gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
   353     }
   354 }
   356 void main_window_set_running( main_window_t win, gboolean running )
   357 {
   358     const char *text = running ? _("Running") : _("Stopped");
   359     gtk_gui_enable_action( "Pause", running );
   360     gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
   361     main_window_set_status_text( win, text );
   362 }
   364 void main_window_set_framerate( main_window_t win, float rate )
   365 {
   368 }
   370 void main_window_set_speed( main_window_t win, double speed )
   371 {
   372     char buf[32];
   374     snprintf( buf, 32, "Running (%2.4f%%)", speed );
   375     main_window_set_status_text( win, buf );
   376 }
   378 GtkWidget *main_window_get_renderarea( main_window_t win )
   379 {
   380     return win->video;
   381 }
   383 GtkWindow *main_window_get_frame( main_window_t win )
   384 {
   385     return GTK_WINDOW(win->window);
   386 }
   388 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
   389 {
   390     if( fullscreen ) {
   391         gtk_window_fullscreen( GTK_WINDOW(win->window) );
   392     } else {
   393         gtk_window_unfullscreen( GTK_WINDOW(win->window) );
   394     }
   395 }
   397 void main_window_set_use_grab( main_window_t win, gboolean use_grab )
   398 {
   399     if( use_grab != win->use_grab ) {
   400         if( use_grab ) {
   401             GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
   402             gdk_window_set_cursor( win->video->window, cursor );
   403             gdk_cursor_unref( cursor );
   404         } else {
   405             gdk_window_set_cursor( win->video->window, NULL );
   406             if( gdk_pointer_is_grabbed() ) {
   407                 video_window_ungrab_display(win);
   408             }
   409         }
   410         win->use_grab = use_grab;
   411     }
   412 }
   414 void main_window_update_title( main_window_t win )
   415 {
   416     const char *disc = gdrom_get_current_disc_title();
   418     if( disc == NULL ) {
   419         gtk_window_set_title( GTK_WINDOW(win->window), lxdream_package_name );
   420     } else {
   421         char buf[256];
   422         snprintf( buf, sizeof(buf), "%s - %s", lxdream_package_name, disc );
   423         gtk_window_set_title( GTK_WINDOW(win->window), buf );
   424     }
   425 }
.