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 608:4f588e52bce0
prev586:2a3ba82cf243
next609:41b61a0d5f1a
author nkeynes
date Sat Jan 26 02:45:27 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Bug #50: Implement mouse and keyboard
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/gdkx.h>
    21 #include <gdk/gdkkeysyms.h>
    22 #include <stdint.h>
    23 #include "dream.h"
    24 #include "display.h"
    25 #include "dckeysyms.h"
    26 #include "drivers/video_glx.h"
    27 #include "drivers/gl_common.h"
    28 #include "pvr2/pvr2.h"
    29 #include "gtkui/gtkui.h"
    31 static GtkWidget *video_win = NULL;
    32 int video_width = 640;
    33 int video_height = 480;
    35 gboolean video_gtk_init();
    36 void video_gtk_shutdown();
    37 gboolean video_gtk_display_blank( uint32_t colour );
    38 uint16_t video_gtk_resolve_keysym( const gchar *keysym );
    39 uint16_t video_gtk_keycode_to_dckeysym(uint32_t keycode);
    41 struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
    42 					     video_gtk_resolve_keysym,
    43 					     video_gtk_keycode_to_dckeysym,
    44 					     NULL, NULL, NULL, NULL, NULL, 
    45 					     video_gtk_display_blank, NULL };
    47 uint16_t video_gtk_resolve_keysym( const gchar *keysym )
    48 {
    49     int val = gdk_keyval_from_name( keysym );
    50     if( val == GDK_VoidSymbol )
    51 	return 0;
    52     return (uint16_t)val;
    53 }
    55 gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
    56 {
    57     render_buffer_t buffer = pvr2_get_front_buffer();
    58     if( buffer == NULL ) {
    59 	display_gtk_driver.display_blank(pvr2_get_border_colour());
    60     } else {
    61 	display_gtk_driver.display_render_buffer(buffer);
    62     }
    63     return TRUE;
    64 }
    66 gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
    67 {
    68     video_width = event->width;
    69     video_height = event->height;
    70     video_gtk_expose_callback(widget, NULL, data);
    71     return TRUE;
    72 }
    74 uint16_t video_gtk_keycode_to_dckeysym(uint32_t keycode)
    75 {
    76     if( keycode >= 'a' && keycode <= 'z' ) {
    77 	return (keycode - 'a') + DCKB_A;
    78     } else if( keycode >= '1' && keycode <= '9' ) {
    79 	return (keycode - '1') + DCKB_1;
    80     }
    81     switch(keycode) {
    82     case XK_0:         return DCKB_0;
    83     case XK_Return:    return DCKB_ENTER;
    84     case XK_Escape:    return DCKB_ESCAPE;
    85     case XK_BackSpace: return DCKB_BACKSPACE;
    86     case XK_Tab:       return DCKB_TAB;
    87     case XK_space:     return DCKB_SPACE;
    88     case XK_minus:     return DCKB_MINUS;
    89     case XK_equal:     return DCKB_EQUAL;
    90     case XK_bracketleft: return DCKB_LBRACKET;
    91     case XK_bracketright: return DCKB_RBRACKET;
    92     case XK_semicolon: return DCKB_SEMICOLON;
    93     case XK_apostrophe:return DCKB_QUOTE;
    94     case XK_grave : return DCKB_BACKQUOTE;
    95     case XK_comma:     return DCKB_COMMA;
    96     case XK_period:    return DCKB_PERIOD;
    97     case XK_slash:     return DCKB_SLASH; 
    98     case XK_Caps_Lock: return DCKB_CAPSLOCK;
    99     case XK_F1:        return DCKB_F1;
   100     case XK_F2:        return DCKB_F2;
   101     case XK_F3:        return DCKB_F3;
   102     case XK_F4:        return DCKB_F4;
   103     case XK_F5:        return DCKB_F5;
   104     case XK_F6:        return DCKB_F6;
   105     case XK_F7:        return DCKB_F7;
   106     case XK_F8:        return DCKB_F8;
   107     case XK_F9:        return DCKB_F9;
   108     case XK_F10:       return DCKB_F10;
   109     case XK_F11:       return DCKB_F11;
   110     case XK_F12:       return DCKB_F12;
   111     case XK_Scroll_Lock: return DCKB_SCROLLLOCK;
   112     case XK_Pause:     return DCKB_PAUSE;
   113     case XK_Insert:    return DCKB_INSERT;
   114     case XK_Home:      return DCKB_HOME;
   115     case XK_Page_Up:   return DCKB_PAGEUP;
   116     case XK_Delete:    return DCKB_DELETE;
   117     case XK_End:       return DCKB_END;
   118     case XK_Page_Down: return DCKB_PAGEDOWN;
   119     case XK_Right:     return DCKB_RIGHT;
   120     case XK_Left:      return DCKB_LEFT;
   121     case XK_Down:      return DCKB_DOWN;
   122     case XK_Up:        return DCKB_UP;
   123     case XK_Num_Lock:  return DCKB_NUMLOCK;
   124     case XK_KP_Divide: return DCKB_KP_SLASH;
   125     case XK_KP_Multiply: return DCKB_KP_STAR;
   126     case XK_KP_Subtract: return DCKB_KP_MINUS;
   127     case XK_KP_Add:    return DCKB_KP_PLUS;
   128     case XK_KP_Enter:  return DCKB_KP_ENTER;
   129     case XK_KP_1:      return DCKB_KP_1;
   130     case XK_KP_2:      return DCKB_KP_2;
   131     case XK_KP_3:      return DCKB_KP_3;
   132     case XK_KP_4:      return DCKB_KP_4;
   133     case XK_KP_5:      return DCKB_KP_5;
   134     case XK_KP_6:      return DCKB_KP_6;
   135     case XK_KP_7:      return DCKB_KP_7;
   136     case XK_KP_8:      return DCKB_KP_8;
   137     case XK_KP_9:      return DCKB_KP_9;
   138     case XK_KP_0:      return DCKB_KP_0;
   139     case XK_KP_Decimal:return DCKB_KP_PERIOD;
   140     case XK_backslash: return DCKB_BACKSLASH;
   141     case XK_Control_L: return DCKB_CONTROL_L;
   142     case XK_Shift_L:   return DCKB_SHIFT_L;
   143     case XK_Alt_L:     return DCKB_ALT_L;
   144     case XK_Meta_L:    return DCKB_S1;
   145     case XK_Control_R: return DCKB_CONTROL_R;
   146     case XK_Shift_R:   return DCKB_SHIFT_R;
   147     case XK_Alt_R:     return DCKB_ALT_R;
   148     case XK_Meta_R:    return DCKB_S2;
   149     }
   150     return DCKB_NONE;
   151 }
   153 gboolean video_gtk_init()
   154 {
   156     video_win = gtk_gui_get_renderarea();
   157     if( video_win == NULL ) {
   158 	return FALSE;
   159     }
   161     g_signal_connect( video_win, "expose_event",
   162 		      G_CALLBACK(video_gtk_expose_callback), NULL );
   163     g_signal_connect( video_win, "configure_event",
   164 		      G_CALLBACK(video_gtk_resize_callback), NULL );
   165     video_width = video_win->allocation.width;
   166     video_height = video_win->allocation.height;
   167     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(video_win)));
   168     Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(video_win)->window );
   169     if( ! video_glx_init_context( display, window ) ||
   170         ! video_glx_init_driver( &display_gtk_driver ) ) {
   171         return FALSE;
   172     }
   173     return TRUE;
   174 }
   176 gboolean video_gtk_display_blank( uint32_t colour )
   177 {
   178     GdkGC *gc = gdk_gc_new(video_win->window);
   179     GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
   180     GdkColormap *cmap = gdk_colormap_get_system();
   181     gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
   182     gdk_gc_set_foreground( gc, &color );
   183     gdk_gc_set_background( gc, &color );
   184     gdk_draw_rectangle( video_win->window, gc, TRUE, 0, 0, video_width, video_height );
   185     gdk_gc_destroy(gc);
   186     gdk_colormap_free_colors( cmap, &color, 1 );
   187 }
   189 void video_gtk_shutdown()
   190 {
   191     if( video_win != NULL ) {
   192 	video_glx_shutdown();
   193     }
   195 }
.