window-size sdl surfaces instead of snes
[drnoksnes] / platform / hgw.cpp
1 #include <stdio.h>
2 #include <hgw/hgw.h>
3
4 #include "platform.h"
5 #include "hgw.h"
6 #include "snes9x.h"
7
8 #define DIE(format, ...) do { \
9                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
10                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
11                 abort(); \
12         } while (0);
13
14
15 bool hgwLaunched;
16 static HgwContext *hgw;
17
18 static void parseGConfKeyMappings();
19
20 void HgwInit()
21 {
22         // hildon-games-wrapper sets this env variable for itself.
23         char* service = getenv("HGW_EXEC_SERVICE");
24         
25         if (!service) {
26                 // Not launched from hildon-games-wrapper
27                 hgwLaunched = false;
28                 return;
29         }
30         
31         hgw = hgw_context_init();
32         
33         if (!hgw) {
34                 fprintf(stderr, "Error opening hgw context\n");
35                 hgwLaunched = false;
36         }
37         
38         hgwLaunched = true;
39         printf("Loading in HGW mode\n");
40 }
41
42 void HgwDeinit()
43 {
44         if (!hgwLaunched) return;
45
46         hgw_context_destroy(hgw,
47                 (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
48
49         hgw = 0;
50 }
51
52 void HgwConfig()
53 {
54         if (!hgwLaunched) return;
55         
56         Config.fullscreen = true;
57         
58         char romFile[PATH_MAX + 1];
59         if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
60                 S9xSetRomFile(romFile);
61         } else {
62                 hgw_context_destroy(hgw, HGW_BYE_INACTIVE);
63                 DIE("No Rom in Gconf!");
64         }
65
66         char no_audio = FALSE;
67         if (hgw_conf_request_bool(hgw, kGConfDisableAudio, &no_audio) == HGW_ERR_NONE) {
68                 Config.enableAudio = no_audio ? false : true;
69         }
70
71         char turbo = FALSE;
72         if (hgw_conf_request_bool(hgw, kGConfTurboMode, &turbo) == HGW_ERR_NONE) {
73                 Settings.TurboMode = turbo ? TRUE : FALSE;
74         }
75
76         int frameskip = 0;
77         if (hgw_conf_request_int(hgw, kGConfFrameskip, &frameskip) == HGW_ERR_NONE) {
78                 Settings.SkipFrames = (frameskip > 0 ? frameskip : AUTO_FRAMERATE);
79         }
80
81         char transparency = FALSE;
82         if (hgw_conf_request_bool(hgw, kGConfTransparency, &transparency) == HGW_ERR_NONE) {
83                 Settings.Transparency = transparency ? TRUE : FALSE;
84         }
85
86         int speedhacks = 0;
87         if (hgw_conf_request_int(hgw, kGConfFrameskip, &speedhacks) == HGW_ERR_NONE) {
88                 if (speedhacks <= 0) {
89                         Settings.HacksEnabled = FALSE;
90                         Settings.HacksFilter = FALSE;
91                 } else if (speedhacks == 1) {
92                         Settings.HacksEnabled = TRUE;
93                         Settings.HacksFilter = TRUE;
94                 } else {
95                         Settings.HacksEnabled = TRUE;
96                         Settings.HacksFilter = FALSE;
97                 }
98         }
99
100         int mappings = 0;
101         if (hgw_conf_request_int(hgw, kGConfMapping, &mappings) == HGW_ERR_NONE) {
102                 switch (mappings) {
103                         case 1:
104                                 parseGConfKeyMappings();
105                                 break;
106                 }
107         }
108
109         HgwStartCommand cmd = hgw_context_get_start_command(hgw);
110         switch (cmd) {
111                 default:
112                 case HGW_COMM_NONE:     // called from libosso
113                 case HGW_COMM_CONT:
114                         Config.snapshotLoad = true;
115                         Config.snapshotSave = true;
116                         break;
117                 case HGW_COMM_RESTART:
118                         Config.snapshotLoad = false;
119                         Config.snapshotSave = true;
120                         break;
121                 case HGW_COMM_QUIT:
122                         // hum, what?
123                         Config.snapshotLoad = false;
124                         Config.snapshotSave = false;
125                         Config.quitting = true;
126                         break;
127         }
128 }
129
130 void HgwPollEvents()
131 {
132         if (!hgwLaunched) return;
133         
134         HgwMessage msg;
135         HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
136         
137         if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
138                 // Message Incoming, process msg
139                 
140                 switch (msg.type) {
141                         case HGW_MSG_TYPE_CBREQ:
142                                 switch (msg.e_val) {
143                                         case HGW_CB_QUIT:
144                                         case HGW_CB_EXIT:
145                                                 Config.quitting = true;
146                                                 break;
147                                 }
148                                 break;
149                         case HGW_MSG_TYPE_DEVSTATE:
150                                 switch (msg.e_val) {
151                                         case HGW_DEVICE_STATE_SHUTDOWN:
152                                                 Config.quitting = true; // try to quit gracefully
153                                                 break;
154                                 }
155                                 break;
156                         default:
157                                 // do nothing
158                                 break;
159                 }
160                 
161                 hgw_msg_free_data(&msg);
162         }
163 }
164
165 // For now, please keep this in sync with ../gui/controls.c
166 typedef struct ButtonEntry {
167         char * gconf_key;
168         unsigned long mask;
169         bool is_action;
170 } ButtonEntry;
171 #define BUTTON_INITIALIZER(button, name) \
172         { kGConfKeysPath "/" name, SNES_##button##_MASK, false }
173 #define ACTION_INITIALIZER(action, name) \
174         { kGConfKeysPath "/" name, kAction##action, true }
175 #define BUTTON_LAST     \
176         { 0 }
177 static const ButtonEntry buttons[] = {
178         BUTTON_INITIALIZER(A, "a"),
179         BUTTON_INITIALIZER(B, "b"),
180         BUTTON_INITIALIZER(X, "x"),
181         BUTTON_INITIALIZER(Y, "y"),
182         BUTTON_INITIALIZER(TL, "l"),
183         BUTTON_INITIALIZER(TR, "r"),
184         BUTTON_INITIALIZER(START, "start"),
185         BUTTON_INITIALIZER(SELECT, "select"),
186         BUTTON_INITIALIZER(UP, "up"),
187         BUTTON_INITIALIZER(DOWN, "down"),
188         BUTTON_INITIALIZER(LEFT, "left"),
189         BUTTON_INITIALIZER(RIGHT, "right"),
190         ACTION_INITIALIZER(Quit, "quit"),
191         ACTION_INITIALIZER(ToggleFullscreen, "fullscreen"),
192         BUTTON_LAST
193 };
194
195 static void parseGConfKeyMappings()
196 {
197         // Discard any other mapping
198         ZeroMemory(Config.joypad1Mapping, sizeof(Config.joypad1Mapping));
199         ZeroMemory(Config.action, sizeof(Config.action));
200
201         // If the user does not map fullscreen or quit
202         bool quit_mapped = false;
203
204         printf("Hgw: Using gconf key mappings\n");
205
206         int i, scancode;
207         for (i = 0; buttons[i].gconf_key; i++) {
208                 if (hgw_conf_request_int(hgw, buttons[i].gconf_key, &scancode) == HGW_ERR_NONE) {
209                         if (scancode <= 0 || scancode > 255) continue;
210
211                         if (buttons[i].is_action) {
212                                 Config.action[scancode] |= buttons[i].mask;
213                                 if (buttons[i].mask & (kActionQuit | kActionToggleFullscreen)) {
214                                         quit_mapped = true;
215                                 }
216                         } else {
217                                 Config.joypad1Mapping[scancode] |= buttons[i].mask;
218                         }
219                 }
220         }
221
222         // Safeguards
223         if (!quit_mapped) {
224                 // Newbie user won't know how to quit game.
225                 if (!Config.joypad1Mapping[72] && !Config.action[72]) {
226                         // Fullscreen key is not mapped, map
227                         Config.action[72] = kActionQuit;
228                         quit_mapped = true;
229                 }
230                 if (!quit_mapped && !Config.joypad1Mapping[9] && !Config.action[9]) {
231                         // Escape key is not mapped, map
232                         // But only if we couldn't map quit to fullscreen. Some people
233                         // actually want Quit not to be mapped.
234                         Config.action[9] = kActionQuit;
235                         quit_mapped = true;
236                 }
237                 if (!quit_mapped) {
238                         // Force mapping of fullscreen to Quit if can't map anywhere else.
239                         Config.joypad1Mapping[72] = 0;
240                         Config.action[72] = kActionQuit;
241                 }
242         }
243 }
244