2 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Test out the window manager interaction functions */
21 /* Is the cursor visible? */
22 static int visible = 1;
24 static Uint8 video_bpp;
25 static Uint32 video_flags;
27 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
36 SetVideoMode(int w, int h)
41 SDL_Color palette[256];
43 screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
45 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
46 w, h, video_bpp, SDL_GetError());
49 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
50 "fullscreen" : "windowed");
52 /* Set the surface pixels and refresh! */
53 for (i = 0; i < 256; ++i) {
54 palette[i].r = 255 - i;
55 palette[i].g = 255 - i;
56 palette[i].b = 255 - i;
58 SDL_SetColors(screen, palette, 0, 256);
59 if (SDL_LockSurface(screen) < 0) {
60 fprintf(stderr, "Couldn't lock display surface: %s\n",
64 buffer = (Uint8 *) screen->pixels;
65 for (i = 0; i < screen->h; ++i) {
66 memset(buffer, (i * 255) / screen->h,
67 screen->w * screen->format->BytesPerPixel);
68 buffer += screen->pitch;
70 SDL_UnlockSurface(screen);
71 SDL_UpdateRect(screen, 0, 0, 0, 0);
77 LoadIconSurface(char *file, Uint8 ** maskp)
86 /* Load the icon surface */
87 icon = SDL_LoadBMP(file);
89 fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
93 /* Check width and height
94 if ( (icon->w%8) != 0 ) {
95 fprintf(stderr, "Icon width must be a multiple of 8!\n");
96 SDL_FreeSurface(icon);
102 if (icon->format->palette == NULL) {
103 fprintf(stderr, "Icon must have a palette!\n");
104 SDL_FreeSurface(icon);
108 /* Set the colorkey */
109 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
111 /* Create the mask */
112 pixels = (Uint8 *) icon->pixels;
113 printf("Transparent pixel: (%d,%d,%d)\n",
114 icon->format->palette->colors[*pixels].r,
115 icon->format->palette->colors[*pixels].g,
116 icon->format->palette->colors[*pixels].b);
117 mlen = (icon->w * icon->h + 7) / 8;
118 mask = (Uint8 *) malloc(mlen);
120 fprintf(stderr, "Out of memory!\n");
121 SDL_FreeSurface(icon);
124 memset(mask, 0, mlen);
125 for (i = 0; i < icon->h; i++)
126 for (j = 0; j < icon->w; j++) {
127 int pindex = i * icon->pitch + j;
128 int mindex = i * icon->w + j;
129 if (pixels[pindex] != *pixels)
130 mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
137 HotKey_ToggleFullScreen(void)
141 screen = SDL_GetVideoSurface();
142 if (SDL_WM_ToggleFullScreen(screen)) {
143 printf("Toggled fullscreen mode - now %s\n",
144 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
146 printf("Unable to toggle fullscreen mode\n");
147 video_flags ^= SDL_FULLSCREEN;
148 SetVideoMode(screen->w, screen->h);
153 HotKey_ToggleGrab(void)
157 printf("Ctrl-G: toggling input grab!\n");
158 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
159 if (mode == SDL_GRAB_ON) {
160 printf("Grab was on\n");
162 printf("Grab was off\n");
164 mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
165 if (mode == SDL_GRAB_ON) {
166 printf("Grab is now on\n");
168 printf("Grab is now off\n");
175 printf("Ctrl-Z: iconifying window!\n");
176 SDL_WM_IconifyWindow();
184 printf("Posting internal quit request\n");
185 event.type = SDL_USEREVENT;
186 SDL_PushEvent(&event);
191 print_modifiers(void)
194 printf(" modifiers:");
195 mod = SDL_GetModState();
200 if(mod & KMOD_LSHIFT)
202 if(mod & KMOD_RSHIFT)
224 static void PrintKey(const SDL_Keysym *sym, int pressed)
226 /* Print the keycode, name and state */
228 printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
229 sym->sym, SDL_GetKeyName(sym->sym));
231 printf("Unknown Key (scancode = %d) %s ", sym->scancode,
232 pressed ? "pressed" : "released");
235 /* Print the translated character, if one exists */
236 if ( sym->unicode ) {
237 /* Is it a control-character? */
238 if ( sym->unicode < ' ' ) {
239 printf(" (^%c)", sym->unicode+'@');
242 printf(" (%c)", sym->unicode);
244 /* This is a Latin-1 program, so only show 8-bits */
245 if ( !(sym->unicode & 0xFF00) )
246 printf(" (%c)", sym->unicode);
248 printf(" (0x%X)", sym->unicode);
257 static int (SDLCALL * old_filterfunc) (void *, SDL_Event *);
258 static void *old_filterdata;
261 FilterEvents(void *userdata, SDL_Event * event)
263 static int reallyquit = 0;
265 if (old_filterfunc) {
266 old_filterfunc(old_filterdata, event);
269 switch (event->type) {
271 case SDL_ACTIVEEVENT:
272 /* See what happened */
273 printf("App %s ", event->active.gain ? "gained" : "lost");
274 if (event->active.state & SDL_APPACTIVE)
276 if (event->active.state & SDL_APPINPUTFOCUS)
278 if (event->active.state & SDL_APPMOUSEFOCUS)
282 /* See if we are iconified or restored */
283 if (event->active.state & SDL_APPACTIVE) {
284 printf("App has been %s\n",
285 event->active.gain ? "restored" : "iconified");
289 /* We want to toggle visibility on buttonpress */
290 case SDL_MOUSEBUTTONDOWN:
291 case SDL_MOUSEBUTTONUP:
292 if (event->button.state == SDL_PRESSED) {
294 SDL_ShowCursor(visible);
296 printf("Mouse button %d has been %s at %d,%d\n",
297 event->button.button,
298 (event->button.state == SDL_PRESSED) ? "pressed" : "released",
299 event->button.x, event->button.y);
302 /* Show relative mouse motion */
303 case SDL_MOUSEMOTION:
305 printf("Mouse motion: {%d,%d} (%d,%d)\n",
306 event->motion.x, event->motion.y,
307 event->motion.xrel, event->motion.yrel);
312 PrintKey(&event->key.keysym, 1);
313 if (event->key.keysym.sym == SDLK_ESCAPE) {
316 if ((event->key.keysym.sym == SDLK_g) &&
317 (event->key.keysym.mod & KMOD_CTRL)) {
320 if ((event->key.keysym.sym == SDLK_z) &&
321 (event->key.keysym.mod & KMOD_CTRL)) {
324 if ((event->key.keysym.sym == SDLK_RETURN) &&
325 (event->key.keysym.mod & (KMOD_ALT|KMOD_META))) {
326 HotKey_ToggleFullScreen();
331 PrintKey(&event->key.keysym, 0);
334 /* Pass the video resize event through .. */
335 case SDL_VIDEORESIZE:
338 /* This is important! Queue it if we want to quit. */
342 printf("Quit requested\n");
345 printf("Quit demanded\n");
348 /* This will never happen because events queued directly
349 to the event queue are not filtered.
354 /* Drop all other events */
361 main(int argc, char *argv[])
370 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
371 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
375 /* Check command line arguments */
379 video_flags = SDL_SWSURFACE;
382 if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
383 video_flags |= SDL_FULLSCREEN;
386 } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
387 video_flags |= SDL_RESIZABLE;
390 } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
391 video_flags |= SDL_NOFRAME;
394 } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
398 } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
402 } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
403 video_bpp = atoi(argv[2]);
411 /* Set the icon -- this must be done before the first mode set */
412 icon = LoadIconSurface("icon.bmp", &icon_mask);
414 SDL_WM_SetIcon(icon, icon_mask);
416 if (icon_mask != NULL)
419 /* Set the title bar */
421 title = "Testing 1.. 2.. 3...";
424 SDL_WM_SetCaption(title, "testwm");
426 /* See if it's really set */
427 SDL_WM_GetCaption(&title, NULL);
429 printf("Title was set to: %s\n", title);
431 printf("No window title was set!\n");
433 /* Initialize the display */
434 if (SetVideoMode(w, h) < 0) {
438 /* Set an event filter that discards everything but QUIT */
439 SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
440 SDL_SetEventFilter(FilterEvents, NULL);
442 /* Loop, waiting for QUIT */
443 while (SDL_WaitEvent(&event)) {
444 switch (event.type) {
445 case SDL_VIDEORESIZE:
446 printf("Got a resize event: %dx%d\n",
447 event.resize.w, event.resize.h);
448 SetVideoMode(event.resize.w, event.resize.h);
451 printf("Handling internal quit request\n");
452 /* Fall through to the quit handler */
454 printf("Bye bye..\n");
457 /* This should never happen */
458 printf("Warning: Event %d wasn't filtered\n", event.type);
462 printf("SDL_WaitEvent() error: %s\n", SDL_GetError());