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 780:4e4ea322cb84
prev770:429ff505c450
next781:88d48559380a
author nkeynes
date Mon Jul 28 10:13:13 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Move grab management to window
Clear grab if the window loses key focus (probably a good idea in general)
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@681
    33
static gboolean video_osx_init();
nkeynes@681
    34
static void video_osx_shutdown();
nkeynes@681
    35
static void video_osx_display_blank( uint32_t colour );
nkeynes@681
    36
static uint16_t video_osx_resolve_keysym( const gchar *keysym );
nkeynes@681
    37
static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode);
nkeynes@770
    38
static gchar *video_osx_keycode_to_keysym(uint16_t keycode);
nkeynes@681
    39
nkeynes@700
    40
struct display_driver display_osx_driver = { 
nkeynes@700
    41
        "osx",
nkeynes@700
    42
        N_("OS X Cocoa GUI-based OpenGL driver"),
nkeynes@700
    43
        video_osx_init, video_osx_shutdown,
nkeynes@700
    44
        video_osx_resolve_keysym,
nkeynes@700
    45
        video_osx_keycode_to_dckeysym,
nkeynes@770
    46
        video_osx_keycode_to_keysym,
nkeynes@700
    47
        NULL, NULL, NULL, NULL, NULL, 
nkeynes@700
    48
        video_osx_display_blank, NULL };
nkeynes@681
    49
nkeynes@681
    50
nkeynes@681
    51
static NSView *video_view = NULL;
nkeynes@681
    52
int video_width = 640;
nkeynes@681
    53
int video_height = 480;
nkeynes@681
    54
nkeynes@681
    55
#define MAX_MASK_KEYCODE 128
nkeynes@681
    56
nkeynes@780
    57
@interface LxdreamOSXView : LxdreamVideoView
nkeynes@681
    58
{
nkeynes@681
    59
    int buttonMask;
nkeynes@681
    60
    int flagsMask[MAX_MASK_KEYCODE];
nkeynes@681
    61
}
nkeynes@681
    62
@end
nkeynes@681
    63
nkeynes@681
    64
@implementation LxdreamVideoView
nkeynes@780
    65
- (void)setIsGrabbed: (BOOL)grabbed
nkeynes@780
    66
{
nkeynes@780
    67
    isGrabbed = grabbed;
nkeynes@780
    68
}
nkeynes@780
    69
- (void) setDelegate: (id)other
nkeynes@780
    70
{
nkeynes@780
    71
    delegate = other;
nkeynes@780
    72
}
nkeynes@780
    73
- (id)delegate 
nkeynes@780
    74
{
nkeynes@780
    75
    return delegate;
nkeynes@780
    76
}
nkeynes@780
    77
@end
nkeynes@780
    78
nkeynes@780
    79
@implementation LxdreamOSXView
nkeynes@681
    80
//--------------------------------------------------------------------
nkeynes@681
    81
- (id)initWithFrame: (NSRect)contentRect
nkeynes@681
    82
{
nkeynes@681
    83
    if( [super initWithFrame: contentRect] != nil ) {
nkeynes@681
    84
        int i;
nkeynes@681
    85
        isGrabbed = NO;
nkeynes@681
    86
        buttonMask = 0;
nkeynes@681
    87
        for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
nkeynes@681
    88
            flagsMask[i] = 0;
nkeynes@681
    89
        }
nkeynes@681
    90
        return self;
nkeynes@681
    91
    }
nkeynes@681
    92
    return nil;
nkeynes@681
    93
}
nkeynes@780
    94
- (void)requestGrab
nkeynes@780
    95
{
nkeynes@780
    96
    if( delegate && [delegate respondsToSelector: @selector(viewRequestedGrab:)] )
nkeynes@780
    97
        [delegate performSelector: @selector(viewRequestedGrab:) withObject: self];
nkeynes@780
    98
}
nkeynes@780
    99
- (void)requestUngrab
nkeynes@780
   100
{
nkeynes@780
   101
    if( delegate && [delegate respondsToSelector: @selector(viewRequestedUngrab:)] )
nkeynes@780
   102
        [delegate performSelector: @selector(viewRequestedUngrab:) withObject: self];
nkeynes@780
   103
}
nkeynes@681
   104
