Search
lxdream.org :: lxdream/src/drivers/video_osx.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_osx.c
changeset 805:b355f7b3ff2e
prev781:88d48559380a
next839:51f1c4195790
author nkeynes
date Thu Aug 07 23:53:17 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add ability to bind a render buffer to a texture, with output going to the texture.
(RTT work in progress)
file annotate diff log raw
nkeynes@681
     1
/**
nkeynes@681
     2
 * $Id$
nkeynes@681
     3
 *
nkeynes@681
     4
 * The OS/X side of the video support (responsible for actually displaying / 
nkeynes@681
     5
 * rendering frames)
nkeynes@681
     6
 *
nkeynes@681
     7
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@681
     8
 *
nkeynes@681
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@681
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@681
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@681
    12
 * (at your option) any later version.
nkeynes@681
    13
 *
nkeynes@681
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@681
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@681
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@681
    17
 * GNU General Public License for more details.
nkeynes@681
    18
 */
nkeynes@681
    19
nkeynes@681
    20
#include <stdlib.h>
nkeynes@681
    21
#include <string.h>
nkeynes@681
    22
#include "lxdream.h"
nkeynes@681
    23
#include "display.h"
nkeynes@681
    24
#include "dckeysyms.h"
nkeynes@681
    25
#include "cocoaui/cocoaui.h"
nkeynes@681
    26
#include "drivers/video_nsgl.h"
nkeynes@681
    27
#include "drivers/video_gl.h"
nkeynes@681
    28
#include "pvr2/pvr2.h"
nkeynes@681
    29
#import <AppKit/AppKit.h>
nkeynes@681
    30
nkeynes@681
    31
#include "mac_keymap.h"
nkeynes@681
    32
nkeynes@781
    33
#define MOUSE_X_SCALE 5
nkeynes@781
    34
#define MOUSE_Y_SCALE 5
nkeynes@781
    35
nkeynes@681
    36
static gboolean video_osx_init();
nkeynes@681
    37
static void video_osx_shutdown();
nkeynes@681
    38
static void video_osx_display_blank( uint32_t colour );
nkeynes@681
    39
static uint16_t video_osx_resolve_keysym( const gchar *keysym );
nkeynes@681
    40
static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode);
nkeynes@770
    41
static gchar *video_osx_keycode_to_keysym(uint16_t keycode);
nkeynes@681
    42
nkeynes@700
    43
struct display_driver display_osx_driver = { 
nkeynes@700
    44
        "osx",
nkeynes@700
    45
        N_("OS X Cocoa GUI-based OpenGL driver"),
nkeynes@700
    46
        video_osx_init, video_osx_shutdown,
nkeynes@700
    47
        video_osx_resolve_keysym,
nkeynes@700
    48
        video_osx_keycode_to_dckeysym,
nkeynes@770
    49
        video_osx_keycode_to_keysym,
nkeynes@805
    50
        NULL, NULL, NULL, NULL, NULL,
nkeynes@805
    51
        NULL,
nkeynes@700
    52
        video_osx_display_blank, NULL };
nkeynes@681
    53
nkeynes@681
    54
nkeynes@681
    55
static NSView *video_view = NULL;
nkeynes@681
    56
int video_width = 640;
nkeynes@681
    57
int video_height = 480;
nkeynes@681
    58
nkeynes@681
    59
#define MAX_MASK_KEYCODE 128
nkeynes@681
    60
nkeynes@780
    61
@interface LxdreamOSXView : LxdreamVideoView
nkeynes@681
    62
{
nkeynes@681
    63
    int buttonMask;
nkeynes@681
    64
    int flagsMask[MAX_MASK_KEYCODE];
nkeynes@681
    65
}
nkeynes@681
    66
@end
nkeynes@681
    67
nkeynes@681
    68
@implementation LxdreamVideoView
nkeynes@780
    69
- (void)setIsGrabbed: (BOOL)grabbed
nkeynes@780
    70
{
nkeynes@780
    71
    isGrabbed = grabbed;
nkeynes@780
    72
}
nkeynes@780
    73
- (void) setDelegate: (id)other
nkeynes@780
    74
{
nkeynes@780
    75
    delegate = other;
nkeynes@780
    76
}
nkeynes@780
    77
- (id)delegate 
nkeynes@780
    78
{
nkeynes@780
    79
    return delegate;
nkeynes@780
    80
}
nkeynes@780
    81
@end
nkeynes@780
    82
nkeynes@780
    83
@implementation LxdreamOSXView
nkeynes@681
    84
//--------------------------------------------------------------------
nkeynes@681
    85
