snapshot support in gui
[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         printf("Loading in HGW mode\n");
39 }
40
41 void HgwDeinit()
42 {
43         if (!hgwLaunched) return;
44
45         hgw_context_destroy(hgw,
46                 (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
47
48         hgw = 0;
49 }
50
51 void HgwConfig()
52 {
53         if (!hgwLaunched) return;
54         
55         Config.fullscreen = true;
56         
57         char romFile[PATH_MAX];
58         if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
59                 S9xSetRomFile(romFile);
60         } else {
61                 hgw_context_destroy(hgw, HGW_BYE_INACTIVE);
62                 DIE("No Rom in Gconf!");
63         }
64
65         HgwStartCommand cmd = hgw_context_get_start_command(hgw);
66         switch (cmd) {
67                 default:
68                 case HGW_COMM_NONE:     // called from libosso
69                 case HGW_COMM_CONT:
70                         Config.snapshotLoad = true;
71                         Config.snapshotSave = true;
72                         break;
73                 case HGW_COMM_RESTART:
74                         Config.snapshotLoad = false;
75                         Config.snapshotSave = true;
76                         break;
77                 case HGW_COMM_QUIT:
78                         // hum, what?
79                         Config.snapshotLoad = false;
80                         Config.snapshotSave = false;
81                         Config.quitting = true;
82                         break;
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