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 552:02ddb55a507b
prev545:fdcdcd8b9fd1
next561:533f6b478071
next586:2a3ba82cf243
author nkeynes
date Wed Dec 19 00:47:13 2007 +0000 (11 years ago)
permissions -rw-r--r--
last change Fix video_gtk_init() not returning a failing status if the glx init failed
Add a gtk-based blank method to prevent crash on expose
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@485
     2
 * $Id: video_gtk.c,v 1.19 2007-10-31 12:43:51 nkeynes Exp $
nkeynes@94
     3
 *
nkeynes@94
     4
 * The PC side of the video support (responsible for actually displaying / 
nkeynes@94
     5
 * rendering frames)
nkeynes@94
     6
 *
nkeynes@94
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@94
     8
 *
nkeynes@94
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@94
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@94
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@94
    12
 * (at your option) any later version.
nkeynes@94
    13
 *
nkeynes@94
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@94
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@94
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@94
    17
 * GNU General Public License for more details.
nkeynes@94
    18
 */
nkeynes@94
    19
nkeynes@94
    20
#include <gdk/gdkx.h>
nkeynes@485
    21
#include <gdk/gdkkeysyms.h>
nkeynes@94
    22
#include <stdint.h>
nkeynes@180
    23
#include "dream.h"
nkeynes@144
    24
#include "display.h"
nkeynes@545
    25
#include "drivers/video_glx.h"
nkeynes@481
    26
#include "drivers/gl_common.h"
nkeynes@537
    27
#include "gtkui/gtkui.h"
nkeynes@94
    28
nkeynes@436
    29
static GtkWidget *video_win = NULL;
nkeynes@436
    30
int video_width = 640;
nkeynes@436
    31
int video_height = 480;
nkeynes@94
    32
nkeynes@352
    33
gboolean video_gtk_init();
nkeynes@352
    34
void video_gtk_shutdown();
nkeynes@552
    35
gboolean video_gtk_display_blank( uint32_t colour );
nkeynes@144
    36
uint16_t video_gtk_resolve_keysym( const gchar *keysym );
nkeynes@94
    37
nkeynes@370
    38
struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
nkeynes@352
    39
					     video_gtk_resolve_keysym,
nkeynes@552
    40
					     NULL, NULL, NULL, NULL, NULL, 
nkeynes@552
    41
					     video_gtk_display_blank, NULL };
nkeynes@449
    42
nkeynes@449
    43
/**
nkeynes@449
    44
 * Extract the keyval of the key event if no modifier keys were pressed -
nkeynes@449
    45
 * in other words get the keyval of the key by itself. The other way around
nkeynes@449
    46
 * would be to use the hardware keysyms directly rather than the keyvals,
nkeynes@449
    47
 * but the mapping looks to be messier.
nkeynes@449
    48
 */
nkeynes@449
    49
uint16_t video_gtk_unmodified_keyval( GdkEventKey *event )
nkeynes@449
    50
{
nkeynes@449
    51
    GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@449
    52
    guint keyval;
nkeynes@449
    53
    
nkeynes@449
    54
    gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@449
    55
					 NULL, NULL, NULL );
nkeynes@449
    56
    return keyval;
nkeynes@449
    57
}
nkeynes@144
    58
nkeynes@144
    59
gboolean video_gtk_keydown_callback(GtkWidget       *widget,
nkeynes@144
    60
				     GdkEventKey     *event,
nkeynes@144
    61
				     gpointer         user_data)
nkeynes@144
    62
{
nkeynes@449
    63
    input_event_keydown( video_gtk_unmodified_keyval(event) );
nkeynes@449
    64
    return TRUE;
nkeynes@449
    65
}
nkeynes@449
    66
nkeynes@449
    67
gboolean video_gtk_keyup_callback(GtkWidget       *widget,
nkeynes@449
    68
				  GdkEventKey     *event,
nkeynes@449
    69
				  gpointer         user_data)
nkeynes@449
    70
{
nkeynes@449
    71
    input_event_keyup( video_gtk_unmodified_keyval(event) );
nkeynes@424
    72
    return TRUE;
nkeynes@144
    73
}
nkeynes@144
    74
nkeynes@144
    75
