1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/* FreeJ
* (c) Copyright 2009 Andrea Guzzo <xant@dyne.org>
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <CKbdController.h>
FACTORY_REGISTER_INSTANTIATOR(Controller, CKbdController, KeyboardController, cocoa);
CKbdController::CKbdController()
: Controller()
{
windowController = NULL;
}
CKbdController::~CKbdController()
{
if (windowController)
[windowController release];
}
bool CKbdController::init(Context *freej) {
if (freej) {
// XXX - we are assuming we will always get a CVSCreen on OSX
CVScreen *screen = (CVScreen *)freej->screen;
CVScreenView *sView = screen->get_view();
windowController = [[CVScreenController alloc] initWithWindow:[sView getWindow]];
[windowController setKbdController:this];
}
return Controller::init(freej);
}
int CKbdController::dispatch()
{
return 0;
}
int CKbdController::poll()
{
NSDictionary *entry;
while (entry = [windowController getEvent]) {
char funcname[256];
static jsval fval = JSVAL_NULL;
NSEvent *event = [entry valueForKey:@"event"];
NSString *state = [entry valueForKey:@"state"];
NSUInteger modifierFlags = [event modifierFlags];
snprintf(funcname, sizeof(funcname), "%s_%s%s%s%s%s",
[state UTF8String],
((modifierFlags&NSShiftKeyMask) ? "shift_" : ""),
((modifierFlags&NSControlKeyMask) ? "ctrl_" : ""),
((modifierFlags&NSAlternateKeyMask) ? "alt_" : ""),
((modifierFlags&NSNumericPadKeyMask) ? "num_" : ""),
[[event charactersIgnoringModifiers] UTF8String]
);
func("%s calling method %s()", __func__, funcname);
[entry release];
return JSCall(funcname, 0, &fval);
}
return 0;
}
@implementation CVScreenController
- (id)initWithWindow:(NSWindow *)window
{
_keyEvents = [[NSMutableArray arrayWithCapacity:100] retain];
return [super initWithWindow:window];
}
- (NSDictionary *)getEvent
{
NSDictionary *event = NULL;
@synchronized(self) {
if ([_keyEvents count]) {
event = [_keyEvents objectAtIndex:0];
[_keyEvents removeObjectAtIndex:0];
}
}
return event;
}
- (void)setKbdController:(CKbdController *)kbdController
{
_kbdController = kbdController;
}
- (void)insertEvent:(NSEvent *)event OfType:(NSString *)type WithState:(NSString *)state
{
NSDictionary *entry;
// create the entry
entry = [[NSDictionary
dictionaryWithObjects:
[NSArray arrayWithObjects:
event, state, type, nil
]
forKeys:
[NSArray arrayWithObjects:
@"event", @"state", @"type", nil
]
] retain];
@synchronized(self) {
[_keyEvents addObject:entry];
}
}
- (void)keyUp:(NSEvent *)event
{
//NSLog(@"Keyrelease (%hu, modifier flags: 0x%x) %@\n", [event keyCode], [event modifierFlags], [event charactersIgnoringModifiers]);
[self insertEvent:[event retain] OfType:@"kbd" WithState:@"released"];
}
// handle keystrokes
- (void)keyDown:(NSEvent *)event
{
//NSLog(@"Keypress (%hu, modifier flags: 0x%x) %@\n", [event keyCode], [event modifierFlags], [event charactersIgnoringModifiers]);
[self insertEvent:event OfType:@"kbd" WithState:@"pressed"];
}
@end
|