Debian Codegraph @

test/testwm2.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 <stdlib.h>
14 #include <stdio.h>
15
16 #ifdef __EMSCRIPTEN__
17 #include <emscripten/emscripten.h>
18 #endif
19
20 #include "SDL_test_common.h"
21 #include "SDL_test_font.h"
22
23 static SDLTest_CommonState *state;
24 int done;
25
26 static const char *cursorNames[] = {
27 "arrow",
28 "ibeam",
29 "wait",
30 "crosshair",
31 "waitarrow",
32 "sizeNWSE",
33 "sizeNESW",
34 "sizeWE",
35 "sizeNS",
36 "sizeALL",
37 "NO",
38 "hand",
39 };
40 int system_cursor = -1;
41 SDL_Cursor *cursor = NULL;
42 SDL_bool relative_mode = SDL_FALSE;
43 int highlighted_mode = -1;
44
45 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
46 static void
47 quit(int rc)
48 {
49 SDLTest_CommonQuit(state);
50 exit(rc);
51 }
52
53 /* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */
54 static void
55 draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
56 {
57 SDL_DisplayMode mode;
58 char text[1024];
59 const int lineHeight = 10;
60 const int display_index = SDL_GetWindowDisplayIndex(window);
61 const int num_modes = SDL_GetNumDisplayModes(display_index);
62 int i;
63 int column_chars = 0;
64 int text_length;
65 int x, y;
66 int table_top;
67 SDL_Point mouse_pos = { -1, -1 };
68
69 /* Get mouse position */
70 if (SDL_GetMouseFocus() == window) {
71 int window_x, window_y;
72 float logical_x, logical_y;
73
74 SDL_GetMouseState(&window_x, &window_y);
75 SDL_RenderWindowToLogical(renderer, window_x, window_y, &logical_x, &logical_y);
76
77 mouse_pos.x = (int)logical_x;
78 mouse_pos.y = (int)logical_y;
79 }
80
81 x = 0;
82 y = viewport.y;
83
84 y += lineHeight;
85
86 SDL_strlcpy(text, "Click on a mode to set it with SDL_SetWindowDisplayMode", sizeof(text));
87 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
88 SDLTest_DrawString(renderer, x, y, text);
89 y += lineHeight;
90
91 SDL_strlcpy(text, "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN", sizeof(text));
92 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
93 SDLTest_DrawString(renderer, x, y, text);
94 y += lineHeight;
95
96 table_top = y;
97
98 /* Clear the cached mode under the mouse */
99 if (window == SDL_GetMouseFocus()) {
100 highlighted_mode = -1;
101 }
102
103 for (i = 0; i < num_modes; ++i) {
104 SDL_Rect cell_rect;
105
106 if (0 != SDL_GetDisplayMode(display_index, i, &mode)) {
107 return;
108 }
109
110 (void)SDL_snprintf(text, sizeof(text), "%d: %dx%d@%dHz",
111 i, mode.w, mode.h, mode.refresh_rate);
112
113 /* Update column width */
114 text_length = (int)SDL_strlen(text);
115 column_chars = SDL_max(column_chars, text_length);
116
117 /* Check if under mouse */
118 cell_rect.x = x;
119 cell_rect.y = y;
120 cell_rect.w = text_length * FONT_CHARACTER_SIZE;
121 cell_rect.h = lineHeight;
122
123 if (SDL_PointInRect(&mouse_pos, &cell_rect)) {
124 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
125
126 /* Update cached mode under the mouse */
127 if (window == SDL_GetMouseFocus()) {
128 highlighted_mode = i;
129 }
130 } else {
131 SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
132 }
133
134 SDLTest_DrawString(renderer, x, y, text);
135 y += lineHeight;
136
137 if (y + lineHeight > (viewport.y + viewport.h)) {
138 /* Advance to next column */
139 x += (column_chars + 1) * FONT_CHARACTER_SIZE;
140 y = table_top;
141 column_chars = 0;
142 }
143 }
144 }
145
146 void loop(void)
147 {
148 int i;
149 SDL_Event event;
150 /* Check for events */
151 while (SDL_PollEvent(&event)) {
152 SDLTest_CommonEvent(state, &event, &done);
153
154 if (event.type == SDL_WINDOWEVENT) {
155 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
156 SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
157 if (window) {
158 SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32 "\n",
159 event.window.windowID,
160 event.window.data1,
161 event.window.data2);
162 }
163 }
164 if (event.window.event == SDL_WINDOWEVENT_MOVED) {
165 SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
166 if (window) {
167 SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)\n",
168 event.window.windowID,
169 event.window.data1,
170 event.window.data2,
171 SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
172 }
173 }
174 if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
175 relative_mode = SDL_GetRelativeMouseMode();
176 if (relative_mode) {
177 SDL_SetRelativeMouseMode(SDL_FALSE);
178 }
179 }
180 if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
181 if (relative_mode) {
182 SDL_SetRelativeMouseMode(SDL_TRUE);
183 }
184 }
185 }
186 if (event.type == SDL_KEYUP) {
187 SDL_bool updateCursor = SDL_FALSE;
188
189 if (event.key.keysym.sym == SDLK_LEFT) {
190 --system_cursor;
191 if (system_cursor < 0) {
192 system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
193 }
194 updateCursor = SDL_TRUE;
195 } else if (event.key.keysym.sym == SDLK_RIGHT) {
196 ++system_cursor;
197 if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
198 system_cursor = 0;
199 }
200 updateCursor = SDL_TRUE;
201 }
202 if (updateCursor) {
203 SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
204 SDL_FreeCursor(cursor);
205 cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
206 SDL_SetCursor(cursor);
207 }
208 }
209 if (event.type == SDL_MOUSEBUTTONUP) {
210 SDL_Window *window = SDL_GetMouseFocus();
211 if (highlighted_mode != -1 && window) {
212 const int display_index = SDL_GetWindowDisplayIndex(window);
213 SDL_DisplayMode mode;
214 if (0 != SDL_GetDisplayMode(display_index, highlighted_mode, &mode)) {
215 SDL_Log("Couldn't get display mode");
216 } else {
217 SDL_SetWindowDisplayMode(window, &mode);
218 }
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 int y = 0;
228 SDL_Rect viewport, menurect;
229
230 SDL_RenderGetViewport(renderer, &viewport);
231
232 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
233 SDL_RenderClear(renderer);
234
235 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
236 SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y);
237
238 menurect.x = 0;
239 menurect.y = y;
240 menurect.w = viewport.w;
241 menurect.h = viewport.h - y;
242 draw_modes_menu(window, renderer, menurect);
243
244 SDL_RenderPresent(renderer);
245 }
246 }
247 #ifdef __EMSCRIPTEN__
248 if (done) {
249 emscripten_cancel_main_loop();
250 }
251 #endif
252 }
253
254 int main(int argc, char *argv[])
255 {
256 int i;
257
258 /* Enable standard application logging */
259 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
260
261 SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
262
263 /* Initialize test framework */
264 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
265 if (!state) {
266 return 1;
267 }
268
269 if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
270 SDLTest_CommonQuit(state);
271 return 1;
272 }
273
274 SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
275 SDL_EventState(SDL_DROPTEXT, SDL_ENABLE);
276
277 for (i = 0; i < state->num_windows; ++i) {
278 SDL_Renderer *renderer = state->renderers[i];
279 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
280 SDL_RenderClear(renderer);
281 }
282
283 /* Main render loop */
284 done = 0;
285 #ifdef __EMSCRIPTEN__
286 emscripten_set_main_loop(loop, 0, 1);
287 #else
288 while (!done) {
289 loop();
290 }
291 #endif
292 SDL_FreeCursor(cursor);
293
294 quit(0);
295 /* keep the compiler happy ... */
296 return 0;
297 }
298
299 /* vi: set ts=4 sw=4 expandtab: */