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