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 770:429ff505c450
prev736:a02d1475ccfd
next771:749f0f4575d2
author nkeynes
date Mon Jul 28 03:41:25 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Implement key-binding configuration pane for Cocoa UI
Minor tweaks for consistency and static-correctness
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@677
    22
#include <stdlib.h>
nkeynes@658
    23
#include "lxdream.h"
nkeynes@144
    24
#include "display.h"
nkeynes@608
    25
#include "dckeysyms.h"
nkeynes@653
    26
#include "drivers/video_gl.h"
nkeynes@669
    27
#include "drivers/joy_linux.h"
nkeynes@586
    28
#include "pvr2/pvr2.h"
nkeynes@537
    29
#include "gtkui/gtkui.h"
nkeynes@94
    30
nkeynes@658
    31
#ifdef HAVE_GTK_X11
nkeynes@658
    32
nkeynes@658
    33
#include <gdk/gdkx.h>
nkeynes@658
    34
#include "drivers/video_glx.h"
nkeynes@658
    35
nkeynes@658
    36
/************* X11-specificness **********/
nkeynes@658
    37
nkeynes@658
    38
guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
nkeynes@658
    39
{
nkeynes@736
    40
    int i;
nkeynes@736
    41
    int result = 0;
nkeynes@736
    42
    Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@736
    43
    XModifierKeymap *keymap = XGetModifierMapping( xdisplay );
nkeynes@736
    44
    for( i=0; i<8*keymap->max_keypermod; i++ ) {
nkeynes@736
    45
        if( keymap->modifiermap[i] == keycode ) {
nkeynes@736
    46
            result = 1 << (i/keymap->max_keypermod);
nkeynes@736
    47
            break;
nkeynes@736
    48
        }
nkeynes@658
    49
    }
nkeynes@736
    50
    XFreeModifiermap(keymap);
nkeynes@736
    51
    return result;
nkeynes@658
    52
}
nkeynes@658
    53
nkeynes@658
    54
#if !(GTK_CHECK_VERSION(2,8,0))
nkeynes@658
    55
/* gdk_display_warp_pointer was added in GTK 2.8. If we're using an earlier
nkeynes@658
    56
 * version, include the code here. (Can't just set the dependency on 2.8 as
nkeynes@658
    57
 * it still hasn't been included in fink yet...) Original copyright statement
nkeynes@658
    58
 * below.
nkeynes@658
    59
 */
nkeynes@658
    60
nkeynes@658
    61
/* GDK - The GIMP Drawing Kit
nkeynes@658
    62
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
nkeynes@658
    63
 *
nkeynes@658
    64
 * This library is free software; you can redistribute it and/or
nkeynes@658
    65
 * modify it under the terms of the GNU Lesser General Public
nkeynes@658
    66
 * License as published by the Free Software Foundation; either
nkeynes@658
    67
 * version 2 of the License, or (at your option) any later version.
nkeynes@658
    68
 *
nkeynes@658
    69
 * This library is distributed in the hope that it will be useful,
nkeynes@658
    70
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@658
    71
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nkeynes@658
    72
 * Lesser General Public License for more details.
nkeynes@658
    73
 *
nkeynes@658
    74
 * You should have received a copy of the GNU Lesser General Public
nkeynes@658
    75
 * License along with this library; if not, write to the
nkeynes@658
    76
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
nkeynes@658
    77
 * Boston, MA 02111-1307, USA.
nkeynes@658
    78
 */
nkeynes@658
    79
nkeynes@658
    80
/*
nkeynes@658
    81
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
nkeynes@658
    82
 * file for a list of people on the GTK+ Team.  See the ChangeLog
nkeynes@658
    83
 * files for a list of changes.  These files are distributed with
nkeynes@658
    84
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
nkeynes@658
    85
 */
nkeynes@658
    86
void gdk_display_warp_pointer (GdkDisplay *display,
nkeynes@736
    87
                               GdkScreen  *screen,
nkeynes@736
    88
                               gint        x,
nkeynes@736
    89
                               gint        y)
nkeynes@658
    90
{
nkeynes@736
    91
    Display *xdisplay;
nkeynes@736
    92
    Window dest;
nkeynes@658
    93
nkeynes@736
    94
    xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@736
    95
    dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
nkeynes@658
    96
nkeynes@736
    97
    XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);  
