test/testwm.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 07:32:26 +0000
branchSDL-1.2
changeset 4209 62e86ab81e3c
parent 1779 67fc81efcfc3
child 1895 c121d94672cb
child 4257 14195cfdb66e
permissions -rw-r--r--
Fixed bug #570

SDL_SemWaitTimeout in src/thread/generic/SDL_syssem.c line 179 (SVN trunk):

--sem->count;

should be

if (retval == 0) {
--sem->count;
}

Without this, sem->count will underflow on timeout effectively breaking the
semaphore. It appears that the implementation has been wrong since the initial
revision.
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@1769
   171
int SDLCALL FilterEvents(const SDL_Event *event)
slouken@0
   172
{
slouken@0
   173
	static int reallyquit = 0;
slouken@0
   174
slouken@0
   175
	switch (event->type) {
slouken@0
   176
slouken@0
   177
		case SDL_ACTIVEEVENT:
slouken@0
   178
			/* See what happened */
slouken@0
   179
			printf("App %s ",
slouken@0
   180
				event->active.gain ? "gained" : "lost");
slouken@0
   181
			if ( event->active.state & SDL_APPACTIVE )
slouken@0
   182
				printf("active ");
slouken@1779
   183
			if ( event->active.state & SDL_APPINPUTFOCUS )
slouken@1779
   184
				printf("input ");
slouken@0
   185
			if ( event->active.state & SDL_APPMOUSEFOCUS )
slouken@0
   186
				printf("mouse ");
slouken@0
   187
			printf("focus\n");
slouken@0
   188
slouken@0
   189
			/* See if we are iconified or restored */
slouken@0
   190
			if ( event->active.state & SDL_APPACTIVE ) {
slouken@0
   191
				printf("App has been %s\n",
slouken@0
   192
					event->active.gain ?
slouken@0
   193
						 "restored" : "iconified");
slouken@0
   194
			}
slouken@0
   195
			return(0);
slouken@0
   196
slouken@0
   197
		/* We want to toggle visibility on buttonpress */
slouken@0
   198
		case SDL_MOUSEBUTTONDOWN:
slouken@0
   199
		case SDL_MOUSEBUTTONUP:
slouken@0
   200
			if ( event->button.state == SDL_PRESSED ) {
slouken@0
   201
				visible = !visible;
slouken@0
   202
				SDL_ShowCursor(visible);
slouken@0
   203
			}
slouken@0
   204
			printf("Mouse button %d has been %s\n",
slouken@0
   205
				event->button.button,
slouken@0
   206
				(event->button.state == SDL_PRESSED) ?
slouken@0
   207
						"pressed" : "released");
slouken@0
   208
			return(0);
slouken@0
   209
slouken@0
   210
		/* Show relative mouse motion */
slouken@0
   211
		case SDL_MOUSEMOTION:
slouken@0
   212
#if 0
slouken@1284
   213
			printf("Mouse motion: {%d,%d} (%d,%d)\n",
slouken@1284
   214
				event->motion.x, event->motion.y,
slouken@0
   215
				event->motion.xrel, event->motion.yrel);
slouken@0
   216
#endif
slouken@0
   217
			return(0);
slouken@0
   218
slouken@0
   219
		case SDL_KEYDOWN:
slouken@0
   220
			if ( event->key.keysym.sym == SDLK_ESCAPE ) {
slouken@0
   221
				HotKey_Quit();
slouken@0
   222
			}
slouken@0
   223
			if ( (event->key.keysym.sym == SDLK_g) &&
slouken@0
   224
			     (event->key.keysym.mod & KMOD_CTRL) ) {
slouken@0
   225
				HotKey_ToggleGrab();
slouken@0
   226
			}
slouken@0
   227
			if ( (event->key.keysym.sym == SDLK_z) &&
slouken@0
   228
			     (event->key.keysym.mod & KMOD_CTRL) ) {
slouken@0
   229
				HotKey_Iconify();
slouken@0
   230
			}
slouken@0
   231
			if ( (event->key.keysym.sym == SDLK_RETURN) &&
slouken@0
   232
			     (event->key.keysym.mod & KMOD_ALT) ) {
slouken@0
   233
				HotKey_ToggleFullScreen();
slouken@0
   234
			}
slouken@0
   235
			return(0);
slouken@0
   236
slouken@0
   237
		/* Pass the video resize event through .. */
slouken@0
   238
		case SDL_VIDEORESIZE:
slouken@0
   239
			return(1);
slouken@0
   240
slouken@0
   241
		/* This is important!  Queue it if we want to quit. */
slouken@0
   242
		case SDL_QUIT:
slouken@0
   243
			if ( ! reallyquit ) {
slouken@0
   244
				reallyquit = 1;
slouken@0
   245
				printf("Quit requested\n");
slouken@0
   246
				return(0);
slouken@0
   247
			}
slouken@0
   248
			printf("Quit demanded\n");
slouken@0
   249
			return(1);
slouken@0
   250
slouken@0
   251
		/* This will never happen because events queued directly
slouken@0
   252
		   to the event queue are not filtered.
slouken@0
   253
		 */
slouken@0
   254
		case SDL_USEREVENT:
slouken@0
   255
			return(1);
slouken@0
   256
slouken@0
   257
		/* Drop all other events */
slouken@0
   258
		default:
slouken@0
   259
			return(0);
slouken@0
   260
	}
slouken@0
   261
}
slouken@0
   262
slouken@0
   263
