diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testwm.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testwm.c | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testwm.c b/src/contrib/SDL-3.2.20/test/testwm.c new file mode 100644 index 0000000..b46ee43 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testwm.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* | ||
2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
3 | |||
4 | This software is provided 'as-is', without any express or implied | ||
5 | warranty. In no event will the authors be held liable for any damages | ||
6 | arising from the use of this software. | ||
7 | |||
8 | Permission is granted to anyone to use this software for any purpose, | ||
9 | including commercial applications, and to alter it and redistribute it | ||
10 | freely. | ||
11 | */ | ||
12 | |||
13 | #include <SDL3/SDL_test_common.h> | ||
14 | #include <SDL3/SDL_test_font.h> | ||
15 | #include <SDL3/SDL_main.h> | ||
16 | |||
17 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
18 | #include <emscripten/emscripten.h> | ||
19 | #endif | ||
20 | |||
21 | static SDLTest_CommonState *state; | ||
22 | static int done; | ||
23 | |||
24 | static const char *cursorNames[] = { | ||
25 | "arrow", | ||
26 | "ibeam", | ||
27 | "wait", | ||
28 | "crosshair", | ||
29 | "waitarrow", | ||
30 | "sizeNWSE", | ||
31 | "sizeNESW", | ||
32 | "sizeWE", | ||
33 | "sizeNS", | ||
34 | "sizeALL", | ||
35 | "NO", | ||
36 | "hand", | ||
37 | "window top left", | ||
38 | "window top", | ||
39 | "window top right", | ||
40 | "window right", | ||
41 | "window bottom right", | ||
42 | "window bottom", | ||
43 | "window bottom left", | ||
44 | "window left" | ||
45 | }; | ||
46 | SDL_COMPILE_TIME_ASSERT(cursorNames, SDL_arraysize(cursorNames) == SDL_SYSTEM_CURSOR_COUNT); | ||
47 | |||
48 | static int system_cursor = -1; | ||
49 | static SDL_Cursor *cursor = NULL; | ||
50 | static SDL_DisplayMode highlighted_mode; | ||
51 | |||
52 | /* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */ | ||
53 | static void | ||
54 | draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport) | ||
55 | { | ||
56 | SDL_DisplayMode **modes; | ||
57 | char text[1024]; | ||
58 | const int lineHeight = 10; | ||
59 | int i, j; | ||
60 | int column_chars = 0; | ||
61 | int text_length; | ||
62 | float x, y; | ||
63 | float table_top; | ||
64 | SDL_FPoint mouse_pos = { -1.0f, -1.0f }; | ||
65 | SDL_DisplayID *displays; | ||
66 | |||
67 | /* Get mouse position */ | ||
68 | if (SDL_GetMouseFocus() == window) { | ||
69 | float window_x, window_y; | ||
70 | float logical_x, logical_y; | ||
71 | |||
72 | SDL_GetMouseState(&window_x, &window_y); | ||
73 | SDL_RenderCoordinatesFromWindow(renderer, window_x, window_y, &logical_x, &logical_y); | ||
74 | |||
75 | mouse_pos.x = logical_x; | ||
76 | mouse_pos.y = logical_y; | ||
77 | } | ||
78 | |||
79 | x = 0.0f; | ||
80 | y = viewport.y; | ||
81 | |||
82 | y += lineHeight; | ||
83 | |||
84 | SDL_strlcpy(text, "Click on a mode to set it with SDL_SetWindowFullscreenMode", sizeof(text)); | ||
85 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
86 | SDLTest_DrawString(renderer, x, y, text); | ||
87 | y += lineHeight; | ||
88 | |||
89 | SDL_strlcpy(text, "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN", sizeof(text)); | ||
90 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
91 | SDLTest_DrawString(renderer, x, y, text); | ||
92 | y += lineHeight; | ||
93 | |||
94 | table_top = y; | ||
95 | |||
96 | /* Clear the cached mode under the mouse */ | ||
97 | if (window == SDL_GetMouseFocus()) { | ||
98 | SDL_zero(highlighted_mode); | ||
99 | } | ||
100 | |||
101 | displays = SDL_GetDisplays(NULL); | ||
102 | if (displays) { | ||
103 | for (i = 0; displays[i]; ++i) { | ||
104 | SDL_DisplayID display = displays[i]; | ||
105 | modes = SDL_GetFullscreenDisplayModes(display, NULL); | ||
106 | for (j = 0; modes[j]; ++j) { | ||
107 | SDL_FRect cell_rect; | ||
108 | const SDL_DisplayMode *mode = modes[j]; | ||
109 | |||
110 | (void)SDL_snprintf(text, sizeof(text), "%s mode %d: %dx%d@%gx %gHz", | ||
111 | SDL_GetDisplayName(display), | ||
112 | j, mode->w, mode->h, mode->pixel_density, mode->refresh_rate); | ||
113 | |||
114 | /* Update column width */ | ||
115 | text_length = (int)SDL_strlen(text); | ||
116 | column_chars = SDL_max(column_chars, text_length); | ||
117 | |||
118 | /* Check if under mouse */ | ||
119 | cell_rect.x = x; | ||
120 | cell_rect.y = y; | ||
121 | cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE); | ||
122 | cell_rect.h = (float)lineHeight; | ||
123 | |||
124 | if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) { | ||
125 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
126 | |||
127 | /* Update cached mode under the mouse */ | ||
128 | if (window == SDL_GetMouseFocus()) { | ||
129 | SDL_copyp(&highlighted_mode, mode); | ||
130 | } | ||
131 | } else { | ||
132 | SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); | ||
133 | } | ||
134 | |||
135 | SDLTest_DrawString(renderer, x, y, text); | ||
136 | y += lineHeight; | ||
137 | |||
138 | if ((y + lineHeight) > (viewport.y + viewport.h)) { | ||
139 | /* Advance to next column */ | ||
140 | x += (column_chars + 1) * FONT_CHARACTER_SIZE; | ||
141 | y = table_top; | ||
142 | column_chars = 0; | ||
143 | } | ||
144 | } | ||
145 | SDL_free(modes); | ||
146 | } | ||
147 | SDL_free(displays); | ||
148 | } | ||
149 | } | ||
150 | |||
151 | static void loop(void) | ||
152 | { | ||
153 | int i; | ||
154 | SDL_Event event; | ||
155 | |||
156 | #ifdef TEST_WAITEVENTTIMEOUT | ||
157 | /* Wait up to 20 ms for input, as a test */ | ||
158 | Uint64 then = SDL_GetTicks(); | ||
159 | if (SDL_WaitEventTimeout(NULL, 20)) { | ||
160 | SDL_Log("Got an event!"); | ||
161 | } | ||
162 | Uint64 now = SDL_GetTicks(); | ||
163 | SDL_Log("Waited %d ms for events", (int)(now - then)); | ||
164 | #endif | ||
165 | |||
166 | while (SDL_PollEvent(&event)) { | ||
167 | SDLTest_CommonEvent(state, &event, &done); | ||
168 | SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(&event)), &event); | ||
169 | |||
170 | if (event.type == SDL_EVENT_WINDOW_RESIZED) { | ||
171 | SDL_Window *window = SDL_GetWindowFromEvent(&event); | ||
172 | if (window) { | ||
173 | SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32, | ||
174 | event.window.windowID, | ||
175 | event.window.data1, | ||
176 | event.window.data2); | ||
177 | } | ||
178 | } | ||
179 | if (event.type == SDL_EVENT_WINDOW_MOVED) { | ||
180 | SDL_Window *window = SDL_GetWindowFromEvent(&event); | ||
181 | if (window) { | ||
182 | SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)", | ||
183 | event.window.windowID, | ||
184 | event.window.data1, | ||
185 | event.window.data2, | ||
186 | SDL_GetDisplayName(SDL_GetDisplayForWindow(window))); | ||
187 | } | ||
188 | } | ||
189 | if (event.type == SDL_EVENT_KEY_UP) { | ||
190 | bool updateCursor = false; | ||
191 | |||
192 | if (event.key.key == SDLK_A) { | ||
193 | SDL_assert(!"Keyboard generated assert"); | ||
194 | } else if (event.key.key == SDLK_LEFT) { | ||
195 | --system_cursor; | ||
196 | if (system_cursor < 0) { | ||
197 | system_cursor = SDL_SYSTEM_CURSOR_COUNT - 1; | ||
198 | } | ||
199 | updateCursor = true; | ||
200 | } else if (event.key.key == SDLK_RIGHT) { | ||
201 | ++system_cursor; | ||
202 | if (system_cursor >= SDL_SYSTEM_CURSOR_COUNT) { | ||
203 | system_cursor = 0; | ||
204 | } | ||
205 | updateCursor = true; | ||
206 | } | ||
207 | if (updateCursor) { | ||
208 | SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]); | ||
209 | SDL_DestroyCursor(cursor); | ||
210 | cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor); | ||
211 | SDL_SetCursor(cursor); | ||
212 | } | ||
213 | } | ||
214 | if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { | ||
215 | SDL_Window *window = SDL_GetMouseFocus(); | ||
216 | if (highlighted_mode.w && window) { | ||
217 | SDL_copyp(&state->fullscreen_mode, &highlighted_mode); | ||
218 | SDL_SetWindowFullscreenMode(window, &highlighted_mode); | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | for (i = 0; i < state->num_windows; ++i) { | ||
224 | SDL_Window *window = state->windows[i]; | ||
225 | SDL_Renderer *renderer = state->renderers[i]; | ||
226 | if (window && renderer) { | ||
227 | float y = 0.0f; | ||
228 | SDL_Rect viewport; | ||
229 | SDL_FRect menurect; | ||
230 | |||
231 | SDL_SetRenderViewport(renderer, NULL); | ||
232 | SDL_GetRenderSafeArea(renderer, &viewport); | ||
233 | SDL_SetRenderViewport(renderer, &viewport); | ||
234 | |||
235 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
236 | SDL_RenderClear(renderer); | ||
237 | |||
238 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
239 | SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y); | ||
240 | |||
241 | menurect.x = 0.0f; | ||
242 | menurect.y = y; | ||
243 | menurect.w = (float)viewport.w; | ||
244 | menurect.h = (float)viewport.h - y; | ||
245 | draw_modes_menu(window, renderer, menurect); | ||
246 | |||
247 | SDL_Delay(16); | ||
248 | SDL_RenderPresent(renderer); | ||
249 | } | ||
250 | } | ||
251 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
252 | if (done) { | ||
253 | emscripten_cancel_main_loop(); | ||
254 | } | ||
255 | #endif | ||
256 | } | ||
257 | |||
258 | int main(int argc, char *argv[]) | ||
259 | { | ||
260 | int i; | ||
261 | |||
262 | /* Initialize test framework */ | ||
263 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
264 | if (!state) { | ||
265 | return 1; | ||
266 | } | ||
267 | |||
268 | /* Parse commandline */ | ||
269 | if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { | ||
270 | SDLTest_CommonQuit(state); | ||
271 | return 1; | ||
272 | } | ||
273 | |||
274 | for (i = 0; i < state->num_windows; ++i) { | ||
275 | SDL_Renderer *renderer = state->renderers[i]; | ||
276 | SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); | ||
277 | SDL_RenderClear(renderer); | ||
278 | } | ||
279 | |||
280 | SDL_StopTextInput(state->windows[0]); | ||
281 | SDL_StopTextInput(state->windows[0]); | ||
282 | /* Main render loop */ | ||
283 | done = 0; | ||
284 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
285 | emscripten_set_main_loop(loop, 0, 1); | ||
286 | #else | ||
287 | while (!done) { | ||
288 | loop(); | ||
289 | } | ||
290 | #endif | ||
291 | SDL_DestroyCursor(cursor); | ||
292 | |||
293 | SDLTest_CleanupTextDrawing(); | ||
294 | SDLTest_CommonQuit(state); | ||
295 | return 0; | ||
296 | } | ||