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 561:533f6b478071
prev552:02ddb55a507b
next565:a44f0465bbbe
author nkeynes
date Tue Jan 01 05:48:06 2008 +0000 (16 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Add configure and command-line parameters to perform IO tracing
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 "gtkui/gtkui.h"
    29 static GtkWidget *video_win = NULL;
    30 int video_width = 640;
    31 int video_height = 480;
    33 gboolean video_gtk_init();
    34 void video_gtk_shutdown();
    35 gboolean video_gtk_display_blank( uint32_t colour );
    36 uint16_t video_gtk_resolve_keysym( const gchar *keysym );
    38 struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
    39 					     video_gtk_resolve_keysym,
    40 					     NULL, NULL, NULL, NULL, NULL, 
    41 					     video_gtk_display_blank, NULL };
    43 /**
    44  * Extract the keyval of the key event if no modifier keys were pressed -
    45  * in other words get the keyval of the key by itself. The other way around
    46  * would be to use the hardware keysyms directly rather than the keyvals,
    47  * but the mapping looks to be messier.
    48  */
    49 uint16_t video_gtk_unmodified_keyval( GdkEventKey *event )
    50 {
    51     GdkKeymap *keymap = gdk_keymap_get_default();
    52     guint keyval;
    54     gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    55 					 NULL, NULL, NULL );
    56     return keyval;
    57 }
    59 gboolean video_gtk_keydown_callback(GtkWidget       *widget,
    60 				     GdkEventKey     *event,
    61 				     gpointer         user_data)
    62 {
    63     input_event_keydown( video_gtk_unmodified_keyval(event) );
    64     return TRUE;
    65 }
    67 gboolean video_gtk_keyup_callback(GtkWidget       *widget,
    68 				  GdkEventKey     *event,
    69 				  gpointer         user_data)
    70 {
    71     input_event_keyup( video_gtk_unmodified_keyval(event) );
    72     return TRUE;
    73 }
    75 uint16_t video_gtk_resolve_keysym( const gchar *keysym )
    76 {
    77     int val = gdk_keyval_from_name( keysym );
    78     if( val == GDK_VoidSymbol )
    79 	return 0;
    80     return (uint16_t)val;
    81 }
    83 gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
    84 {
    85     render_buffer_t buffer = pvr2_get_front_buffer();
    86     if( buffer == NULL ) {
    87 	display_gtk_driver.display_blank(pvr2_get_border_colour());
    88     } else {
    89 	display_gtk_driver.display_render_buffer(buffer);
    90     }
    91     return TRUE;
    92 }
    94 gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
    95 {
    96     video_width = event->width;
    97     video_height = event->height;
    98     video_gtk_expose_callback(widget, NULL, data);
    99     return TRUE;
   100 }
   102 gboolean video_gtk_init()
   103 {
   105     video_win = gtk_gui_get_renderarea();
   106     if( video_win == NULL ) {
   107 	return FALSE;
   108     }
   110     g_signal_connect( video_win, "key_press_event", 
   111 		      G_CALLBACK(video_gtk_keydown_callback), NULL );
   112     g_signal_connect( video_win, "key_release_event", 
   113 		      G_CALLBACK(video_gtk_keyup_callback), NULL );
   114     g_signal_connect( video_win, "expose_event",
   115 		      G_CALLBACK(video_gtk_expose_callback), NULL );
   116     g_signal_connect( video_win, "configure_event",
   117 		      G_CALLBACK(video_gtk_resize_callback), NULL );
   118     gtk_widget_add_events( video_win, 
   119 			   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
   120 			   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
   121     gtk_widget_set_double_buffered( video_win, FALSE );
   122     video_width = video_win->allocation.width;
   123     video_height = video_win->allocation.height;
   124     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(video_win)));
   125     Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(video_win)->window );
   126     if( ! video_glx_init_context( display, window ) ||
   127         ! video_glx_init_driver( &display_gtk_driver ) ) {
   128         return FALSE;
   129     }
   130     return TRUE;
   131 }
   133 gboolean video_gtk_display_blank( uint32_t colour )
   134 {
   135     GdkGC *gc = gdk_gc_new(video_win->window);
   136     GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
   137     GdkColormap *cmap = gdk_colormap_get_system();
   138     gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
   139     gdk_gc_set_foreground( gc, &color );
   140     gdk_gc_set_background( gc, &color );
   141     gdk_draw_rectangle( video_win->window, gc, TRUE, 0, 0, video_width, video_height );
   142     gdk_gc_destroy(gc);
   143     gdk_colormap_free_colors( cmap, &color, 1 );
   144 }
   146 void video_gtk_shutdown()
   147 {
   148     if( video_win != NULL ) {
   149 	video_glx_shutdown();
   150     }
   152 }
.