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