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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
|
/*
* Copyright (C) 2010-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include <sys/resource.h>
#include <signal.h>
#include "ServiceBroker.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "FileItem.h"
#include "music/tags/MusicInfoTag.h"
#include "filesystem/SpecialProtocol.h"
#include "playlists/PlayList.h"
#include "messaging/ApplicationMessenger.h"
#include "Application.h"
#include "AppInboundProtocol.h"
#include "input/touch/generic/GenericTouchActionHandler.h"
#include "guilib/GUIControl.h"
#include "input/Key.h"
#include "windowing/ios/WinSystemIOS.h"
#include "windowing/XBMC_events.h"
#include "utils/log.h"
#include "utils/TimeUtils.h"
#include "Util.h"
#include "threads/Event.h"
#define id _id
#include "TextureCache.h"
#undef id
#include <math.h>
using namespace KODI::MESSAGING;
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795028842
#endif
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
#import <AVFoundation/AVAudioSession.h>
#import <MediaPlayer/MPMediaItem.h>
#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import "IOSEAGLView.h"
#import "XBMCController.h"
#import "IOSScreenManager.h"
#import "XBMCApplication.h"
#import "platform/darwin/NSLogDebugHelpers.h"
#import "platform/darwin/AutoPool.h"
XBMCController *g_xbmcController;
//--------------------------------------------------------------
//
@interface XBMCController ()
- (void)rescheduleNetworkAutoSuspend;
@end
@interface UIApplication (extended)
-(void) terminateWithSuccess;
@end
@implementation XBMCController
@synthesize animating;
@synthesize lastGesturePoint;
@synthesize screenScale;
@synthesize touchBeginSignaled;
@synthesize m_screenIdx;
@synthesize screensize;
@synthesize m_networkAutoSuspendTimer;
@synthesize nowPlayingInfo;
@synthesize nativeKeyboardActive;
//--------------------------------------------------------------
- (void) sendKeypressEvent: (XBMC_Event) event
{
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
if (appPort)
{
event.type = XBMC_KEYDOWN;
appPort->OnEvent(event);
event.type = XBMC_KEYUP;
appPort->OnEvent(event);
}
}
// START OF UIKeyInput protocol
- (BOOL)hasText
{
return NO;
}
- (void)insertText:(NSString *)text
{
// in case the native touch keyboard is active
// don't do anything here
// we are only supposed to be called when
// using an external bt keyboard...
if (nativeKeyboardActive)
{
return;
}
XBMC_Event newEvent;
memset(&newEvent, 0, sizeof(newEvent));
unichar currentKey = [text characterAtIndex:0];
// handle upper case letters
if (currentKey >= 'A' && currentKey <= 'Z')
{
newEvent.key.keysym.mod = XBMCKMOD_LSHIFT;
currentKey += 0x20;// convert to lower case
}
// handle return
if (currentKey == '\n' || currentKey == '\r')
currentKey = XBMCK_RETURN;
newEvent.key.keysym.sym = (XBMCKey)currentKey;
newEvent.key.keysym.unicode = currentKey;
[self sendKeypressEvent:newEvent];
}
- (void)deleteBackward
{
[self sendKey:XBMCK_BACKSPACE];
}
// END OF UIKeyInput protocol
// - iOS6 rotation API - will be called on iOS7 runtime!--------
- (NSUInteger)supportedInterfaceOrientations
{
//mask defines available as of ios6 sdk
//return UIInterfaceOrientationMaskLandscape;
return (1 << UIInterfaceOrientationLandscapeLeft) | (1 << UIInterfaceOrientationLandscapeRight);
}
// - old rotation API will be called on iOS6 and lower - removed in iOS7
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//on external screens somehow the logic is rotated by 90°
//so we have to do this with our supported orientations then aswell
if([[IOSScreenManager sharedInstance] isExternalScreen])
{
if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
}
else//internal screen
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
return YES;
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return YES;
}
}
return NO;
}
//--------------------------------------------------------------
- (UIInterfaceOrientation) getOrientation
{
return orientation;
}
-(void)sendKey:(XBMCKey) key
{
XBMC_Event newEvent;
memset(&newEvent, 0, sizeof(newEvent));
//newEvent.key.keysym.unicode = key;
newEvent.key.keysym.sym = key;
[self sendKeypressEvent:newEvent];
}
//--------------------------------------------------------------
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
return YES;
}
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
//--------------------------------------------------------------
- (void)addSwipeGesture:(UISwipeGestureRecognizerDirection)direction numTouches : (NSUInteger)numTouches
{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe:)];
swipe.delaysTouchesBegan = NO;
swipe.numberOfTouchesRequired = numTouches;
swipe.direction = direction;
swipe.delegate = self;
[m_glView addGestureRecognizer:swipe];
[swipe release];
}
//--------------------------------------------------------------
- (void)addTapGesture:(NSUInteger)numTouches
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];
tapGesture.delaysTouchesBegan = NO;
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = numTouches;
[m_glView addGestureRecognizer:tapGesture];
[tapGesture release];
}
//--------------------------------------------------------------
- (void)createGestureRecognizers
{
//1 finger single tap
[self addTapGesture:1];
//2 finger single tap - right mouse
//single finger double tap delays single finger single tap - so we
//go for 2 fingers here - so single finger single tap is instant
[self addTapGesture:2];
//3 finger single tap
[self addTapGesture:3];
//1 finger single long tap - right mouse - alternative
UILongPressGestureRecognizer *singleFingerSingleLongTap = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleFingerSingleLongTap:)];
singleFingerSingleLongTap.delaysTouchesBegan = NO;
singleFingerSingleLongTap.delaysTouchesEnded = NO;
[m_glView addGestureRecognizer:singleFingerSingleLongTap];
[singleFingerSingleLongTap release];
//triple finger swipe left
[self addSwipeGesture:UISwipeGestureRecognizerDirectionLeft numTouches:3];
//double finger swipe left for backspace ... i like this fast backspace feature ;)
[self addSwipeGesture:UISwipeGestureRecognizerDirectionLeft numTouches:2];
//single finger swipe left
[self addSwipeGesture:UISwipeGestureRecognizerDirectionLeft numTouches:1];
//triple finger swipe right
[self addSwipeGesture:UISwipeGestureRecognizerDirectionRight numTouches:3];
//double finger swipe right
[self addSwipeGesture:UISwipeGestureRecognizerDirectionRight numTouches:2];
//single finger swipe right
[self addSwipeGesture:UISwipeGestureRecognizerDirectionRight numTouches:1];
//triple finger swipe up
[self addSwipeGesture:UISwipeGestureRecognizerDirectionUp numTouches:3];
//double finger swipe up
[self addSwipeGesture:UISwipeGestureRecognizerDirectionUp numTouches:2];
//single finger swipe up
[self addSwipeGesture:UISwipeGestureRecognizerDirectionUp numTouches:1];
//triple finger swipe down
[self addSwipeGesture:UISwipeGestureRecognizerDirectionDown numTouches:3];
//double finger swipe down
[self addSwipeGesture:UISwipeGestureRecognizerDirectionDown numTouches:2];
//single finger swipe down
[self addSwipeGesture:UISwipeGestureRecognizerDirectionDown numTouches:1];
//for pan gestures with one finger
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePan:)];
pan.delaysTouchesBegan = NO;
pan.maximumNumberOfTouches = 1;
[m_glView addGestureRecognizer:pan];
[pan release];
//for zoom gesture
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinch:)];
pinch.delaysTouchesBegan = NO;
pinch.delegate = self;
[m_glView addGestureRecognizer:pinch];
[pinch release];
//for rotate gesture
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(handleRotate:)];
rotate.delaysTouchesBegan = NO;
rotate.delegate = self;
[m_glView addGestureRecognizer:rotate];
[rotate release];
}
//--------------------------------------------------------------
- (void) activateKeyboard:(UIView *)view
{
[self.view addSubview:view];
m_glView.userInteractionEnabled = NO;
}
//--------------------------------------------------------------
- (void) deactivateKeyboard:(UIView *)view
{
[view removeFromSuperview];
m_glView.userInteractionEnabled = YES;
[self becomeFirstResponder];
}
//--------------------------------------------------------------
- (void) nativeKeyboardActive: (bool)active
{
nativeKeyboardActive = active;
}
//--------------------------------------------------------------
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if( m_glView && [m_glView isXBMCAlive] )//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
UITouch *touch = (UITouch *)[[touches allObjects] objectAtIndex:0];
CGPoint point = [touch locationInView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
CGenericTouchActionHandler::GetInstance().OnSingleTouchStart(point.x, point.y);
}
}
//--------------------------------------------------------------
-(void)handlePinch:(UIPinchGestureRecognizer*)sender
{
if( m_glView && [m_glView isXBMCAlive] && sender.numberOfTouches )//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
switch(sender.state)
{
case UIGestureRecognizerStateBegan:
CGenericTouchActionHandler::GetInstance().OnTouchGestureStart(point.x, point.y);
break;
case UIGestureRecognizerStateChanged:
CGenericTouchActionHandler::GetInstance().OnZoomPinch(point.x, point.y, [sender scale]);
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
CGenericTouchActionHandler::GetInstance().OnTouchGestureEnd(point.x, point.y, 0, 0, 0, 0);
break;
default:
break;
}
}
}
//--------------------------------------------------------------
-(void)handleRotate:(UIRotationGestureRecognizer*)sender
{
if( m_glView && [m_glView isXBMCAlive] && sender.numberOfTouches )//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
switch(sender.state)
{
case UIGestureRecognizerStateBegan:
CGenericTouchActionHandler::GetInstance().OnTouchGestureStart(point.x, point.y);
break;
case UIGestureRecognizerStateChanged:
CGenericTouchActionHandler::GetInstance().OnRotate(point.x, point.y, RADIANS_TO_DEGREES([sender rotation]));
break;
case UIGestureRecognizerStateEnded:
CGenericTouchActionHandler::GetInstance().OnTouchGestureEnd(point.x, point.y, 0, 0, 0, 0);
break;
default:
break;
}
}
}
//--------------------------------------------------------------
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
if( m_glView && [m_glView isXBMCAlive] )//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
CGPoint velocity = [sender velocityInView:m_glView];
if( [sender state] == UIGestureRecognizerStateBegan && sender.numberOfTouches )
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
touchBeginSignaled = false;
lastGesturePoint = point;
}
if( [sender state] == UIGestureRecognizerStateChanged && sender.numberOfTouches )
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
bool bNotify = false;
CGFloat yMovement=point.y - lastGesturePoint.y;
CGFloat xMovement=point.x - lastGesturePoint.x;
if( xMovement )
{
bNotify = true;
}
if( yMovement )
{
bNotify = true;
}
if( bNotify )
{
if( !touchBeginSignaled )
{
CGenericTouchActionHandler::GetInstance().OnTouchGestureStart((float)point.x, (float)point.y);
touchBeginSignaled = true;
}
CGenericTouchActionHandler::GetInstance().OnTouchGesturePan((float)point.x, (float)point.y,
(float)xMovement, (float)yMovement,
(float)velocity.x, (float)velocity.y);
lastGesturePoint = point;
}
}
if( touchBeginSignaled && ([sender state] == UIGestureRecognizerStateEnded || [sender state] == UIGestureRecognizerStateCancelled))
{
//signal end of pan - this will start inertial scrolling with deacceleration in CApplication
CGenericTouchActionHandler::GetInstance().OnTouchGestureEnd((float)lastGesturePoint.x, (float)lastGesturePoint.y,
(float)0.0, (float)0.0,
(float)velocity.x, (float)velocity.y);
touchBeginSignaled = false;
}
}
}
//--------------------------------------------------------------
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender
{
if( m_glView && [m_glView isXBMCAlive] && sender.numberOfTouches )//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
if (sender.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
TouchMoveDirection direction = TouchMoveDirectionNone;
switch ([sender direction])
{
case UISwipeGestureRecognizerDirectionRight:
direction = TouchMoveDirectionRight;
break;
case UISwipeGestureRecognizerDirectionLeft:
direction = TouchMoveDirectionLeft;
break;
case UISwipeGestureRecognizerDirectionUp:
direction = TouchMoveDirectionUp;
break;
case UISwipeGestureRecognizerDirectionDown:
direction = TouchMoveDirectionDown;
break;
}
CGenericTouchActionHandler::GetInstance().OnSwipe(direction,
0.0, 0.0,
point.x, point.y, 0, 0,
[sender numberOfTouches]);
}
}
}
//--------------------------------------------------------------
- (IBAction)handleTap:(UIGestureRecognizer *)sender
{
//Allow the tap gesture during init
//(for allowing the user to tap away any messageboxes during init)
if( ([m_glView isReadyToRun] && [sender numberOfTouches] == 1) || [m_glView isXBMCAlive])
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
//NSLog(@"%s singleTap", __PRETTY_FUNCTION__);
CGenericTouchActionHandler::GetInstance().OnTap((float)point.x, (float)point.y, [sender numberOfTouches]);
}
}
//--------------------------------------------------------------
- (IBAction)handleSingleFingerSingleLongTap:(UIGestureRecognizer *)sender
{
if( m_glView && [m_glView isXBMCAlive] && sender.numberOfTouches)//NO GESTURES BEFORE WE ARE UP AND RUNNING
{
CGPoint point = [sender locationOfTouch:0 inView:m_glView];
point.x *= screenScale;
point.y *= screenScale;
if (sender.state == UIGestureRecognizerStateBegan)
{
lastGesturePoint = point;
// mark the control
//CGenericTouchActionHandler::GetInstance().OnSingleTouchStart((float)point.x, (float)point.y);
}
if (sender.state == UIGestureRecognizerStateEnded)
{
CGenericTouchActionHandler::GetInstance().OnSingleTouchMove((float)point.x, (float)point.y, point.x - lastGesturePoint.x, point.y - lastGesturePoint.y, 0, 0);
}
if (sender.state == UIGestureRecognizerStateEnded)
{
CGenericTouchActionHandler::GetInstance().OnLongPress((float)point.x, (float)point.y);
}
}
}
//--------------------------------------------------------------
- (id)initWithFrame:(CGRect)frame withScreen:(UIScreen *)screen
{
PRINT_SIGNATURE();
m_screenIdx = 0;
self = [super init];
if ( !self )
return ( nil );
m_glView = NULL;
m_isPlayingBeforeInactive = NO;
m_bgTask = UIBackgroundTaskInvalid;
m_playbackState = IOS_PLAYBACK_STOPPED;
m_window = [[UIWindow alloc] initWithFrame:frame];
[m_window setRootViewController:self];
m_window.screen = screen;
/* Turn off autoresizing */
m_window.autoresizingMask = 0;
m_window.autoresizesSubviews = NO;
NSNotificationCenter *center;
center = [NSNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(observeDefaultCenterStuff:)
name: nil
object: nil];
orientation = UIInterfaceOrientationLandscapeLeft;
[m_window makeKeyAndVisible];
g_xbmcController = self;
return self;
}
//--------------------------------------------------------------
- (void)loadView
{
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
m_glView = [[IOSEAGLView alloc] initWithFrame:self.view.bounds withScreen:[UIScreen mainScreen]];
[[IOSScreenManager sharedInstance] setView:m_glView];
[m_glView setMultipleTouchEnabled:YES];
/* Check if screen is Retina */
screenScale = [m_glView getScreenScale:[UIScreen mainScreen]];
[self.view addSubview: m_glView];
[self createGestureRecognizers];
}
//--------------------------------------------------------------
-(void)viewDidLoad
{
[super viewDidLoad];
}
//--------------------------------------------------------------
- (void)dealloc
{
// stop background task
[m_networkAutoSuspendTimer invalidate];
[self enableNetworkAutoSuspend:nil];
[m_glView stopAnimation];
[m_glView release];
[m_window release];
NSNotificationCenter *center;
// take us off the default center for our app
center = [NSNotificationCenter defaultCenter];
[center removeObserver: self];
[super dealloc];
}
//--------------------------------------------------------------
- (void)viewWillAppear:(BOOL)animated
{
PRINT_SIGNATURE();
// move this later into CocoaPowerSyscall
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[self resumeAnimation];
[super viewWillAppear:animated];
}
//--------------------------------------------------------------
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Notifies UIKit that our view controller updated its preference
// regarding the visual indicator
// this should make ios call prefersHomeIndicatorAutoHidden and
// hide the home indicator on iPhoneX and other devices without
// home button
if ([self respondsToSelector:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden)]) {
[self performSelector:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden)];
}
}
//--------------------------------------------------------------
- (BOOL)prefersHomeIndicatorAutoHidden
{
return YES;
}
//--------------------------------------------------------------
- (void)viewWillDisappear:(BOOL)animated
{
PRINT_SIGNATURE();
[self pauseAnimation];
// move this later into CocoaPowerSyscall
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[super viewWillDisappear:animated];
}
//--------------------------------------------------------------
-(UIView *)inputView
{
// override our input view to an empty view
// this prevents the on screen keyboard
// which would be shown whenever this UIResponder
// becomes the first responder (which is always the case!)
// caused by implementing the UIKeyInput protocol
return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
//--------------------------------------------------------------
- (BOOL) canBecomeFirstResponder
{
return YES;
}
//--------------------------------------------------------------
- (void)viewDidUnload
{
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewDidUnload];
}
//--------------------------------------------------------------
- (void) setFramebuffer
{
[m_glView setFramebuffer];
}
//--------------------------------------------------------------
- (bool) presentFramebuffer
{
return [m_glView presentFramebuffer];
}
//--------------------------------------------------------------
- (CGSize) getScreenSize
{
__block CGSize tmp;
if ([NSThread isMainThread])
{
tmp.width = m_glView.bounds.size.width * screenScale;
tmp.height = m_glView.bounds.size.height * screenScale;
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
tmp.width = m_glView.bounds.size.width * screenScale;
tmp.height = m_glView.bounds.size.height * screenScale;
});
}
screensize = tmp;
return screensize;
}
//--------------------------------------------------------------
- (CGFloat) getScreenScale:(UIScreen *)screen
{
return [m_glView getScreenScale:screen];
}
//--------------------------------------------------------------
//--------------------------------------------------------------
- (BOOL) recreateOnReselect
{
PRINT_SIGNATURE();
return YES;
}
//--------------------------------------------------------------
- (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)disableNetworkAutoSuspend
{
PRINT_SIGNATURE();
if (m_bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask: m_bgTask];
m_bgTask = UIBackgroundTaskInvalid;
}
// we have to alloc the background task for keep network working after screen lock and dark.
UIBackgroundTaskIdentifier newTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
m_bgTask = newTask;
if (m_networkAutoSuspendTimer)
{
[m_networkAutoSuspendTimer invalidate];
self.m_networkAutoSuspendTimer = nil;
}
}
//--------------------------------------------------------------
- (void)enableNetworkAutoSuspend:(id)obj
{
PRINT_SIGNATURE();
if (m_bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask: m_bgTask];
m_bgTask = UIBackgroundTaskInvalid;
}
}
//--------------------------------------------------------------
- (void) disableSystemSleep
{
}
//--------------------------------------------------------------
- (void) enableSystemSleep
{
}
//--------------------------------------------------------------
- (void) disableScreenSaver
{
}
//--------------------------------------------------------------
- (void) enableScreenSaver
{
}
//--------------------------------------------------------------
- (bool) changeScreen: (unsigned int)screenIdx withMode:(UIScreenMode *)mode
{
bool ret = false;
ret = [[IOSScreenManager sharedInstance] changeScreen:screenIdx withMode:mode];
return ret;
}
//--------------------------------------------------------------
- (void) activateScreen: (UIScreen *)screen withOrientation:(UIInterfaceOrientation)newOrientation
{
// Since ios7 we have to handle the orientation manually
// it differs by 90 degree between internal and external screen
float angle = 0;
UIView *view = [m_window.subviews objectAtIndex:0];
switch(newOrientation)
{
case UIInterfaceOrientationUnknown:
case UIInterfaceOrientationPortrait:
angle = 0;
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIInterfaceOrientationLandscapeLeft:
angle = -M_PI_2;
break;
case UIInterfaceOrientationLandscapeRight:
angle = M_PI_2;
break;
}
// reset the rotation of the view
view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);
view.layer.bounds = view.bounds;
m_window.screen = screen;
[view setFrame:m_window.frame];
}
//--------------------------------------------------------------
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
LOG(@"%s: type %zd, subtype: %zd", __PRETTY_FUNCTION__, receivedEvent.type, receivedEvent.subtype);
if (receivedEvent.type == UIEventTypeRemoteControl)
{
[self disableNetworkAutoSuspend];
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_PLAYPAUSE)));
break;
case UIEventSubtypeRemoteControlPlay:
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_PLAY)));
break;
case UIEventSubtypeRemoteControlPause:
// ACTION_PAUSE sometimes cause unpause, use MediaPauseIfPlaying to make sure pause only
CApplicationMessenger::GetInstance().SendMsg(TMSG_MEDIA_PAUSE_IF_PLAYING);
break;
case UIEventSubtypeRemoteControlNextTrack:
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_NEXT_ITEM)));
break;
case UIEventSubtypeRemoteControlPreviousTrack:
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PREV_ITEM)));
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:
// use 4X speed forward.
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_FORWARD)));
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_FORWARD)));
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
// use 4X speed rewind.
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_REWIND)));
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_REWIND)));
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
case UIEventSubtypeRemoteControlEndSeekingBackward:
// restore to normal playback speed.
if (g_application.GetAppPlayer().IsPlaying() && !g_application.GetAppPlayer().IsPaused())
CApplicationMessenger::GetInstance().SendMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, static_cast<void*>(new CAction(ACTION_PLAYER_PLAY)));
break;
default:
LOG(@"unhandled subtype: %zd", receivedEvent.subtype);
break;
}
[self rescheduleNetworkAutoSuspend];
}
}
//--------------------------------------------------------------
- (void)enterBackground
{
PRINT_SIGNATURE();
if (g_application.GetAppPlayer().IsPlaying() && !g_application.GetAppPlayer().IsPaused())
{
m_isPlayingBeforeInactive = YES;
CApplicationMessenger::GetInstance().SendMsg(TMSG_MEDIA_PAUSE_IF_PLAYING);
}
CWinSystemIOS* winSystem = dynamic_cast<CWinSystemIOS*>(CServiceBroker::GetWinSystem());
winSystem->OnAppFocusChange(false);
}
- (void)enterForeground
{
PRINT_SIGNATURE();
CWinSystemIOS* winSystem = dynamic_cast<CWinSystemIOS*>(CServiceBroker::GetWinSystem());
if (winSystem)
winSystem->OnAppFocusChange(true);
// when we come back, restore playing if we were.
if (m_isPlayingBeforeInactive)
{
CApplicationMessenger::GetInstance().SendMsg(TMSG_MEDIA_UNPAUSE);
m_isPlayingBeforeInactive = NO;
}
}
- (void)becomeInactive
{
// if we were interrupted, already paused here
// else if user background us or lock screen, only pause video here, audio keep playing.
if (g_application.GetAppPlayer().IsPlayingVideo() && !g_application.GetAppPlayer().IsPaused())
{
m_isPlayingBeforeInactive = YES;
CApplicationMessenger::GetInstance().SendMsg(TMSG_MEDIA_PAUSE_IF_PLAYING);
}
// check whether we need disable network auto suspend.
[self rescheduleNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)pauseAnimation
{
PRINT_SIGNATURE();
[m_glView pauseAnimation];
}
//--------------------------------------------------------------
- (void)resumeAnimation
{
PRINT_SIGNATURE();
[m_glView resumeAnimation];
}
//--------------------------------------------------------------
- (void)startAnimation
{
PRINT_SIGNATURE();
[m_glView startAnimation];
}
//--------------------------------------------------------------
- (void)stopAnimation
{
PRINT_SIGNATURE();
[m_glView stopAnimation];
}
//--------------------------------------------------------------
- (void)setIOSNowPlayingInfo:(NSDictionary *)info
{
self.nowPlayingInfo = info;
// MPNowPlayingInfoCenter is an ios5+ class, following code will work on ios5 even if compiled by xcode3
Class NowPlayingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (NowPlayingInfoCenter)
[[NowPlayingInfoCenter defaultCenter] setNowPlayingInfo:self.nowPlayingInfo];
}
//--------------------------------------------------------------
- (void)onPlay:(NSDictionary *)item
{
PRINT_SIGNATURE();
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
NSString *title = [item objectForKey:@"title"];
if (title && title.length > 0)
[dict setObject:title forKey:MPMediaItemPropertyTitle];
NSString *album = [item objectForKey:@"album"];
if (album && album.length > 0)
[dict setObject:album forKey:MPMediaItemPropertyAlbumTitle];
NSArray *artists = [item objectForKey:@"artist"];
if (artists && artists.count > 0)
[dict setObject:[artists componentsJoinedByString:@" "] forKey:MPMediaItemPropertyArtist];
NSNumber *track = [item objectForKey:@"track"];
if (track)
[dict setObject:track forKey:MPMediaItemPropertyAlbumTrackNumber];
NSNumber *duration = [item objectForKey:@"duration"];
if (duration)
[dict setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
NSArray *genres = [item objectForKey:@"genre"];
if (genres && genres.count > 0)
[dict setObject:[genres componentsJoinedByString:@" "] forKey:MPMediaItemPropertyGenre];
if (NSClassFromString(@"MPNowPlayingInfoCenter"))
{
NSString *thumb = [item objectForKey:@"thumb"];
if (thumb && thumb.length > 0)
{
UIImage *image = [UIImage imageWithContentsOfFile:thumb];
if (image)
{
MPMediaItemArtwork *mArt = [[MPMediaItemArtwork alloc] initWithImage:image];
if (mArt)
{
[dict setObject:mArt forKey:MPMediaItemPropertyArtwork];
[mArt release];
}
}
}
// these property keys are ios5+ only
NSNumber *elapsed = [item objectForKey:@"elapsed"];
if (elapsed)
[dict setObject:elapsed forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
NSNumber *speed = [item objectForKey:@"speed"];
if (speed)
[dict setObject:speed forKey:MPNowPlayingInfoPropertyPlaybackRate];
NSNumber *current = [item objectForKey:@"current"];
if (current)
[dict setObject:current forKey:MPNowPlayingInfoPropertyPlaybackQueueIndex];
NSNumber *total = [item objectForKey:@"total"];
if (total)
[dict setObject:total forKey:MPNowPlayingInfoPropertyPlaybackQueueCount];
}
/*
other properties can be set:
MPMediaItemPropertyAlbumTrackCount
MPMediaItemPropertyComposer
MPMediaItemPropertyDiscCount
MPMediaItemPropertyDiscNumber
MPMediaItemPropertyPersistentID
Additional metadata properties:
MPNowPlayingInfoPropertyChapterNumber;
MPNowPlayingInfoPropertyChapterCount;
*/
[self setIOSNowPlayingInfo:dict];
[dict release];
m_playbackState = IOS_PLAYBACK_PLAYING;
[self disableNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)OnSpeedChanged:(NSDictionary *)item
{
PRINT_SIGNATURE();
if (NSClassFromString(@"MPNowPlayingInfoCenter"))
{
NSMutableDictionary *info = [self.nowPlayingInfo mutableCopy];
NSNumber *elapsed = [item objectForKey:@"elapsed"];
if (elapsed)
[info setObject:elapsed forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
NSNumber *speed = [item objectForKey:@"speed"];
if (speed)
[info setObject:speed forKey:MPNowPlayingInfoPropertyPlaybackRate];
[self setIOSNowPlayingInfo:info];
}
}
//--------------------------------------------------------------
- (void)onPause:(NSDictionary *)item
{
PRINT_SIGNATURE();
m_playbackState = IOS_PLAYBACK_PAUSED;
// schedule set network auto suspend state for save power if idle.
[self rescheduleNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)onStop:(NSDictionary *)item
{
PRINT_SIGNATURE();
[self setIOSNowPlayingInfo:nil];
m_playbackState = IOS_PLAYBACK_STOPPED;
// delay set network auto suspend state in case we are switching playing item.
[self rescheduleNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)rescheduleNetworkAutoSuspend
{
LOG(@"%s: playback state: %d", __PRETTY_FUNCTION__, m_playbackState);
if (m_playbackState == IOS_PLAYBACK_PLAYING)
{
[self disableNetworkAutoSuspend];
return;
}
if (m_networkAutoSuspendTimer)
[m_networkAutoSuspendTimer invalidate];
int delay = m_playbackState == IOS_PLAYBACK_PAUSED ? 60 : 30; // wait longer if paused than stopped
self.m_networkAutoSuspendTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(enableNetworkAutoSuspend:) userInfo:nil repeats:NO];
}
#pragma mark -
#pragma mark private helper methods
//
- (void)observeDefaultCenterStuff: (NSNotification *) notification
{
// LOG(@"default: %@", [notification name]);
// LOG(@"userInfo: %@", [notification userInfo]);
}
- (void*) getEAGLContextObj
{
return [m_glView getCurrentEAGLContext];
}
@end
|