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