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 659:6b1dff1575b3
prev658:f5926310bfbe
next661:99d9494955a9
author nkeynes
date Wed Apr 16 22:54:40 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Move drawable creation to video_gtk.c - removes the last platform dependency from gtkui
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@485
    20
#include <gdk/gdkkeysyms.h>
nkeynes@94
    21
#include <stdint.h>
nkeynes@658
    22
#include "lxdream.h"
nkeynes@144
    23
#include "display.h"
nkeynes@608
    24
#include "dckeysyms.h"
nkeynes@653
    25
#include "drivers/video_gl.h"
nkeynes@586
    26
#include "pvr2/pvr2.h"
nkeynes@537
    27
#include "gtkui/gtkui.h"
nkeynes@94
    28
nkeynes@658
    29
#ifdef HAVE_GTK_X11
nkeynes@658
    30
nkeynes@658
    31
#include <gdk/gdkx.h>
nkeynes@658
    32
#include "drivers/video_glx.h"
nkeynes@658
    33
nkeynes@658
    34
/************* X11-specificness **********/
nkeynes@658
    35
nkeynes@658
    36
guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
nkeynes@658
    37
{
nkeynes@658
    38
  int i;
nkeynes@658
    39
  int result = 0;
nkeynes@658
    40
  Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@658
    41
  XModifierKeymap *keymap = XGetModifierMapping( xdisplay );
nkeynes@658
    42
  for( i=0; i<8*keymap->max_keypermod; i++ ) {
nkeynes@658
    43
    if( keymap->modifiermap[i] == keycode ) {
nkeynes@658
    44
      result = 1 << (i/keymap->max_keypermod);
nkeynes@658
    45
      break;
nkeynes@658
    46
    }
nkeynes@658
    47
  }
nkeynes@658
    48
  XFreeModifiermap(keymap);
nkeynes@658
    49
  return result;
nkeynes@658
    50
}
nkeynes@658
    51
nkeynes@658
    52
#if !(GTK_CHECK_VERSION(2,8,0))
nkeynes@658
    53
/* gdk_display_warp_pointer was added in GTK 2.8. If we're using an earlier
nkeynes@658
    54
 * version, include the code here. (Can't just set the dependency on 2.8 as
nkeynes@658
    55
 * it still hasn't been included in fink yet...) Original copyright statement
nkeynes@658
    56
 * below.
nkeynes@658
    57
 */
nkeynes@658
    58
nkeynes@658
    59
/* GDK - The GIMP Drawing Kit
nkeynes@658
    60
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
nkeynes@658
    61
 *
nkeynes@658
    62
 * This library is free software; you can redistribute it and/or
nkeynes@658
    63
 * modify it under the terms of the GNU Lesser General Public
nkeynes@658
    64
 * License as published by the Free Software Foundation; either
nkeynes@658
    65
 * version 2 of the License, or (at your option) any later version.
nkeynes@658
    66
 *
nkeynes@658
    67
 * This library is distributed in the hope that it will be useful,
nkeynes@658
    68
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@658
    69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nkeynes@658
    70
 * Lesser General Public License for more details.
nkeynes@658
    71
 *
nkeynes@658
    72
 * You should have received a copy of the GNU Lesser General Public
nkeynes@658
    73
 * License along with this library; if not, write to the
nkeynes@658
    74
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
nkeynes@658
    75
 * Boston, MA 02111-1307, USA.
nkeynes@658
    76
 */
nkeynes@658
    77
nkeynes@658
    78
/*
nkeynes@658
    79
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
nkeynes@658
    80
 * file for a list of people on the GTK+ Team.  See the ChangeLog
nkeynes@658
    81
 * files for a list of changes.  These files are distributed with
nkeynes@658
    82
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
nkeynes@658
    83
 */
nkeynes@658
    84
void gdk_display_warp_pointer (GdkDisplay *display,
nkeynes@658
    85
                          GdkScreen  *screen,
nkeynes@658
    86
                          gint        x,
nkeynes@658
    87
                          gint        y)
nkeynes@658
    88
{
nkeynes@658
    89
  Display *xdisplay;
nkeynes@658
    90
  Window dest;
nkeynes@658
    91
nkeynes@658
    92
  xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@658
    93
  dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
nkeynes@658
    94
nkeynes@658
    95
  XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);  
nkeynes@658
    96
}
nkeynes@658
    97
nkeynes@658
    98
#endif
nkeynes@658
    99
nkeynes@658
   100
#endif
nkeynes@659
   101
nkeynes@659
   102
static GtkWidget *gtk_video_drawable = NULL;
nkeynes@436
   103
int video_width = 640;
nkeynes@436
   104
int video_height = 480;
nkeynes@94
   105
