Commit 76980e30f29e92529f62bc910a991e3baf233e2b

Sam Lantinga 2020-10-08T16:42:20

Added events for dynamically connecting and disconnecting displays, with an iOS implementation

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
diff --git a/Xcode-iOS/Test/TestiPhoneOS.xcodeproj/project.pbxproj b/Xcode-iOS/Test/TestiPhoneOS.xcodeproj/project.pbxproj
index 7767c06..191864d 100644
--- a/Xcode-iOS/Test/TestiPhoneOS.xcodeproj/project.pbxproj
+++ b/Xcode-iOS/Test/TestiPhoneOS.xcodeproj/project.pbxproj
@@ -1795,6 +1795,9 @@
 					FABA34961D8B582100915323 = {
 						ProvisioningStyle = Automatic;
 					};
+					FDD2C53D0E2E80E400B7A85F = {
+						DevelopmentTeam = MXGJJ98X76;
+					};
 				};
 			};
 			buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "TestiPhoneOS" */;
@@ -2865,6 +2868,7 @@
 		FDD2C54C0E2E80E400B7A85F /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
+				DEVELOPMENT_TEAM = MXGJJ98X76;
 				INFOPLIST_FILE = Info.plist;
 				PRODUCT_NAME = testsprite2;
 			};
@@ -2873,6 +2877,7 @@
 		FDD2C54D0E2E80E400B7A85F /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
+				DEVELOPMENT_TEAM = MXGJJ98X76;
 				INFOPLIST_FILE = Info.plist;
 				PRODUCT_NAME = testsprite2;
 			};
diff --git a/include/SDL_video.h b/include/SDL_video.h
index c7411ca..9f6f179 100644
--- a/include/SDL_video.h
+++ b/include/SDL_video.h
@@ -175,7 +175,9 @@ typedef enum
 typedef enum
 {
     SDL_DISPLAYEVENT_NONE,          /**< Never used */
-    SDL_DISPLAYEVENT_ORIENTATION    /**< Display orientation has changed to data1 */
+    SDL_DISPLAYEVENT_ORIENTATION,   /**< Display orientation has changed to data1 */
+    SDL_DISPLAYEVENT_CONNECTED,     /**< Display has been added to the system */
+    SDL_DISPLAYEVENT_DISCONNECTED,  /**< Display has been removed from the system */
 } SDL_DisplayEventID;
 
 typedef enum
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 8690c34..81322b3 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -439,7 +439,8 @@ extern VideoBootStrap OFFSCREEN_bootstrap;
 
 extern SDL_VideoDevice *SDL_GetVideoDevice(void);
 extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode);
-extern int SDL_AddVideoDisplay(const SDL_VideoDisplay * display);
+extern int SDL_AddVideoDisplay(const SDL_VideoDisplay * display, SDL_bool send_event);
+extern void SDL_DelVideoDisplay(int index);
 extern SDL_bool SDL_AddDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode * mode);
 extern int SDL_GetIndexOfDisplay(SDL_VideoDisplay *display);
 extern SDL_VideoDisplay *SDL_GetDisplay(int displayIndex);
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 6696e6c..2800151 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -595,11 +595,11 @@ SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
     }
     display.current_mode = display.desktop_mode;
 
-    return SDL_AddVideoDisplay(&display);
+    return SDL_AddVideoDisplay(&display, SDL_FALSE);
 }
 
 int
-SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
+SDL_AddVideoDisplay(const SDL_VideoDisplay * display, SDL_bool send_event)
 {
     SDL_VideoDisplay *displays;
     int index = -1;
@@ -621,12 +621,31 @@ SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
             SDL_itoa(index, name, 10);
             displays[index].name = SDL_strdup(name);
         }
+
+		if (send_event) {
+			SDL_SendDisplayEvent(&_this->displays[index], SDL_DISPLAYEVENT_CONNECTED, 0);
+		}
     } else {
         SDL_OutOfMemory();
     }
     return index;
 }
 
