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