9 #define DIE(format, ...) do { \
10 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
11 fprintf(stderr, format "\n", ## __VA_ARGS__); \
17 static HgwContext *hgw;
19 static void createActionMappingsOnly();
20 static void parseGConfKeyMappings();
24 // hildon-games-wrapper sets this env variable for itself.
25 char* service = getenv("HGW_EXEC_SERVICE");
28 // Not launched from hildon-games-wrapper
33 hgw = hgw_context_init();
36 fprintf(stderr, "Error opening hgw context\n");
41 printf("Loading in HGW mode\n");
46 if (!hgwLaunched) return;
48 hgw_context_destroy(hgw,
49 (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
56 if (!hgwLaunched) return;
58 Config.fullscreen = true;
60 char romFile[PATH_MAX + 1];
61 if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
62 S9xSetRomFile(romFile);
64 hgw_context_destroy(hgw, HGW_BYE_INACTIVE);
65 DIE("No Rom in Gconf!");
69 if (hgw_conf_request_bool(hgw, kGConfSound, &sound) == HGW_ERR_NONE) {
70 Config.enableAudio = sound ? true : false;
74 if (hgw_conf_request_bool(hgw, kGConfTurboMode, &turbo) == HGW_ERR_NONE) {
75 Settings.TurboMode = turbo ? TRUE : FALSE;
79 if (hgw_conf_request_int(hgw, kGConfFrameskip, &frameskip) == HGW_ERR_NONE) {
80 Settings.SkipFrames = (frameskip > 0 ? frameskip : AUTO_FRAMERATE);
83 char transparency = FALSE;
84 if (hgw_conf_request_bool(hgw, kGConfTransparency, &transparency) == HGW_ERR_NONE) {
85 Settings.Transparency = transparency ? TRUE : FALSE;
90 if (hgw_conf_request_bool(hgw, kGConfXSP, &xsp) == HGW_ERR_NONE) {
93 Config.scaler = strdup("2x");
98 char displayFramerate = FALSE;
99 if (hgw_conf_request_bool(hgw, kGConfDisplayFramerate, &displayFramerate) == HGW_ERR_NONE) {
100 Settings.DisplayFrameRate = displayFramerate ? TRUE : FALSE;
103 char displayControls = FALSE;
104 if (hgw_conf_request_bool(hgw, kGConfDisplayControls, &displayControls) == HGW_ERR_NONE) {
105 Config.touchscreenShow = displayControls ? true : false;
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;
117 Settings.HacksEnabled = TRUE;
118 Settings.HacksFilter = FALSE;
121 if (Settings.HacksEnabled && !Config.hacksFile) {
122 // Provide a default speedhacks file
123 if (asprintf(&Config.hacksFile, "%s/snesadvance.dat", dirname(romFile))
125 Config.hacksFile = 0; // malloc error.
127 // romFile[] is garbled from now on.
131 if (hgw_conf_request_int(hgw, kGConfMapping, &mappings) == HGW_ERR_NONE) {
134 // Do nothing, leave mappings as is.
137 parseGConfKeyMappings();
139 case 2: // Touchscreen
140 Config.touchscreenInput = true;
141 createActionMappingsOnly();
143 case 3: // Touchscreen + keys
144 Config.touchscreenInput = true;
145 parseGConfKeyMappings();
148 Settings.Mouse = TRUE;
149 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
150 createActionMappingsOnly();
152 case 5: // Mouse + keys
153 Settings.Mouse = TRUE;
154 Settings.ControllerOption = SNES_MOUSE;
155 parseGConfKeyMappings();
160 HgwStartCommand cmd = hgw_context_get_start_command(hgw);
163 case HGW_COMM_NONE: // called from libosso
165 Config.snapshotLoad = true;
166 Config.snapshotSave = true;
168 case HGW_COMM_RESTART:
169 Config.snapshotLoad = false;
170 Config.snapshotSave = true;
174 Config.snapshotLoad = false;
175 Config.snapshotSave = false;
176 Config.quitting = true;
183 if (!hgwLaunched) return;
186 HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
188 if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
189 // Message Incoming, process msg
192 case HGW_MSG_TYPE_CBREQ:
196 Config.quitting = true;
200 case HGW_MSG_TYPE_DEVSTATE:
202 case HGW_DEVICE_STATE_SHUTDOWN:
203 Config.quitting = true; // try to quit gracefully
212 hgw_msg_free_data(&msg);
216 // For now, please keep this in sync with ../gui/controls.c
217 typedef struct 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 \
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"),
250 static void createActionMappingsOnly()
252 // Discard any other mapping
253 ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
254 ZeroMemory(Config.action, sizeof(Config.action));
256 // Map quit to fullscreen, escape and task switch.
257 Config.action[72] = kActionQuit;
258 Config.action[9] = kActionQuit;
259 Config.action[71] = kActionQuit;
262 static void parseGConfKeyMappings()
264 // Discard any other mapping
265 ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
266 ZeroMemory(Config.action, sizeof(Config.action));
268 // If the user does not map fullscreen or quit
269 bool quit_mapped = false;
271 printf("Hgw: Using gconf key mappings\n");
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;
278 if (buttons[i].is_action) {
279 Config.action[scancode] |= buttons[i].mask;
280 if (buttons[i].mask & (kActionQuit | kActionToggleFullscreen)) {
284 Config.joypad1Mapping[scancode] |= buttons[i].mask;
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;
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;
305 // Force mapping of fullscreen to Quit if can't map anywhere else.
306 Config.joypad1Mapping[72] = 0;
307 Config.action[72] = kActionQuit;
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;