Mercurial > sdl-ios-xcode
annotate test/checkkeys.c @ 1287:15a89a0c52bf
Date: Tue, 15 Feb 2005 21:28:48 +0900 (JST)
From: "Michael Leonhard"
Subject: [SDL] resize bug on Win32 and patch
This is my first post to this mailing list. In this email I will detail a
bug in the behavior of resizable SDL windows on Win32. Then I will
explain the solution and provide a patch.
Symptoms:
Under Windows, an SDL display created with the SDL_RESIZABLE flag exhibits
quirky behavior when being maximized. The window is resized to the proper
size, but it is shifted upwards about half the height of the title bar.
Similarly, a window whose origin is above the top of the screen will
spontaneously move its upper-left origin upon being resized. After two
such resize-induced moves, the title bar will be entirely off the top edge
of the screen. Subsequently, when the mouse is clicked and released on
the window border, the window will shrink its height spontaneously. This
height shrinkage occurs even if the user did not resize the border.
To observe this curious situation, please invoke:
SDL-1.2.8/test/testwm.exe -resize
Cause:
A pair of integers, SDL_windowX and SDL_windowY, are defined in
video/wincommon/SDL_sysevents.c. They are used by the DirectX video
driver and the DIB video driver:
video/windx5/SDL_dx5video.c
video/windib/SDL_dibvideo.c
As I understand the source code, the primary use of these variables is to
create a rectangle that represents the surface area in CLIENT SPACE.
Client space refers to a coordinate system that originates at the upper
left corner of a Win32 Window's drawable area. This is just inside the
window border and title bar. This client space rectangle, called bounds,
is subsequently converted to screen space with a call to
AdjustWindowRectEx. The problem is found in SDL's handling of the
WM_WINDOWPOSCHANGED message. According to MSDN,
"The WM_WINDOWPOSCHANGED message is sent to a window whose
size, position, or place in the Z order has changed as a
result of a call to the SetWindowPos function or another
window-management function."
I have confirmed that this message is indeed being sent to the SDL window
when the mouse is clicked on the window border, even if the window border
is not dragged.
In video/wincommon/SDL_sysevents.c, on line 464, in response to the
WM_WINDOWPOSCHANGED message, the (potentially) new client rectangle is
obtained. This rectangle is translated into screen coordinates and THEN
assigned to the SDL_windowX and Y variables. Thus screen coordinates are
being assigned to client coordinate variables. Once this is understood,
the solution is apparent: assign SDL_windowX and Y before translating the
rectangle to screen coordinates. This is accomplished by the following
patch.
-Mike_L
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 29 Jan 2006 08:50:06 +0000 |
parents | 7c7ddaf195bf |
children | 782fd950bd46 c121d94672cb |
rev | line source |
---|---|
0 | 1 |
2 /* Simple program: Loop, watching keystrokes | |
3 Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to | |
4 pump the event loop and catch keystrokes. | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 | |
11 #include "SDL.h" | |
12 | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
13 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
14 static void quit(int rc) |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
15 { |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
16 SDL_Quit(); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
17 exit(rc); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
18 } |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
19 |
0 | 20 static void print_modifiers(void) |
21 { | |
22 int mod; | |
23 printf(" modifiers:"); | |
24 mod = SDL_GetModState(); | |
25 if(!mod) { | |
26 printf(" (none)"); | |
27 return; | |
28 } | |
29 if(mod & KMOD_LSHIFT) | |
30 printf(" LSHIFT"); | |
31 if(mod & KMOD_RSHIFT) | |
32 printf(" RSHIFT"); | |
33 if(mod & KMOD_LCTRL) | |
34 printf(" LCTRL"); | |
35 if(mod & KMOD_RCTRL) | |
36 printf(" RCTRL"); | |
37 if(mod & KMOD_LALT) | |
38 printf(" LALT"); | |
39 if(mod & KMOD_RALT) | |
40 printf(" RALT"); | |
41 if(mod & KMOD_LMETA) | |
42 printf(" LMETA"); | |
43 if(mod & KMOD_RMETA) | |
44 printf(" RMETA"); | |
45 if(mod & KMOD_NUM) | |
46 printf(" NUM"); | |
47 if(mod & KMOD_CAPS) | |
48 printf(" CAPS"); | |
49 if(mod & KMOD_MODE) | |
50 printf(" MODE"); | |
51 } | |
52 | |
53 static void PrintKey(SDL_keysym *sym, int pressed) | |
54 { | |
55 /* Print the keycode, name and state */ | |
56 if ( sym->sym ) { | |
57 printf("Key %s: %d-%s ", pressed ? "pressed" : "released", | |
58 sym->sym, SDL_GetKeyName(sym->sym)); | |
59 } else { | |
60 printf("Unknown Key (scancode = %d) %s ", sym->scancode, | |
61 pressed ? "pressed" : "released"); | |
62 } | |
63 | |
64 /* Print the translated character, if one exists */ | |
65 if ( sym->unicode ) { | |
66 /* Is it a control-character? */ | |
67 if ( sym->unicode < ' ' ) { | |
68 printf(" (^%c)", sym->unicode+'@'); | |
69 } else { | |
70 #ifdef UNICODE | |
71 printf(" (%c)", sym->unicode); | |
72 #else | |
73 /* This is a Latin-1 program, so only show 8-bits */ | |
74 if ( !(sym->unicode & 0xFF00) ) | |
75 printf(" (%c)", sym->unicode); | |
1253
7c7ddaf195bf
Implemented ToUnicode() support on Windows 95/98/ME/NT/2000/XP
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
76 else |
7c7ddaf195bf
Implemented ToUnicode() support on Windows 95/98/ME/NT/2000/XP
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
77 printf(" (0x%X)", sym->unicode); |
0 | 78 #endif |
79 } | |
80 } | |
81 print_modifiers(); | |
82 printf("\n"); | |
83 } | |
84 | |
85 int main(int argc, char *argv[]) | |
86 { | |
87 SDL_Event event; | |
88 int done; | |
473 | 89 Uint32 videoflags; |
0 | 90 |
91 /* Initialize SDL */ | |
92 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
93 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
94 return(1); |
0 | 95 } |
96 | |
473 | 97 videoflags = SDL_SWSURFACE; |
493
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
98 while( argc > 1 ) { |
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
99 --argc; |
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
100 if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) { |
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
101 videoflags |= SDL_FULLSCREEN; |
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
102 } else { |
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
103 fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]); |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
104 quit(1); |
493
806fcbde0af3
Fixed a crash bug in checkkeys.c (thanks John!)
Sam Lantinga <slouken@libsdl.org>
parents:
473
diff
changeset
|
105 } |
473 | 106 } |
107 | |
0 | 108 /* Set 640x480 video mode */ |
473 | 109 if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) { |
0 | 110 fprintf(stderr, "Couldn't set 640x480 video mode: %s\n", |
111 SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
112 quit(2); |
0 | 113 } |
114 | |
115 /* Enable UNICODE translation for keyboard input */ | |
116 SDL_EnableUNICODE(1); | |
117 | |
118 /* Enable auto repeat for keyboard input */ | |
119 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, | |
120 SDL_DEFAULT_REPEAT_INTERVAL); | |
121 | |
122 /* Watch keystrokes */ | |
123 done = 0; | |
124 while ( !done ) { | |
125 /* Check for events */ | |
126 SDL_WaitEvent(&event); | |
127 switch (event.type) { | |
128 case SDL_KEYDOWN: | |
129 PrintKey(&event.key.keysym, 1); | |
130 break; | |
131 case SDL_KEYUP: | |
132 PrintKey(&event.key.keysym, 0); | |
133 break; | |
134 case SDL_MOUSEBUTTONDOWN: | |
135 /* Any button press quits the app... */ | |
136 case SDL_QUIT: | |
137 done = 1; | |
138 break; | |
139 default: | |
140 break; | |
141 } | |
142 } | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
143 |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
493
diff
changeset
|
144 SDL_Quit(); |
0 | 145 return(0); |
146 } |