nkeynes@658
    98
}
nkeynes@658
    99
nkeynes@658
   100
#endif
nkeynes@658
   101
nkeynes@658
   102
#endif
nkeynes@659
   103
nkeynes@663
   104
#ifdef HAVE_GTK_OSX
nkeynes@663
   105
#include "drivers/video_nsgl.h"
nkeynes@663
   106
nkeynes@663
   107
// Include this prototype as some systems don't have gdkquartz.h installed
nkeynes@663
   108
NSView  *gdk_quartz_window_get_nsview( GdkWindow *window);
nkeynes@663
   109
nkeynes@663
   110
guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
nkeynes@663
   111
{
nkeynes@736
   112
    return 0;
nkeynes@663
   113
}
nkeynes@663
   114
nkeynes@663
   115
#endif
nkeynes@663
   116
nkeynes@663
   117
nkeynes@663
   118
nkeynes@661
   119
GtkWidget *gtk_video_drawable = NULL;
nkeynes@436
   120
int video_width = 640;
nkeynes@436
   121
int video_height = 480;
nkeynes@94
   122
nkeynes@352
   123
gboolean video_gtk_init();
nkeynes@352
   124
void video_gtk_shutdown();
nkeynes@669
   125
void video_gtk_display_blank( uint32_t colour );
nkeynes@770
   126
static uint16_t video_gtk_resolve_keysym( const gchar *keysym );
nkeynes@770
   127
static uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode);
nkeynes@770
   128
static gchar *video_gtk_get_keysym_for_keycode(uint16_t keycode);
nkeynes@94
   129
nkeynes@700
   130
struct display_driver display_gtk_driver = { 
nkeynes@700
   131
        "gtk",
nkeynes@700
   132
        N_("GTK-based OpenGL driver"),
nkeynes@700
   133
        video_gtk_init, 
nkeynes@700
   134
        video_gtk_shutdown,
nkeynes@700
   135
        video_gtk_resolve_keysym,
nkeynes@700
   136
        video_gtk_keycode_to_dckeysym,
nkeynes@770
   137
        video_gtk_keycode_to_keysym,
nkeynes@700
   138
        NULL, NULL, NULL, NULL, NULL, 
nkeynes@700
   139
        video_gtk_display_blank, NULL };
nkeynes@736
   140
nkeynes@435
   141
gboolean video_gtk_expose_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data )
nkeynes@435
   142
{
nkeynes@677
   143
    pvr2_redraw_display();
nkeynes@443
   144
    return TRUE;
nkeynes@435
   145
}
nkeynes@435
   146
nkeynes@436
   147
gboolean video_gtk_resize_callback(GtkWidget *widget, GdkEventConfigure *event, gpointer data )
nkeynes@436
   148
{
nkeynes@436
   149
    video_width = event->width;
nkeynes@436
   150
    video_height = event->height;
nkeynes@677
   151
    pvr2_redraw_display();
nkeynes@443
   152
    return TRUE;
nkeynes@436
   153
}
nkeynes@436
   154
nkeynes@770
   155
static uint16_t video_gtk_resolve_keysym( const gchar *keysym )
nkeynes@770
   156
{
nkeynes@770
   157
    int val = gdk_keyval_from_name( keysym );
nkeynes@770
   158
    if( val == GDK_VoidSymbol )
nkeynes@770
   159
        return 0;
nkeynes@770
   160
    return (uint16_t)val;
nkeynes@770
   161
}
nkeynes@770
   162
nkeynes@770
   163
static gchar *video_gtk_get_keycode_to_keysym( uint16_t keycode )
nkeynes@770
   164
{
nkeynes@770
   165
    return g_strdup(gdk_keyval_name(keycode));
nkeynes@770
   166
}
nkeynes@770
   167
nkeynes@770
   168
