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 653:3202ff01d48e
prev632:740e7b35a857
next658:f5926310bfbe
author nkeynes
date Fri Mar 28 12:32:25 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Merge lxdream-render branch (643:670) to trunk
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@586
     2
 * $Id$
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@608
    25
#include "dckeysyms.h"
nkeynes@545
    26
#include "drivers/video_glx.h"
nkeynes@653
    27
#include "drivers/video_gl.h"
nkeynes@586
    28
#include "pvr2/pvr2.h"
nkeynes@537
    29
#include "gtkui/gtkui.h"
nkeynes@94
    30
nkeynes@653
    31
GtkWidget *gtk_video_win = NULL;
nkeynes@436
    32
int video_width = 640;
nkeynes@436
    33
int video_height = 480;
nkeynes@94
    34
nkeynes@352
    35
gboolean video_gtk_init();
nkeynes@352
    36
void video_gtk_shutdown();
nkeynes@552
    37
gboolean video_gtk_display_blank( uint32_t colour );
nkeynes@144
    38
uint16_t video_gtk_resolve_keysym( const gchar *keysym );
nkeynes@614
    39
uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode);
nkeynes@94
    40
nkeynes@370
    41
struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
nkeynes@352
    42
					     video_gtk_resolve_keysym,
nkeynes@608
    43
					     video_gtk_keycode_to_dckeysym,
nkeynes@614
    44
					     NULL,
nkeynes@552
    45
					     NULL, NULL, NULL, NULL, NULL, 
nkeynes@552
    46
					     video_gtk_display_blank, NULL };
nkeynes@449
    47
nkeynes@144
    48
uint16_t video_gtk_resolve_keysym( const gchar *keysym )
nkeynes@144
    49
{
nkeynes@144
    50
    int val = gdk_keyval_from_name( keysym );
nkeynes@144
    51
    if( val == GDK_VoidSymbol )
nkeynes@144
    52
	return 0;
nkeynes@144
    53
    return (uint16_t)val;
nkeynes@144
    54
}
nkeynes@144
    55
nkeynes@435
    56
gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
nkeynes@435
    57
{
nkeynes@545
    58
    render_buffer_t buffer = pvr2_get_front_buffer();
nkeynes@545
    59
    if( buffer == NULL ) {
nkeynes@545
    60
	display_gtk_driver.display_blank(pvr2_get_border_colour());
nkeynes@545
    61
    } else {
nkeynes@545
    62
	display_gtk_driver.display_render_buffer(buffer);
nkeynes@545
    63
    }
nkeynes@443
    64
    return TRUE;
nkeynes@435
    65
}
nkeynes@435
    66
nkeynes@436
    67
gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
nkeynes@436
    68
{
nkeynes@436
    69
    video_width = event->width;
nkeynes@436
    70
    video_height = event->height;
nkeynes@545
    71
    video_gtk_expose_callback(widget, NULL, data);
nkeynes@443
    72
    return TRUE;
nkeynes@436
    73
}
nkeynes@436
    74
nkeynes@614
    75
uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode)
nkeynes@608
    76
{
nkeynes@608
    77
    if( keycode >= 'a' && keycode <= 'z' ) {
nkeynes@608
    78
	return (keycode - 'a') + DCKB_A;
nkeynes@608
    79
    } else if( keycode >= '1' && keycode <= '9' ) {
nkeynes@608
    80
	return (keycode - '1') + DCKB_1;
nkeynes@608
    81
    }
nkeynes@608
    82
    switch(keycode) {
nkeynes@609
    83
    case GDK_0:         return DCKB_0;
nkeynes@609
    84
    case GDK_Return:    return DCKB_ENTER;
nkeynes@609
    85
    case GDK_Escape:    return DCKB_ESCAPE;
nkeynes@609
    86
    case GDK_BackSpace: return DCKB_BACKSPACE;
nkeynes@609
    87
    case GDK_Tab:       return DCKB_TAB;
nkeynes@609
    88
    case GDK_space:     return DCKB_SPACE;
nkeynes@609
    89
    case GDK_minus:     return DCKB_MINUS;
nkeynes@609
    90
    case GDK_equal:     return DCKB_EQUAL;
nkeynes@609
    91
    case GDK_bracketleft: return DCKB_LBRACKET;
nkeynes@609
    92
    case GDK_bracketright: return DCKB_RBRACKET;
nkeynes@609
    93
    case GDK_semicolon: return DCKB_SEMICOLON;
nkeynes@609
    94
    case GDK_apostrophe:return DCKB_QUOTE;
nkeynes@609
    95
    case GDK_grave : return DCKB_BACKQUOTE;
nkeynes@609
    96
    case GDK_comma:     return DCKB_COMMA;
nkeynes@609
    97
    case GDK_period:    return DCKB_PERIOD;
nkeynes@609
    98
    case GDK_slash:     return DCKB_SLASH; 
nkeynes@609
    99
    case GDK_Caps_Lock: return DCKB_CAPSLOCK;
nkeynes@609
   100
    case GDK_F1:        return DCKB_F1;
nkeynes@609
   101
    case GDK_F2:        return DCKB_F2;
nkeynes@609
   102
    case GDK_F3:        return DCKB_F3;
nkeynes@609
   103
    case GDK_F4:        return DCKB_F4;
nkeynes@609
   104
    case GDK_F5:        return DCKB_F5;
nkeynes@609
   105
    case GDK_F6:        return DCKB_F6;
nkeynes@609
   106
    case GDK_F7:        return DCKB_F7;
nkeynes@609
   107
    case GDK_F8:        return DCKB_F8;
nkeynes@609
   108
    case GDK_F9:        return DCKB_F9;
nkeynes@609
   109
    case GDK_F10:       return DCKB_F10;
nkeynes@609
   110
    case GDK_F11:       return DCKB_F11;
nkeynes@609
   111
    case GDK_F12:       return DCKB_F12;
nkeynes@609
   112
    case GDK_Scroll_Lock: return DCKB_SCROLLLOCK;
nkeynes@609
   113
    case GDK_Pause:     return DCKB_PAUSE;
nkeynes@609
   114
    case GDK_Insert:    return DCKB_INSERT;
nkeynes@609
   115
    case GDK_Home:      return DCKB_HOME;
nkeynes@609
   116
    case GDK_Page_Up:   return DCKB_PAGEUP;
nkeynes@609
   117
    case GDK_Delete:    return DCKB_DELETE;
nkeynes@609
   118
    case GDK_End:       return DCKB_END;
nkeynes@609
   119
    case GDK_Page_Down: return DCKB_PAGEDOWN;
nkeynes@609
   120
    case GDK_Right:     return DCKB_RIGHT;
nkeynes@609
   121
    case GDK_Left:      return DCKB_LEFT;
nkeynes@609
   122
    case GDK_Down:      return DCKB_DOWN;
nkeynes@609
   123
    case GDK_Up:        return DCKB_UP;
nkeynes@609
   124
    case GDK_Num_Lock:  return DCKB_NUMLOCK;
nkeynes@609
   125
    case GDK_KP_Divide: return DCKB_KP_SLASH;
nkeynes@609
   126
    case GDK_KP_Multiply: return DCKB_KP_STAR;
nkeynes@609
   127
    case GDK_KP_Subtract: return DCKB_KP_MINUS;
nkeynes@609
   128
    case GDK_KP_Add:    return DCKB_KP_PLUS;
nkeynes@609
   129
    case GDK_KP_Enter:  return DCKB_KP_ENTER;
nkeynes@631
   130
    case GDK_KP_End:    return DCKB_KP_1;
nkeynes@631
   131
    case GDK_KP_Down:   return DCKB_KP_2;
nkeynes@631
   132
    case GDK_KP_Page_Down: return DCKB_KP_3;
nkeynes@631
   133
    case GDK_KP_Left:   return DCKB_KP_4;
nkeynes@631
   134
    case GDK_KP_Begin:  return DCKB_KP_5;
nkeynes@631
   135
    case GDK_KP_Right:  return DCKB_KP_6;
nkeynes@631
   136
    case GDK_KP_Home:   return DCKB_KP_7;
nkeynes@631
   137
    case GDK_KP_Up:     return DCKB_KP_8;
nkeynes@631
   138
    case GDK_KP_Page_Up:return DCKB_KP_9;
nkeynes@631
   139
    case GDK_KP_Insert: return DCKB_KP_0;
nkeynes@632
   140
    case GDK_KP_Delete: return DCKB_KP_PERIOD;
nkeynes@609
   141
    case GDK_backslash: return DCKB_BACKSLASH;
nkeynes@609
   142
    case GDK_Control_L: return DCKB_CONTROL_L;
nkeynes@609
   143
    case GDK_Shift_L:   return DCKB_SHIFT_L;
nkeynes@609
   144
    case GDK_Alt_L:     return DCKB_ALT_L;
nkeynes@609
   145
    case GDK_Meta_L:    return DCKB_S1;
nkeynes@609
   146
    case GDK_Control_R: return DCKB_CONTROL_R;
nkeynes@609
   147
    case GDK_Shift_R:   return DCKB_SHIFT_R;
nkeynes@609
   148
    case GDK_Alt_R:     return DCKB_ALT_R;
nkeynes@609
   149
    case GDK_Meta_R:    return DCKB_S2;
nkeynes@608
   150
    }
nkeynes@608
   151
    return DCKB_NONE;
nkeynes@608
   152
}
nkeynes@608
   153
