This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testwm.c
375 lines (329 loc) · 9.79 KB
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;
13
static Uint8 video_bpp;
14
15
static Uint32 video_flags;
16
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
17
static void
18
quit(int rc)
19
{
20
21
SDL_Quit();
exit(rc);
22
23
}
24
int
25
SetVideoMode(int w, int h)
26
{
27
28
29
30
31
SDL_Surface *screen;
int i;
Uint8 *buffer;
SDL_Color palette[256];
32
screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
33
if (screen == NULL) {
34
35
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, video_bpp, SDL_GetError());
36
37
return (-1);
}
38
39
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
"fullscreen" : "windowed");
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;
}
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());
51
52
53
54
return (-1);
}
buffer = (Uint8 *) screen->pixels;
for (i = 0; i < screen->h; ++i) {
55
56
memset(buffer, (i * 255) / screen->h,
screen->w * screen->format->BytesPerPixel);
57
58
buffer += screen->pitch;
}
59
60
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
61
62
return (0);
63
64
}
65
SDL_Surface *
66
LoadIconSurface(char *file, Uint8 ** maskp)
67
{
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 */
76
icon = SDL_LoadBMP(file);
77
if (icon == NULL) {
78
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
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) {
92
93
fprintf(stderr, "Icon must have a palette!\n");
SDL_FreeSurface(icon);
94
95
96
97
return (NULL);
}
/* Set the colorkey */
98
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
99
100
101
/* Create the mask */
pixels = (Uint8 *) icon->pixels;
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);
106
mlen = (icon->w * icon->h + 7) / 8;
107
mask = (Uint8 *) malloc(mlen);
108
if (mask == NULL) {
109
110
fprintf(stderr, "Out of memory!\n");
SDL_FreeSurface(icon);
111
112
return (NULL);
}
113
memset(mask, 0, mlen);
114
115
for (i = 0; i < icon->h; i++)
for (j = 0; j < icon->w; j++) {
116
117
int pindex = i * icon->pitch + j;
int mindex = i * icon->w + j;
118
119
if (pixels[pindex] != *pixels)
mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
120
}
121
122
*maskp = mask;
return (icon);
123
124
}
125
void
126
HotKey_ToggleFullScreen(void)
127
{
128
129
SDL_Surface *screen;
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");
134
} else {
135
printf("Unable to toggle fullscreen mode\n");
136
video_flags ^= SDL_FULLSCREEN;
137
SetVideoMode(screen->w, screen->h);
138
}
139
140
}
141
void
142
HotKey_ToggleGrab(void)
143
{
144
145
SDL_GrabMode mode;
146
147
printf("Ctrl-G: toggling input grab!\n");
mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
148
if (mode == SDL_GRAB_ON) {
149
printf("Grab was on\n");
150
} else {
151
printf("Grab was off\n");
152
}
153
mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
154
if (mode == SDL_GRAB_ON) {
155
printf("Grab is now on\n");
156
} else {
157
printf("Grab is now off\n");
158
}
159
160
}
161
void
162
HotKey_Iconify(void)
163
{
164
165
printf("Ctrl-Z: iconifying window!\n");
SDL_WM_IconifyWindow();
166
167
}
168
void
169
HotKey_Quit(void)
170
{
171
SDL_Event event;
172
173
printf("Posting internal quit request\n");
174
event.type = SDL_USEREVENT;
175
SDL_PushEvent(&event);
176
177
}
178
int SDLCALL
179
FilterEvents(SDL_Event * event)
180
{
181
182
183
184
185
186
static int reallyquit = 0;
switch (event->type) {
case SDL_ACTIVEEVENT:
/* See what happened */
187
printf("App %s ", event->active.gain ? "gained" : "lost");
188
if (event->active.state & SDL_APPACTIVE)
189
printf("active ");
190
if (event->active.state & SDL_APPINPUTFOCUS)
191
printf("input ");
192
if (event->active.state & SDL_APPMOUSEFOCUS)
193
194
printf("mouse ");
printf("focus\n");
195
196
197
/* See if we are iconified or restored */
if (event->active.state & SDL_APPACTIVE) {
198
199
printf("App has been %s\n",
event->active.gain ? "restored" : "iconified");
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;
208
SDL_ShowCursor(visible);
209
}
210
211
212
printf("Mouse button %d has been %s\n",
event->button.button,
(event->button.state == SDL_PRESSED) ? "pressed" : "released");
213
214
215
216
return (0);
/* Show relative mouse motion */
case SDL_MOUSEMOTION:
217
#if 0
218
219
220
printf("Mouse motion: {%d,%d} (%d,%d)\n",
event->motion.x, event->motion.y,
event->motion.xrel, event->motion.yrel);
221
#endif
222
223
224
225
return (0);
case SDL_KEYDOWN:
if (event->key.keysym.sym == SDLK_ESCAPE) {
226
HotKey_Quit();
227
228
229
}
if ((event->key.keysym.sym == SDLK_g) &&
(event->key.keysym.mod & KMOD_CTRL)) {
230
HotKey_ToggleGrab();
231
232
233
}
if ((event->key.keysym.sym == SDLK_z) &&
(event->key.keysym.mod & KMOD_CTRL)) {
234
HotKey_Iconify();
235
236
237
}
if ((event->key.keysym.sym == SDLK_RETURN) &&
(event->key.keysym.mod & KMOD_ALT)) {
238
HotKey_ToggleFullScreen();
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;
250
printf("Quit requested\n");
251
252
return (0);
}
253
printf("Quit demanded\n");
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);
}
266
267
}
268
int
269
main(int argc, char *argv[])
270
{
271
272
273
274
275
276
277
SDL_Event event;
char *title;
SDL_Surface *icon;
Uint8 *icon_mask;
int parsed;
int w, h;
278
279
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
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) {
290
if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
291
292
293
video_flags |= SDL_FULLSCREEN;
argc -= 1;
argv += 1;
294
} else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
295
296
297
video_flags |= SDL_RESIZABLE;
argc -= 1;
argv += 1;
298
} else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
299
300
301
video_flags |= SDL_NOFRAME;
argc -= 1;
argv += 1;
302
303
} else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
w = atoi(argv[2]);
304
305
argc -= 2;
argv += 2;
306
307
} else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
h = atoi(argv[2]);
308
309
argc -= 2;
argv += 2;
310
311
} else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
video_bpp = atoi(argv[2]);
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 */
320
icon = LoadIconSurface("icon.bmp", &icon_mask);
321
if (icon != NULL) {
322
SDL_WM_SetIcon(icon, icon_mask);
323
324
}
if (icon_mask != NULL)
325
free(icon_mask);
326
327
328
329
330
331
/* Set the title bar */
if (argv[1] == NULL)
title = "Testing 1.. 2.. 3...";
else
title = argv[1];
332
SDL_WM_SetCaption(title, "testwm");
333
334
/* See if it's really set */
335
SDL_WM_GetCaption(&title, NULL);
336
if (title)
337
printf("Title was set to: %s\n", title);
338
else
339
printf("No window title was set!\n");
340
341
/* Initialize the display */
342
343
if (SetVideoMode(w, h) < 0) {
quit(1);
344
345
346
}
/* Set an event filter that discards everything but QUIT */
347
SDL_SetEventFilter(FilterEvents);
348
349
/* Ignore key up events, they don't even get filtered */
350
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
351
352
/* Loop, waiting for QUIT */
353
while (SDL_WaitEvent(&event)) {
354
355
switch (event.type) {
case SDL_VIDEORESIZE:
356
357
358
printf("Got a resize event: %dx%d\n",
event.resize.w, event.resize.h);
SetVideoMode(event.resize.w, event.resize.h);
359
360
break;
case SDL_USEREVENT:
361
printf("Handling internal quit request\n");
362
363
/* Fall through to the quit handler */
case SDL_QUIT:
364
365
printf("Bye bye..\n");
quit(0);
366
367
default:
/* This should never happen */
368
printf("Warning: Event %d wasn't filtered\n", event.type);
369
370
371
break;
}
}
372
373
printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
SDL_Quit();
374
return (255);
375
}