static uint16_t video_gtk_keycode_to_dckeysym(uint16_t keycode)
nkeynes@608
   169
{
nkeynes@608
   170
    if( keycode >= 'a' && keycode <= 'z' ) {
nkeynes@736
   171
        return (keycode - 'a') + DCKB_a;
nkeynes@608
   172
    } else if( keycode >= '1' && keycode <= '9' ) {
nkeynes@736
   173
        return (keycode - '1') + DCKB_1;
nkeynes@608
   174
    }
nkeynes@608
   175
    switch(keycode) {
nkeynes@609
   176
    case GDK_0:         return DCKB_0;
nkeynes@681
   177
    case GDK_Return:    return DCKB_Return;
nkeynes@681
   178
    case GDK_Escape:    return DCKB_Escape;
nkeynes@681
   179
    case GDK_BackSpace: return DCKB_BackSpace;
nkeynes@681
   180
    case GDK_Tab:       return DCKB_Tab;
nkeynes@681
   181
    case GDK_space:     return DCKB_space;
nkeynes@681
   182
    case GDK_minus:     return DCKB_minus;
nkeynes@681
   183
    case GDK_equal:     return DCKB_equal;
nkeynes@681
   184
    case GDK_bracketleft: return DCKB_bracketleft;
nkeynes@681
   185
    case GDK_bracketright: return DCKB_bracketright;
nkeynes@681
   186
    case GDK_semicolon: return DCKB_semicolon;
nkeynes@681
   187
    case GDK_apostrophe:return DCKB_apostrophe;
nkeynes@681
   188
    case GDK_grave : return DCKB_grave;
nkeynes@681
   189
    case GDK_comma:     return DCKB_comma;
nkeynes@681
   190
    case GDK_period:    return DCKB_period;
nkeynes@681
   191
    case GDK_slash:     return DCKB_slash; 
nkeynes@681
   192
    case GDK_Caps_Lock: return DCKB_Caps_Lock;
nkeynes@609
   193
    case GDK_F1:        return DCKB_F1;
nkeynes@609
   194
    case GDK_F2:        return DCKB_F2;
nkeynes@609
   195
    case GDK_F3:        return DCKB_F3;
nkeynes@609
   196
    case GDK_F4:        return DCKB_F4;
nkeynes@609
   197
    case GDK_F5:        return DCKB_F5;
nkeynes@609
   198
    case GDK_F6:        return DCKB_F6;
nkeynes@609
   199
    case GDK_F7:        return DCKB_F7;
nkeynes@609
   200
    case GDK_F8:        return DCKB_F8;
nkeynes@609
   201
    case GDK_F9:        return DCKB_F9;
nkeynes@609
   202
    case GDK_F10:       return DCKB_F10;
nkeynes@609
   203
    case GDK_F11:       return DCKB_F11;
nkeynes@609
   204
    case GDK_F12:       return DCKB_F12;
nkeynes@681
   205
    case GDK_Scroll_Lock: return DCKB_Scroll_Lock;
nkeynes@681
   206
    case GDK_Pause:     return DCKB_Pause;
nkeynes@681
   207
    case GDK_Insert:    return DCKB_Insert;
nkeynes@681
   208
    case GDK_Home:      return DCKB_Home;
nkeynes@681
   209
    case GDK_Page_Up:   return DCKB_Page_Up;
nkeynes@681
   210
    case GDK_Delete:    return DCKB_Delete;
nkeynes@682
   211
    case GDK_End:       return DCKB_End;
nkeynes@681
   212
    case GDK_Page_Down: return DCKB_Page_Down;
nkeynes@681
   213
    case GDK_Right:     return DCKB_Right;
nkeynes@681
   214
    case GDK_Left:      return DCKB_Left;
nkeynes@681
   215
    case GDK_Down:      return DCKB_Down;
nkeynes@681
   216
    case GDK_Up:        return DCKB_Up;
nkeynes@681
   217
    case GDK_Num_Lock:  return DCKB_Num_Lock;
nkeynes@681
   218
    case GDK_KP_Divide: return DCKB_KP_Divide;
nkeynes@681
   219
    case GDK_KP_Multiply: return DCKB_KP_Multiply;
nkeynes@681
   220
    case GDK_KP_Subtract: return DCKB_KP_Subtract;
nkeynes@681
   221
    case GDK_KP_Add:    return DCKB_KP_Add;
nkeynes@681
   222
    case GDK_KP_Enter:  return DCKB_KP_Enter;
nkeynes@681
   223
    case GDK_KP_End:    return DCKB_KP_End;
nkeynes@681
   224
    case GDK_KP_Down:   return DCKB_KP_Down;
nkeynes@681
   225
    case GDK_KP_Page_Down: return DCKB_KP_Page_Down;
nkeynes@681
   226
    case GDK_KP_Left:   return DCKB_KP_Left;
nkeynes@681
   227
    case GDK_KP_Begin:  return DCKB_KP_Begin;
nkeynes@681
   228
    case GDK_KP_Right:  return DCKB_KP_Right;
nkeynes@681
   229
    case GDK_KP_Home:   return DCKB_KP_Home;
nkeynes@681
   230
    case GDK_KP_Up:     return DCKB_KP_Up;
nkeynes@681
   231
    case GDK_KP_Page_Up:return DCKB_KP_Page_Up;
nkeynes@681
   232
    case GDK_KP_Insert: return DCKB_KP_Insert;
nkeynes@681
   233
    case GDK_KP_Delete: return DCKB_KP_Delete;
nkeynes@681
   234
    case GDK_backslash: return DCKB_backslash;
nkeynes@681
   235
    case GDK_Control_L: return DCKB_Control_L;
nkeynes@681
   236
    case GDK_Shift_L:   return DCKB_Shift_L;
nkeynes@681
   237
    case GDK_Alt_L:     return DCKB_Alt_L;
nkeynes@681
   238
    case GDK_Meta_L:    return DCKB_Meta_L;
nkeynes@681
   239
    case GDK_Control_R: return DCKB_Control_R;
nkeynes@681
   240
    case GDK_Shift_R:   return DCKB_Shift_R;
nkeynes@681
   241
    case GDK_Alt_R:     return DCKB_Alt_R;
nkeynes@681
   242
    case GDK_Meta_R:    return DCKB_Meta_R;
nkeynes@608
   243
    }
nkeynes@608
   244
    return DCKB_NONE;
nkeynes@608
   245
}
nkeynes@608
   246