+void
+SDL_DelVideoDisplay(int index)
+{
+	if (index < 0 || index >= _this->num_displays) {
+		return;
+	}
+
+	SDL_SendDisplayEvent(&_this->displays[index], SDL_DISPLAYEVENT_DISCONNECTED, 0);
+
+	if (index < (_this->num_displays - 1)) {
+		SDL_memmove(&_this->displays[index], &_this->displays[index+1], (_this->num_displays - index - 1)*sizeof(_this->displays[index]));
+	}
+	--_this->num_displays;
+}
+
 int
 SDL_GetNumVideoDisplays(void)
 {
diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m
index c40e418..79210ff 100644
--- a/src/video/cocoa/SDL_cocoamodes.m
+++ b/src/video/cocoa/SDL_cocoamodes.m
@@ -389,7 +389,7 @@ Cocoa_InitModes(_THIS)
             display.desktop_mode = mode;
             display.current_mode = mode;
             display.driverdata = displaydata;
-            SDL_AddVideoDisplay(&display);
+            SDL_AddVideoDisplay(&display, SDL_FALSE);
             SDL_free(display.name);
         }
     }
diff --git a/src/video/directfb/SDL_DirectFB_modes.c b/src/video/directfb/SDL_DirectFB_modes.c
index 5b3e534..114d95c 100644
--- a/src/video/directfb/SDL_DirectFB_modes.c
+++ b/src/video/directfb/SDL_DirectFB_modes.c
@@ -258,7 +258,7 @@ DirectFB_InitModes(_THIS)
 
         SDL_DFB_CHECKERR(layer->SetCooperativeLevel(layer, DLSCL_SHARED));
 
-        SDL_AddVideoDisplay(&display);
+        SDL_AddVideoDisplay(&display, SDL_FALSE);
     }
     SDL_DFB_FREE(screencbdata);
     return;
diff --git a/src/video/haiku/SDL_bmodes.cc b/src/video/haiku/SDL_bmodes.cc
index 5fa147f..bf41b1b 100644
--- a/src/video/haiku/SDL_bmodes.cc
+++ b/src/video/haiku/SDL_bmodes.cc
@@ -228,7 +228,7 @@ static void _AddDisplay(BScreen *screen) {
     display.desktop_mode = *mode;
     display.current_mode = *mode;
     
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
 }
 
 /*
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index ef25895..a125365 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -1262,7 +1262,7 @@ KMSDRM_VideoInit(_THIS)
 
     display.current_mode = display.desktop_mode;
     display.driverdata = dispdata;
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
 
     /****************/
     /* Atomic block */
diff --git a/src/video/pandora/SDL_pandora.c b/src/video/pandora/SDL_pandora.c
index 8817e06..e78c0f2 100644
--- a/src/video/pandora/SDL_pandora.c
+++ b/src/video/pandora/SDL_pandora.c
@@ -173,7 +173,7 @@ PND_videoinit(_THIS)
     display.current_mode = current_mode;
     display.driverdata = NULL;
 
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
 
     return 1;
 }
diff --git a/src/video/psp/SDL_pspvideo.c b/src/video/psp/SDL_pspvideo.c
index 20ac665..fa1139e 100644
--- a/src/video/psp/SDL_pspvideo.c
+++ b/src/video/psp/SDL_pspvideo.c
@@ -167,7 +167,7 @@ PSP_VideoInit(_THIS)
     display.current_mode = current_mode;
     display.driverdata = NULL;
 
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
 
     return 1;
 }
diff --git a/src/video/qnx/video.c b/src/video/qnx/video.c
index 3457ca0..049d814 100644
--- a/src/video/qnx/video.c
+++ b/src/video/qnx/video.c
@@ -47,7 +47,7 @@ videoInit(_THIS)
 
     SDL_zero(display);
 
-    if (SDL_AddVideoDisplay(&display) < 0) {
+    if (SDL_AddVideoDisplay(&display, SDL_FALSE) < 0) {
         return -1;
     }
 
diff --git a/src/video/raspberry/SDL_rpivideo.c b/src/video/raspberry/SDL_rpivideo.c
index bed3b49..d077ecd 100644
--- a/src/video/raspberry/SDL_rpivideo.c
+++ b/src/video/raspberry/SDL_rpivideo.c
@@ -196,7 +196,7 @@ AddDispManXDisplay(const int display_id)
 
     display.driverdata = data;
 
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
 }
 
 int
