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 586:2a3ba82cf243
prev552:02ddb55a507b
next608:4f588e52bce0
author nkeynes
date Tue Jan 15 20:50:23 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Merged lxdream-mmu r570:596 to trunk
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 "drivers/video_glx.h"
    26 #include "drivers/gl_common.h"
    27 #include "pvr2/pvr2.h"
    28 #include "gtkui/gtkui.h"
    30 static GtkWidget *video_win = NULL;
    31 int video_width = 640;
    32 int video_height = 480;
    34 gboolean video_gtk_init();
    35 void video_gtk_shutdown();
    36 gboolean video_gtk_display_blank( uint32_t colour );
    37 uint16_t video_gtk_resolve_keysym( const gchar *keysym );
    39 struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
    40 					     video_gtk_resolve_keysym,
    41 					     NULL, NULL, NULL, NULL, NULL, 
    42 					     video_gtk_display_blank, NULL };
    44 /**
    45  * Extract the keyval of the key event if no modifier keys were pressed -
    46  * in other words get the keyval of the key by itself. The other way around
    47  * would be to use the hardware keysyms directly rather than the keyvals,
    48  * but the mapping looks to be messier.
    49  */
    50 uint16_t video_gtk_unmodified_keyval( GdkEventKey *event )
    51 {
    52     GdkKeymap *keymap = gdk_keymap_get_default();
    53     guint keyval;
    55     gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    56 					 NULL, NULL, NULL );
    57     return keyval;
    58 }
    60 gboolean video_gtk_keydown_callback(GtkWidget       *widget,
    61 				     GdkEventKey     *event,
    62 				     gpointer         user_data)
    63 {
    64     input_event_keydown( video_gtk_unmodified_keyval(event) );
    65     return TRUE;
    66 }
    68 gboolean video_gtk_keyup_callback(GtkWidget       *widget,
    69 				  GdkEventKey     *event,
    70 				  gpointer         user_data)
    71 {
    72     input_event_keyup( video_gtk_unmodified_keyval(event) );
    73     return TRUE;
    74 }
    76 uint16_t video_gtk_resolve_keysym( const gchar *keysym )
    77 {
    78     int val = gdk_keyval_from_name( keysym );
    79     if( val == GDK_VoidSymbol )
    80 	return 0;
    81     return (uint16_t)val;
    82 }
    84 gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
    85 {
    86     render_buffer_t buffer = pvr2_get_front_buffer();
    87     if( buffer == NULL ) {
    88 	display_gtk_driver.display_blank(pvr2_get_border_colour());
    89     } else {
    90 	display_gtk_driver.display_render_buffer(buffer);
    91     }
    92     return TRUE;
    93 }
    95 gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
    96 {
    97     video_width = event->width;
    98     video_height = event->height;
    99     video_gtk_expose_callback(widget, NULL, data);
   100     return TRUE;
   101 }
   103 gboolean video_gtk_init()
   104 {
   106     video_win = gtk_gui_get_renderarea();
   107     if( video_win == NULL ) {
   108 	return FALSE;
   109     }
   111     g_signal_connect( video_win, "key_press_event", 
   112 		      G_CALLBACK(video_gtk_keydown_callback), NULL );
   113     g_signal_connect( video_win, "key_release_event", 
   114 		      G_CALLBACK(video_gtk_keyup_callback), NULL );
   115     g_signal_connect( video_win, "expose_event",
   116 		      G_CALLBACK(video_gtk_expose_callback), NULL );
   117     g_signal_connect( video_win, "configure_event",
   118 		      G_CALLBACK(video_gtk_resize_callback), NULL );
   119     gtk_widget_add_events( video_win, 
   120 			   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
   121 			   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
   122     gtk_widget_set_double_buffered( video_win, FALSE );
   123     video_width = video_win->allocation.width;
   124     video_height = video_win->allocation.height;
   125     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(video_win)));
   126     Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(video_win)->window );
   127     if( ! video_glx_init_context( display, window ) ||
   128         ! video_glx_init_driver( &display_gtk_driver ) ) {
   129         return FALSE;
   130     }
   131     return TRUE;
   132 }
   134 gboolean video_gtk_display_blank( uint32_t colour )
   135 {
   136     GdkGC *gc = gdk_gc_new(video_win->window);
   137     GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
   138     GdkColormap *cmap = gdk_colormap_get_system();
   139     gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
   140     gdk_gc_set_foreground( gc, &color );
   141     gdk_gc_set_background( gc, &color );
   142     gdk_draw_rectangle( video_win->window, gc, TRUE, 0, 0, video_width, video_height );
   143     gdk_gc_destroy(gc);
   144     gdk_colormap_free_colors( cmap, &color, 1 );
   145 }
   147 void video_gtk_shutdown()
   148 {
   149     if( video_win != NULL ) {
   150 	video_glx_shutdown();
   151     }
   153 }
.