1 /*
2 * sdl_darw.c SDL event handler
3 *
4 * Copyright (C) 2000- Fumihiko Murata <fmurata@p1.tcnet.ne.jp>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 /* $Id: sdl_event.c,v 1.5 2001/12/16 17:12:56 chikama Exp $ */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <SDL/SDL.h>
29
30 #include "portab.h"
31 #include "system.h"
32 #include "counter.h"
33 #include "nact.h"
34 #include "sdl_private.h"
35 #include "key.h"
36 #include "menu.h"
37 #include "imput.h"
38 #include "joystick.h"
39 #include "sdl_input.c"
40
41 static void sdl_getEvent(void);
42 static void keyEventProsess(SDL_KeyboardEvent *e, boolean bool);
43 static int check_button(void);
44
45
46 /* pointer ��� */
47 static int mousex, mousey, mouseb;
48 boolean RawKeyInfo[256];
49 /* SDL Joystick */
50 static int joyinfo=0;
51
mouse_to_rawkey(int button)52 static int mouse_to_rawkey(int button) {
53 switch(button) {
54 case SDL_BUTTON_LEFT:
55 return KEY_MOUSE_LEFT;
56 case SDL_BUTTON_MIDDLE:
57 return KEY_MOUSE_MIDDLE;
58 case SDL_BUTTON_RIGHT:
59 return KEY_MOUSE_RIGHT;
60 }
61 return 0;
62 }
63
64 /* Event���� */
sdl_getEvent(void)65 static void sdl_getEvent(void) {
66 SDL_Event e;
67 boolean m2b = FALSE, msg_skip = FALSE;
68 int i;
69
70 while (SDL_PollEvent(&e)) {
71 switch (e.type) {
72 case SDL_ACTIVEEVENT:
73 if (e.active.state & SDL_APPMOUSEFOCUS) {
74 ms_active = e.active.gain;
75 #if 0
76 if (sdl_fs_on) {
77 if (ms_active) {
78 SDL_WM_GrabInput(SDL_GRAB_ON);
79 } else {
80 SDL_WM_GrabInput(SDL_GRAB_OFF);
81 }
82 }
83 #endif
84 }
85 if (e.active.state & SDL_APPINPUTFOCUS) {
86 }
87
88 break;
89 case SDL_KEYDOWN:
90 keyEventProsess(&e.key, TRUE);
91 break;
92 case SDL_KEYUP:
93 keyEventProsess(&e.key, FALSE);
94 if (e.key.keysym.sym == SDLK_F1) msg_skip = TRUE;
95 if (e.key.keysym.sym == SDLK_F4) {
96 SDL_WM_ToggleFullScreen(sdl_display);
97 sdl_fs_on = !sdl_fs_on;
98 }
99 break;
100 case SDL_MOUSEMOTION:
101 mousex = e.motion.x;
102 mousey = e.motion.y;
103 break;
104 case SDL_MOUSEBUTTONDOWN:
105 mouseb |= (1 << e.button.button);
106 RawKeyInfo[mouse_to_rawkey(e.button.button)] = TRUE;
107 #if 0
108 if (e.button.button == 2) {
109 keywait_flag=TRUE;
110 }
111 #endif
112 break;
113 case SDL_MOUSEBUTTONUP:
114 mouseb &= (0xffffffff ^ (1 << e.button.button));
115 RawKeyInfo[mouse_to_rawkey(e.button.button)] = FALSE;
116 if (e.button.button == 2) {
117 m2b = TRUE;
118 }
119 break;
120 #if HAVE_SDLJOY
121 case SDL_JOYAXISMOTION:
122 if (abs(e.jaxis.value) < 0x4000) {
123 joyinfo &= e.jaxis.axis == 0 ? ~0xc : ~3;
124 } else {
125 i = (e.jaxis.axis == 0 ? 2 : 0) +
126 (e.jaxis.value > 0 ? 1 : 0);
127 joyinfo |= 1 << i;
128 }
129 break;
130 case SDL_JOYBALLMOTION:
131 break;
132 case SDL_JOYHATMOTION:
133 break;
134 case SDL_JOYBUTTONDOWN:
135 case SDL_JOYBUTTONUP:
136 if (e.jbutton.button < 4) {
137 i = 1 << (e.jbutton.button+4);
138 if (e.jbutton.state == SDL_PRESSED)
139 joyinfo |= i;
140 else
141 joyinfo &= ~i;
142 } else {
143 if (e.jbutton.state == SDL_RELEASED) {
144 }
145 }
146 break;
147 #endif
148 default:
149 printf("ev %x\n", e.type);
150 break;
151 }
152 }
153
154 if (m2b) {
155 menu_open();
156 }
157
158 if (msg_skip) set_skipMode(!get_skipMode());
159 }
160
sdl_keywait(int msec,boolean cancel)161 int sdl_keywait(int msec, boolean cancel) {
162 int key=0, n;
163 int cnt = get_high_counter(SYSTEMCOUNTER_MSEC);
164
165 if (msec < 0) return 0;
166
167 while (msec > (get_high_counter(SYSTEMCOUNTER_MSEC) - cnt)) {
168 sdl_getEvent();
169 key = check_button() | sdl_getKeyInfo() | joy_getinfo();
170 if (cancel && key) break;
171 n = msec - (get_high_counter(SYSTEMCOUNTER_MSEC) - cnt);
172 if (n < 0) break;
173 if (n < 10) {
174 usleep(1000 * n);
175 } else {
176 usleep(10000);
177 }
178 nact->callback();
179 }
180
181 return key;
182 }
183
184 /* ���������� */
keyEventProsess(SDL_KeyboardEvent * e,boolean bool)185 static void keyEventProsess(SDL_KeyboardEvent *e, boolean bool) {
186 RawKeyInfo[sdl_keytable[e->keysym.sym]] = bool;
187 }
188
sdl_getKeyInfo()189 int sdl_getKeyInfo() {
190 int rt;
191
192 sdl_getEvent();
193
194 rt = ((RawKeyInfo[KEY_UP] || RawKeyInfo[KEY_PAD_8]) |
195 ((RawKeyInfo[KEY_DOWN] || RawKeyInfo[KEY_PAD_2]) << 1) |
196 ((RawKeyInfo[KEY_LEFT] || RawKeyInfo[KEY_PAD_4]) << 2) |
197 ((RawKeyInfo[KEY_RIGHT] || RawKeyInfo[KEY_PAD_6]) << 3) |
198 (RawKeyInfo[KEY_RETURN] << 4) |
199 (RawKeyInfo[KEY_SPACE ] << 5) |
200 (RawKeyInfo[KEY_ESC] << 6) |
201 (RawKeyInfo[KEY_TAB] << 7));
202
203 return rt;
204 }
205
check_button(void)206 static int check_button(void) {
207 int m1, m2;
208
209 m1 = mouseb & (1 << 1) ? SYS35KEY_RET : 0;
210 m2 = mouseb & (1 << 3) ? SYS35KEY_SPC : 0;
211
212 return m1 | m2;
213 }
214
sdl_getMouseInfo(MyPoint * p)215 int sdl_getMouseInfo(MyPoint *p) {
216 sdl_getEvent();
217
218 if (!ms_active) {
219 if (p) {
220 p->x = 0; p->y = 0;
221 }
222 return 0;
223 }
224
225 if (p) {
226 p->x = mousex - winoffset_x;
227 p->y = mousey - winoffset_y;
228 }
229 return check_button();
230 }
231
232 #ifdef HAVE_SDLJOY
sdl_getjoyinfo(void)233 int sdl_getjoyinfo(void) {
234 sdl_getEvent();
235 return joyinfo;
236 }
237 #endif
238
239