Atlas - testwaylandcustom.c
Home / ext / SDL / test Lines: 1 | Size: 11585 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2025 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 <SDL3/SDL.h> 14#include <SDL3/SDL_test.h> 15#include <wayland-client.h> 16#include <xdg-shell-client-protocol.h> 17 18#include "icon.h" 19 20#define WINDOW_WIDTH 640 21#define WINDOW_HEIGHT 480 22#define NUM_SPRITES 100 23#define MAX_SPEED 1 24 25static SDL_Window *window; 26static SDL_Renderer *renderer; 27static SDL_Texture *sprite; 28static SDL_FRect positions[NUM_SPRITES]; 29static SDL_FRect velocities[NUM_SPRITES]; 30static int sprite_w, sprite_h; 31static int done; 32 33static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h) 34{ 35 SDL_Texture *texture = NULL; 36 SDL_Surface *surface; 37 SDL_IOStream *src = SDL_IOFromConstMem(data, len); 38 if (src) { 39 surface = SDL_LoadPNG_IO(src, true); 40 if (surface) { 41 /* Treat white as transparent */ 42 SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255)); 43 44 texture = SDL_CreateTextureFromSurface(r, surface); 45 *w = surface->w; 46 *h = surface->h; 47 SDL_DestroySurface(surface); 48 } 49 } 50 return texture; 51} 52 53static void MoveSprites(void) 54{ 55 int i; 56 int window_w; 57 int window_h; 58 SDL_FRect *position, *velocity; 59 60 /* Get the window size */ 61 SDL_GetWindowSizeInPixels(window, &window_w, &window_h); 62 63 /* Draw a gray background */ 64 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 65 SDL_RenderClear(renderer); 66 67 /* Move the sprite, bounce at the wall, and draw */ 68 for (i = 0; i < NUM_SPRITES; ++i) { 69 position = &positions[i]; 70 velocity = &velocities[i]; 71 position->x += velocity->x; 72 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { 73 velocity->x = -velocity->x; 74 position->x += velocity->x; 75 } 76 position->y += velocity->y; 77 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { 78 velocity->y = -velocity->y; 79 position->y += velocity->y; 80 } 81 82 /* Blit the sprite onto the screen */ 83 SDL_RenderTexture(renderer, sprite, NULL, position); 84 } 85 86 /* Update the screen! */ 87 SDL_RenderPresent(renderer); 88} 89 90static int InitSprites(void) 91{ 92 /* Create the sprite texture and initialize the sprite positions */ 93 sprite = CreateTexture(renderer, icon_png, icon_png_len, &sprite_w, &sprite_h); 94 95 if (!sprite) { 96 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite texture"); 97 return -1; 98 } 99 100 for (int i = 0; i < NUM_SPRITES; ++i) { 101 positions[i].x = (float)SDL_rand(WINDOW_WIDTH - sprite_w); 102 positions[i].y = (float)SDL_rand(WINDOW_HEIGHT - sprite_h); 103 positions[i].w = (float)sprite_w; 104 positions[i].h = (float)sprite_h; 105 velocities[i].x = 0.0f; 106 velocities[i].y = 0.0f; 107 while (velocities[i].x == 0.f && velocities[i].y == 0.f) { 108 velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED); 109 velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED); 110 } 111 } 112 113 return 0; 114} 115 116/* Encapsulated in a struct to silence shadow variable warnings */ 117static struct 118{ 119 /* These are owned by SDL and must not be destroyed! */ 120 struct wl_display *wl_display; 121 struct wl_surface *wl_surface; 122 123 /* These are owned by the application and need to be cleaned up on exit. */ 124 struct wl_registry *wl_registry; 125 struct xdg_wm_base *xdg_wm_base; 126 struct xdg_surface *xdg_surface; 127 struct xdg_toplevel *xdg_toplevel; 128} test_wl_state; 129 130static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) 131{ 132 xdg_surface_ack_configure(test_wl_state.xdg_surface, serial); 133} 134 135static const struct xdg_surface_listener xdg_surface_listener = { 136 .configure = xdg_surface_configure, 137}; 138 139static void xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states) 140{ 141 /* NOP */ 142} 143 144static void xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) 145{ 146 done = 1; 147} 148 149static void xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) 150{ 151 /* NOP */ 152} 153 154static void xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities) 155{ 156 /* NOP */ 157} 158 159static const struct xdg_toplevel_listener xdg_toplevel_listener = { 160 .configure = xdg_toplevel_configure, 161 .close = xdg_toplevel_close, 162 .configure_bounds = xdg_toplevel_configure_bounds, 163 .wm_capabilities = xdg_toplevel_wm_capabilities 164}; 165 166static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) 167{ 168 xdg_wm_base_pong(test_wl_state.xdg_wm_base, serial); 169} 170 171static const struct xdg_wm_base_listener xdg_wm_base_listener = { 172 .ping = xdg_wm_base_ping, 173}; 174 175static void registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) 176{ 177 if (SDL_strcmp(interface, xdg_wm_base_interface.name) == 0) { 178 test_wl_state.xdg_wm_base = wl_registry_bind(test_wl_state.wl_registry, name, &xdg_wm_base_interface, 1); 179 xdg_wm_base_add_listener(test_wl_state.xdg_wm_base, &xdg_wm_base_listener, NULL); 180 } 181} 182 183static void registry_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) 184{ 185 /* NOP */ 186} 187 188static const struct wl_registry_listener wl_registry_listener = { 189 .global = registry_global, 190 .global_remove = registry_global_remove, 191}; 192 193int main(int argc, char **argv) 194{ 195 int ret = -1; 196 SDL_PropertiesID props; 197 SDLTest_CommonState *state; 198 199 state = SDLTest_CommonCreateState(argv, 0); 200 if (!state) { 201 return 1; 202 } 203 204 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { 205 goto exit; 206 } 207 208 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { 209 goto exit; 210 } 211 212 if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") != 0) { 213 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Video driver must be 'wayland', not '%s'", SDL_GetCurrentVideoDriver()); 214 goto exit; 215 } 216 217 /* Create a window with the custom surface role property set. */ 218 props = SDL_CreateProperties(); 219 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_WAYLAND_SURFACE_ROLE_CUSTOM_BOOLEAN, true); /* Roleless surface */ 220 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, true); /* OpenGL enabled */ 221 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_WIDTH); /* Default width */ 222 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_HEIGHT); /* Default height */ 223 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true); /* Handle DPI scaling internally */ 224 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Wayland custom surface role test"); /* Default title */ 225 226 window = SDL_CreateWindowWithProperties(props); 227 SDL_DestroyProperties(props); 228 if (!window) { 229 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation failed"); 230 goto exit; 231 } 232 233 /* Create the renderer */ 234 renderer = SDL_CreateRenderer(window, NULL); 235 if (!renderer) { 236 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Renderer creation failed"); 237 goto exit; 238 } 239 240 /* Get the display object and use it to create a registry object, which will enumerate the xdg_wm_base protocol. */ 241 test_wl_state.wl_display = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL); 242 test_wl_state.wl_registry = wl_display_get_registry(test_wl_state.wl_display); 243 wl_registry_add_listener(test_wl_state.wl_registry, &wl_registry_listener, NULL); 244 245 /* Roundtrip to enumerate registry objects. */ 246 wl_display_roundtrip(test_wl_state.wl_display); 247 248 if (!test_wl_state.xdg_wm_base) { 249 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "'xdg_wm_base' protocol not found!"); 250 goto exit; 251 } 252 253 /* Get the wl_surface object from the SDL_Window, and create a toplevel window with it. */ 254 test_wl_state.wl_surface = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL); 255 256 /* Create the xdg_surface from the wl_surface. */ 257 test_wl_state.xdg_surface = xdg_wm_base_get_xdg_surface(test_wl_state.xdg_wm_base, test_wl_state.wl_surface); 258 xdg_surface_add_listener(test_wl_state.xdg_surface, &xdg_surface_listener, NULL); 259 260 /* Create the xdg_toplevel from the xdg_surface. */ 261 test_wl_state.xdg_toplevel = xdg_surface_get_toplevel(test_wl_state.xdg_surface); 262 xdg_toplevel_add_listener(test_wl_state.xdg_toplevel, &xdg_toplevel_listener, NULL); 263 xdg_toplevel_set_title(test_wl_state.xdg_toplevel, SDL_GetWindowTitle(window)); 264 265 /* Initialize the sprites. */ 266 if (InitSprites() < 0) { 267 goto exit; 268 } 269 270 while (!done) { 271 SDL_Event event; 272 while (SDL_PollEvent(&event)) { 273 if (event.type == SDL_EVENT_KEY_DOWN) { 274 switch (event.key.key) { 275 case SDLK_ESCAPE: 276 done = 1; 277 break; 278 case SDLK_EQUALS: 279 /* Ctrl+ enlarges the window */ 280 if (event.key.mod & SDL_KMOD_CTRL) { 281 int w, h; 282 SDL_GetWindowSize(window, &w, &h); 283 SDL_SetWindowSize(window, w * 2, h * 2); 284 } 285 break; 286 case SDLK_MINUS: 287 /* Ctrl- shrinks the window */ 288 if (event.key.mod & SDL_KMOD_CTRL) { 289 int w, h; 290 SDL_GetWindowSize(window, &w, &h); 291 SDL_SetWindowSize(window, w / 2, h / 2); 292 } 293 break; 294 default: 295 break; 296 } 297 } 298 } 299 300 /* Draw the sprites */ 301 MoveSprites(); 302 } 303 304 ret = 0; 305 306exit: 307 /* The display and surface handles obtained from SDL are owned by SDL and must *NOT* be destroyed here! */ 308 if (test_wl_state.xdg_toplevel) { 309 xdg_toplevel_destroy(test_wl_state.xdg_toplevel); 310 test_wl_state.xdg_toplevel = NULL; 311 } 312 if (test_wl_state.xdg_surface) { 313 xdg_surface_destroy(test_wl_state.xdg_surface); 314 test_wl_state.xdg_surface = NULL; 315 } 316 if (test_wl_state.xdg_wm_base) { 317 xdg_wm_base_destroy(test_wl_state.xdg_wm_base); 318 test_wl_state.xdg_wm_base = NULL; 319 } 320 if (test_wl_state.wl_registry) { 321 wl_registry_destroy(test_wl_state.wl_registry); 322 test_wl_state.wl_registry = NULL; 323 } 324 325 /* Destroy the SDL resources */ 326 if (sprite) { 327 SDL_DestroyTexture(sprite); 328 sprite = NULL; 329 } 330 if (renderer) { 331 SDL_DestroyRenderer(renderer); 332 renderer = NULL; 333 } 334 if (window) { 335 SDL_DestroyWindow(window); 336 window = NULL; 337 } 338 339 SDL_Quit(); 340 SDLTest_CommonDestroyState(state); 341 return ret; 342} 343[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.