test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 07:32:26 +0000
branchSDL-1.2
changeset 4209 62e86ab81e3c
parent 1253 7c7ddaf195bf
child 1662 782fd950bd46
child 1895 c121d94672cb
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
/* Simple program:  Loop, watching keystrokes
slouken@0
     3
   Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to 
slouken@0
     4
   pump the event loop and catch keystrokes.
slouken@0
     5
*/
slouken@0
     6
slouken@0
     7
#include <stdio.h>
slouken@0
     8
#include <stdlib.h>
slouken@0
     9
#include <string.h>
slouken@0
    10
slouken@0
    11
#include "SDL.h"
slouken@0
    12
icculus@1151
    13
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    14
static void quit(int rc)
icculus@1151
    15
{
icculus@1151
    16
	SDL_Quit();
icculus@1151
    17
	exit(rc);
icculus@1151
    18
}
icculus@1151
    19
slouken@0
    20
static void print_modifiers(void)
slouken@0
    21
{
slouken@0
    22
	int mod;
slouken@0
    23
	printf(" modifiers:");
slouken@0
    24
	mod = SDL_GetModState();
slouken@0
    25
	if(!mod) {
slouken@0
    26
		printf(" (none)");
slouken@0
    27
		return;
slouken@0
    28
	}
slouken@0
    29
	if(mod & KMOD_LSHIFT)
slouken@0
    30
		printf(" LSHIFT");
slouken@0
    31
	if(mod & KMOD_RSHIFT)
slouken@0
    32
		printf(" RSHIFT");
slouken@0
    33
	if(mod & KMOD_LCTRL)
slouken@0
    34
		printf(" LCTRL");
slouken@0
    35
	if(mod & KMOD_RCTRL)
slouken@0
    36
		printf(" RCTRL");
slouken@0
    37
	if(mod & KMOD_LALT)
slouken@0
    38
		printf(" LALT");
slouken@0
    39
	if(mod & KMOD_RALT)
slouken@0
    40
		printf(" RALT");
slouken@0
    41
	if(mod & KMOD_LMETA)
slouken@0
    42
		printf(" LMETA");
slouken@0
    43
	if(mod & KMOD_RMETA)
slouken@0
    44
		printf(" RMETA");
slouken@0
    45
	if(mod & KMOD_NUM)
slouken@0
    46
		printf(" NUM");
slouken@0
    47
	if(mod & KMOD_CAPS)
slouken@0
    48
		printf(" CAPS");
slouken@0
    49
	if(mod & KMOD_MODE)
slouken@0
    50
		printf(" MODE");
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
static void PrintKey(SDL_keysym *sym, int pressed)
slouken@0
    54
{
slouken@0
    55
	/* Print the keycode, name and state */
slouken@0
    56
	if ( sym->sym ) {
slouken@0
    57
		printf("Key %s:  %d-%s ", pressed ?  "pressed" : "released",
slouken@0
    58
					sym->sym, SDL_GetKeyName(sym->sym));
slouken@0
    59
	} else {
slouken@0
    60
		printf("Unknown Key (scancode = %d) %s ", sym->scancode,
slouken@0
    61
					pressed ?  "pressed" : "released");
slouken@0
    62
	}
slouken@0
    63
slouken@0
    64
	/* Print the translated character, if one exists */
slouken@0
    65
	if ( sym->unicode ) {
slouken@0
    66
		/* Is it a control-character? */
slouken@0
    67
		if ( sym->unicode < ' ' ) {
slouken@0
    68
			printf(" (^%c)", sym->unicode+'@');
slouken@0
    69
		} else {
slouken@0
    70
#ifdef UNICODE
slouken@0
    71
			printf(" (%c)", sym->unicode);
slouken@0
    72
#else
slouken@0
    73
			/* This is a Latin-1 program, so only show 8-bits */
slouken@0
    74
			if ( !(sym->unicode & 0xFF00) )
slouken@0
    75
				printf(" (%c)", sym->unicode);
slouken@1253
    76
			else
slouken@1253
    77
				printf(" (0x%X)", sym->unicode);
slouken@0
    78
#endif
slouken@0
    79
		}
slouken@0
    80
	}
slouken@0
    81
	print_modifiers();
slouken@0
    82
	printf("\n");
slouken@0
    83
}
slouken@0
    84
slouken@0
    85
int main(int argc, char *argv[])
slouken@0
    86
{
slouken@0
    87
	SDL_Event event;
slouken@0
    88
	int done;
slouken@473
    89
	Uint32 videoflags;
slouken@0
    90
slouken@0
    91
	/* Initialize SDL */
slouken@0
    92
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@0
    93
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    94
		return(1);
slouken@0
    95
	}
slouken@0
    96
slouken@473
    97
	videoflags = SDL_SWSURFACE;
slouken@493
    98
	while( argc > 1 ) {
slouken@493
    99
		--argc;
slouken@493
   100
		if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
slouken@493
   101
			videoflags |= SDL_FULLSCREEN;
slouken@493
   102
		} else {
slouken@493
   103
			fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
icculus@1151
   104
			quit(1);
slouken@493
   105
		}
slouken@473
   106
	}
slouken@473
   107
slouken@0
   108
	/* Set 640x480 video mode */
slouken@473
   109
	if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
slouken@0
   110
		fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
slouken@0
   111
							SDL_GetError());
icculus@1151
   112
		quit(2);
slouken@0
   113
	}
slouken@0
   114
slouken@0
   115
	/* Enable UNICODE translation for keyboard input */
slouken@0
   116
	SDL_EnableUNICODE(1);
slouken@0
   117
slouken@0
   118
	/* Enable auto repeat for keyboard input */
slouken@0
   119
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
slouken@0
   120
	                    SDL_DEFAULT_REPEAT_INTERVAL);
slouken@0
   121
slouken@0
   122
	/* Watch keystrokes */
slouken@0
   123
	done = 0;
slouken@0
   124
	while ( !done ) {
slouken@0
   125
		/* Check for events */
slouken@0
   126
		SDL_WaitEvent(&event);
slouken@0
   127
		switch (event.type) {
slouken@0
   128
			case SDL_KEYDOWN:
slouken@0
   129
				PrintKey(&event.key.keysym, 1);
slouken@0
   130
				break;
slouken@0
   131
			case SDL_KEYUP:
slouken@0
   132
				PrintKey(&event.key.keysym, 0);
slouken@0
   133
				break;
slouken@0
   134
			case SDL_MOUSEBUTTONDOWN:
slouken@0
   135
				/* Any button press quits the app... */
slouken@0
   136
			case SDL_QUIT:
slouken@0
   137
				done = 1;
slouken@0
   138
				break;
slouken@0
   139
			default:
slouken@0
   140
				break;
slouken@0
   141
		}
slouken@0
   142
	}
icculus@1151
   143
icculus@1151
   144
	SDL_Quit();
slouken@0
   145
	return(0);
slouken@0
   146
}