4f3c7dc4326b41a469c65a1e15a177d39fbe8655
[platform/upstream/SDL.git] / src / video / uikit / SDL_uikitvideo.m
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22
23 #if SDL_VIDEO_DRIVER_UIKIT
24
25 #import <UIKit/UIKit.h>
26
27 #include "SDL_video.h"
28 #include "SDL_mouse.h"
29 #include "SDL_hints.h"
30 #include "../SDL_sysvideo.h"
31 #include "../SDL_pixels_c.h"
32 #include "../../events/SDL_events_c.h"
33
34 #include "SDL_uikitvideo.h"
35 #include "SDL_uikitevents.h"
36 #include "SDL_uikitmodes.h"
37 #include "SDL_uikitwindow.h"
38 #include "SDL_uikitopengles.h"
39
40 #define UIKITVID_DRIVER_NAME "uikit"
41
42 /* Initialization/Query functions */
43 static int UIKit_VideoInit(_THIS);
44 static void UIKit_VideoQuit(_THIS);
45
46 /* DUMMY driver bootstrap functions */
47
48 static int
49 UIKit_Available(void)
50 {
51     return 1;
52 }
53
54 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
55 {
56     SDL_free(device);
57 }
58
59 static SDL_VideoDevice *
60 UIKit_CreateDevice(int devindex)
61 {
62     SDL_VideoDevice *device;
63
64     /* Initialize all variables that we clean on shutdown */
65     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
66     if (!device) {
67         SDL_free(device);
68         SDL_OutOfMemory();
69         return (0);
70     }
71
72     /* Set the function pointers */
73     device->VideoInit = UIKit_VideoInit;
74     device->VideoQuit = UIKit_VideoQuit;
75     device->GetDisplayModes = UIKit_GetDisplayModes;
76     device->SetDisplayMode = UIKit_SetDisplayMode;
77     device->PumpEvents = UIKit_PumpEvents;
78     device->SuspendScreenSaver = UIKit_SuspendScreenSaver;
79     device->CreateWindow = UIKit_CreateWindow;
80     device->SetWindowTitle = UIKit_SetWindowTitle;
81     device->ShowWindow = UIKit_ShowWindow;
82     device->HideWindow = UIKit_HideWindow;
83     device->RaiseWindow = UIKit_RaiseWindow;
84     device->SetWindowBordered = UIKit_SetWindowBordered;
85     device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
86     device->DestroyWindow = UIKit_DestroyWindow;
87     device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
88
89 #if SDL_IPHONE_KEYBOARD
90     device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
91     device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
92     device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
93     device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
94     device->SetTextInputRect = UIKit_SetTextInputRect;
95 #endif
96
97     /* OpenGL (ES) functions */
98     device->GL_MakeCurrent      = UIKit_GL_MakeCurrent;
99     device->GL_GetDrawableSize  = UIKit_GL_GetDrawableSize;
100     device->GL_SwapWindow       = UIKit_GL_SwapWindow;
101     device->GL_CreateContext    = UIKit_GL_CreateContext;
102     device->GL_DeleteContext    = UIKit_GL_DeleteContext;
103     device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
104     device->GL_LoadLibrary      = UIKit_GL_LoadLibrary;
105     device->free = UIKit_DeleteDevice;
106
107     device->gl_config.accelerated = 1;
108
109     return device;
110 }
111
112 VideoBootStrap UIKIT_bootstrap = {
113     UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
114     UIKit_Available, UIKit_CreateDevice
115 };
116
117
118 int
119 UIKit_VideoInit(_THIS)
120 {
121     _this->gl_config.driver_loaded = 1;
122
123     if (UIKit_InitModes(_this) < 0) {
124         return -1;
125     }
126     return 0;
127 }
128
129 void
130 UIKit_VideoQuit(_THIS)
131 {
132     UIKit_QuitModes(_this);
133 }
134
135 void
136 UIKit_SuspendScreenSaver(_THIS)
137 {
138     @autoreleasepool {
139         /* Ignore ScreenSaver API calls if the idle timer hint has been set. */
140         /* FIXME: The idle timer hint should be deprecated for SDL 2.1. */
141         if (SDL_GetHint(SDL_HINT_IDLE_TIMER_DISABLED) == NULL) {
142             UIApplication *app = [UIApplication sharedApplication];
143
144             /* Prevent the display from dimming and going to sleep. */
145             app.idleTimerDisabled = (_this->suspend_screensaver != SDL_FALSE);
146         }
147     }
148 }
149
150 BOOL
151 UIKit_IsSystemVersionAtLeast(double version)
152 {
153     return [[UIDevice currentDevice].systemVersion doubleValue] >= version;
154 }
155
156 CGRect
157 UIKit_ComputeViewFrame(SDL_Window *window, UIScreen *screen)
158 {
159     BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0);
160
161     if (hasiOS7 || (window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
162         /* The view should always show behind the status bar in iOS 7+. */
163         return screen.bounds;
164     } else {
165         return screen.applicationFrame;
166     }
167 }
168
169 /*
170  * iOS log support.
171  *
172  * This doesn't really have aything to do with the interfaces of the SDL video
173  *  subsystem, but we need to stuff this into an Objective-C source code file.
174  */
175
176 void SDL_NSLog(const char *text)
177 {
178     NSLog(@"%s", text);
179 }
180
181 #endif /* SDL_VIDEO_DRIVER_UIKIT */
182
183 /* vi: set ts=4 sw=4 expandtab: */