test/testwm.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 13 Aug 2014 11:02:11 +0200
branchSDL-1.2
changeset 9059 9a9f56143400
parent 4257 14195cfdb66e
permissions -rw-r--r--
atari:gem: Remove unused variables.
slouken@0
     1
slouken@0
     2
/* Test out the window manager interaction functions */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
slouken@0
    10
/* Is the cursor visible? */
slouken@0
    11
static int visible = 1;
slouken@0
    12
slouken@826
    13
static Uint8  video_bpp;
slouken@826
    14
static Uint32 video_flags;
slouken@826
    15
icculus@1151
    16
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    17
static void quit(int rc)
icculus@1151
    18
{
icculus@1151
    19
	SDL_Quit();
icculus@1151
    20
	exit(rc);
icculus@1151
    21
}
icculus@1151
    22
slouken@826
    23
int SetVideoMode(int w, int h)
slouken@826
    24
{
slouken@826
    25
	SDL_Surface *screen;
slouken@826
    26
	int i;
slouken@826
    27
	Uint8 *buffer;
slouken@826
    28
	SDL_Color palette[256];
slouken@826
    29
slouken@826
    30
	screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
slouken@826
    31
	if (  screen == NULL ) {
slouken@826
    32
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@826
    33
					w, h, video_bpp, SDL_GetError());
slouken@826
    34
		return(-1);
slouken@826
    35
	}
slouken@826
    36
	printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
slouken@826
    37
						"fullscreen" : "windowed");
slouken@826
    38
slouken@826
    39
	/* Set the surface pixels and refresh! */
slouken@826
    40
	for ( i=0; i<256; ++i ) {
slouken@826
    41
		palette[i].r = 255-i;
slouken@826
    42
		palette[i].g = 255-i;
slouken@826
    43
		palette[i].b = 255-i;
slouken@826
    44
	}
slouken@826
    45
	SDL_SetColors(screen, palette, 0, 256);
slouken@826
    46
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@826
    47
		fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@826
    48
							SDL_GetError());
slouken@826
    49
		return(-1);
slouken@826
    50
	}
slouken@826
    51
	buffer = (Uint8 *)screen->pixels;
slouken@826
    52
	for ( i=0; i<screen->h; ++i ) {
slouken@826
    53
		memset(buffer,(i*255)/screen->h,
slouken@826
    54
				screen->w*screen->format->BytesPerPixel);
slouken@826
    55
		buffer += screen->pitch;
slouken@826
    56
	}
slouken@826
    57
	SDL_UnlockSurface(screen);
slouken@826
    58
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@826
    59
slouken@826
    60
	return(0);
slouken@826
    61
}
slouken@826
    62
slouken@0
    63
SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
slouken@0
    64
{
slouken@0
    65
	SDL_Surface *icon;
slouken@0
    66
	Uint8       *pixels;
slouken@0
    67
	Uint8       *mask;
slouken@591
    68
	int          mlen, i, j;
slouken@0
    69
slouken@0
    70
	*maskp = NULL;
slouken@0
    71
slouken@0
    72
	/* Load the icon surface */
slouken@0
    73
	icon = SDL_LoadBMP(file);
slouken@0
    74
	if ( icon == NULL ) {
slouken@0
    75
		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@0
    76
		return(NULL);
slouken@0
    77
	}
slouken@0
    78
slouken@591
    79
	/* Check width and height 
slouken@0
    80
	if ( (icon->w%8) != 0 ) {
slouken@0
    81
		fprintf(stderr, "Icon width must be a multiple of 8!\n");
slouken@0
    82
		SDL_FreeSurface(icon);
slouken@0
    83
		return(NULL);
slouken@0
    84
	}
slouken@591
    85
*/
slouken@591
    86
    
slouken@591
    87
    
slouken@0
    88
	if ( icon->format->palette == NULL ) {
slouken@0
    89
		fprintf(stderr, "Icon must have a palette!\n");
slouken@0
    90
		SDL_FreeSurface(icon);
slouken@0
    91
		return(NULL);
slouken@0
    92
	}
slouken@0
    93
slouken@0
    94
	/* Set the colorkey */
slouken@0
    95
	SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
slouken@0
    96
slouken@0
    97
	/* Create the mask */
slouken@0
    98
	pixels = (Uint8 *)icon->pixels;
slouken@0
    99
	printf("Transparent pixel: (%d,%d,%d)\n",
slouken@0
   100
				icon->format->palette->colors[*pixels].r,
slouken@0
   101
				icon->format->palette->colors[*pixels].g,
slouken@0
   102
				icon->format->palette->colors[*pixels].b);
slouken@591
   103
	mlen = (icon->w*icon->h + 7) / 8;
slouken@591
   104
	mask = (Uint8 *)malloc(mlen);
slouken@0
   105
	if ( mask == NULL ) {
slouken@0
   106
		fprintf(stderr, "Out of memory!\n");
slouken@0
   107
		SDL_FreeSurface(icon);
slouken@0
   108
		return(NULL);
slouken@0
   109
	}
slouken@591
   110
	memset(mask, 0, mlen);
slouken@591
   111
	for ( i=0; i < icon->h; i++)
slouken@591
   112
        for (j=0; j < icon->w; j++) {
slouken@591
   113
            int pindex = i * icon->pitch + j;
slouken@591
   114
            int mindex = i * icon->w + j;
slouken@591
   115
            if ( pixels[pindex] != *pixels )
slouken@591
   116
                mask[mindex>>3] |= 1 << (7 - (mindex & 7));
slouken@591
   117
        }
slouken@0
   118
	*maskp = mask;
slouken@0
   119
	return(icon);
slouken@0
   120
}
slouken@0
   121