- (id)initWithFrame: (NSRect)contentRect
nkeynes@681
    86
{
nkeynes@681
    87
    if( [super initWithFrame: contentRect] != nil ) {
nkeynes@681
    88
        int i;
nkeynes@681
    89
        isGrabbed = NO;
nkeynes@681
    90
        buttonMask = 0;
nkeynes@681
    91
        for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
nkeynes@681
    92
            flagsMask[i] = 0;
nkeynes@681
    93
        }
nkeynes@681
    94
        return self;
nkeynes@681
    95
    }
nkeynes@681
    96
    return nil;
nkeynes@681
    97
}
nkeynes@780
    98
- (void)requestGrab
nkeynes@780
    99
{
nkeynes@780
   100
    if( delegate && [delegate respondsToSelector: @selector(viewRequestedGrab:)] )
nkeynes@780
   101
        [delegate performSelector: @selector(viewRequestedGrab:) withObject: self];
nkeynes@780
   102
}
nkeynes@780
   103
- (void)requestUngrab
nkeynes@780
   104
{
nkeynes@780
   105
    if( delegate && [delegate respondsToSelector: @selector(viewRequestedUngrab:)] )
nkeynes@780
   106
        [delegate performSelector: @selector(viewRequestedUngrab:) withObject: self];
nkeynes@780
   107
}
nkeynes@681
   108
- (BOOL)isOpaque
nkeynes@681
   109
{
nkeynes@736
   110
    return YES;
nkeynes@681
   111
}
nkeynes@681
   112
- (BOOL)acceptsFirstResponder
nkeynes@681
   113
{
nkeynes@736
   114
    return YES;
nkeynes@681
   115
}
nkeynes@681
   116
- (BOOL)isFlipped
nkeynes@681
   117
{
nkeynes@736
   118
    return YES;
nkeynes@681
   119
}
nkeynes@681
   120
//--------------------------------------------------------------------
nkeynes@681
   121
- (void)drawRect: (NSRect) rect
nkeynes@681
   122
{
nkeynes@681
   123
    NSSize size = [self frame].size;
nkeynes@681
   124
    if( video_width != size.width || video_height != size.height ) {
nkeynes@681
   125
        video_width = size.width;
nkeynes@681
   126
        video_height = size.height;
nkeynes@681
   127
        video_nsgl_update();
nkeynes@681
   128
    }
nkeynes@681
   129
    pvr2_redraw_display();
nkeynes@681
   130
}
nkeynes@681
   131
- (void)keyDown: (NSEvent *) event
nkeynes@681
   132
{
nkeynes@681
   133
    if( ![event isARepeat] ) {
nkeynes@681
   134
        input_event_keydown( NULL, [event keyCode]+1, 1 );
nkeynes@681
   135
    }
nkeynes@681
   136
}
nkeynes@681
   137
- (void)keyUp: (NSEvent *) event
nkeynes@681
   138
{
nkeynes@681
   139
    input_event_keyup( NULL, [event keyCode]+1, 1 );
nkeynes@681
   140
}
nkeynes@681
   141
- (void)flagsChanged: (NSEvent *) event
nkeynes@681
   142
{
nkeynes@681
   143
    int keycode = [event keyCode];
nkeynes@780
   144
    if( ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
nkeynes@780
   145
        [self requestUngrab];
nkeynes@681
   146
    }
nkeynes@736
   147
nkeynes@681
   148
    if( flagsMask[keycode] == 0 ) {
nkeynes@681
   149
        input_event_keydown( NULL, keycode+1, 1 );
nkeynes@681
   150
        flagsMask[keycode] = 1;
nkeynes@681
   151
    } else {
nkeynes@681
   152
        input_event_keyup( NULL, keycode+1, 1 );
nkeynes@681
   153
        flagsMask[keycode] = 0;
nkeynes@681
   154
    }
nkeynes@681
   155
}
nkeynes@681
   156
- (void)mouseDown: (NSEvent *) event
nkeynes@681
   157
{
nkeynes@681
   158
    if( isGrabbed ) { 
nkeynes@681
   159
        buttonMask |= 1;
nkeynes@681
   160
        input_event_mouse( buttonMask, 0, 0 );
nkeynes@780
   161
    } else {
nkeynes@780
   162
        [self requestGrab];
nkeynes@681
   163
    }
nkeynes@681
   164
}
nkeynes@681
   165
- (void)mouseUp: (NSEvent *)event
nkeynes@681
   166
{
nkeynes@681
   167
    buttonMask &= ~1;
nkeynes@681
   168
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   169
}
nkeynes@681
   170
nkeynes@681
   171
- (void)rightMouseDown: (NSEvent *) event
nkeynes@681
   172
{
nkeynes@681
   173
    buttonMask |= 2;
nkeynes@681
   174
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   175
}
nkeynes@681
   176
- (void)rightMouseUp: (NSEvent *)event
nkeynes@681
   177
{
nkeynes@681
   178
    buttonMask &= ~2;
nkeynes@681
   179
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   180
}
nkeynes@681
   181