nkeynes@352
   106
gboolean video_gtk_init();
nkeynes@352
   107
void video_gtk_shutdown();
nkeynes@552
   108
gboolean video_gtk_display_blank( uint32_t colour );
nkeynes@144
   109
uint16_t video_gtk_resolve_keysym( const gchar *keysym );
nkeynes@614
   110
uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode);
nkeynes@94
   111
nkeynes@370
   112
struct display_driver display_gtk_driver = { "gtk", video_gtk_init, video_gtk_shutdown,
nkeynes@352
   113
					     video_gtk_resolve_keysym,
nkeynes@608
   114
					     video_gtk_keycode_to_dckeysym,
nkeynes@614
   115
					     NULL,
nkeynes@552
   116
					     NULL, NULL, NULL, NULL, NULL, 
nkeynes@552
   117
					     video_gtk_display_blank, NULL };
nkeynes@449
   118
nkeynes@144
   119
uint16_t video_gtk_resolve_keysym( const gchar *keysym )
nkeynes@144
   120
{
nkeynes@144
   121
    int val = gdk_keyval_from_name( keysym );
nkeynes@144
   122
    if( val == GDK_VoidSymbol )
nkeynes@144
   123
	return 0;
nkeynes@144
   124
    return (uint16_t)val;
nkeynes@144
   125
}
nkeynes@144
   126
nkeynes@435
   127
gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
nkeynes@435
   128
{
nkeynes@545
   129
    render_buffer_t buffer = pvr2_get_front_buffer();
nkeynes@545
   130
    if( buffer == NULL ) {
nkeynes@545
   131
	display_gtk_driver.display_blank(pvr2_get_border_colour());
nkeynes@545
   132
    } else {
nkeynes@545
   133
	display_gtk_driver.display_render_buffer(buffer);
nkeynes@545
   134
    }
nkeynes@443
   135
    return TRUE;
nkeynes@435
   136
}
nkeynes@435
   137
nkeynes@436
   138
gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
nkeynes@436
   139
{
nkeynes@436
   140
    video_width = event->width;
nkeynes@436
   141
    video_height = event->height;
nkeynes@545
   142
    video_gtk_expose_callback(widget, NULL, data);
nkeynes@443
   143
    return TRUE;
nkeynes@436
   144
}
nkeynes@436
   145
nkeynes@614
   146
uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode)
nkeynes@608
   147
{
nkeynes@608
   148
    if( keycode >= 'a' && keycode <= 'z' ) {
nkeynes@608
   149
	return (keycode - 'a') + DCKB_A;
nkeynes@608
   150
    } else if( keycode >= '1' && keycode <= '9' ) {
nkeynes@608
   151
	return (keycode - '1') + DCKB_1;
nkeynes@608
   152
    }
nkeynes@608
   153
    switch(keycode) {
nkeynes@609
   154
    case GDK_0:         return DCKB_0;
nkeynes@609
   155
    case GDK_Return:    return DCKB_ENTER;
nkeynes@609
   156
    case GDK_Escape:    return DCKB_ESCAPE;
nkeynes@609
   157
    case GDK_BackSpace: return DCKB_BACKSPACE;
nkeynes@609
   158
    case GDK_Tab:       return DCKB_TAB;
nkeynes@609
   159
    case GDK_space:     return DCKB_SPACE;
nkeynes@609
   160
    case GDK_minus:     return DCKB_MINUS;
nkeynes@609
   161
    case GDK_equal:     return DCKB_EQUAL;
nkeynes@609
   162
    case GDK_bracketleft: return DCKB_LBRACKET;
nkeynes@609
   163
    case GDK_bracketright: return DCKB_RBRACKET;
nkeynes@609
   164
    case GDK_semicolon: return DCKB_SEMICOLON;
nkeynes@609
   165
    case GDK_apostrophe:return DCKB_QUOTE;
nkeynes@609
   166
    case GDK_grave : return DCKB_BACKQUOTE;
nkeynes@609
   167
    case GDK_comma:     return DCKB_COMMA;
nkeynes@609
   168
    case GDK_period:    return DCKB_PERIOD;
nkeynes@609
   169
    case GDK_slash:     return DCKB_SLASH; 
nkeynes@609
   170
    case GDK_Caps_Lock: return DCKB_CAPSLOCK;
nkeynes@609
   171
    case GDK_F1:        return DCKB_F1;
nkeynes@609
   172
    case GDK_F2:        return DCKB_F2;
nkeynes@609
   173
    case GDK_F3:        return DCKB_F3;
nkeynes@609
   174
    case GDK_F4:        return DCKB_F4;
nkeynes@609
   175
    case GDK_F5:        return DCKB_F5;
nkeynes@609
   176
    case GDK_F6:        return DCKB_F6;
nkeynes@609
   177
    case GDK_F7:        return DCKB_F7;
nkeynes@609
   178
    case GDK_F8:        return DCKB_F8;
nkeynes@609
   179
    case GDK_F9:        return DCKB_F9;
nkeynes@609
   180
    case GDK_F10:       return DCKB_F10;
nkeynes@609
   181
    case GDK_F11:       return DCKB_F11;
nkeynes@609
   182
    case GDK_F12:       return DCKB_F12;
nkeynes@609
   183
    case GDK_Scroll_Lock: return DCKB_SCROLLLOCK;
nkeynes@609
   184
    case GDK_Pause:     return DCKB_PAUSE;
nkeynes@609
   185
    case GDK_Insert:    return DCKB_INSERT;
nkeynes@609
   186
    case GDK_Home:      return DCKB_HOME;
nkeynes@609
   187
    case GDK_Page_Up:   return DCKB_PAGEUP;
nkeynes@609
   188
    case GDK_Delete:    return DCKB_DELETE;
nkeynes@609
   189
    case GDK_End:       return DCKB_END;
nkeynes@609
   190
    case GDK_Page_Down: return DCKB_PAGEDOWN;
nkeynes@609
   191
    case GDK_Right:     return DCKB_RIGHT;
nkeynes@609
   192
    case GDK_Left:      return DCKB_LEFT;
nkeynes@609
   193
    case GDK_Down:      return DCKB_DOWN;
nkeynes@609
   194
    case GDK_Up:        return DCKB_UP;
nkeynes@609
   195
    case GDK_Num_Lock:  return DCKB_NUMLOCK;
nkeynes@609
   196
    case GDK_KP_Divide: return DCKB_KP_SLASH;
nkeynes@609
   197
    case GDK_KP_Multiply: return DCKB_KP_STAR;
nkeynes@609
   198
    case GDK_KP_Subtract: return DCKB_KP_MINUS;
nkeynes@609
   199
    case GDK_KP_Add:    return DCKB_KP_PLUS;
nkeynes@609
   200
    case GDK_KP_Enter:  return DCKB_KP_ENTER;
nkeynes@631
   201
    case GDK_KP_End:    return DCKB_KP_1;
nkeynes@631
   202
    case GDK_KP_Down:   return DCKB_KP_2;
nkeynes@631
   203
    case GDK_KP_Page_Down: return DCKB_KP_3;
nkeynes@631
   204
    case GDK_KP_Left:   return DCKB_KP_4;
nkeynes@631
   205
    case GDK_KP_Begin:  return DCKB_KP_5;
nkeynes@631
   206
    case GDK_KP_Right:  return DCKB_KP_6;
nkeynes@631
   207
    case GDK_KP_Home:   return DCKB_KP_7;
nkeynes@631
   208
    case GDK_KP_Up:     return DCKB_KP_8;
nkeynes@631
   209
    case GDK_KP_Page_Up:return DCKB_KP_9;
nkeynes@631
   210
    case GDK_KP_Insert: return DCKB_KP_0;
nkeynes@632
   211
    case GDK_KP_Delete: return DCKB_KP_PERIOD;
nkeynes@609
   212
    case GDK_backslash: return DCKB_BACKSLASH;
nkeynes@609
   213
    case GDK_Control_L: return DCKB_CONTROL_L;
nkeynes@609
   214
    case GDK_Shift_L:   return DCKB_SHIFT_L;
nkeynes@609
   215
    case GDK_Alt_L:     return DCKB_ALT_L;
nkeynes@609
   216
    case GDK_Meta_L:    return DCKB_S1;
nkeynes@609
   217
    case GDK_Control_R: return DCKB_CONTROL_R;
nkeynes@609
   218
    case GDK_Shift_R:   return DCKB_SHIFT_R;
nkeynes@609
   219
    case GDK_Alt_R:     return DCKB_ALT_R;
nkeynes@609
   220
    case GDK_Meta_R:    return DCKB_S2;
nkeynes@608
   221
    }
nkeynes@608
   222
    return DCKB_NONE;
nkeynes@608
   223
}
nkeynes@608
   224
nkeynes@659
   225
GtkWidget *video_gtk_create_drawable()
nkeynes@659
   226
{
nkeynes@659
   227
    GtkWidget *drawable = gtk_drawing_area_new();
nkeynes@659
   228
    GTK_WIDGET_SET_FLAGS(drawable, GTK_CAN_FOCUS|GTK_CAN_DEFAULT);
nkeynes@659
   229
nkeynes@659
   230
    g_signal_connect( drawable, "expose_event",
nkeynes@659
   231
		      G_CALLBACK(video_gtk_expose_callback), NULL );
nkeynes@659
   232
    g_signal_connect( drawable, "configure_event",
nkeynes@659
   233
		      G_CALLBACK(video_gtk_resize_callback), NULL );
nkeynes@659
   234
nkeynes@659
   235
#ifdef HAVE_GLX
nkeynes@659
   236
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(drawable));
nkeynes@659
   237
    Screen *screen = gdk_x11_screen_get_xscreen( gtk_widget_get_screen(drawable));
