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 658:f5926310bfbe
prev653:3202ff01d48e
next659:6b1dff1575b3
author nkeynes
date Wed Apr 16 12:43:52 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Start removing X11isms from gtkui into drivers
Overhaul the configure rules for figuring out the video driver
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 "pvr2/pvr2.h"
    27 #include "gtkui/gtkui.h"
    29 #ifdef HAVE_GTK_X11
    31 #include <gdk/gdkx.h>
    32 #include "drivers/video_glx.h"
    34 /************* X11-specificness **********/
    36 guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
    37 {
    38   int i;
    39   int result = 0;
    40   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
    41   XModifierKeymap *keymap = XGetModifierMapping( xdisplay );
    42   for( i=0; i<8*keymap->max_keypermod; i++ ) {
    43     if( keymap->modifiermap[i] == keycode ) {
    44       result = 1 << (i/keymap->max_keypermod);
    45       break;
    46     }
    47   }
    48   XFreeModifiermap(keymap);
    49   return result;
    50 }
    52 #if !(GTK_CHECK_VERSION(2,8,0))
    53 /* gdk_display_warp_pointer was added in GTK 2.8. If we're using an earlier
    54  * version, include the code here. (Can't just set the dependency on 2.8 as
    55  * it still hasn't been included in fink yet...) Original copyright statement
    56  * below.
    57  */
    59 /* GDK - The GIMP Drawing Kit
    60  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
    61  *
    62  * This library is free software; you can redistribute it and/or
    63  * modify it under the terms of the GNU Lesser General Public
    64  * License as published by the Free Software Foundation; either
    65  * version 2 of the License, or (at your option) any later version.
    66  *
    67  * This library is distributed in the hope that it will be useful,
    68  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    69  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    70  * Lesser General Public License for more details.
    71  *
    72  * You should have received a copy of the GNU Lesser General Public
    73  * License along with this library; if not, write to the
    74  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    75  * Boston, MA 02111-1307, USA.
    76  */
    78 /*
    79  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
    80  * file for a list of people on the GTK+ Team.  See the ChangeLog
    81  * files for a list of changes.  These files are distributed with
    82  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
    83  */
    84 void gdk_display_warp_pointer (GdkDisplay *display,
    85                           GdkScreen  *screen,
    86                           gint        x,
    87                           gint        y)
    88 {
    89   Display *xdisplay;
    90   Window dest;
    92   xdisplay = GDK_DISPLAY_XDISPLAY (display);
    93   dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
    95   XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);  
    96 }
    98 #endif
   100 #endif
   101 GtkWidget *gtk_video_win = NULL;
   102 int video_width = 640;
   103 int video_height = 480;
   105 gboolean video_gtk_init();
   106 void video_gtk_shutdown();
   107 gboolean video_gtk_display_blank( uint32_t colour );
   108 uint16_t video_gtk_resolve_keysym( const gchar *keysym );
   109 uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode);
   111 struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
   112 					     video_gtk_resolve_keysym,
   113 					     video_gtk_keycode_to_dckeysym,
   114 					     NULL,
   115 					     NULL, NULL, NULL, NULL, NULL, 
   116 					     video_gtk_display_blank, NULL };
   118 uint16_t video_gtk_resolve_keysym( const gchar *keysym )
   119 {
   120     int val = gdk_keyval_from_name( keysym );
   121     if( val == GDK_VoidSymbol )
   122 	return 0;
   123     return (uint16_t)val;
   124 }
   126 gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
   127 {
   128     render_buffer_t buffer = pvr2_get_front_buffer();
   129     if( buffer == NULL ) {
   130 	display_gtk_driver.display_blank(pvr2_get_border_colour());
   131     } else {
   132 	display_gtk_driver.display_render_buffer(buffer);
   133     }
   134     return TRUE;
   135 }
   137 gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
   138 {
   139     video_width = event->width;
   140     video_height = event->height;
   141     video_gtk_expose_callback(widget, NULL, data);
   142     return TRUE;
   143 }
   145 uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode)
   146 {
   147     if( keycode >= 'a' && keycode <= 'z' ) {
   148 	return (keycode - 'a') + DCKB_A;
   149     } else if( keycode >= '1' && keycode <= '9' ) {
   150 	return (keycode - '1') + DCKB_1;
   151     }
   152     switch(keycode) {
   153     case GDK_0:         return DCKB_0;
   154     case GDK_Return:    return DCKB_ENTER;
   155     case GDK_Escape:    return DCKB_ESCAPE;
   156     case GDK_BackSpace: return DCKB_BACKSPACE;
   157     case GDK_Tab:       return DCKB_TAB;
   158     case GDK_space:     return DCKB_SPACE;
   159     case GDK_minus:     return DCKB_MINUS;
   160     case GDK_equal:     return DCKB_EQUAL;
   161     case GDK_bracketleft: return DCKB_LBRACKET;
   162     case GDK_bracketright: return DCKB_RBRACKET;
   163     case GDK_semicolon: return DCKB_SEMICOLON;
   164     case GDK_apostrophe:return DCKB_QUOTE;
   165     case GDK_grave : return DCKB_BACKQUOTE;
   166     case GDK_comma:     return DCKB_COMMA;
   167     case GDK_period:    return DCKB_PERIOD;
   168     case GDK_slash:     return DCKB_SLASH; 
   169     case GDK_Caps_Lock: return DCKB_CAPSLOCK;
   170     case GDK_F1:        return DCKB_F1;
   171     case GDK_F2:        return DCKB_F2;
   172     case GDK_F3:        return DCKB_F3;
   173     case GDK_F4:        return DCKB_F4;
   174     case GDK_F5:        return DCKB_F5;
   175     case GDK_F6:        return DCKB_F6;
   176     case GDK_F7:        return DCKB_F7;
   177     case GDK_F8:        return DCKB_F8;
   178     case GDK_F9:        return DCKB_F9;
   179     case GDK_F10:       return DCKB_F10;
   180     case GDK_F11:       return DCKB_F11;
   181     case GDK_F12:       return DCKB_F12;
   182     case GDK_Scroll_Lock: return DCKB_SCROLLLOCK;
   183     case GDK_Pause:     return DCKB_PAUSE;
   184     case GDK_Insert:    return DCKB_INSERT;
   185     case GDK_Home:      return DCKB_HOME;
   186     case GDK_Page_Up:   return DCKB_PAGEUP;
   187     case GDK_Delete:    return DCKB_DELETE;
   188     case GDK_End:       return DCKB_END;
   189     case GDK_Page_Down: return DCKB_PAGEDOWN;
   190     case GDK_Right:     return DCKB_RIGHT;
   191     case GDK_Left:      return DCKB_LEFT;
   192     case GDK_Down:      return DCKB_DOWN;
   193     case GDK_Up:        return DCKB_UP;
   194     case GDK_Num_Lock:  return DCKB_NUMLOCK;
   195     case GDK_KP_Divide: return DCKB_KP_SLASH;
   196     case GDK_KP_Multiply: return DCKB_KP_STAR;
   197     case GDK_KP_Subtract: return DCKB_KP_MINUS;
   198     case GDK_KP_Add:    return DCKB_KP_PLUS;
   199     case GDK_KP_Enter:  return DCKB_KP_ENTER;
   200     case GDK_KP_End:    return DCKB_KP_1;
   201     case GDK_KP_Down:   return DCKB_KP_2;
   202     case GDK_KP_Page_Down: return DCKB_KP_3;
   203     case GDK_KP_Left:   return DCKB_KP_4;
   204     case GDK_KP_Begin:  return DCKB_KP_5;
   205     case GDK_KP_Right:  return DCKB_KP_6;
   206     case GDK_KP_Home:   return DCKB_KP_7;
   207     case GDK_KP_Up:     return DCKB_KP_8;
   208     case GDK_KP_Page_Up:return DCKB_KP_9;
   209     case GDK_KP_Insert: return DCKB_KP_0;
   210     case GDK_KP_Delete: return DCKB_KP_PERIOD;
   211     case GDK_backslash: return DCKB_BACKSLASH;
   212     case GDK_Control_L: return DCKB_CONTROL_L;
   213     case GDK_Shift_L:   return DCKB_SHIFT_L;
   214     case GDK_Alt_L:     return DCKB_ALT_L;
   215     case GDK_Meta_L:    return DCKB_S1;
   216     case GDK_Control_R: return DCKB_CONTROL_R;
   217     case GDK_Shift_R:   return DCKB_SHIFT_R;
   218     case GDK_Alt_R:     return DCKB_ALT_R;
   219     case GDK_Meta_R:    return DCKB_S2;
   220     }
   221     return DCKB_NONE;
   222 }
   224 gboolean video_gtk_init()
   225 {
   227     gtk_video_win = gtk_gui_get_renderarea();
   228     if( gtk_video_win == NULL ) {
   229 	return FALSE;
   230     }
   232     g_signal_connect( gtk_video_win, "expose_event",
   233 		      G_CALLBACK(video_gtk_expose_callback), NULL );
   234     g_signal_connect( gtk_video_win, "configure_event",
   235 		      G_CALLBACK(video_gtk_resize_callback), NULL );
   236     video_width = gtk_video_win->allocation.width;
   237     video_height = gtk_video_win->allocation.height;
   238 #ifdef HAVE_OSMESA
   239     video_gdk_init_driver( &display_gtk_driver );
   240 #else
   241 #ifdef HAVE_GLX
   242     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(gtk_video_win)));
   243     Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(gtk_video_win)->window );
   244     if( ! video_glx_init_context( display, window ) ||
   245         ! video_glx_init_driver( &display_gtk_driver ) ) {
   246         return FALSE;
   247     }
   248 #endif
   249 #endif
   251 #ifdef HAVE_LINUX_JOYSTICK
   252     linux_joystick_init();
   253 #endif
   254     return TRUE;
   255 }
   257 gboolean video_gtk_display_blank( uint32_t colour )
   258 {
   259     GdkGC *gc = gdk_gc_new(gtk_video_win->window);
   260     GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
   261     GdkColormap *cmap = gdk_colormap_get_system();
   262     gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
   263     gdk_gc_set_foreground( gc, &color );
   264     gdk_gc_set_background( gc, &color );
   265     gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, video_height );
   266     gdk_gc_destroy(gc);
   267     gdk_colormap_free_colors( cmap, &color, 1 );
   268 }
   270 #ifdef HAVE_GTK_X11
   271 XVisualInfo *video_gtk_get_visual()
   272 {
   273 #ifdef HAVE_OSMESA
   274     return NULL;
   275 #else
   276     return video_glx_get_visual();
   277 #endif
   278 }
   279 #endif
   281 void video_gtk_shutdown()
   282 {
   283     if( gtk_video_win != NULL ) {
   284 #ifdef HAVE_OSMESA
   285         video_gdk_shutdown();
   286 #else
   287 	video_glx_shutdown();
   288 #endif
   289     }
   290 #ifdef HAVE_LINUX_JOYSTICK
   291     linux_joystick_shutdown();
   292 #endif
   293 }
.