Merged r4874:4875 from branches/SDL-1.2: testwm keyboard debug output.
2 /* Test out the window manager interaction functions */
10 /* Is the cursor visible? */
11 static int visible = 1;
13 static Uint8 video_bpp;
14 static Uint32 video_flags;
16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
25 SetVideoMode(int w, int h)
30 SDL_Color palette[256];
32 screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
34 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
35 w, h, video_bpp, SDL_GetError());
38 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
39 "fullscreen" : "windowed");
41 /* Set the surface pixels and refresh! */
42 for (i = 0; i < 256; ++i) {
43 palette[i].r = 255 - i;
44 palette[i].g = 255 - i;
45 palette[i].b = 255 - i;
47 SDL_SetColors(screen, palette, 0, 256);
48 if (SDL_LockSurface(screen) < 0) {
49 fprintf(stderr, "Couldn't lock display surface: %s\n",
53 buffer = (Uint8 *) screen->pixels;
54 for (i = 0; i < screen->h; ++i) {
55 memset(buffer, (i * 255) / screen->h,
56 screen->w * screen->format->BytesPerPixel);
57 buffer += screen->pitch;
59 SDL_UnlockSurface(screen);
60 SDL_UpdateRect(screen, 0, 0, 0, 0);
66 LoadIconSurface(char *file, Uint8 ** maskp)
75 /* Load the icon surface */
76 icon = SDL_LoadBMP(file);
78 fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
82 /* Check width and height
83 if ( (icon->w%8) != 0 ) {
84 fprintf(stderr, "Icon width must be a multiple of 8!\n");
85 SDL_FreeSurface(icon);
91 if (icon->format->palette == NULL) {
92 fprintf(stderr, "Icon must have a palette!\n");
93 SDL_FreeSurface(icon);
97 /* Set the colorkey */
98 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
100 /* Create the mask */
101 pixels = (Uint8 *) icon->pixels;
102 printf("Transparent pixel: (%d,%d,%d)\n",
103 icon->format->palette->colors[*pixels].r,
104 icon->format->palette->colors[*pixels].g,
105 icon->format->palette->colors[*pixels].b);
106 mlen = (icon->w * icon->h + 7) / 8;
107 mask = (Uint8 *) malloc(mlen);
109 fprintf(stderr, "Out of memory!\n");
110 SDL_FreeSurface(icon);
113 memset(mask, 0, mlen);
114 for (i = 0; i < icon->h; i++)
115 for (j = 0; j < icon->w; j++) {
116 int pindex = i * icon->pitch + j;
117 int mindex = i * icon->w + j;
118 if (pixels[pindex] != *pixels)
119 mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
126 HotKey_ToggleFullScreen(void)
130 screen = SDL_GetVideoSurface();
131 if (SDL_WM_ToggleFullScreen(screen)) {
132 printf("Toggled fullscreen mode - now %s\n",
133 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
135 printf("Unable to toggle fullscreen mode\n");
136 video_flags ^= SDL_FULLSCREEN;
137 SetVideoMode(screen->w, screen->h);
142 HotKey_ToggleGrab(void)
146 printf("Ctrl-G: toggling input grab!\n");
147 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
148 if (mode == SDL_GRAB_ON) {
149 printf("Grab was on\n");
151 printf("Grab was off\n");
153 mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
154 if (mode == SDL_GRAB_ON) {
155 printf("Grab is now on\n");
157 printf("Grab is now off\n");
164 printf("Ctrl-Z: iconifying window!\n");
165 SDL_WM_IconifyWindow();
173 printf("Posting internal quit request\n");
174 event.type = SDL_USEREVENT;
175 SDL_PushEvent(&event);
180 print_modifiers(void)
183 printf(" modifiers:");
184 mod = SDL_GetModState();
189 if(mod & KMOD_LSHIFT)
191 if(mod & KMOD_RSHIFT)
213 static void PrintKey(const SDL_keysym *sym, int pressed)
215 /* Print the keycode, name and state */
217 printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
218 sym->sym, SDL_GetKeyName(sym->sym));
220 printf("Unknown Key (scancode = %d) %s ", sym->scancode,
221 pressed ? "pressed" : "released");
224 /* Print the translated character, if one exists */
225 if ( sym->unicode ) {
226 /* Is it a control-character? */
227 if ( sym->unicode < ' ' ) {
228 printf(" (^%c)", sym->unicode+'@');
231 printf(" (%c)", sym->unicode);
233 /* This is a Latin-1 program, so only show 8-bits */
234 if ( !(sym->unicode & 0xFF00) )
235 printf(" (%c)", sym->unicode);
237 printf(" (0x%X)", sym->unicode);
246 static int (SDLCALL * old_filterfunc) (void *, SDL_Event *);
247 static void *old_filterdata;
250 FilterEvents(void *userdata, SDL_Event * event)
252 static int reallyquit = 0;
254 if (old_filterfunc) {
255 old_filterfunc(old_filterdata, event);
258 switch (event->type) {
260 case SDL_ACTIVEEVENT:
261 /* See what happened */
262 printf("App %s ", event->active.gain ? "gained" : "lost");
263 if (event->active.state & SDL_APPACTIVE)
265 if (event->active.state & SDL_APPINPUTFOCUS)
267 if (event->active.state & SDL_APPMOUSEFOCUS)
271 /* See if we are iconified or restored */
272 if (event->active.state & SDL_APPACTIVE) {
273 printf("App has been %s\n",
274 event->active.gain ? "restored" : "iconified");
278 /* We want to toggle visibility on buttonpress */
279 case SDL_MOUSEBUTTONDOWN:
280 case SDL_MOUSEBUTTONUP:
281 if (event->button.state == SDL_PRESSED) {
283 SDL_ShowCursor(visible);
285 printf("Mouse button %d has been %s at %d,%d\n",
286 event->button.button,
287 (event->button.state == SDL_PRESSED) ? "pressed" : "released",
288 event->button.x, event->button.y);
291 /* Show relative mouse motion */
292 case SDL_MOUSEMOTION:
294 printf("Mouse motion: {%d,%d} (%d,%d)\n",
295 event->motion.x, event->motion.y,
296 event->motion.xrel, event->motion.yrel);
301 PrintKey(&event->key.keysym, 1);
302 if (event->key.keysym.sym == SDLK_ESCAPE) {
305 if ((event->key.keysym.sym == SDLK_g) &&
306 (event->key.keysym.mod & KMOD_CTRL)) {
309 if ((event->key.keysym.sym == SDLK_z) &&
310 (event->key.keysym.mod & KMOD_CTRL)) {
313 if ((event->key.keysym.sym == SDLK_RETURN) &&
314 (event->key.keysym.mod & KMOD_ALT)) {
315 HotKey_ToggleFullScreen();
320 PrintKey(&event->key.keysym, 0);
323 /* Pass the video resize event through .. */
324 case SDL_VIDEORESIZE:
327 /* This is important! Queue it if we want to quit. */
331 printf("Quit requested\n");
334 printf("Quit demanded\n");
337 /* This will never happen because events queued directly
338 to the event queue are not filtered.
343 /* Drop all other events */
350 main(int argc, char *argv[])
359 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
360 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
364 /* Check command line arguments */
368 video_flags = SDL_SWSURFACE;
371 if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
372 video_flags |= SDL_FULLSCREEN;
375 } else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
376 video_flags |= SDL_RESIZABLE;
379 } else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
380 video_flags |= SDL_NOFRAME;
383 } else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
387 } else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
391 } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
392 video_bpp = atoi(argv[2]);
400 /* Set the icon -- this must be done before the first mode set */
401 icon = LoadIconSurface("icon.bmp", &icon_mask);
403 SDL_WM_SetIcon(icon, icon_mask);
405 if (icon_mask != NULL)
408 /* Set the title bar */
410 title = "Testing 1.. 2.. 3...";
413 SDL_WM_SetCaption(title, "testwm");
415 /* See if it's really set */
416 SDL_WM_GetCaption(&title, NULL);
418 printf("Title was set to: %s\n", title);
420 printf("No window title was set!\n");
422 /* Initialize the display */
423 if (SetVideoMode(w, h) < 0) {
427 /* Set an event filter that discards everything but QUIT */
428 SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
429 SDL_SetEventFilter(FilterEvents, NULL);
431 /* Loop, waiting for QUIT */
432 while (SDL_WaitEvent(&event)) {
433 switch (event.type) {
434 case SDL_VIDEORESIZE:
435 printf("Got a resize event: %dx%d\n",
436 event.resize.w, event.resize.h);
437 SetVideoMode(event.resize.w, event.resize.h);
440 printf("Handling internal quit request\n");
441 /* Fall through to the quit handler */
443 printf("Bye bye..\n");
446 /* This should never happen */
447 printf("Warning: Event %d wasn't filtered\n", event.type);
451 printf("SDL_WaitEvent() error: %s\n", SDL_GetError());