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