office-gobmx/apple_remote/GlobalKeyboardDevice.m
Vladimir Glazounov ef6cb13a30 CWS-TOOLING: integrate CWS appleremote02
2009-01-11 00:33:34 +0100 ericb  r266127 : #i97925# start the presentation if and only if an Impress document is focused 
2009-01-10 00:02:19 +0100 ericb  r266124 : #i96978# cosmetic changes 
2009-01-09 23:56:02 +0100 ericb  r266123 : #i96978# simplify the code, and make the remote work in windowed mode 
2009-01-08 21:25:52 +0100 ericb  r266040 : #i96978# avoid to add the eventlistener several times
2009-01-08 14:54:38 +0100 ericb  r266013 : #i96978# keep the MEDIA_COMMAND_VOLUME cases for QUARTZ only for the moment 
2009-01-08 14:49:17 +0100 ericb  r266011 : #i96978# fix wrong comments 
2009-01-03 01:42:02 +0100 cloph  r265848 : CWS-TOOLING: rebase CWS appleremote02 -> DEV300_m38 (manually, module was left out when using the tooling)
2008-12-30 00:11:30 +0100 ericb  r265827 : CWS-TOOLING: rebase CWS appleremote02 to trunk@265758 (milestone: DEV300:m38)
2008-12-29 21:42:14 +0100 ericb  r265825 : #i96978# comment the menu in appleremote02 
2008-12-29 10:30:40 +0100 ericb  r265816 : #i97195# remove the event listener in the dtor
2008-12-23 23:39:55 +0100 ericb  r265794 : #i96978# cleanup 
2008-12-23 08:49:49 +0100 ericb  r265771 : #i96978# add MEDIA_COMMAND_VOLUME_UP and MEDIA_COMMAND_VOLUME_DOWN and the kRemoteButtonMenu (temporarly) 
2008-12-23 08:47:27 +0100 ericb  r265769 : #i96978# add all cases, for future use
2008-12-20 13:38:25 +0100 ericb  r265743 : #i96978# fix a potential breaker (causing warning) 
2008-12-20 12:22:15 +0100 ericb  r265742 : #i97195# implement the new event listener + callback in Impress, to detect play 
2008-12-20 12:20:42 +0100 ericb  r265741 : #i97195# add the event listener in SdModule 
2008-12-20 12:19:02 +0100 ericb  r265740 : #i97195# add the nCommand for MEDIA_COMMAND_PLAY 
2008-12-14 18:32:52 +0100 ericb  r265456 : #i96978# add the possibility to switch the way to send the PLAY event in debug mode (will be removed once i97195 will be fixed) 
2008-12-08 22:18:29 +0100 ericb  r265050 : #i96978# removing Carbon dependancy and simplify 
2008-12-07 19:54:18 +0100 ericb  r264966 : #i96978# implement the new MEDIA_COMMAND events in salinst 
2008-12-07 19:53:10 +0100 ericb  r264965 : #i96978# add new event type for the Apple Remote on Mac OS X 
2008-12-07 19:51:21 +0100 ericb  r264964 : #i96978# add gotoLastSlide with MEDIA_COMMAND events (was missing)
2008-12-07 19:46:35 +0100 ericb  r264962 : #i96992# fix an error when building with debug enabled 
2008-12-07 19:45:04 +0100 ericb  r264961 : #i96978# Implement a new event mangement for the Apple remote
2008-12-07 18:59:58 +0100 ericb  r264957 : #i96554# fix wrong argument causing warnings 
2008-12-07 18:57:56 +0100 ericb  r264956 : #i96554# fix wrong argument type 
2008-12-07 18:55:22 +0100 ericb  r264955 : #i96978# adding a new event type
2009-01-20 15:57:31 +00:00

249 lines
8.4 KiB
Objective-C

