File: hotkeys.m

package info (click to toggle)
pcsxr 1.9.94-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,500 kB
  • ctags: 16,110
  • sloc: ansic: 117,276; objc: 8,159; cpp: 1,140; makefile: 316; asm: 145; pascal: 30; sh: 20
file content (134 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (2)
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
//
//  hotkeys.m
//  Pcsxr
//
//  Created by Nicolas Pepin-Perreault on 12-12-12.
//
//

#import <Cocoa/Cocoa.h>
#import <AppKit/AppKit.h>
#import "hotkeys.h"
#import "EmuThread.h"
#include "plugins.h"
#include "ExtendedKeys.h"
#import "PcsxrController.h"

#define HK_MAX_STATE 10
static id monitor;
static id gpuMonitor;
static int currentState = 0;
static NSMutableDictionary *hotkeys = nil;
enum {
    HK_FAST_FORWARD,
    HK_SAVE_STATE,
    HK_LOAD_STATE,
    HK_NEXT_STATE,
    HK_PREV_STATE,
    HK_FRAME_LIMIT
};

void nextState() {
    currentState++;
    if(currentState == HK_MAX_STATE) {
        currentState = 0;
    }
}

void prevState() {
    currentState--;
    if(currentState < 0) {
        currentState = HK_MAX_STATE-1;
    }
}

BOOL handleHotkey(NSString* keyCode) {
    if([EmuThread active]) { // Don't catch hotkeys if there is no emulation
        NSNumber *ident = hotkeys[keyCode];

        if(ident != nil) {
            switch([ident intValue]) {
                case HK_FAST_FORWARD:
                    // We ignore FastForward requests if the emulation is paused
                    if(![EmuThread isPaused]) {
                        GPU_keypressed(GPU_FAST_FORWARD);
                    }
                    break;
                case HK_FRAME_LIMIT:
                    // Ignore FrameLimit requests if paused
                    if(![EmuThread isPaused]) {
                        GPU_keypressed(GPU_FRAME_LIMIT);
                    }
                    break;
                case HK_SAVE_STATE:
                    [PcsxrController saveState:currentState];
                    break;
                case HK_LOAD_STATE:
                    [PcsxrController loadState:currentState];
                    break;
                case HK_NEXT_STATE:
                    nextState();
                    GPU_displayText((char*)[[NSString stringWithFormat:@"State Slot: %d", currentState] UTF8String]);
                    break;
                case HK_PREV_STATE:
                    prevState();
                    GPU_displayText((char*)[[NSString stringWithFormat:@"State Slot: %d", currentState] UTF8String]);
                    break;
                default:
                    NSLog(@"Invalid hotkey identifier %i.", [ident intValue]);
            }
        
            return YES;
        }
    }
    
    return NO;
}

void setupHotkey(int hk, NSString *label, NSDictionary *binding) {
	if(binding != nil)
		hotkeys[binding[@"keyCode"]] = @(hk);
}

void setupHotkeys() {
    NSDictionary *bindings = [[NSUserDefaults standardUserDefaults] objectForKey:@"HotkeyBindings"];
    hotkeys = [[NSMutableDictionary alloc] initWithCapacity:[bindings count]];
    
    setupHotkey(HK_FAST_FORWARD, @"FastForward", bindings[@"FastForward"]);
    setupHotkey(HK_SAVE_STATE, @"SaveState", bindings[@"SaveState"]);
    setupHotkey(HK_LOAD_STATE, @"LoadState", bindings[@"LoadState"]);
    setupHotkey(HK_NEXT_STATE, @"NextState", bindings[@"NextState"]);
    setupHotkey(HK_PREV_STATE, @"PrevState", bindings[@"PrevState"]);
    setupHotkey(HK_FRAME_LIMIT, @"FrameLimit", bindings[@"FrameLimit"]);
    
    currentState = 0;
}

void attachHotkeys() {
    // Configurable hotkeys
    NSEvent* (^handler)(NSEvent*) = ^(NSEvent *event) {
        if(handleHotkey([NSString stringWithFormat:@"%d", [event keyCode]])) {
            return (NSEvent*)nil; // handled
        }
        
        // Not handled
        return event;
    };
    setupHotkeys();
    monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyUpMask handler:handler];
    
    // GPU key presses
    NSEvent* (^gpuKeypress)(NSEvent*) = ^(NSEvent *event) {
        GPU_keypressed([event keyCode]);
        return (NSEvent*)nil;
    };
    gpuMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSKeyUpMask | NSControlKeyMask) handler:gpuKeypress];
}

void detachHotkeys() {
	hotkeys = nil;
    [NSEvent removeMonitor:monitor];
    [NSEvent removeMonitor:gpuMonitor];
    monitor = nil;
    gpuMonitor = nil;
}