Search
lxdream.org :: lxdream/src/drivers/video_gtk.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gtk.c
changeset 669:ab344e42bca9
prev665:99ae9dc4cab7
next677:3ee62740ff8f
author nkeynes
date Mon May 12 10:00:13 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Cleanup most of the -Wall warnings (getting a bit sloppy...)
Convert FP code to use fixed banks rather than indirect pointer
(3-4% faster this way now)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * The PC side of the video support (responsible for actually displaying / 
     5  * rendering frames)
     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 <gdk/gdkkeysyms.h>
    21 #include <stdint.h>
    22 #include "lxdream.h"
    23 #include "display.h"
    24 #include "dckeysyms.h"
    25 #include "drivers/video_gl.h"
    26 #include "drivers/joy_linux.h"
    27 #include "pvr2/pvr2.h"
    28 #include "gtkui/gtkui.h"
    30 #ifdef HAVE_GTK_X11
    32 #include <gdk/gdkx.h>
    33 #include "drivers/video_glx.h"
    35 /************* X11-specificness **********/
    37 guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
    38 {
    39   int i;
    40   int result = 0;
    41   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
    42   XModifierKeymap *keymap = XGetModifierMapping( xdisplay );
    43   for( i=0; i<8*keymap->max_keypermod; i++ ) {
    44     if( keymap->modifiermap[i] == keycode ) {
    45       result = 1 << (i/keymap->max_keypermod);
    46       break;
    47     }
    48   }
    49   XFreeModifiermap(keymap);
    50   return result;
    51 }
    53 #if !(GTK_CHECK_VERSION(2,8,0))
    54 /* gdk_display_warp_pointer was added in GTK 2.8. If we're using an earlier
    55  * version, include the code here. (Can't just set the dependency on 2.8 as
    56  * it still hasn't been included in fink yet...) Original copyright statement
    57  * below.
    58  */
    60 /* GDK - The GIMP Drawing Kit
    61  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
    62  *
    63  * This library is free software; you can redistribute it and/or
    64  * modify it under the terms of the GNU Lesser General Public
    65  * License as published by the Free Software Foundation; either
    66  * version 2 of the License, or (at your option) any later version.
    67  *
    68  * This library is distributed in the hope that it will be useful,
    69  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    70  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    71  * Lesser General Public License for more details.
    72  *
    73  * You should have received a copy of the GNU Lesser General Public
    74  * License along with this library; if not, write to the
    75  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    76  * Boston, MA 02111-1307, USA.
    77  */
    79 /*
    80  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
    81  * file for a list of people on the GTK+ Team.  See the ChangeLog
    82  * files for a list of changes.  These files are distributed with
    83  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
    84  */
    85 void gdk_display_warp_pointer (GdkDisplay *display,
    86                           GdkScreen  *screen,
    87                           gint        x,
    88                           gint        y)
    89 {
    90   Display *xdisplay;
    91   Window dest;
    93   xdisplay = GDK_DISPLAY_XDISPLAY (display);
    94   dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
    96   XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);  
    97 }
    99 #endif
   101 #endif
   103 #ifdef HAVE_GTK_OSX
   104 #include "drivers/video_nsgl.h"
   106 // Include this prototype as some systems don't have gdkquartz.h installed
   107 NSView  *gdk_quartz_window_get_nsview( GdkWindow *window);
   109 guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
   110 {
   111         return 0;
   112 }
   114 #endif
   118 GtkWidget *gtk_video_drawable = NULL;
   119 int video_width = 640;
   120 int video_height = 480;
   122 gboolean video_gtk_init();
   123 void video_gtk_shutdown();
   124 void video_gtk_display_blank( uint32_t colour );
   125 uint16_t video_gtk_resolve_keysym( const gchar *keysym );
   126 uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode);
   128 struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
   129 					     video_gtk_resolve_keysym,
   130 					     video_gtk_keycode_to_dckeysym,
   131 					     NULL,
   132 					     NULL, NULL, NULL, NULL, NULL, 
   133 					     video_gtk_display_blank, NULL };
   135 uint16_t video_gtk_resolve_keysym( const gchar *keysym )
   136 {
   137     int val = gdk_keyval_from_name( keysym );
   138     if( val == GDK_VoidSymbol )
   139 	return 0;
   140     return (uint16_t)val;
   141 }
   143 gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
   144 {
   145     render_buffer_t buffer = pvr2_get_front_buffer();
   146     if( buffer == NULL ) {
   147 	display_gtk_driver.display_blank(pvr2_get_border_colour());
   148     } else {
   149 	display_gtk_driver.display_render_buffer(buffer);
   150     }
   151     return TRUE;
   152 }
   154 gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
   155 {
   156     video_width = event->width;
   157     video_height = event->height;
   158     video_gtk_expose_callback(widget, NULL, data);
   159     return TRUE;
   160 }
   162 uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode)
   163 {
   164     if( keycode >= 'a' && keycode <= 'z' ) {
   165 	return (keycode - 'a') + DCKB_A;
   166     } else if( keycode >= '1' && keycode <= '9' ) {
   167 	return (keycode - '1') + DCKB_1;
   168     }
   169     switch(keycode) {
   170     case GDK_0:         return DCKB_0;
   171     case GDK_Return:    return DCKB_ENTER;
   172     case GDK_Escape:    return DCKB_ESCAPE;
   173     case GDK_BackSpace: return DCKB_BACKSPACE;
   174     case GDK_Tab:       return DCKB_TAB;
   175     case GDK_space:     return DCKB_SPACE;
   176     case GDK_minus:     return DCKB_MINUS;
   177     case GDK_equal:     return DCKB_EQUAL;
   178     case GDK_bracketleft: return DCKB_LBRACKET;
   179     case GDK_bracketright: return DCKB_RBRACKET;
   180     case GDK_semicolon: return DCKB_SEMICOLON;
   181     case GDK_apostrophe:return DCKB_QUOTE;
   182     case GDK_grave : return DCKB_BACKQUOTE;
   183     case GDK_comma:     return DCKB_COMMA;
   184     case GDK_period:    return DCKB_PERIOD;
   185     case GDK_slash:     return DCKB_SLASH; 
   186     case GDK_Caps_Lock: return DCKB_CAPSLOCK;
   187     case GDK_F1:        return DCKB_F1;
   188     case GDK_F2:        return DCKB_F2;
   189     case GDK_F3:        return DCKB_F3;
   190     case GDK_F4:        return DCKB_F4;
   191     case GDK_F5:        return DCKB_F5;
   192     case GDK_F6:        return DCKB_F6;
   193     case GDK_F7:        return DCKB_F7;
   194     case GDK_F8:        return DCKB_F8;
   195     case GDK_F9:        return DCKB_F9;
   196     case GDK_F10:       return DCKB_F10;
   197     case GDK_F11:       return DCKB_F11;
   198     case GDK_F12:       return DCKB_F12;
   199     case GDK_Scroll_Lock: return DCKB_SCROLLLOCK;
   200     case GDK_Pause:     return DCKB_PAUSE;
   201     case GDK_Insert:    return DCKB_INSERT;
   202     case GDK_Home:      return DCKB_HOME;
   203     case GDK_Page_Up:   return DCKB_PAGEUP;
   204     case GDK_Delete:    return DCKB_DELETE;
   205     case GDK_End:       return DCKB_END;
   206     case GDK_Page_Down: return DCKB_PAGEDOWN;
   207     case GDK_Right:     return DCKB_RIGHT;
   208     case GDK_Left:      return DCKB_LEFT;
   209     case GDK_Down:      return DCKB_DOWN;
   210     case GDK_Up:        return DCKB_UP;
   211     case GDK_Num_Lock:  return DCKB_NUMLOCK;
   212     case GDK_KP_Divide: return DCKB_KP_SLASH;
   213     case GDK_KP_Multiply: return DCKB_KP_STAR;
   214     case GDK_KP_Subtract: return DCKB_KP_MINUS;
   215     case GDK_KP_Add:    return DCKB_KP_PLUS;
   216     case GDK_KP_Enter:  return DCKB_KP_ENTER;
   217     case GDK_KP_End:    return DCKB_KP_1;
   218     case GDK_KP_Down:   return DCKB_KP_2;
   219     case GDK_KP_Page_Down: return DCKB_KP_3;
   220     case GDK_KP_Left:   return DCKB_KP_4;
   221     case GDK_KP_Begin:  return DCKB_KP_5;
   222     case GDK_KP_Right:  return DCKB_KP_6;
   223     case GDK_KP_Home:   return DCKB_KP_7;
   224     case GDK_KP_Up:     return DCKB_KP_8;
   225     case GDK_KP_Page_Up:return DCKB_KP_9;
   226     case GDK_KP_Insert: return DCKB_KP_0;
   227     case GDK_KP_Delete: return DCKB_KP_PERIOD;
   228     case GDK_backslash: return DCKB_BACKSLASH;
   229     case GDK_Control_L: return DCKB_CONTROL_L;
   230     case GDK_Shift_L:   return DCKB_SHIFT_L;
   231     case GDK_Alt_L:     return DCKB_ALT_L;
   232     case GDK_Meta_L:    return DCKB_S1;
   233     case GDK_Control_R: return DCKB_CONTROL_R;
   234     case GDK_Shift_R:   return DCKB_SHIFT_R;
   235     case GDK_Alt_R:     return DCKB_ALT_R;
   236     case GDK_Meta_R:    return DCKB_S2;
   237     }
   238     return DCKB_NONE;
   239 }
   241 GtkWidget *video_gtk_create_drawable()
   242 {
   243     GtkWidget *drawable = gtk_drawing_area_new();
   244     GTK_WIDGET_SET_FLAGS(drawable, GTK_CAN_FOCUS|GTK_CAN_DEFAULT);
   246     g_signal_connect( drawable, "expose_event",
   247 		      G_CALLBACK(video_gtk_expose_callback), NULL );
   248     g_signal_connect( drawable, "configure_event",
   249 		      G_CALLBACK(video_gtk_resize_callback), NULL );
   251 #ifdef HAVE_GLX
   252     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(drawable));
   253     Screen *screen = gdk_x11_screen_get_xscreen( gtk_widget_get_screen(drawable));
   254     int screen_no = XScreenNumberOfScreen(screen);
   255     if( !video_glx_init(display, screen_no) ) {
   256         ERROR( "Unable to initialize GLX, aborting" );
   257         exit(3);
   258     }
   260     XVisualInfo *visual = video_glx_get_visual();
   261     if( visual != NULL ) {
   262         GdkVisual *gdkvis = gdk_x11_screen_lookup_visual( gtk_widget_get_screen(drawable), visual->visualid );
   263         GdkColormap *colormap = gdk_colormap_new( gdkvis, FALSE );
   264         gtk_widget_set_colormap( drawable, colormap );
   265     }
   266 #endif
   267     gtk_video_drawable = drawable;
   268     return drawable;
   269 }
   271 gboolean video_gtk_init()
   272 {
   274     if( gtk_video_drawable == NULL ) {
   275 	return FALSE;
   276     }
   278     video_width = gtk_video_drawable->allocation.width;
   279     video_height = gtk_video_drawable->allocation.height;
   280 #ifdef HAVE_OSMESA
   281     video_gdk_init_driver( &display_gtk_driver );
   282 #else
   283 #ifdef HAVE_GLX
   284     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(gtk_video_drawable)));
   285     Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(gtk_video_drawable)->window );
   286     if( ! video_glx_init_context( display, window ) ||
   287         ! video_glx_init_driver( &display_gtk_driver ) ) {
   288         return FALSE;
   289     }
   290 #else
   291 #ifdef HAVE_NSGL
   292     NSView *view = gdk_quartz_window_get_nsview(gtk_video_drawable->window);
   293     if( ! video_nsgl_init_driver( view, &display_gtk_driver ) ) {
   294     	return FALSE;
   295     }
   296 #endif
   297 #endif
   298 #endif
   300     pvr2_setup_gl_context();
   302 #ifdef HAVE_LINUX_JOYSTICK
   303     linux_joystick_init();
   304 #endif
   305     return TRUE;
   306 }
   308 void video_gtk_display_blank( uint32_t colour )
   309 {
   310     GdkGC *gc = gdk_gc_new(gtk_video_drawable->window);
   311     GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
   312     GdkColormap *cmap = gdk_colormap_get_system();
   313     gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
   314     gdk_gc_set_foreground( gc, &color );
   315     gdk_gc_set_background( gc, &color );
   316     gdk_draw_rectangle( gtk_video_drawable->window, gc, TRUE, 0, 0, video_width, video_height );
   317     gdk_gc_destroy(gc);
   318     gdk_colormap_free_colors( cmap, &color, 1 );
   319 }
   321 void video_gtk_shutdown()
   322 {
   323     if( gtk_video_drawable != NULL ) {
   324 #ifdef HAVE_OSMESA
   325         video_gdk_shutdown();
   326 #else
   327 #ifdef HAVE_GLX
   328 	video_glx_shutdown();
   329 #else
   330 #ifdef HAVE_NSGL
   331 	video_nsgl_shutdown();
   332 #endif
   333 #endif
   334 #endif
   335     }
   336 #ifdef HAVE_LINUX_JOYSTICK
   337     linux_joystick_shutdown();
   338 #endif
   339 }
.