preparing emu for gui snapshot support
[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
16 bool hgwLaunched;
17 static HgwContext *hgw;
18
19 void HgwInit()
20 {
21         // hildon-games-wrapper sets this env variable for itself.
22         char* service = getenv("HGW_EXEC_SERVICE");
23         
24         if (!service) {
25                 // Not launched from hildon-games-wrapper
26                 hgwLaunched = false;
27                 return;
28         }
29         
30         hgw = hgw_context_init();
31         
32         if (!hgw) {
33                 fprintf(stderr, "Error opening hgw context\n");
34                 hgwLaunched = false;
35         }
36         
37         hgwLaunched = true;
38         HgwStartCommand cmd = hgw_context_get_start_command(hgw);
39
40         switch (cmd) {
41                 default:
42                 case HGW_COMM_NONE:     // called from libosso
43                 case HGW_COMM_CONT:
44                         Config.snapshotLoad = true;
45                         Config.snapshotSave = true;
46                         break;
47                 case HGW_COMM_RESTART:
48                         Config.snapshotLoad = false;
49                         Config.snapshotSave = true;
50                         break;
51                 case HGW_COMM_QUIT:
52                         // hum, what?
53                         Config.snapshotLoad = false;
54                         Config.snapshotSave = false;
55                         Config.quitting = true;
56                         break;
57         }
58
59         printf("Loading in HGW mode\n");
60 }
61
62 void HgwDeinit()
63 {
64         if (!hgwLaunched) return;
65
66         hgw_context_destroy(hgw,
67                 (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
68
69         hgw = 0;
70 }
71
72 void HgwConfig()
73 {
74         if (!hgwLaunched) return;
75         
76         Config.fullscreen = true;
77         
78         char romFile[PATH_MAX];
79         if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
80                 S9xSetRomFile(romFile);
81         } else {
82                 DIE("No Rom in Gconf!");
83         }
84 }
85
86 void HgwPollEvents()
87 {
88         if (!hgwLaunched) return;
89         
90         HgwMessage msg;
91         HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
92         
93         if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
94                 // Message Incoming, process msg
95                 
96                 switch (msg.type) {
97                         case HGW_MSG_TYPE_CBREQ:
98                                 switch (msg.e_val) {
99                                         case HGW_CB_QUIT:
100                                         case HGW_CB_EXIT:
101                                                 Config.quitting = true;
102                                                 break;
103                                 }
104                                 break;
105                         case HGW_MSG_TYPE_DEVSTATE:
106                                 switch (msg.e_val) {
107                                         case HGW_DEVICE_STATE_SHUTDOWN:
108                                                 Config.quitting = true; // try to quit gracefully
109                                                 break;
110                                 }
111                                 break;
112                         default:
113                                 // do nothing
114                                 break;
115                 }
116                 
117                 hgw_msg_free_data(&msg);
118         }
119 }
120
121