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