/*****************************************************************************
* GlobalKeyboardDevice.m
* RemoteControlWrapper
*
* Created by Martin Kahr on 11.03.06 under a MIT-style license.
* Copyright (c) 2006 martinkahr.com. All rights reserved.
*
* Code modified and adapted to OpenOffice.org
* by Eric Bachard on 11.08.2008 under the same license
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*****************************************************************************/
#import "GlobalKeyboardDevice.h"
#define F1 122
#define F2 120
#define F3 99
#define F4 118
#define F5 96
#define F6 97
#define F7 98
/*
the following default keys are read and shall be used to change the keyboard mapping
mac.remotecontrols.GlobalKeyboardDevice.plus_modifiers
mac.remotecontrols.GlobalKeyboardDevice.plus_keycode
mac.remotecontrols.GlobalKeyboardDevice.minus_modifiers
mac.remotecontrols.GlobalKeyboardDevice.minus_keycode
mac.remotecontrols.GlobalKeyboardDevice.play_modifiers
mac.remotecontrols.GlobalKeyboardDevice.play_keycode
mac.remotecontrols.GlobalKeyboardDevice.left_modifiers
mac.remotecontrols.GlobalKeyboardDevice.left_keycode
mac.remotecontrols.GlobalKeyboardDevice.right_modifiers
mac.remotecontrols.GlobalKeyboardDevice.right_keycode
mac.remotecontrols.GlobalKeyboardDevice.menu_modifiers
mac.remotecontrols.GlobalKeyboardDevice.menu_keycode
mac.remotecontrols.GlobalKeyboardDevice.playhold_modifiers
mac.remotecontrols.GlobalKeyboardDevice.playhold_keycode
*/
static OSStatus hotKeyEventHandler(EventHandlerCallRef, EventRef, void*);
@implementation GlobalKeyboardDevice
- (id) initWithDelegate: (id) _remoteControlDelegate {
if ( (self = [super initWithDelegate: _remoteControlDelegate]) ) {
hotKeyRemoteEventMapping = [[NSMutableDictionary alloc] init];
unsigned int modifiers = cmdKey + shiftKey /*+ optionKey*/ + controlKey;
[self mapRemoteButton:kRemoteButtonPlus defaultKeycode:F1 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonMinus defaultKeycode:F2 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonPlay defaultKeycode:F3 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonLeft defaultKeycode:F4 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonRight defaultKeycode:F5 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonMenu defaultKeycode:F6 defaultModifiers:modifiers];
[self mapRemoteButton:kRemoteButtonPlay_Hold defaultKeycode:F7 defaultModifiers:modifiers];
}
return self;
}
- (void) dealloc {
[hotKeyRemoteEventMapping release];
[super dealloc];
}
- (void) mapRemoteButton: (RemoteControlEventIdentifier) remoteButtonIdentifier defaultKeycode: (unsigned int) defaultKeycode defaultModifiers: (unsigned int) defaultModifiers {
NSString* defaultsKey = NULL;
switch(remoteButtonIdentifier) {
case kRemoteButtonPlus:
defaultsKey = @"plus";
break;
case kRemoteButtonMinus:
defaultsKey = @"minus";
break;
case kRemoteButtonMenu:
defaultsKey = @"menu";
break;
case kRemoteButtonPlay:
defaultsKey = @"play";
break;
case kRemoteButtonRight:
defaultsKey = @"right";
break;
case kRemoteButtonLeft:
defaultsKey = @"left";
break;
case kRemoteButtonPlay_Hold:
defaultsKey = @"playhold";
break;
default:
#ifdef DEBUG
NSLog(@"Unknown global keyboard defaults key for remote button identifier %d", remoteButtonIdentifier);
#endif
break;
}
NSNumber* modifiersCfg = [[NSUserDefaults standardUserDefaults] objectForKey: [NSString stringWithFormat: @"mac.remotecontrols.GlobalKeyboardDevice.%@_modifiers", defaultsKey]];
NSNumber* keycodeCfg = [[NSUserDefaults standardUserDefaults] objectForKey: [NSString stringWithFormat: @"mac.remotecontrols.GlobalKeyboardDevice.%@_keycode", defaultsKey]];
unsigned int modifiers = defaultModifiers;
if (modifiersCfg) modifiers = [modifiersCfg unsignedIntValue];
unsigned int keycode = defaultKeycode;
if (keycodeCfg) keycode = [keycodeCfg unsignedIntValue];
[self registerHotKeyCode: keycode modifiers: modifiers remoteEventIdentifier: remoteButtonIdentifier];
}
- (void) setListeningToRemote: (BOOL) value {
if (value == [self isListeningToRemote]) return;
if (value) {
[self startListening: self];
} else {
[self stopListening: self];
}
}
- (BOOL) isListeningToRemote {
return (eventHandlerRef!=NULL);
}
- (void) startListening: (id) sender {
if (eventHandlerRef) return;
EventTypeSpec eventSpec[2] = {
{ kEventClassKeyboard, kEventHotKeyPressed },
{ kEventClassKeyboard, kEventHotKeyReleased }
};
InstallEventHandler( GetEventDispatcherTarget(),
(EventHandlerProcPtr)hotKeyEventHandler,
2, eventSpec, self, &eventHandlerRef);
}
- (void) stopListening: (id) sender {
RemoveEventHandler(eventHandlerRef);
eventHandlerRef = NULL;
}
- (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier {
NSEnumerator* values = [hotKeyRemoteEventMapping objectEnumerator];
NSNumber* remoteIdentifier;
while( (remoteIdentifier = [values nextObject]) ) {
if ([remoteIdentifier unsignedIntValue] == identifier) return YES;
}
return NO;
}
+ (const char*) remoteControlDeviceName {
return "Keyboard";
}
- (BOOL)registerHotKeyCode: (unsigned int) keycode modifiers: (unsigned int) modifiers remoteEventIdentifier: (RemoteControlEventIdentifier) identifier {
OSStatus err;
EventHotKeyID hotKeyID;
EventHotKeyRef carbonHotKey;
hotKeyID.signature = 'PTHk';
hotKeyID.id = (long)keycode;
err = RegisterEventHotKey(keycode, modifiers, hotKeyID, GetEventDispatcherTarget(), 0, &carbonHotKey );
if( err )
return NO;
[hotKeyRemoteEventMapping setObject: [NSNumber numberWithInt:identifier] forKey: [NSNumber numberWithUnsignedInt: hotKeyID.id]];
return YES;
}
/*
- (void)unregisterHotKey: (PTHotKey*)hotKey
{
OSStatus err;
EventHotKeyRef carbonHotKey;
NSValue* key;
if( [[self allHotKeys] containsObject: hotKey] == NO )
return;
carbonHotKey = [self _carbonHotKeyForHotKey: hotKey];
NSAssert( carbonHotKey != nil, @"" );
err = UnregisterEventHotKey( carbonHotKey );
//Watch as we ignore 'err':
key = [NSValue valueWithPointer: carbonHotKey];
[mHotKeys removeObjectForKey: key];
[self _updateEventHandler];
//See that? Completely ignored
}
*/
- (RemoteControlEventIdentifier) remoteControlEventIdentifierForID: (unsigned int) id {
NSNumber* remoteEventIdentifier = [hotKeyRemoteEventMapping objectForKey:[NSNumber numberWithUnsignedInt: id]];
return [remoteEventIdentifier unsignedIntValue];
}
- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown {
[delegate sendRemoteButtonEvent: event pressedDown: pressedDown remoteControl:self];
}
static RemoteControlEventIdentifier lastEvent;
static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* userData )
{
GlobalKeyboardDevice* keyboardDevice = (GlobalKeyboardDevice*) userData;
EventHotKeyID hkCom;
GetEventParameter(inEvent,kEventParamDirectObject,typeEventHotKeyID,NULL,sizeof(hkCom),NULL,&hkCom);
RemoteControlEventIdentifier identifier = [keyboardDevice remoteControlEventIdentifierForID:hkCom.id];
if (identifier == 0) return noErr;
BOOL pressedDown = YES;
if (identifier != lastEvent) {
lastEvent = identifier;
} else {
lastEvent = 0;
pressedDown = NO;
}
[keyboardDevice sendRemoteButtonEvent: identifier pressedDown: pressedDown];
return noErr;
}
@end