diff --git a/src/video/uikit/SDL_uikitappdelegate.m b/src/video/uikit/SDL_uikitappdelegate.m
index ac37ebd..a38f00b 100644
--- a/src/video/uikit/SDL_uikitappdelegate.m
+++ b/src/video/uikit/SDL_uikitappdelegate.m
@@ -47,7 +47,7 @@ static int exit_status;
 /* SDL is being built as a static library, include main() */
 int main(int argc, char *argv[])
 {
-	return SDL_UIKitRunApp(argc, argv, SDL_main);
+    return SDL_UIKitRunApp(argc, argv, SDL_main);
 }
 #endif /* SDL_MAIN_NEEDED && !IOS_DYLIB */
 
@@ -56,7 +56,7 @@ int SDL_UIKitRunApp(int argc, char *argv[], SDL_main_func mainFunction)
     int i;
 
     /* store arguments */
-	forward_main = mainFunction;
+    forward_main = mainFunction;
     forward_argc = argc;
     forward_argv = (char **)malloc((argc+1) * sizeof(char *));
     for (i = 0; i < argc; i++) {
@@ -349,6 +349,14 @@ SDL_LoadLaunchImageNamed(NSString *name, int screenh)
 
 - (void)postFinishLaunch
 {
+    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ 
+    [center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
+            name:UIScreenDidConnectNotification object:nil];
+    [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
+            name:UIScreenDidDisconnectNotification object:nil];
+ 
+
     /* Hide the launch screen the next time the run loop is run. SDL apps will
      * have a chance to load resources while the launch screen is still up. */
     [self performSelector:@selector(hideLaunchScreen) withObject:nil afterDelay:0.0];
@@ -520,6 +528,18 @@ SDL_LoadLaunchImageNamed(NSString *name, int screenh)
 
 #endif
 
+- (void)handleScreenDidConnectNotification:(NSNotification*)aNotification
+{
+    UIScreen *uiscreen = [aNotification object];
+    UIKit_AddDisplay(uiscreen, SDL_TRUE);
+}
+ 
+- (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification
+{
+    UIScreen *uiscreen = [aNotification object];
+    UIKit_DelDisplay(uiscreen);
+}
+
 @end
 
 #endif /* SDL_VIDEO_DRIVER_UIKIT */
diff --git a/src/video/uikit/SDL_uikitmodes.h b/src/video/uikit/SDL_uikitmodes.h
index e895ce4..25b9eca 100644
--- a/src/video/uikit/SDL_uikitmodes.h
+++ b/src/video/uikit/SDL_uikitmodes.h
@@ -43,6 +43,8 @@
 extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen);
 
 extern int UIKit_InitModes(_THIS);
+extern int UIKit_AddDisplay(UIScreen *uiscreen, SDL_bool send_event);
+extern void UIKit_DelDisplay(UIScreen *uiscreen);
 extern void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
 extern int UIKit_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
 extern int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
diff --git a/src/video/uikit/SDL_uikitmodes.m b/src/video/uikit/SDL_uikitmodes.m
index 4c1c09a..8db5fbe 100644
--- a/src/video/uikit/SDL_uikitmodes.m
+++ b/src/video/uikit/SDL_uikitmodes.m
@@ -265,10 +265,11 @@ UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, UIScreen * uiscre
     return 0;
 }
 
-static int
-UIKit_AddDisplay(UIScreen *uiscreen)
+int
+UIKit_AddDisplay(UIScreen *uiscreen, SDL_bool send_event)
 {
     UIScreenMode *uiscreenmode = uiscreen.currentMode;
+    CGPoint origin = uiscreen.bounds.origin;
     CGSize size = uiscreen.bounds.size;
     SDL_VideoDisplay display;
     SDL_DisplayMode mode;
@@ -302,11 +303,27 @@ UIKit_AddDisplay(UIScreen *uiscreen)
     }
 
     display.driverdata = (void *) CFBridgingRetain(data);
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, send_event);
 
     return 0;
 }
 
