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 681:1755a126b109
next700:4650d0c7f6f9
author nkeynes
date Sun Jun 01 00:47:45 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change First cut of the Cocoa GUI implementation
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@681
    38
nkeynes@681
    39
struct display_driver display_osx_driver = { "osx", video_osx_init, video_osx_shutdown,
nkeynes@681
    40
                    video_osx_resolve_keysym,
nkeynes@681
    41
                    video_osx_keycode_to_dckeysym,
nkeynes@681
    42
                    NULL,
nkeynes@681
    43
                    NULL, NULL, NULL, NULL, NULL, 
nkeynes@681
    44
                    video_osx_display_blank, NULL };
nkeynes@681
    45
nkeynes@681
    46
nkeynes@681
    47
static NSView *video_view = NULL;
nkeynes@681
    48
int video_width = 640;
nkeynes@681
    49
int video_height = 480;
nkeynes@681
    50
nkeynes@681
    51
#define MAX_MASK_KEYCODE 128
nkeynes@681
    52
nkeynes@681
    53
@interface LxdreamVideoView : NSView
nkeynes@681
    54
{
nkeynes@681
    55
    BOOL isGrabbed;
nkeynes@681
    56
    int buttonMask;
nkeynes@681
    57
    int flagsMask[MAX_MASK_KEYCODE];
nkeynes@681
    58
}
nkeynes@681
    59
- (BOOL)isOpaque;
nkeynes@681
    60
- (BOOL)isFlipped;
nkeynes@681
    61
- (void)drawRect: (NSRect) rect;
nkeynes@681
    62
@end
nkeynes@681
    63
nkeynes@681
    64
@implementation LxdreamVideoView
nkeynes@681
    65
//--------------------------------------------------------------------
nkeynes@681
    66
- (id)initWithFrame: (NSRect)contentRect
nkeynes@681
    67
{
nkeynes@681
    68
    if( [super initWithFrame: contentRect] != nil ) {
nkeynes@681
    69
        int i;
nkeynes@681
    70
        isGrabbed = NO;
nkeynes@681
    71
        buttonMask = 0;
nkeynes@681
    72
        for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
nkeynes@681
    73
            flagsMask[i] = 0;
nkeynes@681
    74
        }
nkeynes@681
    75
        return self;
nkeynes@681
    76
    }
nkeynes@681
    77
    return nil;
nkeynes@681
    78
}
nkeynes@681
    79
- (BOOL)isOpaque
nkeynes@681
    80
{
nkeynes@681
    81
   return YES;
nkeynes@681
    82
}
nkeynes@681
    83
- (BOOL)acceptsFirstResponder
nkeynes@681
    84
{
nkeynes@681
    85
   return YES;
nkeynes@681
    86
}
nkeynes@681
    87
- (BOOL)isFlipped
nkeynes@681
    88
{
nkeynes@681
    89
   return YES;
nkeynes@681
    90
}
nkeynes@681
    91
//--------------------------------------------------------------------
nkeynes@681
    92
- (void)drawRect: (NSRect) rect
nkeynes@681
    93
{
nkeynes@681
    94
    NSSize size = [self frame].size;
nkeynes@681
    95
    if( video_width != size.width || video_height != size.height ) {
nkeynes@681
    96
        video_width = size.width;
nkeynes@681
    97
        video_height = size.height;
nkeynes@681
    98
        video_nsgl_update();
nkeynes@681
    99
    }
nkeynes@681
   100
    pvr2_redraw_display();
nkeynes@681
   101
}
nkeynes@681
   102
- (void)keyDown: (NSEvent *) event
nkeynes@681
   103
{
nkeynes@681
   104
    if( ![event isARepeat] ) {
nkeynes@681
   105
        input_event_keydown( NULL, [event keyCode]+1, 1 );
nkeynes@681
   106
    }
nkeynes@681
   107
}
nkeynes@681
   108
- (void)keyUp: (NSEvent *) event
nkeynes@681
   109
{
nkeynes@681
   110
    input_event_keyup( NULL, [event keyCode]+1, 1 );
nkeynes@681
   111
}
nkeynes@681
   112
- (void)flagsChanged: (NSEvent *) event
nkeynes@681
   113
{
nkeynes@681
   114
    int keycode = [event keyCode];
nkeynes@681
   115
    if ( isGrabbed && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
nkeynes@681
   116
        // Release the display grab
nkeynes@681
   117
        isGrabbed = NO;
nkeynes@681
   118
        [NSCursor unhide];
nkeynes@681
   119
        CGAssociateMouseAndMouseCursorPosition(YES);
nkeynes@681
   120
        [((LxdreamMainWindow *)[self window]) setIsGrabbed: NO];
nkeynes@681
   121
    }
nkeynes@681
   122
    
nkeynes@681
   123
    if( flagsMask[keycode] == 0 ) {
nkeynes@681
   124
        input_event_keydown( NULL, keycode+1, 1 );
nkeynes@681
   125
        flagsMask[keycode] = 1;
nkeynes@681
   126
    } else {
nkeynes@681
   127
        input_event_keyup( NULL, keycode+1, 1 );
nkeynes@681
   128
        flagsMask[keycode] = 0;
nkeynes@681
   129
    }
nkeynes@681
   130
}
nkeynes@681
   131
