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!");
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;
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;
88 char displayFramerate = FALSE;
89 if (hgw_conf_request_bool(hgw, kGConfDisplayFramerate, &displayFramerate) == HGW_ERR_NONE) {
90 Settings.DisplayFrameRate = displayFramerate ? TRUE : FALSE;
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;
102 Settings.HacksEnabled = TRUE;
103 Settings.HacksFilter = FALSE;
106 if (Settings.HacksEnabled && !Config.hacksFile) {
107 // Provide a default speedhacks file
108 if (asprintf(&Config.hacksFile, "%s/snesadvance.dat", dirname(romFile))
110 Config.hacksFile = 0; // malloc error.
112 // romFile[] is garbled from now on.
116 if (hgw_conf_request_int(hgw, kGConfMapping, &mappings) == HGW_ERR_NONE) {
119 // Do nothing, leave mappings as is.
122 parseGConfKeyMappings();
124 case 2: // Touchscreen
125 Config.touchscreenInput = true;
126 createActionMappingsOnly();
128 case 3: // Touchscreen + keys
129 Config.touchscreenInput = true;
130 parseGConfKeyMappings();
133 Settings.Mouse = TRUE;
134 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
135 createActionMappingsOnly();
137 case 5: // Mouse + keys
138 Settings.Mouse = TRUE;
139 Settings.ControllerOption = SNES_MOUSE;
140 parseGConfKeyMappings();
145 HgwStartCommand cmd = hgw_context_get_start_command(hgw);
148 case HGW_COMM_NONE: // called from libosso
150 Config.snapshotLoad = true;
151 Config.snapshotSave = true;
153 case HGW_COMM_RESTART:
154 Config.snapshotLoad = false;
155 Config.snapshotSave = true;
159 Config.snapshotLoad = false;
160 Config.snapshotSave = false;
161 Config.quitting = true;
168 if (!hgwLaunched) return;
171 HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
173 if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
174 // Message Incoming, process msg
177 case HGW_MSG_TYPE_CBREQ:
181 Config.quitting = true;
185 case HGW_MSG_TYPE_DEVSTATE:
187 case HGW_DEVICE_STATE_SHUTDOWN:
188 Config.quitting = true; // try to quit gracefully
197 hgw_msg_free_data(&msg);
201 // For now, please keep this in sync with ../gui/controls.c
202 typedef struct 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 \
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"),
231 static void createActionMappingsOnly()
233 // Discard any other mapping
234 ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
235 ZeroMemory(Config.action, sizeof(Config.action));
237 // Map quit to fullscreen and escape
238 Config.action[72] = kActionQuit;
239 Config.action[9] = kActionQuit;
242 static void parseGConfKeyMappings()
244 // Discard any other mapping
245 ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
246 ZeroMemory(Config.action, sizeof(Config.action));
248 // If the user does not map fullscreen or quit
249 bool quit_mapped = false;
251 printf("Hgw: Using gconf key mappings\n");
254 for (i = 0; buttons[i].gconf_key; i++) {
255 if (hgw_conf_request_int(hgw, buttons[i].gconf_key, &scancode) == HGW_ERR_NONE) {
256 if (scancode <= 0 || scancode > 255) continue;
258 if (buttons[i].is_action) {
259 Config.action[scancode] |= buttons[i].mask;
260 if (buttons[i].mask & (kActionQuit | kActionToggleFullscreen)) {
264 Config.joypad1Mapping[scancode] |= buttons[i].mask;
271 // Newbie user won't know how to quit game.
272 if (!Config.joypad1Mapping[72] && !Config.action[72]) {
273 // Fullscreen key is not mapped, map
274 Config.action[72] = kActionQuit;
277 if (!quit_mapped && !Config.joypad1Mapping[9] && !Config.action[9]) {
278 // Escape key is not mapped, map
279 // But only if we couldn't map quit to fullscreen. Some people
280 // actually want Quit not to be mapped.
281 Config.action[9] = kActionQuit;
285 // Force mapping of fullscreen to Quit if can't map anywhere else.
286 Config.joypad1Mapping[72] = 0;
287 Config.action[72] = kActionQuit;