2 * Acess GUI (AxWin) Version 2
3 * By John Hodge (thePowersGang)
8 #include <axwin2/messages.h>
11 #define AXWIN_PORT 4101
13 #define STATICBUF_SIZE 64
19 void IPC_FillSelect(int *nfds, fd_set *set);
20 void IPC_HandleSelect(fd_set *set);
21 void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg);
22 int IPC_Type_Datagram_GetSize(void *Ident);
23 int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2);
24 void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data);
25 int IPC_Type_Sys_GetSize(void *Ident);
26 int IPC_Type_Sys_Compare(void *Ident1, void *Ident2);
27 void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data);
30 int giNetworkFileHandle = -1;
31 int giMessagesFileHandle = -1;
32 tIPC_Type gIPC_Type_Datagram = {
33 IPC_Type_Datagram_GetSize,
34 IPC_Type_Datagram_Compare,
35 IPC_Type_Datagram_Send
37 tIPC_Type gIPC_Type_SysMessage = {
48 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
49 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
52 void IPC_FillSelect(int *nfds, fd_set *set)
54 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
55 FD_SET(giNetworkFileHandle, set);
58 void IPC_HandleSelect(fd_set *set)
60 if( FD_ISSET(giNetworkFileHandle, set) )
62 char staticBuf[STATICBUF_SIZE];
63 int readlen, identlen;
66 readlen = read(giNetworkFileHandle, sizeof(staticBuf), staticBuf);
68 // Assume that all connections are from localhost
69 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
70 msg = staticBuf + identlen;
72 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
75 while(SysGetMessage(NULL, NULL))
78 int len = SysGetMessage(&tid, NULL);
80 SysGetMessage(NULL, data);
82 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
86 void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg)
90 if( MsgLen < sizeof(tAxWin_Message) )
92 if( MsgLen < sizeof(tAxWin_Message) + Msg->Size )
95 app = AxWin_GetClient(IPCType, Ident);
97 switch((enum eAxWin_Messages) Msg->ID)
99 // --- Ping message (reset timeout and get server version)
101 if( MsgLen < sizeof(tAxWin_Message) + 4 ) return;
102 Msg->ID = MSG_SRSP_VERSION;
106 *(uint16_t*)&Msg->Data[2] = -1;
107 IPCType->SendMessage(Ident, sizeof(Msg->ID), Msg);
111 // --- Register an application
112 case MSG_SREQ_REGISTER:
113 if( Msg->Data[Msg->Size-1] != '\0' ) {
119 _SysDebug("Notice: Duplicate registration (%s)\n", Msg->Data);
123 // TODO: Should this function be implemented here?
124 AxWin_RegisterClient(IPCType, Ident, Msg->Data);
127 // --- Create a window
128 case MSG_SREQ_ADDWIN:
129 if( Msg->Data[Msg->Size-1] != '\0' ) {
136 // --- Unknown message
138 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
139 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
144 int IPC_Type_Datagram_GetSize(void *Ident)
146 return 4 + Net_GetAddressSize( ((uint16_t*)Ident)[1] );
149 int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2)
152 // - No need to worry about mis-matching sizes, as the size is computed
153 // from the 3rd/4th bytes, hence it will differ before the size is hit.
154 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
157 void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data)
159 int identlen = IPC_Type_Datagram_GetSize(Ident);
160 char tmpbuf[ identlen + Length ];
161 memcpy(tmpbuf, Ident, identlen); // Header
162 memcpy(tmpbuf + identlen, Data, Length); // Data
163 // TODO: Handle fragmented packets
164 write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf);
167 int IPC_Type_Sys_GetSize(void *Ident)
169 return sizeof(pid_t);
172 int IPC_Type_Sys_Compare(void *Ident1, void *Ident2)
174 return *(int*)Ident1 - *(int*)Ident2;
177 void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data)
179 SysSendMessage( *(tid_t*)Ident, Length, Data );