configure script now cleans
[drnoksnes] / platform / hgw.cpp
1 #include <stdio.h>
2 #include <libgen.h>
3 #include <hgw/hgw.h>
4
5 #include "platform.h"
6 #include "hgw.h"
7 #include "snes9x.h"
8
9 #define DIE(format, ...) do { \
10                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
11                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
12                 abort(); \
13         } while (0);
14
15
16 bool hgwLaunched;
17 static HgwContext *hgw;
18
19 static void createActionMappingsOnly();
20 static void parseGConfKeyMappings();
21
22 void HgwInit()
23 {
24         // hildon-games-wrapper sets this env variable for itself.
25         char* service = getenv("HGW_EXEC_SERVICE");
26
27         if (!service) {
28                 // Not launched from hildon-games-wrapper
29                 hgwLaunched = false;
30                 return;
31         }
32
33         hgw = hgw_context_init();
34
35         if (!hgw) {
36                 fprintf(stderr, "Error opening hgw context\n");
37                 hgwLaunched = false;
38         }
39
40         hgwLaunched = true;
41         printf("Loading in HGW mode\n");
42 }
43
44 void HgwDeinit()
45 {
46         if (!hgwLaunched) return;
47
48         hgw_context_destroy(hgw,
49                 (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
50
51         hgw = 0;
52 }
53
54 void HgwConfig()
55 {
56         if (!hgwLaunched) return;
57
58         Config.fullscreen = true;
59
60         char romFile[PATH_MAX + 1];
61         if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE
62                 && strlen(romFile) > 0) {
63                 S9xSetRomFile(romFile);
64         } else {
65                 printf("Exiting gracefully because there's no ROM in Gconf\n");
66                 HgwDeinit();
67                 exit(0);
68         }
69
70         char sound = FALSE;
71         if (hgw_conf_request_bool(hgw, kGConfSound, &sound) == HGW_ERR_NONE) {
72                 Config.enableAudio = sound ? true : false;
73         }
74
75         char turbo = FALSE;
76         if (hgw_conf_request_bool(hgw, kGConfTurboMode, &turbo) == HGW_ERR_NONE) {
77                 Settings.TurboMode = turbo ? TRUE : FALSE;
78         }
79
80         int frameskip = 0;
81         if (hgw_conf_request_int(hgw, kGConfFrameskip, &frameskip) == HGW_ERR_NONE) {
82                 Settings.SkipFrames = (frameskip > 0 ? frameskip : AUTO_FRAMERATE);
83         }
84
85         char transparency = FALSE;
86         if (hgw_conf_request_bool(hgw, kGConfTransparency, &transparency) == HGW_ERR_NONE) {
87                 Settings.Transparency = transparency ? TRUE : FALSE;
88         }
89
90 #if CONF_XSP
91         char xsp = TRUE;
92         if (hgw_conf_request_bool(hgw, kGConfXSP, &xsp) == HGW_ERR_NONE) {
93                 if (!xsp) {
94                         free(Config.scaler);
95                         Config.scaler = strdup("2x");
96                 }
97         }
98 #endif
99
100         char displayFramerate = FALSE;
101         if (hgw_conf_request_bool(hgw, kGConfDisplayFramerate, &displayFramerate) == HGW_ERR_NONE) {
102                 Settings.DisplayFrameRate = displayFramerate ? TRUE : FALSE;
103         }
104
105         char displayControls = FALSE;
106         if (hgw_conf_request_bool(hgw, kGConfDisplayControls, &displayControls) == HGW_ERR_NONE) {
107                 Config.touchscreenShow = displayControls ? true : false;
108         }
109
110         int speedhacks = 0;
111         if (hgw_conf_request_int(hgw, kGConfSpeedhacks, &speedhacks) == HGW_ERR_NONE) {
112                 if (speedhacks <= 0) {
113                         Settings.HacksEnabled = FALSE;
114                         Settings.HacksFilter = FALSE;
115                 } else if (speedhacks == 1) {
116                         Settings.HacksEnabled = TRUE;
117                         Settings.HacksFilter = TRUE;
118                 } else {
119                         Settings.HacksEnabled = TRUE;
120                         Settings.HacksFilter = FALSE;
121                 }
122         }
123         if (Settings.HacksEnabled && !Config.hacksFile) {
124                 // Provide a default speedhacks file
125                 if (asprintf(&Config.hacksFile, "%s/snesadvance.dat", dirname(romFile))
126                                 < 0) {
127                         Config.hacksFile = 0; // malloc error.
128                 }
129                 // romFile[] is garbled from now on.
130         }
131
132         int mappings = 0;
133         if (hgw_conf_request_int(hgw, kGConfMapping, &mappings) == HGW_ERR_NONE) {
134                 switch (mappings) {
135                         case 0:
136                                 // Do nothing, leave mappings as is.
137                                 break;
138                         case 1: // Keys
139                                 parseGConfKeyMappings();
140                                 break;
141                         case 2: // Touchscreen
142                                 Config.touchscreenInput = true;
143                                 createActionMappingsOnly();
144                                 break;
145                         case 3: // Touchscreen + keys
146                                 Config.touchscreenInput = true;
147                                 parseGConfKeyMappings();
148                                 break;
149                         case 4: // Mouse
150                                 Settings.Mouse = TRUE;
151                                 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
152                                 createActionMappingsOnly();
153                                 break;
154                         case 5: // Mouse + keys
155                                 Settings.Mouse = TRUE;
156                                 Settings.ControllerOption = SNES_MOUSE;
157                                 parseGConfKeyMappings();
158                                 break;
159                 }
160         }
161
162         HgwStartCommand cmd = hgw_context_get_start_command(hgw);
163         switch (cmd) {
164                 default:
165                 case HGW_COMM_NONE:     // called from libosso
166                 case HGW_COMM_CONT:
167                         Config.snapshotLoad = true;
168                         Config.snapshotSave = true;
169                         break;
170                 case HGW_COMM_RESTART:
171                         Config.snapshotLoad = false;
172                         Config.snapshotSave = true;
173                         break;
174                 case HGW_COMM_QUIT:
175                         // hum, what?
176                         Config.snapshotLoad = false;
177                         Config.snapshotSave = false;
178                         Config.quitting = true;
179                         break;
180         }
181 }
182
183 void HgwPollEvents()
184 {
185         if (!hgwLaunched) return;
186         
187         HgwMessage msg;
188         HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
189         
190         if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
191                 // Message Incoming, process msg
192                 
193                 switch (msg.type) {
194                         case HGW_MSG_TYPE_CBREQ:
195                                 switch (msg.e_val) {
196                                         case HGW_CB_QUIT:
197                                         case HGW_CB_EXIT:
198                                                 Config.quitting = true;
199                                                 break;
200                                 }
201                                 break;
202                         case HGW_MSG_TYPE_DEVSTATE:
203                                 switch (msg.e_val) {
204                                         case HGW_DEVICE_STATE_SHUTDOWN:
205                                                 Config.quitting = true; // try to quit gracefully
206                                                 break;
207                                 }
208                                 break;
209                         default:
210                                 // do nothing
211                                 break;
212                 }
213                 
214                 hgw_msg_free_data(&msg);
215         }
216 }
217
218 // For now, please keep this in sync with ../gui/controls.c
219 typedef struct ButtonEntry {
220         char * gconf_key;
221         unsigned long mask;
222         bool is_action;
223 } ButtonEntry;
224 #define BUTTON_INITIALIZER(button, name) \
225         { kGConfKeysPath "/" name, SNES_##button##_MASK, false }
226 #define ACTION_INITIALIZER(action, name) \
227         { kGConfKeysPath "/" name, kAction##action, true }
228 #define BUTTON_LAST     \
229         { 0 }
230 static const ButtonEntry buttons[] = {
231         BUTTON_INITIALIZER(A, "a"),
232         BUTTON_INITIALIZER(B, "b"),
233         BUTTON_INITIALIZER(X, "x"),
234         BUTTON_INITIALIZER(Y, "y"),
235         BUTTON_INITIALIZER(TL, "l"),
236         BUTTON_INITIALIZER(TR, "r"),
237         BUTTON_INITIALIZER(START, "start"),
238         BUTTON_INITIALIZER(SELECT, "select"),
239         BUTTON_INITIALIZER(UP, "up"),
240         BUTTON_INITIALIZER(DOWN, "down"),
241         BUTTON_INITIALIZER(LEFT, "left"),
242         BUTTON_INITIALIZER(RIGHT, "right"),
243         ACTION_INITIALIZER(Quit, "quit"),
244         ACTION_INITIALIZER(ToggleFullscreen, "fullscreen"),
245         ACTION_INITIALIZER(QuickLoad1, "quickload1"),
246         ACTION_INITIALIZER(QuickSave1, "quicksave1"),
247         ACTION_INITIALIZER(QuickLoad2, "quickload2"),
248         ACTION_INITIALIZER(QuickSave2, "quicksave2"),
249         BUTTON_LAST
250 };
251
252 static void createActionMappingsOnly()
253 {
254         // Discard any other mapping
255         ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
256         ZeroMemory(Config.action, sizeof(Config.action));
257         
258         // Map quit to fullscreen, escape and task switch.
259         Config.action[72] = kActionQuit;
260         Config.action[9] = kActionQuit;
261         Config.action[71] = kActionQuit;
262 }
263
264 static void parseGConfKeyMappings()
265 {
266         // Discard any other mapping
267         ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
268         ZeroMemory(Config.action, sizeof(Config.action));
269
270         // If the user does not map fullscreen or quit
271         bool quit_mapped = false;
272
273         printf("Hgw: Using gconf key mappings\n");
274
275         int i, scancode;
276         for (i = 0; buttons[i].gconf_key; i++) {
277                 if (hgw_conf_request_int(hgw, buttons[i].gconf_key, &scancode) == HGW_ERR_NONE) {
278                         if (scancode <= 0 || scancode > 255) continue;
279
280                         if (buttons[i].is_action) {
281                                 Config.action[scancode] |= buttons[i].mask;
282                                 if (buttons[i].mask & (kActionQuit | kActionToggleFullscreen)) {
283                                         quit_mapped = true;
284                                 }
285                         } else {
286                                 Config.joypad1Mapping[scancode] |= buttons[i].mask;
287                         }
288                 }
289         }
290
291         // Safeguards
292         if (!quit_mapped) {
293                 // Newbie user won't know how to quit game.
294                 if (!Config.joypad1Mapping[72] && !Config.action[72]) {
295                         // Fullscreen key is not mapped, map
296                         Config.action[72] = kActionQuit;
297                         quit_mapped = true;
298                 }
299                 if (!quit_mapped && !Config.joypad1Mapping[9] && !Config.action[9]) {
300                         // Escape key is not mapped, map
301                         // But only if we couldn't map quit to fullscreen. Some people
302                         // actually want Quit not to be mapped.
303                         Config.action[9] = kActionQuit;
304                         quit_mapped = true;
305                 }
306                 if (!quit_mapped) {
307                         // Force mapping of fullscreen to Quit if can't map anywhere else.
308                         Config.joypad1Mapping[72] = 0;
309                         Config.action[72] = kActionQuit;
310                 }
311         }
312
313         // If task switch key is not mapped, map it to Quit by default.
314         if (!Config.action[71] && !Config.joypad1Mapping[71]) {
315                 Config.action[71] = kActionQuit;
316         }
317 }
318