nkeynes@352
   154
gboolean video_gtk_init()
nkeynes@94
   155
{
nkeynes@545
   156
  
nkeynes@653
   157
    gtk_video_win = gtk_gui_get_renderarea();
nkeynes@653
   158
    if( gtk_video_win == NULL ) {
nkeynes@530
   159
	return FALSE;
nkeynes@530
   160
    }
nkeynes@435
   161
nkeynes@653
   162
    g_signal_connect( gtk_video_win, "expose_event",
nkeynes@435
   163
		      G_CALLBACK(video_gtk_expose_callback), NULL );
nkeynes@653
   164
    g_signal_connect( gtk_video_win, "configure_event",
nkeynes@436
   165
		      G_CALLBACK(video_gtk_resize_callback), NULL );
nkeynes@653
   166
    video_width = gtk_video_win->allocation.width;
nkeynes@653
   167
    video_height = gtk_video_win->allocation.height;
nkeynes@653
   168
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(gtk_video_win)));
nkeynes@653
   169
    Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(gtk_video_win)->window );
nkeynes@653
   170
#ifdef HAVE_LIBOSMESA
nkeynes@653
   171
    video_gdk_init_driver( &display_gtk_driver );
nkeynes@653
   172
#else
nkeynes@552
   173
    if( ! video_glx_init_context( display, window ) ||
nkeynes@552
   174
        ! video_glx_init_driver( &display_gtk_driver ) ) {
nkeynes@552
   175
        return FALSE;
nkeynes@552
   176
    }
nkeynes@653
   177
#endif
nkeynes@653
   178
nkeynes@614
   179
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@614
   180
    linux_joystick_init();
nkeynes@614
   181
#endif
nkeynes@552
   182
    return TRUE;
nkeynes@552
   183
}
nkeynes@552
   184
nkeynes@552
   185
gboolean video_gtk_display_blank( uint32_t colour )
nkeynes@552
   186
{
nkeynes@653
   187
    GdkGC *gc = gdk_gc_new(gtk_video_win->window);
nkeynes@552
   188
    GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
nkeynes@552
   189
    GdkColormap *cmap = gdk_colormap_get_system();
nkeynes@552
   190
    gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
nkeynes@552
   191
    gdk_gc_set_foreground( gc, &color );
nkeynes@552
   192
    gdk_gc_set_background( gc, &color );
nkeynes@653
   193
    gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, video_height );
nkeynes@552
   194
    gdk_gc_destroy(gc);
nkeynes@552
   195
    gdk_colormap_free_colors( cmap, &color, 1 );
nkeynes@94
   196
}
nkeynes@94
   197
nkeynes@653
   198
XVisualInfo *video_gtk_get_visual()
nkeynes@653
   199
{
nkeynes@653
   200
#ifdef HAVE_LIBOSMESA
nkeynes@653
   201
    return NULL;
nkeynes@653
   202
#else
nkeynes@653
   203
    return video_glx_get_visual();
nkeynes@653
   204
#endif
nkeynes@653
   205
}
nkeynes@653
   206
nkeynes@352
   207
void video_gtk_shutdown()
nkeynes@352
   208
{
nkeynes@653
   209
    if( gtk_video_win != NULL ) {
nkeynes@653
   210
#ifdef HAVE_LIBOSMESA
nkeynes@653
   211
        video_gdk_shutdown();
nkeynes@653
   212
#else
nkeynes@530
   213
	video_glx_shutdown();
nkeynes@653
   214
#endif
nkeynes@530
   215
    }
nkeynes@620
   216
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@620
   217
    linux_joystick_shutdown();
nkeynes@620
   218
#endif
nkeynes@94
   219
}
nkeynes@94
   220
.