summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/n3ds
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/n3ds
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/n3ds')
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents.c45
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents_c.h29
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c161
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer_c.h31
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.c69
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.h36
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.c86
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.h29
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.c259
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.h41
10 files changed, 786 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents.c
new file mode 100644
index 0000000..3d5e0e3
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents.c
@@ -0,0 +1,45 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#ifdef SDL_VIDEO_DRIVER_N3DS
24
25#include <3ds.h>
26
27#include "../../events/SDL_events_c.h"
28#include "SDL_n3dsevents_c.h"
29#include "SDL_n3dstouch.h"
30
31void N3DS_PumpEvents(SDL_VideoDevice *_this)
32{
33 hidScanInput();
34 N3DS_PollTouch(_this);
35
36 if (!aptMainLoop()) {
37 SDL_Event ev;
38 ev.type = SDL_EVENT_QUIT;
39 ev.common.timestamp = 0;
40 SDL_PushEvent(&ev);
41 return;
42 }
43}
44
45#endif // SDL_VIDEO_DRIVER_N3DS
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents_c.h b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents_c.h
new file mode 100644
index 0000000..898fbb2
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsevents_c.h
@@ -0,0 +1,29 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#ifndef SDL_n3dsevents_c_h_
23#define SDL_n3dsevents_c_h_
24
25#include "SDL_internal.h"
26
27extern void N3DS_PumpEvents(SDL_VideoDevice *_this);
28
29#endif // SDL_n3dsevents_c_h_
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c
new file mode 100644
index 0000000..d632545
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c
@@ -0,0 +1,161 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#ifdef SDL_VIDEO_DRIVER_N3DS
24
25#include "../SDL_sysvideo.h"
26#include "../../SDL_properties_c.h"
27#include "SDL_n3dsframebuffer_c.h"
28#include "SDL_n3dsvideo.h"
29
30#define N3DS_SURFACE "SDL.internal.window.surface"
31
32typedef struct
33{
34 int width, height;
35} Dimensions;
36
37static void CopyFramebuffertoN3DS_16(u16 *dest, const Dimensions dest_dim, const u16 *source, const Dimensions source_dim);
38static void CopyFramebuffertoN3DS_24(u8 *dest, const Dimensions dest_dim, const u8 *source, const Dimensions source_dim);
39static void CopyFramebuffertoN3DS_32(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim);
40static int GetDestOffset(int x, int y, int dest_width);
41static int GetSourceOffset(int x, int y, int source_width);
42static void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen);
43
44
45bool SDL_N3DS_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
46{
47 SDL_Surface *framebuffer;
48 const SDL_DisplayMode *mode;
49 int w, h;
50
51 SDL_N3DS_DestroyWindowFramebuffer(_this, window);
52
53 mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(window));
54 SDL_GetWindowSizeInPixels(window, &w, &h);
55 framebuffer = SDL_CreateSurface(w, h, mode->format);
56
57 if (!framebuffer) {
58 return false;
59 }
60
61 SDL_SetSurfaceProperty(SDL_GetWindowProperties(window), N3DS_SURFACE, framebuffer);
62 *format = mode->format;
63 *pixels = framebuffer->pixels;
64 *pitch = framebuffer->pitch;
65 return true;
66}
67
68bool SDL_N3DS_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
69{
70 SDL_WindowData *drv_data = window->internal;
71 SDL_Surface *surface;
72 u16 width, height;
73 void *framebuffer;
74 u32 bufsize;
75
76 surface = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), N3DS_SURFACE, NULL);
77 if (!surface) {
78 return SDL_SetError("%s: Unable to get the window surface.", __func__);
79 }
80
81 // Get the N3DS internal framebuffer and its size
82 framebuffer = gfxGetFramebuffer(drv_data->screen, GFX_LEFT, &width, &height);
83 bufsize = width * height * 4;
84
85 if (SDL_BYTESPERPIXEL(surface->format) == 2)
86 CopyFramebuffertoN3DS_16(framebuffer, (Dimensions){ width, height },
87 surface->pixels, (Dimensions){ surface->w, surface->h });
88 else if (SDL_BYTESPERPIXEL(surface->format) == 3)
89 CopyFramebuffertoN3DS_24(framebuffer, (Dimensions){ width, height },
90 surface->pixels, (Dimensions){ surface->w, surface->h });
91 else
92 CopyFramebuffertoN3DS_32(framebuffer, (Dimensions){ width, height },
93 surface->pixels, (Dimensions){ surface->w, surface->h });
94 FlushN3DSBuffer(framebuffer, bufsize, drv_data->screen);
95
96 return true;
97}
98
99static void CopyFramebuffertoN3DS_16(u16 *dest, const Dimensions dest_dim, const u16 *source, const Dimensions source_dim)
100{
101 int rows = SDL_min(dest_dim.width, source_dim.height);
102 int cols = SDL_min(dest_dim.height, source_dim.width);
103 for (int y = 0; y < rows; ++y) {
104 for (int x = 0; x < cols; ++x) {
105 const u16 *s = source + GetSourceOffset(x, y, source_dim.width);
106 u16 *d = dest + GetDestOffset(x, y, dest_dim.width);
107 *d = *s;
108 }
109 }
110}
111
112static void CopyFramebuffertoN3DS_24(u8 *dest, const Dimensions dest_dim, const u8 *source, const Dimensions source_dim)
113{
114 int rows = SDL_min(dest_dim.width, source_dim.height);
115 int cols = SDL_min(dest_dim.height, source_dim.width);
116 for (int y = 0; y < rows; ++y) {
117 for (int x = 0; x < cols; ++x) {
118 const u8 *s = source + GetSourceOffset(x, y, source_dim.width) * 3;
119 u8 *d = dest + GetDestOffset(x, y, dest_dim.width) * 3;
120 d[0] = s[0];
121 d[1] = s[1];
122 d[2] = s[2];
123 }
124 }
125}
126
127static void CopyFramebuffertoN3DS_32(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim)
128{
129 int rows = SDL_min(dest_dim.width, source_dim.height);
130 int cols = SDL_min(dest_dim.height, source_dim.width);
131 for (int y = 0; y < rows; ++y) {
132 for (int x = 0; x < cols; ++x) {
133 const u32 *s = source + GetSourceOffset(x, y, source_dim.width);
134 u32 *d = dest + GetDestOffset(x, y, dest_dim.width);
135 *d = *s;
136 }
137 }
138}
139
140static int GetDestOffset(int x, int y, int dest_width)
141{
142 return dest_width - y - 1 + dest_width * x;
143}
144
145static int GetSourceOffset(int x, int y, int source_width)
146{
147 return x + y * source_width;
148}
149
150static void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen)
151{
152 GSPGPU_FlushDataCache(buffer, bufsize);
153 gfxScreenSwapBuffers(screen, false);
154}
155
156void SDL_N3DS_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
157{
158 SDL_ClearProperty(SDL_GetWindowProperties(window), N3DS_SURFACE);
159}
160
161#endif // SDL_VIDEO_DRIVER_N3DS
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer_c.h b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer_c.h
new file mode 100644
index 0000000..d442add
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer_c.h
@@ -0,0 +1,31 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#ifndef SDL_n3dsframebuffer_c_h_
23#define SDL_n3dsframebuffer_c_h_
24
25#include "SDL_internal.h"
26
27extern bool SDL_N3DS_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch);
28extern bool SDL_N3DS_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects);
29extern void SDL_N3DS_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window);
30
31#endif // SDL_n3dsframebuffer_c_h_
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.c
new file mode 100644
index 0000000..95f0466
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.c
@@ -0,0 +1,69 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#include "SDL_internal.h"
23
24#ifdef SDL_VIDEO_DRIVER_N3DS
25
26#include <3ds.h>
27
28#include "SDL_n3dsswkb.h"
29
30static SwkbdState sw_keyboard;
31const static size_t BUFFER_SIZE = 256;
32
33void N3DS_SwkbInit(void)
34{
35 swkbdInit(&sw_keyboard, SWKBD_TYPE_NORMAL, 2, -1);
36}
37
38void N3DS_SwkbPoll(void)
39{
40 return;
41}
42
43void N3DS_SwkbQuit(void)
44{
45 return;
46}
47
48bool N3DS_HasScreenKeyboardSupport(SDL_VideoDevice *_this)
49{
50 return true;
51}
52
53bool N3DS_StartTextInput(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props)
54{
55 char buffer[BUFFER_SIZE];
56 SwkbdButton button_pressed;
57 button_pressed = swkbdInputText(&sw_keyboard, buffer, BUFFER_SIZE);
58 if (button_pressed == SWKBD_BUTTON_CONFIRM) {
59 SDL_SendKeyboardText(buffer);
60 }
61 return true;
62}
63
64bool N3DS_StopTextInput(SDL_VideoDevice *_this, SDL_Window *window)
65{
66 return true;
67}
68
69#endif // SDL_VIDEO_DRIVER_N3DS
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.h b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.h
new file mode 100644
index 0000000..8857c00
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsswkb.h
@@ -0,0 +1,36 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#ifndef SDL_n3dskeyboard_h_
23#define SDL_n3dskeyboard_h_
24
25#include "../../events/SDL_events_c.h"
26
27void N3DS_SwkbInit();
28void N3DS_SwkbPoll();
29void N3DS_SwkbQuit();
30
31bool N3DS_HasScreenKeyboardSupport(SDL_VideoDevice *_this);
32
33bool N3DS_StartTextInput(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props);
34bool N3DS_StopTextInput(SDL_VideoDevice *_this, SDL_Window *window);
35
36#endif // SDL_n3dskeyboard_h_
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.c
new file mode 100644
index 0000000..8116e4a
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.c
@@ -0,0 +1,86 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#include "SDL_internal.h"
23
24#ifdef SDL_VIDEO_DRIVER_N3DS
25
26#include <3ds.h>
27
28#include "../../events/SDL_touch_c.h"
29#include "../SDL_sysvideo.h"
30#include "SDL_n3dstouch.h"
31#include "SDL_n3dsvideo.h"
32
33#define N3DS_TOUCH_ID 1
34#define N3DS_TOUCH_FINGER 1
35
36/*
37 Factors used to convert touchscreen coordinates to
38 SDL's 0-1 values. Note that the N3DS's screen is
39 internally in a portrait disposition so the
40 GSP_SCREEN constants are flipped.
41*/
42#define TOUCHSCREEN_SCALE_X 1.0f / (GSP_SCREEN_HEIGHT_BOTTOM - 1)
43#define TOUCHSCREEN_SCALE_Y 1.0f / (GSP_SCREEN_WIDTH - 1)
44
45void N3DS_InitTouch(void)
46{
47 SDL_AddTouch(N3DS_TOUCH_ID, SDL_TOUCH_DEVICE_DIRECT, "Touchscreen");
48}
49
50void N3DS_QuitTouch(void)
51{
52 SDL_DelTouch(N3DS_TOUCH_ID);
53}
54
55void N3DS_PollTouch(SDL_VideoDevice *_this)
56{
57 SDL_VideoData *internal = (SDL_VideoData *)_this->internal;
58 touchPosition touch;
59 SDL_Window *window;
60 SDL_VideoDisplay *display;
61 static bool was_pressed = false;
62 bool pressed;
63 hidTouchRead(&touch);
64 pressed = (touch.px != 0 || touch.py != 0);
65
66 display = SDL_GetVideoDisplay(internal->touch_display);
67 window = display ? display->fullscreen_window : NULL;
68
69 if (pressed != was_pressed) {
70 was_pressed = pressed;
71 SDL_SendTouch(0, N3DS_TOUCH_ID, N3DS_TOUCH_FINGER,
72 window,
73 pressed ? SDL_EVENT_FINGER_DOWN : SDL_EVENT_FINGER_UP,
74 touch.px * TOUCHSCREEN_SCALE_X,
75 touch.py * TOUCHSCREEN_SCALE_Y,
76 pressed ? 1.0f : 0.0f);
77 } else if (pressed) {
78 SDL_SendTouchMotion(0, N3DS_TOUCH_ID, N3DS_TOUCH_FINGER,
79 window,
80 touch.px * TOUCHSCREEN_SCALE_X,
81 touch.py * TOUCHSCREEN_SCALE_Y,
82 1.0f);
83 }
84}
85
86#endif // SDL_VIDEO_DRIVER_N3DS
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.h b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.h
new file mode 100644
index 0000000..7dac5a8
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dstouch.h
@@ -0,0 +1,29 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
22#ifndef SDL_n3dstouch_h_
23#define SDL_n3dstouch_h_
24
25void N3DS_InitTouch(void);
26void N3DS_QuitTouch(void);
27void N3DS_PollTouch(SDL_VideoDevice *_this);
28
29#endif // SDL_n3dstouch_h_
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.c
new file mode 100644
index 0000000..8e925bf
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.c
@@ -0,0 +1,259 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#ifdef SDL_VIDEO_DRIVER_N3DS
24
25#include "../SDL_sysvideo.h"
26#include "SDL_n3dsevents_c.h"
27#include "SDL_n3dsframebuffer_c.h"
28#include "SDL_n3dsswkb.h"
29#include "SDL_n3dstouch.h"
30#include "SDL_n3dsvideo.h"
31
32#define N3DSVID_DRIVER_NAME "n3ds"
33
34static bool AddN3DSDisplay(gfxScreen_t screen);
35
36static bool N3DS_VideoInit(SDL_VideoDevice *_this);
37static void N3DS_VideoQuit(SDL_VideoDevice *_this);
38static bool N3DS_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display);
39static bool N3DS_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
40static bool N3DS_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect);
41static bool N3DS_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props);
42static void N3DS_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window);
43
44struct SDL_DisplayData
45{
46 gfxScreen_t screen;
47};
48
49struct SDL_DisplayModeData
50{
51 GSPGPU_FramebufferFormat fmt;
52};
53
54static const struct
55{
56 SDL_PixelFormat pixfmt;
57 GSPGPU_FramebufferFormat gspfmt;
58} format_map[] = {
59 { SDL_PIXELFORMAT_RGBA8888, GSP_RGBA8_OES },
60 { SDL_PIXELFORMAT_BGR24, GSP_BGR8_OES },
61 { SDL_PIXELFORMAT_RGB565, GSP_RGB565_OES },
62 { SDL_PIXELFORMAT_RGBA5551, GSP_RGB5_A1_OES },
63 { SDL_PIXELFORMAT_RGBA4444, GSP_RGBA4_OES }
64};
65
66// N3DS driver bootstrap functions
67
68static void N3DS_DeleteDevice(SDL_VideoDevice *device)
69{
70 SDL_free(device->internal);
71 SDL_free(device);
72}
73
74static SDL_VideoDevice *N3DS_CreateDevice(void)
75{
76 SDL_VideoDevice *device;
77 SDL_VideoData *phdata;
78
79 // Initialize all variables that we clean on shutdown
80 device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
81 if (!device) {
82 return NULL;
83 }
84
85 // Initialize internal data
86 phdata = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
87 if (!phdata) {
88 SDL_free(device);
89 return NULL;
90 }
91
92 device->internal = phdata;
93
94 device->VideoInit = N3DS_VideoInit;
95 device->VideoQuit = N3DS_VideoQuit;
96
97 device->GetDisplayModes = N3DS_GetDisplayModes;
98 device->SetDisplayMode = N3DS_SetDisplayMode;
99 device->GetDisplayBounds = N3DS_GetDisplayBounds;
100
101 device->CreateSDLWindow = N3DS_CreateWindow;
102 device->DestroyWindow = N3DS_DestroyWindow;
103
104 device->HasScreenKeyboardSupport = N3DS_HasScreenKeyboardSupport;
105 device->StartTextInput = N3DS_StartTextInput;
106 device->StopTextInput = N3DS_StopTextInput;
107
108 device->PumpEvents = N3DS_PumpEvents;
109
110 device->CreateWindowFramebuffer = SDL_N3DS_CreateWindowFramebuffer;
111 device->UpdateWindowFramebuffer = SDL_N3DS_UpdateWindowFramebuffer;
112 device->DestroyWindowFramebuffer = SDL_N3DS_DestroyWindowFramebuffer;
113
114 device->free = N3DS_DeleteDevice;
115
116 device->device_caps = VIDEO_DEVICE_CAPS_FULLSCREEN_ONLY;
117
118 return device;
119}
120
121VideoBootStrap N3DS_bootstrap = { N3DSVID_DRIVER_NAME, "N3DS Video Driver", N3DS_CreateDevice, NULL, /* no ShowMessageBox implementation */ false };
122
123static bool N3DS_VideoInit(SDL_VideoDevice *_this)
124{
125 SDL_VideoData *internal = (SDL_VideoData *)_this->internal;
126
127 gfxInit(GSP_RGBA8_OES, GSP_RGBA8_OES, false);
128 hidInit();
129
130 internal->top_display = AddN3DSDisplay(GFX_TOP);
131 internal->touch_display = AddN3DSDisplay(GFX_BOTTOM);
132
133 N3DS_InitTouch();
134 N3DS_SwkbInit();
135
136 return true;
137}
138
139static bool AddN3DSDisplay(gfxScreen_t screen)
140{
141 SDL_DisplayMode mode;
142 SDL_DisplayModeData *modedata;
143 SDL_VideoDisplay display;
144 SDL_DisplayData *display_driver_data = SDL_calloc(1, sizeof(SDL_DisplayData));
145 if (!display_driver_data) {
146 return false;
147 }
148
149 SDL_zero(mode);
150 SDL_zero(display);
151
152 display_driver_data->screen = screen;
153
154 modedata = SDL_malloc(sizeof(SDL_DisplayModeData));
155 if (!modedata) {
156 return false;
157 }
158
159 mode.w = (screen == GFX_TOP) ? GSP_SCREEN_HEIGHT_TOP : GSP_SCREEN_HEIGHT_BOTTOM;
160 mode.h = GSP_SCREEN_WIDTH;
161 mode.refresh_rate = 60.0f;
162 mode.format = SDL_PIXELFORMAT_RGBA8888;
163 mode.internal = modedata;
164 modedata->fmt = GSP_RGBA8_OES;
165
166 display.name = (screen == GFX_TOP) ? "N3DS top screen" : "N3DS bottom screen";
167 display.desktop_mode = mode;
168 display.internal = display_driver_data;
169
170 if (SDL_AddVideoDisplay(&display, false) == 0) {
171 return false;
172 }
173 return true;
174}
175
176static void N3DS_VideoQuit(SDL_VideoDevice *_this)
177{
178 N3DS_SwkbQuit();
179 N3DS_QuitTouch();
180
181 hidExit();
182 gfxExit();
183}
184
185static bool N3DS_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display)
186{
187 SDL_DisplayData *displaydata = display->internal;
188 SDL_DisplayModeData *modedata;
189 SDL_DisplayMode mode;
190 int i;
191
192 for (i = 0; i < SDL_arraysize(format_map); i++) {
193 modedata = SDL_malloc(sizeof(SDL_DisplayModeData));
194 if (!modedata)
195 continue;
196
197 SDL_zero(mode);
198 mode.w = (displaydata->screen == GFX_TOP) ? GSP_SCREEN_HEIGHT_TOP : GSP_SCREEN_HEIGHT_BOTTOM;
199 mode.h = GSP_SCREEN_WIDTH;
200 mode.refresh_rate = 60.0f;
201 mode.format = format_map[i].pixfmt;
202 mode.internal = modedata;
203 modedata->fmt = format_map[i].gspfmt;
204
205 if (!SDL_AddFullscreenDisplayMode(display, &mode)) {
206 SDL_free(modedata);
207 }
208 }
209
210 return true;
211}
212
213static bool N3DS_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
214{
215 SDL_DisplayData *displaydata = display->internal;
216 SDL_DisplayModeData *modedata = mode->internal;
217
218 gfxSetScreenFormat(displaydata->screen, modedata->fmt);
219 return true;
220}
221
222static bool N3DS_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
223{
224 SDL_DisplayData *driver_data = display->internal;
225
226 if (!driver_data) {
227 return false;
228 }
229
230 rect->x = 0;
231 rect->y = (driver_data->screen == GFX_TOP) ? 0 : GSP_SCREEN_WIDTH;
232 rect->w = display->current_mode->w;
233 rect->h = display->current_mode->h;
234 return true;
235}
236
237static bool N3DS_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
238{
239 SDL_DisplayData *display_data;
240 SDL_WindowData *window_data = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
241 if (!window_data) {
242 return false;
243 }
244 display_data = SDL_GetDisplayDriverDataForWindow(window);
245 window_data->screen = display_data->screen;
246 window->internal = window_data;
247 SDL_SetKeyboardFocus(window);
248 return true;
249}
250
251static void N3DS_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
252{
253 if (!window) {
254 return;
255 }
256 SDL_free(window->internal);
257}
258
259#endif // SDL_VIDEO_DRIVER_N3DS
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.h b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.h
new file mode 100644
index 0000000..14b70e9
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsvideo.h
@@ -0,0 +1,41 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#ifndef SDL_n3dsvideo_h_
24#define SDL_n3dsvideo_h_
25
26#include <3ds.h>
27
28#include "../SDL_sysvideo.h"
29
30struct SDL_VideoData
31{
32 int top_display;
33 int touch_display;
34};
35
36struct SDL_WindowData
37{
38 gfxScreen_t screen; /**< Keeps track of which N3DS screen is targeted */
39};
40
41#endif // SDL_n3dsvideo_h_