/
DOSPadBaseViewController.m
executable file
·629 lines (553 loc) · 17.4 KB
/
DOSPadBaseViewController.m
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* Copyright (C) 2010 Chaoji Li
*
* DOSPAD is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "DOSPadBaseViewController.h"
#include "keys.h"
#import "DosPadViewController.h"
#import "DosPadViewController_iPhone.h"
#import "AppDelegate.h"
#define NOT_IMPLEMENTED(func) func { NSLog(@"Error: `%s' is not implemented!", #func); }
extern int SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode);
@interface DOSPadBaseViewController()
@property(nonatomic, strong) KeyMapper *keyMapper;
@property(nonatomic, strong) UIAlertView *keyMapperAlertView;
@property(nonatomic, strong) MfiGameControllerHandler *mfiHandler;
@property(nonatomic, strong) MfiControllerInputHandler *mfiInputHandler;
@end
@implementation DOSPadBaseViewController
@synthesize autoExit;
@synthesize configPath;
@synthesize screenView;
- (bool)isInputSourceEnabled:(InputSourceType)type
{
switch (type) {
case InputSource_PCKeyboard:
case InputSource_GamePad:
case InputSource_MouseButtons:
return true;
case InputSource_Joystick:
return DEFS_GET_BOOL(kJoystickEnabled);
case InputSource_NumPad:
return DEFS_GET_BOOL(kNumpadEnabled);
default:
return false;
}
}
- (NSString*)currentCycles
{
AppDelegate *d = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if (d.maxPercent != 0)
{
return [NSString stringWithFormat:@"%3d%%", d.maxPercent];
}
else
{
return [NSString stringWithFormat:@"%4d", d.cycles];
}
}
- (int)currentFrameskip
{
AppDelegate *d = (AppDelegate*)[[UIApplication sharedApplication] delegate];
return d.frameskip;
}
+ (DOSPadBaseViewController*)dospadWithConfig:(NSString*)configPath
{
if (ISIPAD())
{
DosPadViewController *ctrl = [[DosPadViewController alloc] init];
ctrl.configPath = configPath;
return ctrl;
}
else
{
DosPadViewController_iPhone *ctrl = [[DosPadViewController_iPhone alloc] init];
ctrl.configPath = configPath;
return ctrl;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
screenView.alpha = 1; // Try to fix reboot problem on iPad 3.2.x
dospad_resume();
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (ISIPAD() && [self isLandscape])
{
screenView.alpha = 0; // Try to fix reboot problem on iPad 3.2.x
}
dospad_pause();
}
- (void)viewDidLoad
{
[super viewDidLoad];
screenView.delegate=self;
screenView.mouseHoldDelegate=self;
[self.view insertSubview:screenView atIndex:0];
holdIndicator = [[HoldIndicator alloc] initWithFrame:CGRectMake(0,0,128,128)];
holdIndicator.alpha = 0;
holdIndicator.transform = CGAffineTransformMakeScale(1.5, 1.5);
[self.view addSubview:holdIndicator];
#ifdef IDOS
self.title = @"iDOS";
#else
self.title = @"DOSpad";
#endif
if (configPath == nil)
{
self.configPath = [ConfigManager dospadConfigFile];
}
else
{
if (dospad_command_line_ready)
{
strcpy(dospad_launch_config, [configPath UTF8String]);
sprintf(dospad_launch_section, "[start.%s]", ISIPAD()?"ipad":"iphone");
dospad_should_launch_game = 1;
SDL_SendKeyboardKey(0, SDL_PRESSED, SDL_SCANCODE_RETURN);
SDL_SendKeyboardKey(0, SDL_RELEASED, SDL_SCANCODE_RETURN);
}
}
/* TODO: LITCHIE commented out by TVD
vk = [[VKView alloc] initWithFrame:CGRectMake(0,0,1,1)];
vk.alpha = 0;
[self.view addSubview:vk];
*/
//---------------------------------------------------
// Remap controls
//---------------------------------------------------
remappingOnLabel = [[UILabel alloc] initWithFrame:CGRectZero];
remappingOnLabel.text = @"Remapping Controls ON";
remappingOnLabel.textColor = [UIColor redColor];
remappingOnLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:remappingOnLabel];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:remappingOnLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:remappingOnLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.75f constant:0.0f]];
remappingOnLabel.hidden = YES;
resetMappingsButton = [[UIButton alloc] initWithFrame:CGRectZero];
[resetMappingsButton setTitle:@"Reset Mappings" forState:UIControlStateNormal];
[resetMappingsButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[resetMappingsButton setTintColor:[UIColor redColor]];
resetMappingsButton.translatesAutoresizingMaskIntoConstraints = NO;
[resetMappingsButton addTarget:self action:@selector(resetMappingsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:resetMappingsButton];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:resetMappingsButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:resetMappingsButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.5f constant:0.0f]];
resetMappingsButton.layer.borderWidth = 1.0f;
resetMappingsButton.layer.borderColor = [[UIColor redColor] CGColor];
resetMappingsButton.hidden = YES;
self.keyMapper = [[KeyMapper alloc] init];
[self.keyMapper loadFromDefaults];
self.mfiHandler = [[MfiGameControllerHandler alloc] init];
self.mfiInputHandler = [[MfiControllerInputHandler alloc] init];
self.mfiInputHandler.keyMapper = self.keyMapper;
[self.mfiHandler discoverController:^(GCController *gameController) {
[self.mfiInputHandler setupControllerInputsForController:gameController];
} disconnectedCallback:^{
}];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (void)dealloc {
[self removeAllInputSources];
//TODO LITCHIE commented out by TVD
//[vk release];
}
- (void)onLaunchExit
{
if (self.autoExit)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)onMouseLeftDown
{
[screenView sendMouseEvent:0 left:YES down:YES];
}
- (void)onMouseLeftUp
{
[screenView sendMouseEvent:0 left:YES down:NO];
}
- (void)onMouseRightDown
{
[screenView sendMouseEvent:0 left:NO down:YES];
}
- (void)onMouseRightUp
{
[screenView sendMouseEvent:0 left:NO down:NO];
}
-(BOOL)onDoubleTap:(CGPoint)pt
{
// Do nothing
return NO;
}
-(void)onHold:(CGPoint)pt
{
CGPoint pt2 = [self.screenView convertPoint:pt toView:self.view];
holdIndicator.center=pt2;
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.3];
holdIndicator.alpha=1;
[UIView commitAnimations];
}
-(void)cancelHold:(CGPoint)pt
{
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.3];
holdIndicator.alpha=0;
[UIView commitAnimations];
}
- (MouseRightClickMode)currentRightClickMode
{
if (DEFS_GET_INT(kDoubleTapAsRightClick) == 1) {
return MouseRightClickWithDoubleTap;
} else {
return MouseRightClickDefault;
}
}
-(void)onResize:(CGSize)sizeNew
{
NSLog(@"Warning: onResize not implemented");
}
-(void)updateFrameskip:(NSNumber*)skip
{
NSLog(@"Warning: updateFrameSkip not implemented");
}
-(void)updateCpuCycles:(NSString*)title
{
NSLog(@"Warning: updateCpuCyles not implemented");
}
- (void)sendCommandToDOS:(NSString *)cmd
{
if (cmd == nil) return;
const char *p = [cmd UTF8String];
while (*p!=0)
{
int ch = *p;
int shift=0;
int code=get_scancode_for_char(ch, &shift);
if (code >= 0) {
if (shift)
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
[NSThread sleepForTimeInterval:0.05];
if (shift)
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
} else {
break;
}
p++;
}
}
-(void)showOption
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
- (BOOL)isPortrait
{
return (self.interfaceOrientation == UIInterfaceOrientationPortrait ||
self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)isLandscape
{
return (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (BOOL)isInputSourceActive:(InputSourceType)type
{
switch (type)
{
case InputSource_NumPad:
{
return numpad != nil;
break;
}
case InputSource_PCKeyboard:
{
return kbd != nil;
break;
}
case InputSource_GamePad:
{
return gamepad != nil;
break;
}
case InputSource_Joystick:
{
return joystick != nil;
break;
}
case InputSource_PianoKeyboard:
{
return piano != nil;
break;
}
case InputSource_MouseButtons:
{
return btnMouseLeft != nil && btnMouseRight != nil;
break;
}
}
return NO;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)removeiOSKeyboard
{
//TODO: Litchie commented out by tvd
/*
if (vk.useNativeKeyboard != YES)
return;
// Hide the virtual native keyboard
// However, we are still listening to external keyboard input
vk.active = NO;
vk.useNativeKeyboard=NO;
vk.active = YES;
*/
}
- (void)createiOSKeyboard
{
//TODO: Litchie commented out by tvd
/*
if (vk.active)
vk.active = NO;
vk.useNativeKeyboard = YES;
vk.active = YES;*/
}
- (void)NOT_IMPLEMENTED(createPCKeyboard);
- (void)NOT_IMPLEMENTED(createNumpad);
- (void)NOT_IMPLEMENTED(createGamepad);
- (void)NOT_IMPLEMENTED(createJoystick);
- (void)NOT_IMPLEMENTED(createMouseButtons);
- (void)createPianoKeyboard
{
NSString *ui_cfg = 0;//get_temporary_merged_file(configPath, get_default_config());
if (ui_cfg != nil)
{
piano = [[PianoKeyboard alloc] initWithConfig:ui_cfg section:@"[piano.keybinding]"];
if (piano)
{
[self.view addSubview:piano];
}
}
}
- (void)addInputSource:(InputSourceType)type
{
[self removeInputSource:type];
switch (type)
{
case InputSource_NumPad:
{
[self createNumpad];
break;
}
case InputSource_PCKeyboard:
{
[self createPCKeyboard];
break;
}
case InputSource_GamePad:
{
[self createGamepad];
break;
}
case InputSource_Joystick:
{
[self createJoystick];
break;
}
case InputSource_PianoKeyboard:
{
[self createPianoKeyboard];
break;
}
case InputSource_MouseButtons:
{
[self createMouseButtons];
break;
}
}
}
- (void)addInputSourceExclusively:(InputSourceType)type
{
[self removeAllInputSources];
[self addInputSource:type];
}
- (void)removeAllInputSources
{
for (int i = 0; i < InputSource_TotalCount; i++)
{
[self removeInputSource:i];
}
}
- (void)removeInputSource:(InputSourceType)type
{
switch (type)
{
case InputSource_NumPad:
{
if (numpad)
{
[numpad removeFromSuperview];
numpad = nil;
}
break;
}
case InputSource_PCKeyboard:
{
if (kbd)
{
[kbd removeFromSuperview];
kbd = nil;
}
break;
}
case InputSource_iOSKeyboard:
{
[self removeiOSKeyboard];
break;
}
case InputSource_GamePad:
{
if (gamepad)
{
[gamepad removeFromSuperview];
gamepad = nil;
}
break;
}
case InputSource_Joystick:
{
if (joystick)
{
[joystick removeFromSuperview];
joystick = nil;
}
break;
}
case InputSource_PianoKeyboard:
{
if (piano)
{
[piano removeFromSuperview];
piano = nil;
}
break;
}
case InputSource_MouseButtons:
{
if (btnMouseLeft)
{
[btnMouseLeft removeFromSuperview];
btnMouseLeft = nil;
}
if (btnMouseRight)
{
[btnMouseRight removeFromSuperview];
btnMouseRight = nil;
}
break;
}
}
}
-(void) remapControlsButtonTapped:(id)sender {
remapControlsModeOn = !remapControlsModeOn;
remappingOnLabel.hidden = !remapControlsModeOn;
resetMappingsButton.hidden = !remapControlsModeOn;
if ( remapControlsModeOn ) {
kbd.externKeyDelegate = self;
} else {
kbd.externKeyDelegate = nil;
}
}
-(void) refreshKeyMappingsInViews {
for (KeyView *keyView in kbd.keys) {
NSArray *mappedButtons = [self.keyMapper getControlsForMappedKey:keyView.code];
if ( mappedButtons.count > 0 ) {
NSMutableString *displayText = [NSMutableString string];
int index = 0;
for (NSNumber *button in mappedButtons) {
if ( index++ > 0 ) {
[displayText appendString:@","];
}
[displayText appendString:[NSString stringWithFormat:@"%@",[KeyMapper controlToDisplayName:button.integerValue]]];
}
keyView.mappedKey = displayText;
} else {
keyView.mappedKey = @"";
}
[keyView setNeedsDisplay];
}
}
-(void) resetMappingsButtonTapped:(id)sender {
[self.keyMapper resetToDefaults];
[self refreshKeyMappingsInViews];
[self.keyMapper saveKeyMapping];
[self.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
}
# pragma - mark KeyDelegate
-(void)onKeyDown:(KeyView*)k {
}
-(void)onKeyUp:(KeyView*)k {
// show alert view
self.keyMapperAlertView = [[UIAlertView alloc] initWithTitle:@"Remap Key" message:[NSString stringWithFormat:@"Press a button to map the [%@] key",k.title] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Unbind",nil];
self.keyMapperAlertView.tag = k.code;
[self.keyMapperAlertView show];
[self.mfiInputHandler startRemappingControlsForMfiControllerForKey:k.code];
__weak __typeof(self) weakSelf = self;
self.mfiInputHandler.dismiss = ^{
[weakSelf.keyMapperAlertView dismissWithClickedButtonIndex:0 animated:YES];
[weakSelf.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
[weakSelf.keyMapper saveKeyMapping];
[weakSelf refreshKeyMappingsInViews];
};
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ( buttonIndex == 1 ) {
SDL_scancode mappedKey = alertView.tag;
[self.keyMapper unmapKey:mappedKey];
[self.keyMapper saveKeyMapping];
[self refreshKeyMappingsInViews];
[self.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
}
}
@end