comparison src/video/x11/SDL_x11shape.c @ 4862:7b1d35d98294

Merged Eli's Google Summer of Code work from SDL-gsoc2010-shaped_windows
author Sam Lantinga <slouken@libsdl.org>
date Sun, 22 Aug 2010 13:45:56 -0700
parents 5624fb0190b5
children
comparison
equal deleted inserted replaced
4764:102675835e08 4862:7b1d35d98294
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 2010 Eli Gottlieb
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Eli Gottlieb
20 eligottlieb@gmail.com
21 */
22
23 #include "SDL_assert.h"
24 #include "SDL_x11video.h"
25 #include "SDL_x11shape.h"
26 #include "SDL_x11window.h"
27
28 SDL_Window*
29 X11_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
30 return SDL_CreateWindow(title,x,y,w,h,flags);
31 }
32
33 SDL_WindowShaper*
34 X11_CreateShaper(SDL_Window* window) {
35 SDL_WindowShaper* result = NULL;
36
37 #if SDL_VIDEO_DRIVER_X11_XSHAPE
38 if (SDL_X11_HAVE_XSHAPE) { /* Make sure X server supports it. */
39 result = malloc(sizeof(SDL_WindowShaper));
40 result->window = window;
41 result->mode.mode = ShapeModeDefault;
42 result->mode.parameters.binarizationCutoff = 1;
43 result->userx = result->usery = 0;
44 SDL_ShapeData* data = SDL_malloc(sizeof(SDL_ShapeData));
45 result->driverdata = data;
46 data->bitmapsize = 0;
47 data->bitmap = NULL;
48 window->shaper = result;
49 int resized_properly = X11_ResizeWindowShape(window);
50 SDL_assert(resized_properly == 0);
51 }
52 #endif
53
54 return result;
55 }
56
57 int
58 X11_ResizeWindowShape(SDL_Window* window) {
59 SDL_ShapeData* data = window->shaper->driverdata;
60 SDL_assert(data != NULL);
61
62 unsigned int bitmapsize = window->w / 8;
63 if(window->w % 8 > 0)
64 bitmapsize += 1;
65 bitmapsize *= window->h;
66 if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
67 data->bitmapsize = bitmapsize;
68 if(data->bitmap != NULL)
69 free(data->bitmap);
70 data->bitmap = malloc(data->bitmapsize);
71 if(data->bitmap == NULL) {
72 SDL_SetError("Could not allocate memory for shaped-window bitmap.");
73 return -1;
74 }
75 }
76 memset(data->bitmap,0,data->bitmapsize);
77
78 window->shaper->userx = window->x;
79 window->shaper->usery = window->y;
80 SDL_SetWindowPosition(window,-1000,-1000);
81
82 return 0;
83 }
84
85 int
86 X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
87 if(shaper == NULL || shape == NULL || shaper->driverdata == NULL)
88 return -1;
89
90 #if SDL_VIDEO_DRIVER_X11_XSHAPE
91 if(shape->format->Amask == 0 && SDL_SHAPEMODEALPHA(shape_mode->mode))
92 return -2;
93 if(shape->w != shaper->window->w || shape->h != shaper->window->h)
94 return -3;
95 SDL_ShapeData *data = shaper->driverdata;
96
97 /* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
98 SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8);
99
100 SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
101 Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
102
103 XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
104 XSync(windowdata->videodata->display,False);
105
106 XFreePixmap(windowdata->videodata->display,shapemask);
107 #endif
108
109 return 0;
110 }