- (BOOL)isOpaque
nkeynes@681
   105
{
nkeynes@736
   106
    return YES;
nkeynes@681
   107
}
nkeynes@681
   108
- (BOOL)acceptsFirstResponder
nkeynes@681
   109
{
nkeynes@736
   110
    return YES;
nkeynes@681
   111
}
nkeynes@681
   112
- (BOOL)isFlipped
nkeynes@681
   113
{
nkeynes@736
   114
    return YES;
nkeynes@681
   115
}
nkeynes@681
   116
//--------------------------------------------------------------------
nkeynes@681
   117
- (void)drawRect: (NSRect) rect
nkeynes@681
   118
{
nkeynes@681
   119
    NSSize size = [self frame].size;
nkeynes@681
   120
    if( video_width != size.width || video_height != size.height ) {
nkeynes@681
   121
        video_width = size.width;
nkeynes@681
   122
        video_height = size.height;
nkeynes@681
   123
        video_nsgl_update();
nkeynes@681
   124
    }
nkeynes@681
   125
    pvr2_redraw_display();
nkeynes@681
   126
}
nkeynes@681
   127
- (void)keyDown: (NSEvent *) event
nkeynes@681
   128
{
nkeynes@681
   129
    if( ![event isARepeat] ) {
nkeynes@681
   130
        input_event_keydown( NULL, [event keyCode]+1, 1 );
nkeynes@681
   131
    }
nkeynes@681
   132
}
nkeynes@681
   133
- (void)keyUp: (NSEvent *) event
nkeynes@681
   134
{
nkeynes@681
   135
    input_event_keyup( NULL, [event keyCode]+1, 1 );
nkeynes@681
   136
}
nkeynes@681
   137
- (void)flagsChanged: (NSEvent *) event
nkeynes@681
   138
{
nkeynes@681
   139
    int keycode = [event keyCode];
nkeynes@780
   140
    if( ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
nkeynes@780
   141
        [self requestUngrab];
nkeynes@681
   142
    }
nkeynes@736
   143
nkeynes@681
   144
    if( flagsMask[keycode] == 0 ) {
nkeynes@681
   145
        input_event_keydown( NULL, keycode+1, 1 );
nkeynes@681
   146
        flagsMask[keycode] = 1;
nkeynes@681
   147
    } else {
nkeynes@681
   148
        input_event_keyup( NULL, keycode+1, 1 );
nkeynes@681
   149
        flagsMask[keycode] = 0;
nkeynes@681
   150
    }
nkeynes@681
   151
}
nkeynes@681
   152
- (void)mouseDown: (NSEvent *) event
nkeynes@681
   153
{
nkeynes@681
   154
    if( isGrabbed ) { 
nkeynes@681
   155
        buttonMask |= 1;
nkeynes@681
   156
        input_event_mouse( buttonMask, 0, 0 );
nkeynes@780
   157
    } else {
nkeynes@780
   158
        [self requestGrab];
nkeynes@681
   159
    }
nkeynes@681
   160
}
nkeynes@681
   161
- (void)mouseUp: (NSEvent *)event
nkeynes@681
   162
{
nkeynes@681
   163
    buttonMask &= ~1;
nkeynes@681
   164
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   165
}
nkeynes@681
   166
nkeynes@681
   167
- (void)rightMouseDown: (NSEvent *) event
nkeynes@681
   168
{
nkeynes@681
   169
    buttonMask |= 2;
nkeynes@681
   170
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   171
}
nkeynes@681
   172
- (void)rightMouseUp: (NSEvent *)event
nkeynes@681
   173
{
nkeynes@681
   174
    buttonMask &= ~2;
nkeynes@681
   175
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   176
}
nkeynes@681
   177
- (void)otherMouseDown: (NSEvent *) event
nkeynes@681
   178
{
nkeynes@681
   179
    buttonMask |= (1<< [event buttonNumber] );
nkeynes@681
   180
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   181
}
nkeynes@681
   182
- (void)otherMouseUp: (NSEvent *) event
nkeynes@681
   183
{
nkeynes@681
   184
    buttonMask &= ~(1<< [event buttonNumber] );
nkeynes@681
   185
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   186
}
nkeynes@681
   187