nkeynes@659
   247
GtkWidget *video_gtk_create_drawable()
nkeynes@659
   248
{
nkeynes@659
   249
    GtkWidget *drawable = gtk_drawing_area_new();
nkeynes@659
   250
    GTK_WIDGET_SET_FLAGS(drawable, GTK_CAN_FOCUS|GTK_CAN_DEFAULT);
nkeynes@659
   251
nkeynes@659
   252
    g_signal_connect( drawable, "expose_event",
nkeynes@736
   253
                      G_CALLBACK(video_gtk_expose_callback), NULL );
nkeynes@659
   254
    g_signal_connect( drawable, "configure_event",
nkeynes@736
   255
                      G_CALLBACK(video_gtk_resize_callback), NULL );
nkeynes@659
   256
nkeynes@659
   257
#ifdef HAVE_GLX
nkeynes@659
   258
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(drawable));
nkeynes@659
   259
    Screen *screen = gdk_x11_screen_get_xscreen( gtk_widget_get_screen(drawable));
nkeynes@659
   260
    int screen_no = XScreenNumberOfScreen(screen);
nkeynes@659
   261
    if( !video_glx_init(display, screen_no) ) {
nkeynes@659
   262
        ERROR( "Unable to initialize GLX, aborting" );
nkeynes@659
   263
        exit(3);
nkeynes@659
   264
    }
nkeynes@659
   265
nkeynes@659
   266
    XVisualInfo *visual = video_glx_get_visual();
nkeynes@659
   267
    if( visual != NULL ) {
nkeynes@659
   268
        GdkVisual *gdkvis = gdk_x11_screen_lookup_visual( gtk_widget_get_screen(drawable), visual->visualid );
nkeynes@659
   269
        GdkColormap *colormap = gdk_colormap_new( gdkvis, FALSE );
nkeynes@659
   270
        gtk_widget_set_colormap( drawable, colormap );
nkeynes@659
   271
    }
nkeynes@659
   272
#endif
nkeynes@659
   273
    gtk_video_drawable = drawable;
nkeynes@659
   274
    return drawable;
nkeynes@659
   275
}
nkeynes@659
   276
nkeynes@352
   277