slouken@0
   122
void HotKey_ToggleFullScreen(void)
slouken@0
   123
{
slouken@0
   124
	SDL_Surface *screen;
slouken@0
   125
slouken@0
   126
	screen = SDL_GetVideoSurface();
slouken@0
   127
	if ( SDL_WM_ToggleFullScreen(screen) ) {
slouken@0
   128
		printf("Toggled fullscreen mode - now %s\n",
slouken@0
   129
		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@0
   130
	} else {
slouken@0
   131
		printf("Unable to toggle fullscreen mode\n");
slouken@826
   132
		video_flags ^= SDL_FULLSCREEN;
slouken@826
   133
		SetVideoMode(screen->w, screen->h);
slouken@0
   134
	}
slouken@0
   135
}
slouken@0
   136
slouken@0
   137
void HotKey_ToggleGrab(void)
slouken@0
   138
{
slouken@0
   139
	SDL_GrabMode mode;
slouken@0
   140
slouken@0
   141
	printf("Ctrl-G: toggling input grab!\n");
slouken@0
   142
	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
slouken@0
   143
	if ( mode == SDL_GRAB_ON ) {
slouken@0
   144
		printf("Grab was on\n");
slouken@0
   145
	} else {
slouken@0
   146
		printf("Grab was off\n");
slouken@0
   147
	}
slouken@609
   148
	mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
slouken@0
   149
	if ( mode == SDL_GRAB_ON ) {
slouken@0
   150
		printf("Grab is now on\n");
slouken@0
   151
	} else {
slouken@0
   152
		printf("Grab is now off\n");
slouken@0
   153
	}
slouken@0
   154
}
slouken@0
   155
slouken@0
   156
void HotKey_Iconify(void)
slouken@0
   157
{
slouken@0
   158
	printf("Ctrl-Z: iconifying window!\n");
slouken@0
   159
	SDL_WM_IconifyWindow();
slouken@0
   160
}
slouken@0
   161
slouken@0
   162
void HotKey_Quit(void)
slouken@0
   163
{
slouken@0
   164
	SDL_Event event;
slouken@0
   165
slouken@0
   166
	printf("Posting internal quit request\n");
slouken@0
   167
	event.type = SDL_USEREVENT;
slouken@0
   168
	SDL_PushEvent(&event);
slouken@0
   169
}
slouken@0
   170
slouken@4257
   171
static void print_modifiers(void)
slouken@4257
   172
{
slouken@4257
   173
	int mod;
slouken@4257
   174
	printf(" modifiers:");
slouken@4257
   175
	mod = SDL_GetModState();
slouken@4257
   176
	if(!mod) {
slouken@4257
   177
		printf(" (none)");
slouken@4257
   178
		return;
slouken@4257
   179
	}
slouken@4257
   180
	if(mod & KMOD_LSHIFT)
slouken@4257
   181
		printf(" LSHIFT");
slouken@4257
   182
	if(mod & KMOD_RSHIFT)
slouken@4257
   183
		printf(" RSHIFT");
slouken@4257
   184
	if(mod & KMOD_LCTRL)
slouken@4257
   185
		printf(" LCTRL");
slouken@4257
   186
	if(mod & KMOD_RCTRL)
slouken@4257
   187
		printf(" RCTRL");
slouken@4257
   188
	if(mod & KMOD_LALT)
slouken@4257
   189
		printf(" LALT");
slouken@4257
   190
	if(mod & KMOD_RALT)
slouken@4257
   191
		printf(" RALT");
slouken@4257
   192
	if(mod & KMOD_LMETA)
slouken@4257
   193
		printf(" LMETA");
slouken@4257
   194
	if(mod & KMOD_RMETA)
slouken@4257
   195
		printf(" RMETA");
slouken@4257
   196
	if(mod & KMOD_NUM)
slouken@4257
   197
		printf(" NUM");
slouken@4257
   198
	if(mod & KMOD_CAPS)
slouken@4257
   199
		printf(" CAPS");
slouken@4257
   200
	if(mod & KMOD_MODE)
slouken@4257
   201
		printf(" MODE");
slouken@4257
   202
}
slouken@4257
   203
slouken@4257
   204
static void PrintKey(const SDL_keysym *sym, int pressed)
slouken@4257
   205
{
slouken@4257
   206
	/* Print the keycode, name and state */
slouken@4257
   207
	if ( sym->sym ) {
slouken@4257
   208
		printf("Key %s:  %d-%s ", pressed ?  "pressed" : "released",
slouken@4257
   209
					sym->sym, SDL_GetKeyName(sym->sym));
slouken@4257
   210
	} else {
slouken@4257
   211
		printf("Unknown Key (scancode = %d) %s ", sym->scancode,
slouken@4257
   212
					pressed ?  "pressed" : "released");
slouken@4257
   213
	}
slouken@4257
   214
slouken@4257
   215
	/* Print the translated character, if one exists */
slouken@4257
   216
	if ( sym->unicode ) {
slouken@4257
   217
		/* Is it a control-character? */
slouken@4257
   218
		if ( sym->unicode < ' ' ) {
slouken@4257
   219
			printf(" (^%c)", sym->unicode+'@');
slouken@4257
   220
		} else {
slouken@4257
   221
#ifdef UNICODE
slouken@4257
   222
			printf(" (%c)", sym->unicode);
slouken@4257
   223
#else
slouken@4257
   224
			/* This is a Latin-1 program, so only show 8-bits */
slouken@4257
   225
			if ( !(sym->unicode & 0xFF00) )
slouken@4257
   226
				printf(" (%c)", sym->unicode);
slouken@4257
   227
			else
slouken@4257
   228
				printf(" (0x%X)", sym->unicode);
slouken@4257
   229
#endif
slouken@4257
   230
		}
slouken@4257
   231
	}
slouken@4257
   232
	print_modifiers();
slouken@4257
   233
	printf("\n");
slouken@4257
   234
}
slouken@4257
   235
slouken@1769
   236
int SDLCALL FilterEvents(const SDL_Event *event)
slouken@0
   237
{
slouken@0
   238
	static int reallyquit = 0;
slouken@0
   239
slouken@0
   240
	switch (event->type) {
slouken@0
   241
slouken@0
   242
		case SDL_ACTIVEEVENT:
slouken@0
   243
			/* See what happened */
slouken@0
   244
			printf("App %s ",
slouken@0
   245
				event->active.gain ? "gained" : "lost");
slouken@0
   246
			if ( event->active.state & SDL_APPACTIVE )
slouken@0
   247
				printf("active ");
slouken@1779
   248
			if ( event->active.state & SDL_APPINPUTFOCUS )
slouken@1779
   249
				printf("input ");
slouken@0
   250
			if ( event->active.state & SDL_APPMOUSEFOCUS )
slouken@0
   251
				printf("mouse ");
slouken@0
   252
			printf("focus\n");
slouken@0
   253
slouken@0
   254
			/* See if we are iconified or restored */
slouken@0
   255
			if ( event->active.state & SDL_APPACTIVE ) {
slouken@0
   256
				printf("App has been %s\n",
slouken@0
   257
					event->active.gain ?
slouken@0
   258
						 "restored" : "iconified");
slouken@0
   259
			}
slouken@0
   260
			return(0);
slouken@0
   261
slouken@0
   262
		/* We want to toggle visibility on buttonpress */
slouken@0
   263
		case SDL_MOUSEBUTTONDOWN:
slouken@0
   264
		case SDL_MOUSEBUTTONUP:
slouken@0
   265
			if ( event->button.state == SDL_PRESSED ) {
slouken@0
   266
				visible = !visible;
slouken@0
   267
				SDL_ShowCursor(visible);
slouken@0
   268
			}
slouken@0
   269
			printf("Mouse button %d has been %s\n",
slouken@0
   270
				event->button.button,
slouken@0
   271
				(event->button.state == SDL_PRESSED) ?
slouken@0
   272
						"pressed" : "released");
slouken@0
   273
			return(0);
slouken@0
   274
slouken@0
   275
		/* Show relative mouse motion */
slouken@0
   276
		case SDL_MOUSEMOTION:
slouken@0
   277
#if 0
slouken@1284
   278
			printf("Mouse motion: {%d,%d} (%d,%d)\n",
slouken@1284
   279
				event->motion.x, event->motion.y,
slouken@0
   280
				event->motion.xrel, event->motion.yrel);
slouken@0
   281
#endif
slouken@0
   282
			return(0);
slouken@0
   283
slouken@0
   284
		case SDL_KEYDOWN:
slouken@4257
   285
			PrintKey(&event->key.keysym, 1);
slouken@0
   286
			if ( event->key.keysym.sym == SDLK_ESCAPE ) {
slouken@0
   287
				HotKey_Quit();
slouken@0
   288
			}
slouken@0
   289
			if ( (event->key.keysym.sym == SDLK_g) &&
slouken@0
   290
			     (event->key.keysym.mod & KMOD_CTRL) ) {
slouken@0
   291
				HotKey_ToggleGrab();
slouken@0
   292
			}
slouken@0
   293
			if ( (event->key.keysym.sym == SDLK_z) &&
slouken@0
   294
			     (event->key.keysym.mod & KMOD_CTRL) ) {
slouken@0
   295
				HotKey_Iconify();
slouken@0
   296
			}
slouken@0
   297
			if ( (event->key.keysym.sym == SDLK_RETURN) &&
slouken@0
   298
			     (event->key.keysym.mod & KMOD_ALT) ) {
slouken@0
   299
				HotKey_ToggleFullScreen();
slouken@0
   300
			}
slouken@0
   301
			return(0);
slouken@0
   302
slouken@4257
   303
		case SDL_KEYUP:
slouken@4257
   304
			PrintKey(&event->key.keysym, 0);
slouken@4257
   305
			return(0);
slouken@4257
   306
slouken@0
   307
		/* Pass the video resize event through .. */
slouken@0
   308
		case SDL_VIDEORESIZE:
slouken@0
   309
			return(1);
slouken@0
   310
slouken@0
   311
		/* This is important!  Queue it if we want to quit. */
slouken@0
   312
		case SDL_QUIT:
slouken@0
   313
			if ( ! reallyquit ) {
slouken@0
   314
				reallyquit = 1;
slouken@0
   315
				printf("Quit requested\n");
slouken@0
   316
				return(0);
slouken@0
   317
			}
slouken@0
   318
			printf("Quit demanded\n");
slouken@0
   319
			return(1);
slouken@0
   320
slouken@0
   321
		/* This will never happen because events queued directly
slouken@0
   322
		   to the event queue are not filtered.
slouken@0
   323
		 */
slouken@0
   324
		case SDL_USEREVENT:
slouken@0
   325
			return(1);
slouken@0
   326
slouken@0
   327
		/* Drop all other events */
slouken@0
   328
		default:
slouken@0
   329
			return(0);
slouken@0
   330
	}
slouken@0
   331
}
slouken@0
   332
slouken@0
   333
int main(int argc, char *argv[])
slouken@0
   334
{
slouken@0
   335
	SDL_Event event;
slouken@0
   336
	char *title;
slouken@0
   337
	SDL_Surface *icon;
slouken@0
   338
	Uint8 *icon_mask;
slouken@0
   339
	int parsed;
slouken@87
   340
	int w, h;
slouken@0
   341
slouken@0
   342
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
   343
		fprintf(stderr,
slouken@0
   344
			"Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
   345
		return(1);
slouken@0
   346
	}
slouken@0
   347
slouken@0
   348
	/* Check command line arguments */
slouken@87
   349
	w = 640;
slouken@87
   350
	h = 480;
slouken@0
   351
	video_bpp = 8;
slouken@0
   352
	video_flags = SDL_SWSURFACE;
slouken@0
   353
	parsed = 1;
slouken@0
   354
	while ( parsed ) {
slouken@0
   355
		if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
slouken@0
   356
			video_flags |= SDL_FULLSCREEN;
slouken@0
   357
			argc -= 1;
slouken@0
   358
			argv += 1;
slouken@0
   359
		} else
slouken@0
   360
		if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
slouken@0
   361
			video_flags |= SDL_RESIZABLE;
slouken@0
   362
			argc -= 1;
slouken@0
   363
			argv += 1;
slouken@0
   364
		} else
slouken@0
   365
		if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
slouken@0
   366
			video_flags |= SDL_NOFRAME;
slouken@0
   367
			argc -= 1;
slouken@0
   368
			argv += 1;
slouken@0
   369
		} else
