1 /*
2  * xrick/src/sysevt.c
3  *
4  * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
5  *
6  * The use and distribution terms for this software are contained in the file
7  * named README, which can be found in the root of this distribution. By
8  * using this software in any fashion, you are agreeing to be bound by the
9  * terms of this license.
10  *
11  * You must not remove this notice, or any other, from this software.
12  */
13 
14 /*
15  * 20021010 SDLK_n replaced by SDLK_Fn because some non-US keyboards
16  *          requires that SHIFT be pressed to input numbers.
17  */
18 
19 #include <SDL.h>
20 
21 #include "system.h"
22 #include "config.h"
23 #include "game.h"
24 #include "debug.h"
25 
26 #include "control.h"
27 #include "draw.h"
28 
29 #define SYSJOY_RANGE 3280
30 
31 #define SETBIT(x,b) x |= (b)
32 #define CLRBIT(x,b) x &= ~(b)
33 
34 static SDL_Event event;
35 
36 /*
37  * Process an event
38  */
39 static void
processEvent()40 processEvent()
41 {
42 	U16 key;
43 #ifdef ENABLE_FOCUS
44 	SDL_ActiveEvent *aevent;
45 #endif
46 
47   switch (event.type) {
48   case SDL_KEYDOWN:
49     key = event.key.keysym.sym;
50     if (key == syskbd_up || key == SDLK_UP) {
51       SETBIT(control_status, CONTROL_UP);
52       control_last = CONTROL_UP;
53     }
54     else if (key == syskbd_down || key == SDLK_DOWN) {
55       SETBIT(control_status, CONTROL_DOWN);
56       control_last = CONTROL_DOWN;
57     }
58     else if (key == syskbd_left || key == SDLK_LEFT) {
59       SETBIT(control_status, CONTROL_LEFT);
60       control_last = CONTROL_LEFT;
61     }
62     else if (key == syskbd_right || key == SDLK_RIGHT) {
63       SETBIT(control_status, CONTROL_RIGHT);
64       control_last = CONTROL_RIGHT;
65     }
66     else if (key == syskbd_pause) {
67       SETBIT(control_status, CONTROL_PAUSE);
68       control_last = CONTROL_PAUSE;
69     }
70     else if (key == syskbd_end) {
71       SETBIT(control_status, CONTROL_END);
72       control_last = CONTROL_END;
73     }
74     else if (key == syskbd_xtra) {
75       SETBIT(control_status, CONTROL_EXIT);
76       control_last = CONTROL_EXIT;
77     }
78     else if (key == syskbd_fire) {
79       SETBIT(control_status, CONTROL_FIRE);
80       control_last = CONTROL_FIRE;
81     }
82     else if (key == SDLK_F1) {
83       sysvid_toggleFullscreen();
84     }
85     else if (key == SDLK_F2) {
86       sysvid_zoom(-1);
87     }
88     else if (key == SDLK_F3) {
89       sysvid_zoom(+1);
90     }
91 #ifdef ENABLE_SOUND
92     else if (key == SDLK_F4) {
93       syssnd_toggleMute();
94     }
95     else if (key == SDLK_F5) {
96       syssnd_vol(-1);
97     }
98     else if (key == SDLK_F6) {
99       syssnd_vol(+1);
100     }
101 #endif
102 #ifdef ENABLE_CHEATS
103     else if (key == SDLK_F7) {
104       game_toggleCheat(1);
105     }
106     else if (key == SDLK_F8) {
107       game_toggleCheat(2);
108     }
109     else if (key == SDLK_F9) {
110       game_toggleCheat(3);
111     }
112 #endif
113     break;
114   case SDL_KEYUP:
115     key = event.key.keysym.sym;
116     if (key == syskbd_up || key == SDLK_UP) {
117       CLRBIT(control_status, CONTROL_UP);
118       control_last = CONTROL_UP;
119     }
120     else if (key == syskbd_down || key == SDLK_DOWN) {
121       CLRBIT(control_status, CONTROL_DOWN);
122       control_last = CONTROL_DOWN;
123     }
124     else if (key == syskbd_left || key == SDLK_LEFT) {
125       CLRBIT(control_status, CONTROL_LEFT);
126       control_last = CONTROL_LEFT;
127     }
128     else if (key == syskbd_right || key == SDLK_RIGHT) {
129       CLRBIT(control_status, CONTROL_RIGHT);
130       control_last = CONTROL_RIGHT;
131     }
132     else if (key == syskbd_pause) {
133       CLRBIT(control_status, CONTROL_PAUSE);
134       control_last = CONTROL_PAUSE;
135     }
136     else if (key == syskbd_end) {
137       CLRBIT(control_status, CONTROL_END);
138       control_last = CONTROL_END;
139     }
140     else if (key == syskbd_xtra) {
141       CLRBIT(control_status, CONTROL_EXIT);
142       control_last = CONTROL_EXIT;
143     }
144     else if (key == syskbd_fire) {
145       CLRBIT(control_status, CONTROL_FIRE);
146       control_last = CONTROL_FIRE;
147     }
148     break;
149   case SDL_QUIT:
150     /* player tries to close the window -- this is the same as pressing ESC */
151     SETBIT(control_status, CONTROL_EXIT);
152     control_last = CONTROL_EXIT;
153     break;
154 #ifdef ENABLE_FOCUS
155   case SDL_ACTIVEEVENT: {
156     aevent = (SDL_ActiveEvent *)&event;
157     IFDEBUG_EVENTS(
158       printf("xrick/events: active %x %x\n", aevent->gain, aevent->state);
159       );
160     if (aevent->gain == 1)
161       control_active = TRUE;
162     else
163       control_active = FALSE;
164     }
165   break;
166 #endif
167 #ifdef ENABLE_JOYSTICK
168   case SDL_JOYAXISMOTION:
169     IFDEBUG_EVENTS(sys_printf("xrick/events: joystick\n"););
170     if (event.jaxis.axis == 0) {  /* left-right */
171       if (event.jaxis.value < -SYSJOY_RANGE) {  /* left */
172 	SETBIT(control_status, CONTROL_LEFT);
173 	CLRBIT(control_status, CONTROL_RIGHT);
174       }
175       else if (event.jaxis.value > SYSJOY_RANGE) {  /* right */
176 	SETBIT(control_status, CONTROL_RIGHT);
177 	CLRBIT(control_status, CONTROL_LEFT);
178       }
179       else {  /* center */
180 	CLRBIT(control_status, CONTROL_RIGHT);
181 	CLRBIT(control_status, CONTROL_LEFT);
182       }
183     }
184     if (event.jaxis.axis == 1) {  /* up-down */
185       if (event.jaxis.value < -SYSJOY_RANGE) {  /* up */
186 	SETBIT(control_status, CONTROL_UP);
187 	CLRBIT(control_status, CONTROL_DOWN);
188       }
189       else if (event.jaxis.value > SYSJOY_RANGE) {  /* down */
190 	SETBIT(control_status, CONTROL_DOWN);
191 	CLRBIT(control_status, CONTROL_UP);
192       }
193       else {  /* center */
194 	CLRBIT(control_status, CONTROL_DOWN);
195 	CLRBIT(control_status, CONTROL_UP);
196       }
197     }
198     break;
199   case SDL_JOYBUTTONDOWN:
200     SETBIT(control_status, CONTROL_FIRE);
201     break;
202   case SDL_JOYBUTTONUP:
203     CLRBIT(control_status, CONTROL_FIRE);
204     break;
205 #endif
206   default:
207     break;
208   }
209 }
210 
211 /*
212  * Process events, if any, then return
213  */
214 void
sysevt_poll(void)215 sysevt_poll(void)
216 {
217   while (SDL_PollEvent(&event))
218     processEvent();
219 }
220 
221 /*
222  * Wait for an event, then process it and return
223  */
224 void
sysevt_wait(void)225 sysevt_wait(void)
226 {
227   SDL_WaitEvent(&event);
228   processEvent();
229 }
230 
231 /* eof */
232 
233 
234 
235