int main(int argc, char *argv[])
slouken@0
   264
{
slouken@0
   265
	SDL_Event event;
slouken@0
   266
	char *title;
slouken@0
   267
	SDL_Surface *icon;
slouken@0
   268
	Uint8 *icon_mask;
slouken@0
   269
	int parsed;
slouken@87
   270
	int w, h;
slouken@0
   271
slouken@0
   272
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
   273
		fprintf(stderr,
slouken@0
   274
			"Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
   275
		return(1);
slouken@0
   276
	}
slouken@0
   277
slouken@0
   278
	/* Check command line arguments */
slouken@87
   279
	w = 640;
slouken@87
   280
	h = 480;
slouken@0
   281
	video_bpp = 8;
slouken@0
   282
	video_flags = SDL_SWSURFACE;
slouken@0
   283
	parsed = 1;
slouken@0
   284
	while ( parsed ) {
slouken@0
   285
		if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
slouken@0
   286
			video_flags |= SDL_FULLSCREEN;
slouken@0
   287
			argc -= 1;
slouken@0
   288
			argv += 1;
slouken@0
   289
		} else
slouken@0
   290
		if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
slouken@0
   291
			video_flags |= SDL_RESIZABLE;
slouken@0
   292
			argc -= 1;
slouken@0
   293
			argv += 1;
slouken@0
   294
		} else
slouken@0
   295
		if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
slouken@0
   296
			video_flags |= SDL_NOFRAME;
slouken@0
   297
			argc -= 1;
slouken@0
   298
			argv += 1;
slouken@0
   299
		} else
slouken@87
   300
		if ( (argc >= 3) && (strcmp(argv[1], "-width") == 0) ) {
slouken@87
   301
			w = atoi(argv[2]);
slouken@87
   302
			argc -= 2;
slouken@87
   303
			argv += 2;
slouken@87
   304
		} else
slouken@87
   305
		if ( (argc >= 3) && (strcmp(argv[1], "-height") == 0) ) {
slouken@87
   306
			h = atoi(argv[2]);
slouken@87
   307
			argc -= 2;
slouken@87
   308
			argv += 2;
slouken@87
   309
		} else
slouken@0
   310
		if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
slouken@0
   311
			video_bpp = atoi(argv[2]);
slouken@0
   312
			argc -= 2;
slouken@0
   313
			argv += 2;
slouken@0
   314
		} else {
slouken@0
   315
			parsed = 0;
slouken@0
   316
		}
slouken@0
   317
	}
slouken@0
   318
slouken@0
   319
	/* Set the icon -- this must be done before the first mode set */
slouken@0
   320
	icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@0
   321
	if ( icon != NULL ) {
slouken@0
   322
		SDL_WM_SetIcon(icon, icon_mask);
slouken@0
   323
	}
slouken@0
   324
	if ( icon_mask != NULL )
slouken@0
   325
		free(icon_mask);
slouken@0
   326
slouken@0
   327
	/* Set the title bar */
slouken@0
   328
	if ( argv[1] == NULL )
slouken@0
   329
		title = "Testing  1.. 2.. 3...";
slouken@0
   330
	else
slouken@0
   331
		title = argv[1];
slouken@0
   332
	SDL_WM_SetCaption(title, "testwm");
slouken@0
   333
slouken@0
   334
	/* See if it's really set */
slouken@0
   335
	SDL_WM_GetCaption(&title, NULL);
slouken@0
   336
	if ( title )
slouken@0
   337
		printf("Title was set to: %s\n", title);
slouken@0
   338
	else
slouken@0
   339
		printf("No window title was set!\n");
slouken@0
   340
slouken@0
   341
	/* Initialize the display */
slouken@87
   342
	if ( SetVideoMode(w, h) < 0 ) {
icculus@1151
   343
		quit(1);
slouken@0
   344
	}
slouken@0
   345
slouken@0
   346
	/* Set an event filter that discards everything but QUIT */
slouken@0
   347
	SDL_SetEventFilter(FilterEvents);
slouken@0
   348
slouken@0
   349
	/* Ignore key up events, they don't even get filtered */
slouken@0
   350
	SDL_EventState(SDL_KEYUP, SDL_IGNORE);
slouken@0
   351
slouken@0
   352
	/* Loop, waiting for QUIT */
slouken@0
   353
	while ( SDL_WaitEvent(&event) ) {
slouken@0
   354
		switch (event.type) {
slouken@0
   355
			case SDL_VIDEORESIZE:
slouken@0
   356
				printf("Got a resize event: %dx%d\n",
slouken@0
   357
				       event.resize.w, event.resize.h);
slouken@0
   358
				SetVideoMode(event.resize.w, event.resize.h);
slouken@0
   359
				break;
slouken@0
   360
			case SDL_USEREVENT:
slouken@0
   361
				printf("Handling internal quit request\n");
slouken@0
   362
				/* Fall through to the quit handler */
slouken@0
   363
			case SDL_QUIT:
slouken@0
   364
				printf("Bye bye..\n");
icculus@1151
   365
				quit(0);
slouken@0
   366
			default:
slouken@0
   367
				/* This should never happen */
slouken@0
   368
				printf("Warning: Event %d wasn't filtered\n",
slouken@0
   369
								event.type);
slouken@0
   370
				break;
slouken@0
   371
		}
slouken@0
   372
	}
slouken@0
   373
	printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
icculus@1151
   374
	SDL_Quit();
slouken@0
   375
	return(255);
slouken@0
   376
}