slouken@87
   370
		if ( (argc >= 3) && (strcmp(argv[1], "-width") == 0) ) {
slouken@87
   371
			w = atoi(argv[2]);
slouken@87
   372
			argc -= 2;
slouken@87
   373
			argv += 2;
slouken@87
   374
		} else
slouken@87
   375
		if ( (argc >= 3) && (strcmp(argv[1], "-height") == 0) ) {
slouken@87
   376
			h = atoi(argv[2]);
slouken@87
   377
			argc -= 2;
slouken@87
   378
			argv += 2;
slouken@87
   379
		} else
slouken@0
   380
		if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
slouken@0
   381
			video_bpp = atoi(argv[2]);
slouken@0
   382
			argc -= 2;
slouken@0
   383
			argv += 2;
slouken@0
   384
		} else {
slouken@0
   385
			parsed = 0;
slouken@0
   386
		}
slouken@0
   387
	}
slouken@0
   388
slouken@0
   389
	/* Set the icon -- this must be done before the first mode set */
slouken@0
   390
	icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@0
   391
	if ( icon != NULL ) {
slouken@0
   392
		SDL_WM_SetIcon(icon, icon_mask);
slouken@0
   393
	}
slouken@0
   394
	if ( icon_mask != NULL )
slouken@0
   395
		free(icon_mask);