- (void)mouseDown: (NSEvent *) event
nkeynes@681
   132
{
nkeynes@681
   133
    if( isGrabbed ) { 
nkeynes@681
   134
        buttonMask |= 1;
nkeynes@681
   135
        input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   136
    } else { // take display grab
nkeynes@681
   137
        isGrabbed = YES;
nkeynes@681
   138
        [NSCursor hide];
nkeynes@681
   139
        CGAssociateMouseAndMouseCursorPosition(NO);
nkeynes@681
   140
        [((LxdreamMainWindow *)[self window]) setIsGrabbed: YES];
nkeynes@681
   141
    }
nkeynes@681
   142
}
nkeynes@681
   143
- (void)mouseUp: (NSEvent *)event
nkeynes@681
   144
{
nkeynes@681
   145
    buttonMask &= ~1;
nkeynes@681
   146
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   147
}
nkeynes@681
   148
nkeynes@681
   149
- (void)rightMouseDown: (NSEvent *) event
nkeynes@681
   150
{
nkeynes@681
   151
    buttonMask |= 2;
nkeynes@681
   152
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   153
}
nkeynes@681
   154
- (void)rightMouseUp: (NSEvent *)event
nkeynes@681
   155
{
nkeynes@681
   156
    buttonMask &= ~2;
nkeynes@681
   157
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   158
}
nkeynes@681
   159
- (void)otherMouseDown: (NSEvent *) event
nkeynes@681
   160
{
nkeynes@681
   161
    buttonMask |= (1<< [event buttonNumber] );
nkeynes@681
   162
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   163
}
nkeynes@681
   164
- (void)otherMouseUp: (NSEvent *) event
nkeynes@681
   165
{
nkeynes@681
   166
    buttonMask &= ~(1<< [event buttonNumber] );
nkeynes@681
   167
    input_event_mouse( buttonMask, 0, 0 );
nkeynes@681
   168
}
nkeynes@681
   169
- (void)mouseMoved: (NSEvent *) event
nkeynes@681
   170
{
nkeynes@681
   171
    if( isGrabbed ) {
nkeynes@681
   172
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   173
    }
nkeynes@681
   174
}
nkeynes@681
   175
- (void)mouseDragged: (NSEvent *) event
nkeynes@681
   176
{
nkeynes@681
   177
    if( isGrabbed ) {
nkeynes@681
   178
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   179
    }
nkeynes@681
   180
}
nkeynes@681
   181
- (void)rightMouseDragged: (NSEvent *) event
nkeynes@681
   182
{
nkeynes@681
   183
    if( isGrabbed ) {
nkeynes@681
   184
        input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
nkeynes@681
   185
    }
nkeynes@681
   186
}
nkeynes@681
   187
- (void)otherMouseDragged: (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
nkeynes@681
   194
@end
nkeynes@681
   195
nkeynes@681
   196
NSView *video_osx_create_drawable()
nkeynes@681
   197
{
nkeynes@681
   198
    NSRect contentRect = {{0,0},{640,480}};
nkeynes@681
   199
    video_view = [[LxdreamVideoView alloc] initWithFrame: contentRect];
nkeynes@681
   200
    [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
nkeynes@681
   201
    return video_view;
nkeynes@681
   202
}
nkeynes@681
   203
nkeynes@681
   204
static gboolean video_osx_init()
nkeynes@681
   205
{
nkeynes@681
   206
   if( video_view == NULL ) {
nkeynes@681
   207
      return FALSE;
nkeynes@681
   208
   }
nkeynes@681
   209
   if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
nkeynes@681
   210
      return FALSE;
nkeynes@681
   211
   }
nkeynes@681
   212
   pvr2_setup_gl_context();
nkeynes@681
   213
   return TRUE;
nkeynes@681
   214
}
nkeynes@681
   215
nkeynes@681
   216
static void video_osx_shutdown()
nkeynes@681
   217
{
nkeynes@681
   218
}
nkeynes@681
   219
nkeynes@681
   220
static void video_osx_display_blank( uint32_t colour )
nkeynes@681
   221
{
nkeynes@681
   222
}
nkeynes@681
   223
nkeynes@681
   224
static int mac_keymap_cmp(const void *a, const void *b)
nkeynes@681
   225
{
nkeynes@681
   226
    const gchar *key = a;
nkeynes@681
   227
    const struct mac_keymap_struct *kb = b;
nkeynes@681
   228
    return strcasecmp(key, kb->name);
nkeynes@681
   229
}
nkeynes@681
   230
nkeynes@681
   231
static uint16_t video_osx_resolve_keysym( const gchar *keysym )
nkeynes@681
   232
{
nkeynes@681
   233
    struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
nkeynes@681
   234
    if( result == NULL ) {
nkeynes@681
   235
        return 0;
nkeynes@681
   236
    } else {
nkeynes@681
   237
        return result->keycode + 1;
nkeynes@681
   238
    }
nkeynes@681
   239
}
nkeynes@681
   240
nkeynes@681
   241
static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
nkeynes@681
   242
{
nkeynes@681
   243
    if( keycode < 1 || keycode > 128 ) {
nkeynes@681
   244
        return DCKB_NONE;
nkeynes@681
   245
    } else {
nkeynes@681
   246
        return mac_keycode_to_dckeysym[keycode-1];
nkeynes@681
   247
    }
nkeynes@681
   248
}
nkeynes@681
   249
.