uint16_t video_gtk_resolve_keysym( const gchar *keysym )
nkeynes@144
    76
{
nkeynes@144
    77
    int val = gdk_keyval_from_name( keysym );
nkeynes@144
    78
    if( val == GDK_VoidSymbol )
nkeynes@144
    79
	return 0;
nkeynes@144
    80
    return (uint16_t)val;
nkeynes@144
    81
}
nkeynes@144
    82
nkeynes@435
    83
gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
nkeynes@435
    84
{
nkeynes@545
    85
    render_buffer_t buffer = pvr2_get_front_buffer();
nkeynes@545
    86
    if( buffer == NULL ) {
nkeynes@545
    87
	display_gtk_driver.display_blank(pvr2_get_border_colour());
nkeynes@545
    88
    } else {
nkeynes@545
    89
	display_gtk_driver.display_render_buffer(buffer);
nkeynes@545
    90
    }
nkeynes@443
    91
    return TRUE;
nkeynes@435
    92
}
nkeynes@435
    93
nkeynes@436
    94
gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
nkeynes@436
    95
{
nkeynes@436
    96
    video_width = event->width;
nkeynes@436
    97
    video_height = event->height;
nkeynes@545
    98
    video_gtk_expose_callback(widget, NULL, data);
nkeynes@443
    99
    return TRUE;
nkeynes@436
   100
}
nkeynes@436
   101
nkeynes@352
   102
gboolean video_gtk_init()
nkeynes@94
   103
{
nkeynes@545
   104
  
nkeynes@435
   105
    video_win = gtk_gui_get_renderarea();
nkeynes@530
   106
    if( video_win == NULL ) {
nkeynes@530
   107
	return FALSE;
nkeynes@530
   108
    }
nkeynes@435
   109
nkeynes@352
   110
    g_signal_connect( video_win, "key_press_event", 
nkeynes@352
   111
		      G_CALLBACK(video_gtk_keydown_callback), NULL );
nkeynes@352
   112
    g_signal_connect( video_win, "key_release_event", 
nkeynes@352
   113
		      G_CALLBACK(video_gtk_keyup_callback), NULL );
nkeynes@435
   114
    g_signal_connect( video_win, "expose_event",
nkeynes@435
   115
		      G_CALLBACK(video_gtk_expose_callback), NULL );
nkeynes@436
   116
    g_signal_connect( video_win, "configure_event",
nkeynes@436
   117
		      G_CALLBACK(video_gtk_resize_callback), NULL );
nkeynes@435
   118
    gtk_widget_add_events( video_win, 
nkeynes@352
   119
			   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
nkeynes@352
   120
			   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
nkeynes@443
   121
    gtk_widget_set_double_buffered( video_win, FALSE );
nkeynes@436
   122
    video_width = video_win->allocation.width;
nkeynes@436
   123
    video_height = video_win->allocation.height;
nkeynes@545
   124
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(video_win)));
nkeynes@545
   125
    Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(video_win)->window );
nkeynes@552
   126
    if( ! video_glx_init_context( display, window ) ||
nkeynes@552
   127
        ! video_glx_init_driver( &display_gtk_driver ) ) {
nkeynes@552
   128
        return FALSE;
nkeynes@552
   129
    }
nkeynes@552
   130
    return TRUE;
nkeynes@552
   131
}
nkeynes@552
   132
nkeynes@552
   133
gboolean video_gtk_display_blank( uint32_t colour )
nkeynes@552
   134
{
nkeynes@552
   135
    GdkGC *gc = gdk_gc_new(video_win->window);
nkeynes@552
   136
    GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
nkeynes@552
   137
    GdkColormap *cmap = gdk_colormap_get_system();
nkeynes@552
   138
    gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
nkeynes@552
   139
    gdk_gc_set_foreground( gc, &color );
nkeynes@552
   140
    gdk_gc_set_background( gc, &color );
nkeynes@552
   141
    gdk_draw_rectangle( video_win->window, gc, TRUE, 0, 0, video_width, video_height );
nkeynes@552
   142
    gdk_gc_destroy(gc);
nkeynes@552
   143
    gdk_colormap_free_colors( cmap, &color, 1 );
nkeynes@94
   144
}
nkeynes@94
   145
nkeynes@352
   146
void video_gtk_shutdown()
nkeynes@352
   147
{
nkeynes@530
   148
    if( video_win != NULL ) {
nkeynes@530
   149
	video_glx_shutdown();
nkeynes@530
   150
    }
nkeynes@352
   151
nkeynes@94
   152
}
nkeynes@94
   153
.