test/testwm.c
changeset 0 74212992fb08
child 87 3ef4bc90c388
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testwm.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,350 @@
     1.4 +
     1.5 +/* Test out the window manager interaction functions */
     1.6 +
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +
    1.13 +/* Is the cursor visible? */
    1.14 +static int visible = 1;
    1.15 +
    1.16 +SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
    1.17 +{
    1.18 +	SDL_Surface *icon;
    1.19 +	Uint8       *pixels;
    1.20 +	Uint8       *mask;
    1.21 +	int          mlen, i;
    1.22 +
    1.23 +	*maskp = NULL;
    1.24 +
    1.25 +	/* Load the icon surface */
    1.26 +	icon = SDL_LoadBMP(file);
    1.27 +	if ( icon == NULL ) {
    1.28 +		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    1.29 +		return(NULL);
    1.30 +	}
    1.31 +
    1.32 +	/* Check width and height */
    1.33 +	if ( (icon->w%8) != 0 ) {
    1.34 +		fprintf(stderr, "Icon width must be a multiple of 8!\n");
    1.35 +		SDL_FreeSurface(icon);
    1.36 +		return(NULL);
    1.37 +	}
    1.38 +	if ( icon->format->palette == NULL ) {
    1.39 +		fprintf(stderr, "Icon must have a palette!\n");
    1.40 +		SDL_FreeSurface(icon);
    1.41 +		return(NULL);
    1.42 +	}
    1.43 +
    1.44 +	/* Set the colorkey */
    1.45 +	SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
    1.46 +
    1.47 +	/* Create the mask */
    1.48 +	pixels = (Uint8 *)icon->pixels;
    1.49 +	printf("Transparent pixel: (%d,%d,%d)\n",
    1.50 +				icon->format->palette->colors[*pixels].r,
    1.51 +				icon->format->palette->colors[*pixels].g,
    1.52 +				icon->format->palette->colors[*pixels].b);
    1.53 +	mlen = icon->w*icon->h;
    1.54 +	mask = (Uint8 *)malloc(mlen/8);
    1.55 +	if ( mask == NULL ) {
    1.56 +		fprintf(stderr, "Out of memory!\n");
    1.57 +		SDL_FreeSurface(icon);
    1.58 +		return(NULL);
    1.59 +	}
    1.60 +	memset(mask, 0, mlen/8);
    1.61 +	for ( i=0; i<mlen; ) {
    1.62 +		if ( pixels[i] != *pixels )
    1.63 +			mask[i/8] |= 0x01;
    1.64 +		++i;
    1.65 +		if ( (i%8) != 0 )
    1.66 +			mask[i/8] <<= 1;
    1.67 +	}
    1.68 +	*maskp = mask;
    1.69 +	return(icon);
    1.70 +}
    1.71 +
    1.72 +void HotKey_ToggleFullScreen(void)
    1.73 +{
    1.74 +	SDL_Surface *screen;
    1.75 +
    1.76 +	screen = SDL_GetVideoSurface();
    1.77 +	if ( SDL_WM_ToggleFullScreen(screen) ) {
    1.78 +		printf("Toggled fullscreen mode - now %s\n",
    1.79 +		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    1.80 +	} else {
    1.81 +		printf("Unable to toggle fullscreen mode\n");
    1.82 +	}
    1.83 +}
    1.84 +
    1.85 +void HotKey_ToggleGrab(void)
    1.86 +{
    1.87 +	SDL_GrabMode mode;
    1.88 +
    1.89 +	printf("Ctrl-G: toggling input grab!\n");
    1.90 +	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
    1.91 +	if ( mode == SDL_GRAB_ON ) {
    1.92 +		printf("Grab was on\n");
    1.93 +	} else {
    1.94 +		printf("Grab was off\n");
    1.95 +	}
    1.96 +	mode = SDL_WM_GrabInput(!mode);
    1.97 +	if ( mode == SDL_GRAB_ON ) {
    1.98 +		printf("Grab is now on\n");
    1.99 +	} else {
   1.100 +		printf("Grab is now off\n");
   1.101 +	}
   1.102 +}
   1.103 +
   1.104 +void HotKey_Iconify(void)
   1.105 +{
   1.106 +	printf("Ctrl-Z: iconifying window!\n");
   1.107 +	SDL_WM_IconifyWindow();
   1.108 +}
   1.109 +
   1.110 +void HotKey_Quit(void)
   1.111 +{
   1.112 +	SDL_Event event;
   1.113 +
   1.114 +	printf("Posting internal quit request\n");
   1.115 +	event.type = SDL_USEREVENT;
   1.116 +	SDL_PushEvent(&event);
   1.117 +}
   1.118 +
   1.119 +int FilterEvents(const SDL_Event *event)
   1.120 +{
   1.121 +	static int reallyquit = 0;
   1.122 +
   1.123 +	switch (event->type) {
   1.124 +
   1.125 +		case SDL_ACTIVEEVENT:
   1.126 +			/* See what happened */
   1.127 +			printf("App %s ",
   1.128 +				event->active.gain ? "gained" : "lost");
   1.129 +			if ( event->active.state & SDL_APPACTIVE )
   1.130 +				printf("active ");
   1.131 +			if ( event->active.state & SDL_APPMOUSEFOCUS )
   1.132 +				printf("mouse ");
   1.133 +			if ( event->active.state & SDL_APPINPUTFOCUS )
   1.134 +				printf("input ");
   1.135 +			printf("focus\n");
   1.136 +
   1.137 +			/* See if we are iconified or restored */
   1.138 +			if ( event->active.state & SDL_APPACTIVE ) {
   1.139 +				printf("App has been %s\n",
   1.140 +					event->active.gain ?
   1.141 +						 "restored" : "iconified");
   1.142 +			}
   1.143 +			return(0);
   1.144 +
   1.145 +		/* We want to toggle visibility on buttonpress */
   1.146 +		case SDL_MOUSEBUTTONDOWN:
   1.147 +		case SDL_MOUSEBUTTONUP:
   1.148 +			if ( event->button.state == SDL_PRESSED ) {
   1.149 +				visible = !visible;
   1.150 +				SDL_ShowCursor(visible);
   1.151 +			}
   1.152 +			printf("Mouse button %d has been %s\n",
   1.153 +				event->button.button,
   1.154 +				(event->button.state == SDL_PRESSED) ?
   1.155 +						"pressed" : "released");
   1.156 +			return(0);
   1.157 +
   1.158 +		/* Show relative mouse motion */
   1.159 +		case SDL_MOUSEMOTION:
   1.160 +#if 0
   1.161 +			printf("Mouse relative motion: {%d,%d}\n",
   1.162 +				event->motion.xrel, event->motion.yrel);
   1.163 +#endif
   1.164 +			return(0);
   1.165 +
   1.166 +		case SDL_KEYDOWN:
   1.167 +			if ( event->key.keysym.sym == SDLK_ESCAPE ) {
   1.168 +				HotKey_Quit();
   1.169 +			}
   1.170 +			if ( (event->key.keysym.sym == SDLK_g) &&
   1.171 +			     (event->key.keysym.mod & KMOD_CTRL) ) {
   1.172 +				HotKey_ToggleGrab();
   1.173 +			}
   1.174 +			if ( (event->key.keysym.sym == SDLK_z) &&
   1.175 +			     (event->key.keysym.mod & KMOD_CTRL) ) {
   1.176 +				HotKey_Iconify();
   1.177 +			}
   1.178 +			if ( (event->key.keysym.sym == SDLK_RETURN) &&
   1.179 +			     (event->key.keysym.mod & KMOD_ALT) ) {
   1.180 +				HotKey_ToggleFullScreen();
   1.181 +			}
   1.182 +			return(0);
   1.183 +
   1.184 +		/* Pass the video resize event through .. */
   1.185 +		case SDL_VIDEORESIZE:
   1.186 +			return(1);
   1.187 +
   1.188 +		/* This is important!  Queue it if we want to quit. */
   1.189 +		case SDL_QUIT:
   1.190 +			if ( ! reallyquit ) {
   1.191 +				reallyquit = 1;
   1.192 +				printf("Quit requested\n");
   1.193 +				return(0);
   1.194 +			}
   1.195 +			printf("Quit demanded\n");
   1.196 +			return(1);
   1.197 +
   1.198 +		/* This will never happen because events queued directly
   1.199 +		   to the event queue are not filtered.
   1.200 +		 */
   1.201 +		case SDL_USEREVENT:
   1.202 +			return(1);
   1.203 +
   1.204 +		/* Drop all other events */
   1.205 +		default:
   1.206 +			return(0);
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +static Uint8  video_bpp;
   1.211 +static Uint32 video_flags;
   1.212 +
   1.213 +int SetVideoMode(int w, int h)
   1.214 +{
   1.215 +	SDL_Surface *screen;
   1.216 +	int i;
   1.217 +	Uint8 *buffer;
   1.218 +	SDL_Color palette[256];
   1.219 +
   1.220 +	screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
   1.221 +	if (  screen == NULL ) {
   1.222 +		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.223 +					w, h, video_bpp, SDL_GetError());
   1.224 +		return(-1);
   1.225 +	}
   1.226 +	printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
   1.227 +						"fullscreen" : "windowed");
   1.228 +
   1.229 +	/* Set the surface pixels and refresh! */
   1.230 +	for ( i=0; i<256; ++i ) {
   1.231 +		palette[i].r = 255-i;
   1.232 +		palette[i].g = 255-i;
   1.233 +		palette[i].b = 255-i;
   1.234 +	}
   1.235 +	SDL_SetColors(screen, palette, 0, 256);
   1.236 +	if ( SDL_LockSurface(screen) < 0 ) {
   1.237 +		fprintf(stderr, "Couldn't lock display surface: %s\n",
   1.238 +							SDL_GetError());
   1.239 +		return(-1);
   1.240 +	}
   1.241 +	buffer = (Uint8 *)screen->pixels;
   1.242 +	for ( i=0; i<screen->h; ++i ) {
   1.243 +		memset(buffer,(i*255)/screen->h,
   1.244 +				screen->w*screen->format->BytesPerPixel);
   1.245 +		buffer += screen->pitch;
   1.246 +	}
   1.247 +	SDL_UnlockSurface(screen);
   1.248 +	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.249 +
   1.250 +	return(0);
   1.251 +}
   1.252 +
   1.253 +int main(int argc, char *argv[])
   1.254 +{
   1.255 +	SDL_Event event;
   1.256 +	char *title;
   1.257 +	SDL_Surface *icon;
   1.258 +	Uint8 *icon_mask;
   1.259 +	int parsed;
   1.260 +
   1.261 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.262 +		fprintf(stderr,
   1.263 +			"Couldn't initialize SDL: %s\n", SDL_GetError());
   1.264 +		exit(1);
   1.265 +	}
   1.266 +	atexit(SDL_Quit);
   1.267 +
   1.268 +	/* Check command line arguments */
   1.269 +	video_bpp = 8;
   1.270 +	video_flags = SDL_SWSURFACE;
   1.271 +	parsed = 1;
   1.272 +	while ( parsed ) {
   1.273 +		if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
   1.274 +			video_flags |= SDL_FULLSCREEN;
   1.275 +			argc -= 1;
   1.276 +			argv += 1;
   1.277 +		} else
   1.278 +		if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
   1.279 +			video_flags |= SDL_RESIZABLE;
   1.280 +			argc -= 1;
   1.281 +			argv += 1;
   1.282 +		} else
   1.283 +		if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
   1.284 +			video_flags |= SDL_NOFRAME;
   1.285 +			argc -= 1;
   1.286 +			argv += 1;
   1.287 +		} else
   1.288 +		if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
   1.289 +			video_bpp = atoi(argv[2]);
   1.290 +			argc -= 2;
   1.291 +			argv += 2;
   1.292 +		} else {
   1.293 +			parsed = 0;
   1.294 +		}
   1.295 +	}
   1.296 +
   1.297 +	/* Set the icon -- this must be done before the first mode set */
   1.298 +	icon = LoadIconSurface("icon.bmp", &icon_mask);
   1.299 +	if ( icon != NULL ) {
   1.300 +		SDL_WM_SetIcon(icon, icon_mask);
   1.301 +	}
   1.302 +	if ( icon_mask != NULL )
   1.303 +		free(icon_mask);
   1.304 +
   1.305 +	/* Set the title bar */
   1.306 +	if ( argv[1] == NULL )
   1.307 +		title = "Testing  1.. 2.. 3...";
   1.308 +	else
   1.309 +		title = argv[1];
   1.310 +	SDL_WM_SetCaption(title, "testwm");
   1.311 +
   1.312 +	/* See if it's really set */
   1.313 +	SDL_WM_GetCaption(&title, NULL);
   1.314 +	if ( title )
   1.315 +		printf("Title was set to: %s\n", title);
   1.316 +	else
   1.317 +		printf("No window title was set!\n");
   1.318 +
   1.319 +	/* Initialize the display */
   1.320 +	if ( SetVideoMode(640, 480) < 0 ) {
   1.321 +		return(1);
   1.322 +	}
   1.323 +
   1.324 +	/* Set an event filter that discards everything but QUIT */
   1.325 +	SDL_SetEventFilter(FilterEvents);
   1.326 +
   1.327 +	/* Ignore key up events, they don't even get filtered */
   1.328 +	SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   1.329 +
   1.330 +	/* Loop, waiting for QUIT */
   1.331 +	while ( SDL_WaitEvent(&event) ) {
   1.332 +		switch (event.type) {
   1.333 +			case SDL_VIDEORESIZE:
   1.334 +				printf("Got a resize event: %dx%d\n",
   1.335 +				       event.resize.w, event.resize.h);
   1.336 +				SetVideoMode(event.resize.w, event.resize.h);
   1.337 +				break;
   1.338 +			case SDL_USEREVENT:
   1.339 +				printf("Handling internal quit request\n");
   1.340 +				/* Fall through to the quit handler */
   1.341 +			case SDL_QUIT:
   1.342 +				printf("Bye bye..\n");
   1.343 +				return(0);
   1.344 +			default:
   1.345 +				/* This should never happen */
   1.346 +				printf("Warning: Event %d wasn't filtered\n",
   1.347 +								event.type);
   1.348 +				break;
   1.349 +		}
   1.350 +	}
   1.351 +	printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
   1.352 +	return(255);
   1.353 +}