21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_UIKIT
28 #include "../SDL_sysvideo.h"
29 #include "../../events/SDL_events_c.h"
38 #if SDL_IPHONE_KEYBOARD
39 #include "keyinfotable.h"
44 SDL_AppleTVControllerUIHintChanged(
void *userdata,
const char *
name,
const char *oldValue,
const char *hint)
48 viewcontroller.controllerUserInteractionEnabled = hint && (*hint !=
'0');
55 SDL_HideHomeIndicatorHintChanged(
void *userdata,
const char *
name,
const char *oldValue,
const char *hint)
60 #pragma clang diagnostic push
61 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
62 if ([viewcontroller respondsToSelector:
@selector(setNeedsUpdateOfHomeIndicatorAutoHidden)]) {
63 [viewcontroller setNeedsUpdateOfHomeIndicatorAutoHidden];
64 [viewcontroller setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
66 #pragma clang diagnostic pop
72 CADisplayLink *displayLink;
73 int animationInterval;
74 void (*animationCallback)(
void*);
75 void *animationCallbackParam;
77 #if SDL_IPHONE_KEYBOARD
78 UITextField *textField;
79 BOOL hardwareKeyboard;
81 BOOL rotatingOrientation;
83 NSString *obligateForBackspace;
89 - (instancetype)initWithSDLWindow:(
SDL_Window *)_window
91 if (
self = [super initWithNibName:nil bundle:nil]) {
92 self.window = _window;
94 #if SDL_IPHONE_KEYBOARD
96 hardwareKeyboard = NO;
98 rotatingOrientation = NO;
103 SDL_AppleTVControllerUIHintChanged,
104 (__bridge
void *)
self);
109 SDL_HideHomeIndicatorHintChanged,
110 (__bridge
void *)
self);
118 #if SDL_IPHONE_KEYBOARD
119 [
self deinitKeyboard];
124 SDL_AppleTVControllerUIHintChanged,
125 (__bridge
void *)
self);
130 SDL_HideHomeIndicatorHintChanged,
131 (__bridge
void *)
self);
135 - (
void)setAnimationCallback:(
int)interval
136 callback:(
void (*)(
void*))callback
137 callbackParam:(
void*)callbackParam
139 [
self stopAnimation];
141 animationInterval = interval;
143 animationCallbackParam = callbackParam;
145 if (animationCallback) {
146 [
self startAnimation];
150 - (
void)startAnimation
152 displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(doLoop:)];
157 if ([displayLink respondsToSelector:
@selector(preferredFramesPerSecond)]
158 &&
data != nil &&
data.uiwindow != nil
159 && [data.uiwindow.screen respondsToSelector:@selector(maximumFramesPerSecond)]) {
160 displayLink.preferredFramesPerSecond =
data.
uiwindow.screen.maximumFramesPerSecond / animationInterval;
164 #if __IPHONE_OS_VERSION_MIN_REQUIRED < 100300
165 [displayLink setFrameInterval:animationInterval];
169 [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
172 - (
void)stopAnimation
174 [displayLink invalidate];
178 - (
void)doLoop:(CADisplayLink*)sender
181 if (!UIKit_ShowingMessageBox()) {
185 animationCallback(animationCallbackParam);
194 - (
void)viewDidLayoutSubviews
196 const CGSize
size =
self.view.bounds.size;
197 int w = (int)
size.width;
198 int h = (
int)
size.height;
204 - (NSUInteger)supportedInterfaceOrientations
209 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
210 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
212 return ([
self supportedInterfaceOrientations] & (1 << orient)) != 0;
216 - (BOOL)prefersStatusBarHidden
222 - (BOOL)prefersHomeIndicatorAutoHidden
225 if (
self.homeIndicatorHidden == 1) {
231 - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
233 if (
self.homeIndicatorHidden >= 0) {
234 if (
self.homeIndicatorHidden == 2) {
235 return UIRectEdgeAll;
237 return UIRectEdgeNone;
243 return UIRectEdgeAll;
245 return UIRectEdgeNone;
253 #if SDL_IPHONE_KEYBOARD
255 @synthesize textInputRect;
256 @synthesize keyboardHeight;
257 @synthesize keyboardVisible;
263 obligateForBackspace =
@" ";
264 textField = [[UITextField alloc] initWithFrame:CGRectZero];
265 textField.delegate =
self;
267 textField.text = obligateForBackspace;
270 textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
271 textField.autocorrectionType = UITextAutocorrectionTypeNo;
272 textField.enablesReturnKeyAutomatically = NO;
273 textField.keyboardAppearance = UIKeyboardAppearanceDefault;
274 textField.keyboardType = UIKeyboardTypeDefault;
275 textField.returnKeyType = UIReturnKeyDefault;
276 textField.secureTextEntry = NO;
278 textField.hidden = YES;
279 keyboardVisible = NO;
281 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
283 [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
284 [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
286 [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
289 - (NSArray *) keyCommands {
290 NSMutableArray *
commands = [[NSMutableArray alloc] init];
291 [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
292 [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
293 [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
294 [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
295 [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:kNilOptions action:@selector(handleCommand:)]];
296 return [NSArray arrayWithArray:commands];
299 - (
void) handleCommand: (UIKeyCommand *) keyCommand {
302 if (keyCommand.input == UIKeyInputUpArrow) {
304 }
else if (keyCommand.input == UIKeyInputDownArrow) {
306 }
else if (keyCommand.input == UIKeyInputLeftArrow) {
308 }
else if (keyCommand.input == UIKeyInputRightArrow) {
310 }
else if (keyCommand.input == UIKeyInputEscape) {
320 - (
void) downArrow: (UIKeyCommand *) keyCommand {
321 NSLog(
@"down arrow!");
324 - (
void)setView:(UIView *)view
326 [
super setView:view];
328 [view addSubview:textField];
330 if (keyboardVisible) {
336 #if TARGET_OS_TV || __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
337 - (
void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(
id<UIViewControllerTransitionCoordinator>)coordinator
339 [
super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
340 rotatingOrientation = YES;
341 [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {}
342 completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
343 rotatingOrientation = NO;
347 - (
void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
348 [
super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
349 rotatingOrientation = YES;
352 - (
void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
353 [
super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
354 rotatingOrientation = NO;
358 - (
void)deinitKeyboard
360 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
362 [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
363 [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
365 [center removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
371 keyboardVisible = YES;
372 if (textField.window) {
373 showingKeyboard = YES;
374 [textField becomeFirstResponder];
375 showingKeyboard = NO;
382 keyboardVisible = NO;
383 [textField resignFirstResponder];
386 - (
void)keyboardWillShow:(NSNotification *)notification
389 CGRect kbrect = [[notification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
393 kbrect = [
self.view convertRect:kbrect fromView:nil];
395 [
self setKeyboardHeight:(int)kbrect.size.height];
399 - (
void)keyboardWillHide:(NSNotification *)notification
401 if (!showingKeyboard && !rotatingOrientation) {
404 [
self setKeyboardHeight:0];
407 - (
void)textFieldTextDidChange:(NSNotification *)notification
409 if (changeText!=nil && textField.markedTextRange == nil)
411 NSUInteger
len = changeText.length;
416 for (
i = 0;
i <
len;
i++) {
417 unichar
c = [changeText characterAtIndex:i];
451 - (
void)updateKeyboard
453 CGAffineTransform
t =
self.view.transform;
454 CGPoint
offset = CGPointMake(0.0, 0.0);
455 CGRect
frame = UIKit_ComputeViewFrame(
window,
self.view.window.screen);
457 if (
self.keyboardHeight) {
458 int rectbottom =
self.textInputRect.y + self.textInputRect.
h;
459 int keybottom =
self.view.bounds.size.height - self.keyboardHeight;
460 if (keybottom < rectbottom) {
461 offset.y = keybottom - rectbottom;
475 self.view.frame =
frame;
478 - (
void)setKeyboardHeight:(
int)height
480 keyboardVisible =
height > 0;
482 [
self updateKeyboard];
486 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
488 NSUInteger
len =
string.length;
491 if (textField.markedTextRange == nil) {
496 if (textField.text.length < 16) {
497 textField.text = obligateForBackspace;
506 - (BOOL)textFieldShouldReturn:(UITextField*)_textField
510 if (keyboardVisible &&
522 #if SDL_IPHONE_KEYBOARD
538 UIKit_HasScreenKeyboardSupport(
_THIS)
567 return vc.keyboardVisible;
584 vc.textInputRect = *
rect;
586 if (vc.keyboardVisible) {