slouken@0
   396
slouken@0
   397
	/* Set the title bar */
slouken@0
   398
	if ( argv[1] == NULL )
slouken@0
   399
		title = "Testing  1.. 2.. 3...";
slouken@0
   400
	else
slouken@0
   401
		title = argv[1];
slouken@0
   402
	SDL_WM_SetCaption(title, "testwm");
slouken@0
   403
slouken@0
   404
	/* See if it's really set */
slouken@0
   405
	SDL_WM_GetCaption(&title, NULL);
slouken@0
   406
	if ( title )
slouken@0
   407
		printf("Title was set to: %s\n", title);
slouken@0
   408
	else
slouken@0
   409
		printf("No window title was set!\n");
slouken@0
   410
slouken@0
   411
	/* Initialize the display */
slouken@87
   412
	if ( SetVideoMode(w, h) < 0 ) {
icculus@1151
   413
		quit(1);
slouken@0
   414
	}
slouken@0
   415
slouken@0
   416
	/* Set an event filter that discards everything but QUIT */
slouken@0
   417
	SDL_SetEventFilter(FilterEvents);
slouken@0
   418
slouken@0
   419
	/* Loop, waiting for QUIT */
slouken@0
   420
	while ( SDL_WaitEvent(&event) ) {
slouken@0
   421
		switch (event.type) {
slouken@0
   422
			case SDL_VIDEORESIZE:
slouken@0
   423
				printf("Got a resize event: %dx%d\n",
slouken@0
   424
				       event.resize.w, event.resize.h);
slouken@0
   425
				SetVideoMode(event.resize.w, event.resize.h);
slouken@0
   426
				break;
slouken@0
   427
			case SDL_USEREVENT:
slouken@0
   428
				printf("Handling internal quit request\n");
slouken@0
   429
				/* Fall through to the quit handler */
slouken@0
   430
			case SDL_QUIT:
slouken@0
   431
				printf("Bye bye..\n");
icculus@1151
   432
				quit(0);
slouken@0
   433
			default:
slouken@0
   434
				/* This should never happen */
slouken@0
   435
				printf("Warning: Event %d wasn't filtered\n",
slouken@0
   436
								event.type);
slouken@0
   437
				break;
slouken@0
   438
		}
slouken@0
   439
	}
slouken@0
   440
	printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
icculus@1151
   441
	SDL_Quit();
slouken@0
   442
	return(255);
slouken@0
   443
}