- (void)otherMouseDown: (NSEvent *) event
nkeynes@681
   182
{
nkeynes@681
   183
    buttonMask |= (1<< [event buttonNumber] );
nkeynes@681
   184
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   185
}
nkeynes@681
   186
- (void)otherMouseUp: (NSEvent *) event
nkeynes@681
   187
{
nkeynes@681
   188
    buttonMask &= ~(1<< [event buttonNumber] );
nkeynes@681
   189
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   190
}
nkeynes@681
   191
- (void)mouseMoved: (NSEvent *) event
nkeynes@681
   192
{
nkeynes@681
   193
    if( isGrabbed ) {
nkeynes@781
   194
        input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
nkeynes@681
   195
    }
nkeynes@681
   196
}
nkeynes@681
   197
- (void)mouseDragged: (NSEvent *) event
nkeynes@681
   198
{
nkeynes@681
   199
    if( isGrabbed ) {
nkeynes@781
   200
        input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
nkeynes@681
   201
    }
nkeynes@681
   202
}
nkeynes@681
   203
- (void)rightMouseDragged: (NSEvent *) event
nkeynes@681
   204
{
nkeynes@681
   205
    if( isGrabbed ) {
nkeynes@781
   206
        input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
nkeynes@681
   207
    }
nkeynes@681
   208
}
nkeynes@681
   209
- (void)otherMouseDragged: (NSEvent *) event
nkeynes@681
   210
{
nkeynes@681
   211
    if( isGrabbed ) {
nkeynes@781
   212
        input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
nkeynes@681
   213
    }
nkeynes@681
   214
}
nkeynes@681
   215
nkeynes@681
   216
@end
nkeynes@681
   217
nkeynes@681
   218
NSView *video_osx_create_drawable()
nkeynes@681
   219
{
nkeynes@681
   220
    NSRect contentRect = {{0,0},{640,480}};
nkeynes@780
   221
    video_view = [[LxdreamOSXView alloc] initWithFrame: contentRect];
nkeynes@681
   222
    [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
nkeynes@681
   223
    return video_view;
nkeynes@681
   224
}
nkeynes@681
   225
nkeynes@681
   226
static gboolean video_osx_init()
nkeynes@681
   227
{
nkeynes@736
   228
    if( video_view == NULL ) {
nkeynes@736
   229
        return FALSE;
nkeynes@736
   230
    }
nkeynes@736
   231
    if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
nkeynes@736
   232
        return FALSE;
nkeynes@736
   233
    }
nkeynes@736
   234
    pvr2_setup_gl_context();
nkeynes@736
   235
    return TRUE;
nkeynes@681
   236
}
nkeynes@681
   237
nkeynes@681
   238
static void video_osx_shutdown()
nkeynes@681
   239
{
nkeynes@681
   240
}
nkeynes@681
   241
nkeynes@681
   242
static void video_osx_display_blank( uint32_t colour )
nkeynes@681
   243
{
nkeynes@681
   244
}
nkeynes@681
   245
nkeynes@681
   246
static int mac_keymap_cmp(const void *a, const void *b)
nkeynes@681
   247
{
nkeynes@681
   248
    const gchar *key = a;
nkeynes@681
   249
    const struct mac_keymap_struct *kb = b;
nkeynes@681
   250
    return strcasecmp(key, kb->name);
nkeynes@681
   251
}
nkeynes@681
   252
nkeynes@681
   253
static uint16_t video_osx_resolve_keysym( const gchar *keysym )
nkeynes@681
   254
{
nkeynes@681
   255
    struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
nkeynes@681
   256
    if( result == NULL ) {
nkeynes@681
   257
        return 0;
nkeynes@681
   258
    } else {
nkeynes@681
   259
        return result->keycode + 1;
nkeynes@681
   260
    }
nkeynes@681
   261
}
nkeynes@681
   262
nkeynes@681
   263
static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
nkeynes@681
   264
{
nkeynes@681
   265
    if( keycode < 1 || keycode > 128 ) {
nkeynes@681
   266
        return DCKB_NONE;
nkeynes@681
   267
    } else {
nkeynes@681
   268
        return mac_keycode_to_dckeysym[keycode-1];
nkeynes@681
   269
    }
nkeynes@681
   270
}
nkeynes@681
   271
nkeynes@770
   272
static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
nkeynes@770
   273
{
nkeynes@770
   274
    if( keycode < 1 || keycode > 128 ) {
nkeynes@770
   275
        return NULL;
nkeynes@770
   276
    } else {
nkeynes@770
   277
        return g_strdup(mac_keysyms_by_keycode[keycode-1]);
nkeynes@770
   278
    }
nkeynes@770
   279
}
.