- (void)mouseMoved: (NSEvent *) event
nkeynes@681
   188
{
nkeynes@681
   189
    if( isGrabbed ) {
nkeynes@681
   190
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   191
    }
nkeynes@681
   192
}
nkeynes@681
   193
- (void)mouseDragged: (NSEvent *) event
nkeynes@681
   194
{
nkeynes@681
   195
    if( isGrabbed ) {
nkeynes@681
   196
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   197
    }
nkeynes@681
   198
}
nkeynes@681
   199
- (void)rightMouseDragged: (NSEvent *) event
nkeynes@681
   200
{
nkeynes@681
   201
    if( isGrabbed ) {
nkeynes@681
   202
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   203
    }
nkeynes@681
   204
}
nkeynes@681
   205
- (void)otherMouseDragged: (NSEvent *) event
nkeynes@681
   206
{
nkeynes@681
   207
    if( isGrabbed ) {
nkeynes@681
   208
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   209
    }
nkeynes@681
   210
}
nkeynes@681
   211
nkeynes@681
   212
@end
nkeynes@681
   213
nkeynes@681
   214
NSView *video_osx_create_drawable()
nkeynes@681
   215
{
nkeynes@681
   216
    NSRect contentRect = {{0,0},{640,480}};
nkeynes@780
   217
    video_view = [[LxdreamOSXView alloc] initWithFrame: contentRect];
nkeynes@681
   218
    [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
nkeynes@681
   219
    return video_view;
nkeynes@681
   220
}
nkeynes@681
   221
nkeynes@681
   222
static gboolean video_osx_init()
nkeynes@681
   223
{
nkeynes@736
   224
    if( video_view == NULL ) {
nkeynes@736
   225
        return FALSE;
nkeynes@736
   226
    }
nkeynes@736
   227
    if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
nkeynes@736
   228
        return FALSE;
nkeynes@736
   229
    }
nkeynes@736
   230
    pvr2_setup_gl_context();
nkeynes@736
   231
    return TRUE;
nkeynes@681
   232
}
nkeynes@681
   233
nkeynes@681
   234
static void video_osx_shutdown()
nkeynes@681
   235
{
nkeynes@681
   236
}
nkeynes@681
   237
nkeynes@681
   238
static void video_osx_display_blank( uint32_t colour )
nkeynes@681
   239
{
nkeynes@681
   240
}
nkeynes@681
   241
nkeynes@681
   242
static int mac_keymap_cmp(const void *a, const void *b)
nkeynes@681
   243
{
nkeynes@681
   244
    const gchar *key = a;
nkeynes@681
   245
    const struct mac_keymap_struct *kb = b;
nkeynes@681
   246
    return strcasecmp(key, kb->name);
nkeynes@681
   247
}
nkeynes@681
   248
nkeynes@681
   249
static uint16_t video_osx_resolve_keysym( const gchar *keysym )
nkeynes@681
   250
{
nkeynes@681
   251
    struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
nkeynes@681
   252
    if( result == NULL ) {
nkeynes@681
   253
        return 0;
nkeynes@681
   254
    } else {
nkeynes@681
   255
        return result->keycode + 1;
nkeynes@681
   256
    }
nkeynes@681
   257
}
nkeynes@681
   258
nkeynes@681
   259
static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
nkeynes@681
   260
{
nkeynes@681
   261
    if( keycode < 1 || keycode > 128 ) {
nkeynes@681
   262
        return DCKB_NONE;
nkeynes@681
   263
    } else {
nkeynes@681
   264
        return mac_keycode_to_dckeysym[keycode-1];
nkeynes@681
   265
    }
nkeynes@681
   266
}
nkeynes@681
   267
nkeynes@770
   268
static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
nkeynes@770
   269
{
nkeynes@770
   270
    if( keycode < 1 || keycode > 128 ) {
nkeynes@770
   271
        return NULL;
nkeynes@770
   272
    } else {
nkeynes@770
   273
        return g_strdup(mac_keysyms_by_keycode[keycode-1]);
nkeynes@770
   274
    }
nkeynes@770
   275
}
.