2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <SDL_thread.h>
22 /*---------------------------------------------------------------------------*/
25 #include <libcwiimote/wiimote.h>
26 #include <libcwiimote/wiimote_api.h>
29 * This data structure tracks button changes, counting transitions so that
30 * none are missed if the event handling thread falls significantly behind
31 * the device IO thread.
46 static void set_button(struct button_state *B, int s)
48 if ((B->curr == 0) != (s == 0))
63 static int get_button(struct button_state *B)
67 if (B->last == 1 && B->upc > 0)
73 else if (B->last == 0 && B->dnc > 0)
83 /*---------------------------------------------------------------------------*/
90 struct button_state A;
91 struct button_state B;
92 struct button_state plus;
93 struct button_state minus;
94 struct button_state home;
95 struct button_state L;
96 struct button_state R;
97 struct button_state U;
98 struct button_state D;
101 static struct tilt_state state;
102 static SDL_mutex *mutex = NULL;
103 static SDL_Thread *thread = NULL;
107 static int tilt_func(void *data)
109 wiimote_t wiimote = WIIMOTE_INIT;
110 const char *address = config_get_s(CONFIG_WIIMOTE_ADDR);
112 if (strlen(address) > 0)
114 if (wiimote_connect(&wiimote, address) < 0)
115 fprintf(stderr, "%s\n", wiimote_get_error());
120 wiimote.mode.bits = WIIMOTE_MODE_ACC;
124 state.status = running;
127 while (mutex && running && wiimote_is_open(&wiimote))
129 if (wiimote_update(&wiimote) < 0)
134 running = state.status;
136 set_button(&state.A, wiimote.keys.a);
137 set_button(&state.B, wiimote.keys.b);
138 set_button(&state.plus, wiimote.keys.plus);
139 set_button(&state.minus, wiimote.keys.minus);
140 set_button(&state.home, wiimote.keys.home);
141 set_button(&state.L, wiimote.keys.left);
142 set_button(&state.R, wiimote.keys.right);
143 set_button(&state.U, wiimote.keys.up);
144 set_button(&state.D, wiimote.keys.down);
146 if (isnormal(wiimote.tilt.y))
148 state.x = (state.x * (FILTER - 1) +
149 wiimote.tilt.y) / FILTER;
151 if (isnormal(wiimote.tilt.x))
153 state.z = (state.z * (FILTER - 1) +
154 wiimote.tilt.x) / FILTER;
160 wiimote_disconnect(&wiimote);
168 memset(&state, 0, sizeof (struct tilt_state));
170 mutex = SDL_CreateMutex();
171 thread = SDL_CreateThread(tilt_func, NULL);
180 /* Get/set the status of the tilt sensor thread. */
187 /* Kill the thread and destroy the mutex. */
189 SDL_KillThread(thread);
190 SDL_DestroyMutex(mutex);
197 int tilt_get_button(int *b, int *s)
205 if ((ch = get_button(&state.A)))
207 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_A);
208 *s = (ch == BUTTON_DN);
210 else if ((ch = get_button(&state.B)))
212 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_B);
213 *s = (ch == BUTTON_DN);
215 else if ((ch = get_button(&state.plus)))
217 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_R);
218 *s = (ch == BUTTON_DN);
220 else if ((ch = get_button(&state.minus)))
222 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_L);
223 *s = (ch == BUTTON_DN);
225 else if ((ch = get_button(&state.home)))
227 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT);
228 *s = (ch == BUTTON_DN);
230 else if ((ch = get_button(&state.L)))
232 *b = config_get_d(CONFIG_JOYSTICK_DPAD_L);
233 *s = (ch == BUTTON_DN);
235 else if ((ch = get_button(&state.R)))
237 *b = config_get_d(CONFIG_JOYSTICK_DPAD_R);
238 *s = (ch == BUTTON_DN);
240 else if ((ch = get_button(&state.U)))
242 *b = config_get_d(CONFIG_JOYSTICK_DPAD_U);
243 *s = (ch == BUTTON_DN);
245 else if ((ch = get_button(&state.D)))
247 *b = config_get_d(CONFIG_JOYSTICK_DPAD_D);
248 *s = (ch == BUTTON_DN);
256 float tilt_get_x(void)
270 float tilt_get_z(void)
297 /*---------------------------------------------------------------------------*/