nkeynes@659
   238
    int screen_no = XScreenNumberOfScreen(screen);
nkeynes@659
   239
    if( !video_glx_init(display, screen_no) ) {
nkeynes@659
   240
        ERROR( "Unable to initialize GLX, aborting" );
nkeynes@659
   241
        exit(3);
nkeynes@659
   242
    }
nkeynes@659
   243
nkeynes@659
   244
    XVisualInfo *visual = video_glx_get_visual();
nkeynes@659
   245
    if( visual != NULL ) {
nkeynes@659
   246
        GdkVisual *gdkvis = gdk_x11_screen_lookup_visual( gtk_widget_get_screen(drawable), visual->visualid );
nkeynes@659
   247
        GdkColormap *colormap = gdk_colormap_new( gdkvis, FALSE );
nkeynes@659
   248
        gtk_widget_set_colormap( drawable, colormap );
nkeynes@659
   249
    }
nkeynes@659
   250
#endif
nkeynes@659
   251
    gtk_video_drawable = drawable;
nkeynes@659
   252
    return drawable;
nkeynes@659
   253
}
nkeynes@659
   254
nkeynes@352
   255
gboolean video_gtk_init()
nkeynes@94
   256
{
nkeynes@545
   257
  
nkeynes@659
   258
    if( gtk_video_drawable == NULL ) {
nkeynes@530
   259
	return FALSE;
nkeynes@530
   260
    }
nkeynes@435
   261
nkeynes@659
   262
    video_width = gtk_video_drawable->allocation.width;
nkeynes@659
   263
    video_height = gtk_video_drawable->allocation.height;
nkeynes@658
   264
#ifdef HAVE_OSMESA
nkeynes@658
   265
    video_gdk_init_driver( &display_gtk_driver );
nkeynes@658
   266
#else
nkeynes@658
   267
#ifdef HAVE_GLX
nkeynes@659
   268
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(gtk_video_drawable)));
nkeynes@659
   269
    Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(gtk_video_drawable)->window );
nkeynes@552
   270
    if( ! video_glx_init_context( display, window ) ||
nkeynes@552
   271
        ! video_glx_init_driver( &display_gtk_driver ) ) {
nkeynes@552
   272
        return FALSE;
nkeynes@552
   273
    }
nkeynes@653
   274
#endif
nkeynes@658
   275
#endif
nkeynes@653
   276
nkeynes@614
   277
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@614
   278
    linux_joystick_init();
nkeynes@614
   279
#endif
nkeynes@552
   280
    return TRUE;
nkeynes@552
   281
}
nkeynes@552
   282
nkeynes@552
   283
gboolean video_gtk_display_blank( uint32_t colour )
nkeynes@552
   284
{
nkeynes@659
   285
    GdkGC *gc = gdk_gc_new(gtk_video_drawable->window);
nkeynes@552
   286
    GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
nkeynes@552
   287
    GdkColormap *cmap = gdk_colormap_get_system();
nkeynes@552
   288
    gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
nkeynes@552
   289
    gdk_gc_set_foreground( gc, &color );
nkeynes@552
   290
    gdk_gc_set_background( gc, &color );
nkeynes@659
   291
    gdk_draw_rectangle( gtk_video_drawable->window, gc, TRUE, 0, 0, video_width, video_height );
nkeynes@552
   292
    gdk_gc_destroy(gc);
nkeynes@552
   293
    gdk_colormap_free_colors( cmap, &color, 1 );
nkeynes@94
   294
}
nkeynes@94
   295
nkeynes@352
   296
void video_gtk_shutdown()
nkeynes@352
   297
{
nkeynes@659
   298
    if( gtk_video_drawable != NULL ) {
nkeynes@658
   299
#ifdef HAVE_OSMESA
nkeynes@653
   300
        video_gdk_shutdown();
nkeynes@653
   301
#else
nkeynes@659
   302
#ifdef HAVE_GLX
nkeynes@530
   303
	video_glx_shutdown();
nkeynes@653
   304
#endif
nkeynes@659
   305
#endif
nkeynes@530
   306
    }
nkeynes@620
   307
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@620
   308
    linux_joystick_shutdown();
nkeynes@620
   309
#endif
nkeynes@94
   310
}
nkeynes@94
   311
.