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#include "SDL_uikitclipboard.h"
40
41#define UIKITVID_DRIVER_NAME "uikit"
42
43@implementation SDL_VideoData
44
45@end
46
47/* Initialization/Query functions */
48static int UIKit_VideoInit(_THIS);
49static void UIKit_VideoQuit(_THIS);
50
51/* DUMMY driver bootstrap functions */
52
53static int
54UIKit_Available(void)
55{
56    return 1;
57}
58
59static void UIKit_DeleteDevice(SDL_VideoDevice * device)
60{
61    @autoreleasepool {
62        CFRelease(device->driverdata);
63        SDL_free(device);
64    }
65}
66
67static SDL_VideoDevice *
68UIKit_CreateDevice(int devindex)
69{
70    @autoreleasepool {
71        SDL_VideoDevice *device;
72        SDL_VideoData *data;
73
74        /* Initialize all variables that we clean on shutdown */
75        device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
76        if (device) {
77            data = [SDL_VideoData new];
78        } else {
79            SDL_free(device);
80            SDL_OutOfMemory();
81            return (0);
82        }
83
84        device->driverdata = (void *) CFBridgingRetain(data);
85
86        /* Set the function pointers */
87        device->VideoInit = UIKit_VideoInit;
88        device->VideoQuit = UIKit_VideoQuit;
89        device->GetDisplayModes = UIKit_GetDisplayModes;
90        device->SetDisplayMode = UIKit_SetDisplayMode;
91        device->PumpEvents = UIKit_PumpEvents;
92        device->SuspendScreenSaver = UIKit_SuspendScreenSaver;
93        device->CreateWindow = UIKit_CreateWindow;
94        device->SetWindowTitle = UIKit_SetWindowTitle;
95        device->ShowWindow = UIKit_ShowWindow;
96        device->HideWindow = UIKit_HideWindow;
97        device->RaiseWindow = UIKit_RaiseWindow;
98        device->SetWindowBordered = UIKit_SetWindowBordered;
99        device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
100        device->DestroyWindow = UIKit_DestroyWindow;
101        device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
102        device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds;
103
104    #if SDL_IPHONE_KEYBOARD
105        device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
106        device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
107        device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
108        device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
109        device->SetTextInputRect = UIKit_SetTextInputRect;
110    #endif
111
112        device->SetClipboardText = UIKit_SetClipboardText;
113        device->GetClipboardText = UIKit_GetClipboardText;
114        device->HasClipboardText = UIKit_HasClipboardText;
115
116        /* OpenGL (ES) functions */
117        device->GL_MakeCurrent      = UIKit_GL_MakeCurrent;
118        device->GL_GetDrawableSize  = UIKit_GL_GetDrawableSize;
119        device->GL_SwapWindow       = UIKit_GL_SwapWindow;
120        device->GL_CreateContext    = UIKit_GL_CreateContext;
121        device->GL_DeleteContext    = UIKit_GL_DeleteContext;
122        device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
123        device->GL_LoadLibrary      = UIKit_GL_LoadLibrary;
124        device->free = UIKit_DeleteDevice;
125
126        device->gl_config.accelerated = 1;
127
128        return device;
129    }
130}
131
132VideoBootStrap UIKIT_bootstrap = {
133    UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
134    UIKit_Available, UIKit_CreateDevice
135};
136
137
138int
139UIKit_VideoInit(_THIS)
140{
141    _this->gl_config.driver_loaded = 1;
142
143    if (UIKit_InitModes(_this) < 0) {
144        return -1;
145    }
146    return 0;
147}
148
149void
150UIKit_VideoQuit(_THIS)
151{
152    UIKit_QuitModes(_this);
153}
154
155void
156UIKit_SuspendScreenSaver(_THIS)
157{
158    @autoreleasepool {
159        /* Ignore ScreenSaver API calls if the idle timer hint has been set. */
160        /* FIXME: The idle timer hint should be deprecated for SDL 2.1. */
161        if (!SDL_GetHintBoolean(SDL_HINT_IDLE_TIMER_DISABLED, SDL_FALSE)) {
162            UIApplication *app = [UIApplication sharedApplication];
163
164            /* Prevent the display from dimming and going to sleep. */
165            app.idleTimerDisabled = (_this->suspend_screensaver != SDL_FALSE);
166        }
167    }
168}
169
170SDL_bool
171UIKit_IsSystemVersionAtLeast(double version)
172{
173    return [[UIDevice currentDevice].systemVersion doubleValue] >= version;
174}
175
176CGRect
177UIKit_ComputeViewFrame(SDL_Window *window, UIScreen *screen)
178{
179#if !TARGET_OS_TV && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0)
180    BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0);
181
182    if (hasiOS7 || (window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
183        /* The view should always show behind the status bar in iOS 7+. */
184        return screen.bounds;
185    } else {
186        return screen.applicationFrame;
187    }
188#else
189    return screen.bounds;
190#endif
191}
192
193/*
194 * iOS log support.
195 *
196 * This doesn't really have aything to do with the interfaces of the SDL video
197 *  subsystem, but we need to stuff this into an Objective-C source code file.
198 */
199
200void SDL_NSLog(const char *text)
201{
202    NSLog(@"%s", text);
203}
204
205#endif /* SDL_VIDEO_DRIVER_UIKIT */
206
207/* vi: set ts=4 sw=4 expandtab: */
208