0
|
1
|
|
2 /* Test out the window manager interaction functions */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <string.h>
|
|
7
|
|
8 #include "SDL.h"
|
|
9
|
|
10 /* Is the cursor visible? */
|
|
11 static int visible = 1;
|
|
12
|
|
13 SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
|
|
14 {
|
|
15 SDL_Surface *icon;
|
|
16 Uint8 *pixels;
|
|
17 Uint8 *mask;
|
|
18 int mlen, i;
|
|
19
|
|
20 *maskp = NULL;
|
|
21
|
|
22 /* Load the icon surface */
|
|
23 icon = SDL_LoadBMP(file);
|
|
24 if ( icon == NULL ) {
|
|
25 fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
|
26 return(NULL);
|
|
27 }
|
|
28
|
|
29 /* Check width and height */
|
|
30 if ( (icon->w%8) != 0 ) {
|
|
31 fprintf(stderr, "Icon width must be a multiple of 8!\n");
|
|
32 SDL_FreeSurface(icon);
|
|
33 return(NULL);
|
|
34 }
|
|
35 if ( icon->format->palette == NULL ) {
|
|
36 fprintf(stderr, "Icon must have a palette!\n");
|
|
37 SDL_FreeSurface(icon);
|
|
38 return(NULL);
|
|
39 }
|
|
40
|
|
41 /* Set the colorkey */
|
|
42 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
|
|
43
|
|
44 /* Create the mask */
|
|
45 pixels = (Uint8 *)icon->pixels;
|
|
46 printf("Transparent pixel: (%d,%d,%d)\n",
|
|
47 icon->format->palette->colors[*pixels].r,
|
|
48 icon->format->palette->colors[*pixels].g,
|
|
49 icon->format->palette->colors[*pixels].b);
|
|
50 mlen = icon->w*icon->h;
|
|
51 mask = (Uint8 *)malloc(mlen/8);
|
|
52 if ( mask == NULL ) {
|
|
53 fprintf(stderr, "Out of memory!\n");
|
|
54 SDL_FreeSurface(icon);
|
|
55 return(NULL);
|
|
56 }
|
|
57 memset(mask, 0, mlen/8);
|
|
58 for ( i=0; i<mlen; ) {
|
|
59 if ( pixels[i] != *pixels )
|
|
60 mask[i/8] |= 0x01;
|
|
61 ++i;
|
|
62 if ( (i%8) != 0 )
|
|
63 mask[i/8] <<= 1;
|
|
64 }
|
|
65 *maskp = mask;
|
|
66 return(icon);
|
|
67 }
|
|
68
|
|
69 void HotKey_ToggleFullScreen(void)
|
|
70 {
|
|
71 SDL_Surface *screen;
|
|
72
|
|
73 screen = SDL_GetVideoSurface();
|
|
74 if ( SDL_WM_ToggleFullScreen(screen) ) {
|
|
75 printf("Toggled fullscreen mode - now %s\n",
|
|
76 (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
|
77 } else {
|
|
78 printf("Unable to toggle fullscreen mode\n");
|
|
79 }
|
|
80 }
|
|
81
|
|
82 void HotKey_ToggleGrab(void)
|
|
83 {
|
|
84 SDL_GrabMode mode;
|
|
85
|
|
86 printf("Ctrl-G: toggling input grab!\n");
|
|
87 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
|
|
88 if ( mode == SDL_GRAB_ON ) {
|
|
89 printf("Grab was on\n");
|
|
90 } else {
|
|
91 printf("Grab was off\n");
|
|
92 }
|
|
93 mode = SDL_WM_GrabInput(!mode);
|
|
94 if ( mode == SDL_GRAB_ON ) {
|
|
95 printf("Grab is now on\n");
|
|
96 } else {
|
|
97 printf("Grab is now off\n");
|
|
98 }
|
|
99 }
|
|
100
|
|
101 void HotKey_Iconify(void)
|
|
102 {
|
|
103 printf("Ctrl-Z: iconifying window!\n");
|
|
104 SDL_WM_IconifyWindow();
|
|
105 }
|
|
106
|
|
107 void HotKey_Quit(void)
|
|
108 {
|
|
109 SDL_Event event;
|
|
110
|
|
111 printf("Posting internal quit request\n");
|
|
112 event.type = SDL_USEREVENT;
|
|
113 SDL_PushEvent(&event);
|
|
114 }
|
|
115
|
|
116 int FilterEvents(const SDL_Event *event)
|
|
117 {
|
|
118 static int reallyquit = 0;
|
|
119
|
|
120 switch (event->type) {
|
|
121
|
|
122 case SDL_ACTIVEEVENT:
|
|
123 /* See what happened */
|
|
124 printf("App %s ",
|
|
125 event->active.gain ? "gained" : "lost");
|
|
126 if ( event->active.state & SDL_APPACTIVE )
|
|
127 printf("active ");
|
|
128 if ( event->active.state & SDL_APPMOUSEFOCUS )
|
|
129 printf("mouse ");
|
|
130 if ( event->active.state & SDL_APPINPUTFOCUS )
|
|
131 printf("input ");
|
|
132 printf("focus\n");
|
|
133
|
|
134 /* See if we are iconified or restored */
|
|
135 if ( event->active.state & SDL_APPACTIVE ) {
|
|
136 printf("App has been %s\n",
|
|
137 event->active.gain ?
|
|
138 "restored" : "iconified");
|
|
139 }
|
|
140 return(0);
|
|
141
|
|
142 /* We want to toggle visibility on buttonpress */
|
|
143 case SDL_MOUSEBUTTONDOWN:
|
|
144 case SDL_MOUSEBUTTONUP:
|
|
145 if ( event->button.state == SDL_PRESSED ) {
|
|
146 visible = !visible;
|
|
147 SDL_ShowCursor(visible);
|
|
148 }
|
|
149 printf("Mouse button %d has been %s\n",
|
|
150 event->button.button,
|
|
151 (event->button.state == SDL_PRESSED) ?
|
|
152 "pressed" : "released");
|
|
153 return(0);
|
|
154
|
|
155 /* Show relative mouse motion */
|
|
156 case SDL_MOUSEMOTION:
|
|
157 #if 0
|
|
158 printf("Mouse relative motion: {%d,%d}\n",
|
|
159 event->motion.xrel, event->motion.yrel);
|
|
160 #endif
|
|
161 return(0);
|
|
162
|
|
163 case SDL_KEYDOWN:
|
|
164 if ( event->key.keysym.sym == SDLK_ESCAPE ) {
|
|
165 HotKey_Quit();
|
|
166 }
|
|
167 if ( (event->key.keysym.sym == SDLK_g) &&
|
|
168 (event->key.keysym.mod & KMOD_CTRL) ) {
|
|
169 HotKey_ToggleGrab();
|
|
170 }
|
|
171 if ( (event->key.keysym.sym == SDLK_z) &&
|
|
172 (event->key.keysym.mod & KMOD_CTRL) ) {
|
|
173 HotKey_Iconify();
|
|
174 }
|
|
175 if ( (event->key.keysym.sym == SDLK_RETURN) &&
|
|
176 (event->key.keysym.mod & KMOD_ALT) ) {
|
|
177 HotKey_ToggleFullScreen();
|
|
178 }
|
|
179 return(0);
|
|
180
|
|
181 /* Pass the video resize event through .. */
|
|
182 case SDL_VIDEORESIZE:
|
|
183 return(1);
|
|
184
|
|
185 /* This is important! Queue it if we want to quit. */
|
|
186 case SDL_QUIT:
|
|
187 if ( ! reallyquit ) {
|
|
188 reallyquit = 1;
|
|
189 printf("Quit requested\n");
|
|
190 return(0);
|
|
191 }
|
|
192 printf("Quit demanded\n");
|
|
193 return(1);
|
|
194
|
|
195 /* This will never happen because events queued directly
|
|
196 to the event queue are not filtered.
|
|
197 */
|
|
198 case SDL_USEREVENT:
|
|
199 return(1);
|
|
200
|
|
201 /* Drop all other events */
|
|
202 default:
|
|
203 return(0);
|
|
204 }
|
|
205 }
|
|
206
|
|
207 static Uint8 video_bpp;
|
|
208 static Uint32 video_flags;
|
|
209
|
|
210 int SetVideoMode(int w, int h)
|
|
211 {
|
|
212 SDL_Surface *screen;
|
|
213 int i;
|
|
214 Uint8 *buffer;
|
|
215 SDL_Color palette[256];
|
|
216
|
|
217 screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
|
|
218 if ( screen == NULL ) {
|
|
219 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
|
220 w, h, video_bpp, SDL_GetError());
|
|
221 return(-1);
|
|
222 }
|
|
223 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
|
|
224 "fullscreen" : "windowed");
|
|
225
|
|
226 /* Set the surface pixels and refresh! */
|
|
227 for ( i=0; i<256; ++i ) {
|
|
228 palette[i].r = 255-i;
|
|
229 palette[i].g = 255-i;
|
|
230 palette[i].b = 255-i;
|
|
231 }
|
|
232 SDL_SetColors(screen, palette, 0, 256);
|
|
233 if ( SDL_LockSurface(screen) < 0 ) {
|
|
234 fprintf(stderr, "Couldn't lock display surface: %s\n",
|
|
235 SDL_GetError());
|
|
236 return(-1);
|
|
237 }
|
|
238 buffer = (Uint8 *)screen->pixels;
|
|
239 for ( i=0; i<screen->h; ++i ) {
|
|
240 memset(buffer,(i*255)/screen->h,
|
|
241 screen->w*screen->format->BytesPerPixel);
|
|
242 buffer += screen->pitch;
|
|
243 }
|
|
244 SDL_UnlockSurface(screen);
|
|
245 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
246
|
|
247 return(0);
|
|
248 }
|
|
249
|
|
250 int main(int argc, char *argv[])
|
|
251 {
|
|
252 SDL_Event event;
|
|
253 char *title;
|
|
254 SDL_Surface *icon;
|
|
255 Uint8 *icon_mask;
|
|
256 int parsed;
|
|
257
|
|
258 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
|
259 fprintf(stderr,
|
|
260 "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
261 exit(1);
|
|
262 }
|
|
263 atexit(SDL_Quit);
|
|
264
|
|
265 /* Check command line arguments */
|
|
266 video_bpp = 8;
|
|
267 video_flags = SDL_SWSURFACE;
|
|
268 parsed = 1;
|
|
269 while ( parsed ) {
|
|
270 if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
|
|
271 video_flags |= SDL_FULLSCREEN;
|
|
272 argc -= 1;
|
|
273 argv += 1;
|
|
274 } else
|
|
275 if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
|
|
276 video_flags |= SDL_RESIZABLE;
|
|
277 argc -= 1;
|
|
278 argv += 1;
|
|
279 } else
|
|
280 if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
|
|
281 video_flags |= SDL_NOFRAME;
|
|
282 argc -= 1;
|
|
283 argv += 1;
|
|
284 } else
|
|
285 if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
|
|
286 video_bpp = atoi(argv[2]);
|
|
287 argc -= 2;
|
|
288 argv += 2;
|
|
289 } else {
|
|
290 parsed = 0;
|
|
291 }
|
|
292 }
|
|
293
|
|
294 /* Set the icon -- this must be done before the first mode set */
|
|
295 icon = LoadIconSurface("icon.bmp", &icon_mask);
|
|
296 if ( icon != NULL ) {
|
|
297 SDL_WM_SetIcon(icon, icon_mask);
|
|
298 }
|
|
299 if ( icon_mask != NULL )
|
|
300 free(icon_mask);
|
|
301
|
|
302 /* Set the title bar */
|
|
303 if ( argv[1] == NULL )
|
|
304 title = "Testing 1.. 2.. 3...";
|
|
305 else
|
|
306 title = argv[1];
|
|
307 SDL_WM_SetCaption(title, "testwm");
|
|
308
|
|
309 /* See if it's really set */
|
|
310 SDL_WM_GetCaption(&title, NULL);
|
|
311 if ( title )
|
|
312 printf("Title was set to: %s\n", title);
|
|
313 else
|
|
314 printf("No window title was set!\n");
|
|
315
|
|
316 /* Initialize the display */
|
|
317 if ( SetVideoMode(640, 480) < 0 ) {
|
|
318 return(1);
|
|
319 }
|
|
320
|
|
321 /* Set an event filter that discards everything but QUIT */
|
|
322 SDL_SetEventFilter(FilterEvents);
|
|
323
|
|
324 /* Ignore key up events, they don't even get filtered */
|
|
325 SDL_EventState(SDL_KEYUP, SDL_IGNORE);
|
|
326
|
|
327 /* Loop, waiting for QUIT */
|
|
328 while ( SDL_WaitEvent(&event) ) {
|
|
329 switch (event.type) {
|
|
330 case SDL_VIDEORESIZE:
|
|
331 printf("Got a resize event: %dx%d\n",
|
|
332 event.resize.w, event.resize.h);
|
|
333 SetVideoMode(event.resize.w, event.resize.h);
|
|
334 break;
|
|
335 case SDL_USEREVENT:
|
|
336 printf("Handling internal quit request\n");
|
|
337 /* Fall through to the quit handler */
|
|
338 case SDL_QUIT:
|
|
339 printf("Bye bye..\n");
|
|
340 return(0);
|
|
341 default:
|
|
342 /* This should never happen */
|
|
343 printf("Warning: Event %d wasn't filtered\n",
|
|
344 event.type);
|
|
345 break;
|
|
346 }
|
|
347 }
|
|
348 printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
|
|
349 return(255);
|
|
350 }
|