fixing some hgw related bugs and preparing release
[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         // TODO Handle cmd in some way other than assuming HGW_COMM_RESTART
40         
41         printf("Loading in HGW mode\n");
42 }
43
44 void HgwDeinit()
45 {
46         if (!hgwLaunched) return;
47         
48         hgw_context_destroy(hgw, HGW_BYE_INACTIVE);  // TODO
49         hgw = 0;
50 }
51
52 void HgwConfig()
53 {
54         if (!hgwLaunched) return;
55         
56         Config.fullscreen = true;
57         
58         char romFile[PATH_MAX];
59         if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
60                 S9xSetRomFile(romFile);
61         } else {
62                 DIE("No Rom in Gconf!");
63         }
64 }
65
66 void HgwPollEvents()
67 {
68         if (!hgwLaunched) return;
69         
70         HgwMessage msg;
71         HgwMessageFlags flags = HGW_MSG_FLAG_NONE;
72         
73         if ( hgw_msg_check_incoming(hgw, &msg, flags) == HGW_ERR_COMMUNICATION ) {
74                 // Message Incoming, process msg
75                 
76                 switch (msg.type) {
77                         case HGW_MSG_TYPE_CBREQ:
78                                 switch (msg.e_val) {
79                                         case HGW_CB_QUIT:
80                                         case HGW_CB_EXIT:
81                                                 Config.quitting = true;
82                                                 break;
83                                 }
84                                 break;
85                 }
86                 
87                 hgw_msg_free_data(&msg);
88         }
89 }
90
91