7 #include <gconf/gconf.h>
8 #include <gconf/gconf-client.h>
10 /* Zeemote driver contributed by Till Harbaum */
14 #include <zeemote-conf.h>
19 #include "../gui/gconf.h"
22 /* Zeemote analog stick sensivity limit */
23 #define ZEEMOTE_LIMIT 8000
25 static const unsigned short zeemote_mask[] =
27 SNES_A_MASK, SNES_B_MASK, SNES_X_MASK, SNES_Y_MASK
30 typedef struct zeemote_player {
31 /** Pointer to zeemote_t struct. If NULL, zeemote was not enabled for this player. */
36 static zeemote_player_t players[2] = { {0, 0}, {0, 0} };
40 /* Check if Osso initialization was succesful. */
41 if (!OssoOk()) return;
43 /* Since Zeemote means GConf, we can read our own configuration from it */
44 GConfClient *gcc = gconf_client_get_default();
46 /* Check which players (if any) enabled Zeemote */
47 gchar key[kGConfPlayerPathBufferLen];
52 relKey = key + sprintf(key, kGConfPlayerPath, 1);
53 strcpy(relKey, kGConfPlayerZeemoteEnable);
54 enabled[0] = gconf_client_get_bool(gcc, key, NULL);
57 relKey = key + sprintf(key, kGConfPlayerPath, 2);
58 strcpy(relKey, kGConfPlayerZeemoteEnable);
59 enabled[1] = gconf_client_get_bool(gcc, key, NULL);
61 if (!enabled[0] && !enabled[1]) {
62 /* No player wanted a zeemote! */
66 /* Get list of configured zeemotes from the conf tool. One could */
67 /* alternally call zeemote_scan() which would return the same type */
68 /* of list, but instead from the devices currently visible. Also */
69 /* zeemote_scan() blocks for about 10 seconds */
70 zeemote_scan_result_t *scan_result =
71 zeemote_get_scan_results_from_gconf();
74 fprintf(stderr, "Zeemote: No scan results\n");
77 /* If we found a zeemote, assign it to the first player. If we found
78 * two zeemotes, assign the first found one to the first player, etc. */
79 /* Since the order comes from gconf, the user could potentially change
81 if (enabled[0] && scan_result->number_of_devices > 0) {
83 zeemote_connect(&scan_result->device[0].bdaddr);
84 if (!players[0].zeemote) {
85 fprintf(stderr, "Zeemote: Failed to connect for player %d", 1);
88 if (enabled[1] && scan_result->number_of_devices > 1) {
90 zeemote_connect(&scan_result->device[1].bdaddr);
91 if (!players[1].zeemote) {
92 fprintf(stderr, "Zeemote: Failed to connect for player %d", 2);
96 if (players[0].zeemote && players[1].zeemote) {
97 printf("Zeemote: enabled for two players\n");
98 } else if (players[0].zeemote) {
99 printf("Zeemote: enabled for player %d\n", 1);
100 } else if (players[1].zeemote) {
101 printf("Zeemote: enabled for player %d\n", 2);
103 printf("Zeemote: disabled because of error\n");
107 static void read_for_player(zeemote_player_t *player, uint32 *joypad)
109 if (!player->zeemote) return;
111 zeemote_state_t *state = zeemote_get_state(player->zeemote);
112 if (!state) return; // Some error
113 // Zeemote was disconnected
114 if (state->state != ZEEMOTE_STATE_CONNECTED) return;
119 /* check zeemote buttons A-D */
120 for (i = 0; i < 4; i++) {
121 if (state->buttons & (1<<i))
122 buttons |= zeemote_mask[i];
125 /* handle direction */
126 if (state->axis[0] < -ZEEMOTE_LIMIT) buttons |= SNES_LEFT_MASK;
127 if (state->axis[0] > ZEEMOTE_LIMIT) buttons |= SNES_RIGHT_MASK;
128 if (state->axis[1] < -ZEEMOTE_LIMIT) buttons |= SNES_UP_MASK;
129 if (state->axis[1] > ZEEMOTE_LIMIT) buttons |= SNES_DOWN_MASK;
131 /* check which actual buttons were pressed or released */
132 uint32 buttons_changed = buttons ^ player->prev_buttons;
133 uint32 buttons_pressed = buttons_changed & buttons;
134 uint32 buttons_released = buttons_changed & player->prev_buttons;
136 /* prevent device screensaver when zeemote state changes */
139 osso_display_blanking_pause(ossoContext);
140 player->prev_buttons = buttons;
143 *joypad = (*joypad & ~buttons_released) | buttons_pressed;
146 void ZeeRead(uint32* joypads)
148 read_for_player(&players[0], &joypads[0]);
149 read_for_player(&players[1], &joypads[1]);
154 if (players[0].zeemote) {
155 zeemote_disconnect(players[0].zeemote);
156 players[0].zeemote = 0;
158 if (players[1].zeemote) {
159 zeemote_disconnect(players[1].zeemote);
160 players[1].zeemote = 0;