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