| 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_X11 |
| 24 | |
| 25 | #include "SDL_x11video.h" |
| 26 | #include "SDL_x11shape.h" |
| 27 | |
| 28 | |
| 29 | #ifdef SDL_VIDEO_DRIVER_X11_XSHAPE |
| 30 | static Uint8 *GenerateShapeMask(SDL_Surface *shape) |
| 31 | { |
| 32 | int x, y; |
| 33 | const size_t ppb = 8; |
| 34 | const size_t bytes_per_scanline = (shape->w + (ppb - 1)) / ppb; |
| 35 | const Uint8 *a; |
| 36 | Uint8 *mask; |
| 37 | Uint8 *mask_scanline; |
| 38 | Uint8 mask_value; |
| 39 | |
| 40 | mask = (Uint8 *)SDL_calloc(1, shape->h * bytes_per_scanline); |
| 41 | if (mask) { |
| 42 | for (y = 0; y < shape->h; y++) { |
| 43 | a = (const Uint8 *)shape->pixels + y * shape->pitch; |
| 44 | mask_scanline = mask + y * bytes_per_scanline; |
| 45 | for (x = 0; x < shape->w; x++) { |
| 46 | mask_value = (*a == SDL_ALPHA_TRANSPARENT) ? 0 : 1; |
| 47 | mask_scanline[x / ppb] |= mask_value << (x % ppb); |
| 48 | a += 4; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | return mask; |
| 53 | } |
| 54 | #endif // SDL_VIDEO_DRIVER_X11_XSHAPE |
| 55 | |
| 56 | bool X11_UpdateWindowShape(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *shape) |
| 57 | { |
| 58 | bool result = false; |
| 59 | |
| 60 | #ifdef SDL_VIDEO_DRIVER_X11_XSHAPE |
| 61 | SDL_WindowData *windowdata = window->internal; |
| 62 | |
| 63 | // Generate a set of spans for the region |
| 64 | if (shape) { |
| 65 | SDL_Surface *stretched = NULL; |
| 66 | Uint8 *mask; |
| 67 | Pixmap pixmap; |
| 68 | |
| 69 | if (shape->w != window->w || shape->h != window->h) { |
| 70 | stretched = SDL_CreateSurface(window->w, window->h, SDL_PIXELFORMAT_ARGB32); |
| 71 | if (!stretched) { |
| 72 | return false; |
| 73 | } |
| 74 | if (!SDL_StretchSurface(shape, NULL, stretched, NULL, SDL_SCALEMODE_LINEAR)) { |
| 75 | SDL_DestroySurface(stretched); |
| 76 | return false; |
| 77 | } |
| 78 | shape = stretched; |
| 79 | } |
| 80 | |
| 81 | mask = GenerateShapeMask(shape); |
| 82 | if (mask) { |
| 83 | pixmap = X11_XCreateBitmapFromData(windowdata->videodata->display, windowdata->xwindow, (const char *)mask, shape->w, shape->h); |
| 84 | X11_XShapeCombineMask(windowdata->videodata->display, windowdata->xwindow, ShapeInput, 0, 0, pixmap, ShapeSet); |
| 85 | SDL_free(mask); |
| 86 | |
| 87 | result = true; |
| 88 | } |
| 89 | |
| 90 | if (stretched) { |
| 91 | SDL_DestroySurface(stretched); |
| 92 | } |
| 93 | } else { |
| 94 | Region region = X11_XCreateRegion(); |
| 95 | XRectangle rect; |
| 96 | |
| 97 | rect.x = 0; |
| 98 | rect.y = 0; |
| 99 | rect.width = window->w; |
| 100 | rect.height = window->h; |
| 101 | X11_XUnionRectWithRegion(&rect, region, region); |
| 102 | X11_XShapeCombineRegion(windowdata->videodata->display, windowdata->xwindow, ShapeInput, 0, 0, region, ShapeSet); |
| 103 | X11_XDestroyRegion(region); |
| 104 | result = true; |
| 105 | } |
| 106 | #endif // SDL_VIDEO_DRIVER_X11_XSHAPE |
| 107 | |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | #endif // SDL_VIDEO_DRIVER_X11 |
| 112 | |