Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
375 lines (329 loc) · 9.79 KB

testwm.c

File metadata and controls

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