gboolean video_gtk_init()
nkeynes@94
   278
{
nkeynes@736
   279
nkeynes@659
   280
    if( gtk_video_drawable == NULL ) {
nkeynes@736
   281
        return FALSE;
nkeynes@530
   282
    }
nkeynes@435
   283
nkeynes@659
   284
    video_width = gtk_video_drawable->allocation.width;
nkeynes@659
   285
    video_height = gtk_video_drawable->allocation.height;
nkeynes@658
   286
#ifdef HAVE_OSMESA
nkeynes@658
   287
    video_gdk_init_driver( &display_gtk_driver );
nkeynes@658
   288
#else
nkeynes@658
   289
#ifdef HAVE_GLX
nkeynes@659
   290
    Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(GTK_WIDGET(gtk_video_drawable)));
nkeynes@659
   291
    Window window = GDK_WINDOW_XWINDOW( GTK_WIDGET(gtk_video_drawable)->window );
nkeynes@552
   292
    if( ! video_glx_init_context( display, window ) ||
nkeynes@736
   293
            ! video_glx_init_driver( &display_gtk_driver ) ) {
nkeynes@552
   294
        return FALSE;
nkeynes@552
   295
    }
nkeynes@663
   296
#else
nkeynes@663
   297
#ifdef HAVE_NSGL
nkeynes@663
   298
    NSView *view = gdk_quartz_window_get_nsview(gtk_video_drawable->window);
nkeynes@663
   299
    if( ! video_nsgl_init_driver( view, &display_gtk_driver ) ) {
nkeynes@736
   300
        return FALSE;
nkeynes@663
   301
    }
nkeynes@663
   302
#endif
nkeynes@653
   303
#endif
nkeynes@658
   304
#endif
nkeynes@653
   305
nkeynes@665
   306
    pvr2_setup_gl_context();
nkeynes@736
   307
nkeynes@614
   308
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@614
   309
    linux_joystick_init();
nkeynes@614
   310
#endif
nkeynes@552
   311
    return TRUE;
nkeynes@552
   312
}
nkeynes@552
   313
nkeynes@669
   314
void video_gtk_display_blank( uint32_t colour )
nkeynes@552
   315
{
nkeynes@659
   316
    GdkGC *gc = gdk_gc_new(gtk_video_drawable->window);
nkeynes@552
   317
    GdkColor color = {0, ((colour>>16)&0xFF)*257, ((colour>>8)&0xFF)*257, ((colour)&0xFF)*257 };
nkeynes@552
   318
    GdkColormap *cmap = gdk_colormap_get_system();
nkeynes@552
   319
    gdk_colormap_alloc_color( cmap, &color, TRUE, TRUE );
nkeynes@552
   320
    gdk_gc_set_foreground( gc, &color );
nkeynes@552
   321
    gdk_gc_set_background( gc, &color );
nkeynes@659
   322
    gdk_draw_rectangle( gtk_video_drawable->window, gc, TRUE, 0, 0, video_width, video_height );
nkeynes@552
   323
    gdk_gc_destroy(gc);
nkeynes@552
   324
    gdk_colormap_free_colors( cmap, &color, 1 );
nkeynes@94
   325
}
nkeynes@94
   326
nkeynes@352
   327
void video_gtk_shutdown()
nkeynes@352
   328
{
nkeynes@659
   329
    if( gtk_video_drawable != NULL ) {
nkeynes@658
   330
#ifdef HAVE_OSMESA
nkeynes@653
   331
        video_gdk_shutdown();
nkeynes@653
   332
#else
nkeynes@659
   333
#ifdef HAVE_GLX
nkeynes@736
   334
        video_glx_shutdown();
nkeynes@663
   335
#else
nkeynes@663
   336
#ifdef HAVE_NSGL
nkeynes@736
   337
        video_nsgl_shutdown();
nkeynes@663
   338
#endif
nkeynes@653
   339
#endif
nkeynes@659
   340
#endif
nkeynes@530
   341
    }
nkeynes@620
   342
#ifdef HAVE_LINUX_JOYSTICK
nkeynes@620
   343
    linux_joystick_shutdown();
nkeynes@620
   344
#endif
nkeynes@94
   345
}
nkeynes@94
   346
.