2 * AxWin4 Interface Library
3 * - By John Hodge (thePowersGang)
8 #include <axwin4/axwin.h>
9 #include "include/common.hpp"
10 #include "include/IIPCChannel.hpp"
11 #include "include/CIPCChannel_AcessIPCPipe.hpp"
12 #include <ipc_proto.hpp>
22 IIPCChannel* gIPCChannel;
23 ::std::mutex glSyncReply;
25 CDeserialiser gSyncReplyBuf;
27 extern "C" bool AxWin4_Connect(const char *URI)
29 _SysDebug("AxWin4_Connect('%s')", URI);
34 if( strncmp(URI, "ipcpipe://", 3+4+3) == 0 )
36 gIPCChannel = new CIPCChannel_AcessIPCPipe(URI+3+4+3);
40 _SysDebug("Unknown protocol");
44 catch( const ::std::exception& e )
46 fprintf(stderr, "AxWin4_Connect: %s\n", e.what());
52 extern "C" bool AxWin4_PeekEventQueue(void)
57 extern "C" bool AxWin4_WaitEventQueue(uint64_t Timeout)
59 return AxWin4_WaitEventQueueSelect(0, NULL, NULL, NULL, Timeout);
62 extern "C" bool AxWin4_WaitEventQueueSelect(int nFDs, fd_set *rfds, fd_set *wfds, fd_set *efds, uint64_t Timeout)
70 int64_t select_timeout = Timeout;
71 int64_t *select_timeout_p = (Timeout ? &select_timeout : 0);
73 nFDs = ::std::max(nFDs, gIPCChannel->FillSelect(*rfds));
74 _SysSelect(nFDs, rfds, wfds, efds, select_timeout_p, 0);
75 return gIPCChannel->HandleSelect(*rfds);
78 void SendMessage(CSerialiser& message)
80 gIPCChannel->Send(message);
82 void RecvMessage(CDeserialiser& message)
84 uint8_t id = message.ReadU8();
88 // Flag reply and take a copy of this message
91 // Oh... that was unexpected
92 _SysDebug("Unexpected second reply message %i", message.ReadU8());
96 gSyncReplyValid = true;
97 gSyncReplyBuf = message;
101 _SysDebug("TODO: RecvMessage(%i)", id);
106 CDeserialiser GetSyncReply(CSerialiser& request, unsigned int Message)
108 ::std::lock_guard<std::mutex> lock(glSyncReply);
109 gSyncReplyValid = false;
110 // Send once lock is acquired
111 SendMessage(request);
113 while( !gSyncReplyValid )
116 if( !AxWin4_WaitEventQueue(0) )
117 throw ::std::runtime_error("Connection dropped while waiting for reply");
120 uint8_t id = gSyncReplyBuf.ReadU8();
123 _SysDebug("Unexpected reply message '%i', message.ReadU8()");
126 return gSyncReplyBuf;
129 extern "C" void AxWin4_GetScreenDimensions(unsigned int ScreenIndex, unsigned int *Width, unsigned int *Height)
132 req.WriteU8(IPCMSG_GETGLOBAL);
133 req.WriteU16(IPC_GLOBATTR_SCREENDIMS);
134 req.WriteU8(ScreenIndex);
136 CDeserialiser response = GetSyncReply(req, IPCMSG_GETGLOBAL);
137 if( response.ReadU16() != IPC_GLOBATTR_SCREENDIMS ) {
141 *Width = response.ReadU16();
142 *Height = response.ReadU16();
144 _SysDebug("AxWin4_GetScreenDimensions: %i = %ix%i", ScreenIndex, *Width, *Height);
147 IIPCChannel::~IIPCChannel()
151 }; // namespace AxWin