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