+void
+UIKit_DelDisplay(UIScreen *uiscreen)
+{
+    int i;
+
+    for (i = 0; i < SDL_GetNumVideoDisplays(); ++i) {
+        SDL_DisplayData *data = (__bridge SDL_DisplayData *)SDL_GetDisplayDriverData(i);
+
+        if (data && data.uiscreen == uiscreen) {
+            CFRelease(SDL_GetDisplayDriverData(i));
+            SDL_DelVideoDisplay(i);
+            return;
+        }
+    }
+}
+
 SDL_bool
 UIKit_IsDisplayLandscape(UIScreen *uiscreen)
 {
@@ -326,7 +343,7 @@ UIKit_InitModes(_THIS)
 {
     @autoreleasepool {
         for (UIScreen *uiscreen in [UIScreen screens]) {
-            if (UIKit_AddDisplay(uiscreen) < 0) {
+            if (UIKit_AddDisplay(uiscreen, SDL_FALSE) < 0) {
                 return -1;
             }
         }
diff --git a/src/video/vivante/SDL_vivantevideo.c b/src/video/vivante/SDL_vivantevideo.c
index ddae153..bc6bb62 100644
--- a/src/video/vivante/SDL_vivantevideo.c
+++ b/src/video/vivante/SDL_vivantevideo.c
@@ -170,7 +170,7 @@ VIVANTE_AddVideoDisplays(_THIS)
     display.desktop_mode = current_mode;
     display.current_mode = current_mode;
     display.driverdata = data;
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
     return 0;
 }
 
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 251311d..469bb73 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -274,7 +274,7 @@ display_handle_done(void *data,
 {
     /* !!! FIXME: this will fail on any further property changes! */
     SDL_VideoDisplay *display = data;
-    SDL_AddVideoDisplay(display);
+    SDL_AddVideoDisplay(display, SDL_FALSE);
     wl_output_set_user_data(output, display->driverdata);
     SDL_free(display->name);
     SDL_free(display);
diff --git a/src/video/windows/SDL_windowsmodes.c b/src/video/windows/SDL_windowsmodes.c
index 1eb6cb7..412f967 100644
--- a/src/video/windows/SDL_windowsmodes.c
+++ b/src/video/windows/SDL_windowsmodes.c
@@ -171,7 +171,7 @@ WIN_AddDisplay(_THIS, HMONITOR hMonitor, const MONITORINFOEX *info)
     display.desktop_mode = mode;
     display.current_mode = mode;
     display.driverdata = displaydata;
-    SDL_AddVideoDisplay(&display);
+    SDL_AddVideoDisplay(&display, SDL_FALSE);
     SDL_free(display.name);
     return SDL_TRUE;
 }
diff --git a/src/video/winrt/SDL_winrtvideo.cpp b/src/video/winrt/SDL_winrtvideo.cpp
index 37b2ece..99a52f3 100644
--- a/src/video/winrt/SDL_winrtvideo.cpp
+++ b/src/video/winrt/SDL_winrtvideo.cpp
@@ -293,7 +293,7 @@ WINRT_AddDisplaysForOutput (_THIS, IDXGIAdapter1 * dxgiAdapter1, int outputIndex
         }
     }
 
-    if (SDL_AddVideoDisplay(&display) < 0) {
+    if (SDL_AddVideoDisplay(&display, SDL_FALSE) < 0) {
         goto done;
     }
 
@@ -376,7 +376,7 @@ WINRT_AddDisplaysForAdapter (_THIS, IDXGIFactory2 * dxgiFactory2, int adapterInd
                 display.desktop_mode = mode;
                 display.current_mode = mode;
                 if ((SDL_AddDisplayMode(&display, &mode) < 0) ||
-                    (SDL_AddVideoDisplay(&display) < 0))
+                    (SDL_AddVideoDisplay(&display, SDL_FALSE) < 0))
                 {
                     return SDL_SetError("Failed to apply DXGI Display-detection workaround");
                 }
diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c
index 5e32d10..79ad247 100644
--- a/src/video/x11/SDL_x11modes.c
+++ b/src/video/x11/SDL_x11modes.c
@@ -487,7 +487,7 @@ X11_InitModes_XRandR(_THIS)
                 display.desktop_mode = mode;
                 display.current_mode = mode;
                 display.driverdata = displaydata;
-                SDL_AddVideoDisplay(&display);
+                SDL_AddVideoDisplay(&display, SDL_FALSE);
             }
 
             X11_XRRFreeScreenResources(res);
@@ -807,7 +807,7 @@ X11_InitModes(_THIS)
         display.desktop_mode = mode;
         display.current_mode = mode;
         display.driverdata = displaydata;
-        SDL_AddVideoDisplay(&display);
+        SDL_AddVideoDisplay(&display, SDL_FALSE);
     }
 
 #if SDL_VIDEO_DRIVER_X11_XINERAMA