2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
6 * - Interprocess communication
12 #include <ipcmessages.h>
16 #define AXWIN_PORT 4101
18 #define STATICBUF_SIZE 64
19 #define MAX_WINDOWS_PER_APP 128
22 typedef struct sIPC_Type tIPC_Type;
23 typedef struct sIPC_Client tIPC_Client;
27 int (*GetIdentSize)(const void *Ident);
28 int (*CompareIdent)(const void *Ident1, const void *Ident2);
29 void (*SendMessage)(const void *Ident, size_t Length, const void *Data);
34 const tIPC_Type *IPCType;
35 const void *Ident; // Stored after structure
44 void IPC_FillSelect(int *nfds, fd_set *set);
45 void IPC_HandleSelect(fd_set *set);
46 int IPC_Type_Datagram_GetSize(const void *Ident);
47 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
48 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
49 int IPC_Type_Sys_GetSize(const void *Ident);
50 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
51 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
52 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
55 const tIPC_Type gIPC_Type_Datagram = {
56 IPC_Type_Datagram_GetSize,
57 IPC_Type_Datagram_Compare,
58 IPC_Type_Datagram_Send
60 const tIPC_Type gIPC_Type_SysMessage = {
65 int giNetworkFileHandle = -1;
66 int giMessagesFileHandle = -1;
67 int giIPC_ClientCount;
68 tIPC_Client **gIPC_Clients;
75 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
76 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
79 void IPC_FillSelect(int *nfds, fd_set *set)
81 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
82 FD_SET(giNetworkFileHandle, set);
85 void IPC_HandleSelect(fd_set *set)
87 if( FD_ISSET(giNetworkFileHandle, set) )
89 char staticBuf[STATICBUF_SIZE];
90 int readlen, identlen;
93 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
95 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
96 msg = staticBuf + identlen;
98 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
99 _SysDebug("IPC_HandleSelect: UDP handled");
102 while(SysGetMessage(NULL, NULL))
105 int len = SysGetMessage(&tid, NULL);
107 SysGetMessage(NULL, data);
109 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
110 _SysDebug("IPC_HandleSelect: Message handled");
114 int IPC_Type_Datagram_GetSize(const void *Ident)
116 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
119 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
122 // - No need to worry about mis-matching sizes, as the size is computed
123 // from the 3rd/4th bytes, hence it will differ before the size is hit.
124 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
127 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
129 int identlen = IPC_Type_Datagram_GetSize(Ident);
130 char tmpbuf[ identlen + Length ];
131 memcpy(tmpbuf, Ident, identlen); // Header
132 memcpy(tmpbuf + identlen, Data, Length); // Data
133 // TODO: Handle fragmented packets
134 write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
137 int IPC_Type_Sys_GetSize(const void *Ident)
139 return sizeof(pid_t);
142 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
144 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
147 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
149 SysSendMessage( *(const tid_t*)Ident, Length, Data );
152 // --- Client -> Window Mappings
153 int _CompareClientPtrs(const void *_a, const void *_b)
155 tIPC_Client *a = *(tIPC_Client**)_a;
156 tIPC_Client *b = *(tIPC_Client**)_b;
158 if(a->IPCType < b->IPCType) return -1;
159 if(a->IPCType > b->IPCType) return 1;
161 return a->IPCType->CompareIdent(a->Ident, b->Ident);
164 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
166 int pos = 0; // Position where the new client will be inserted
170 // - Search list of registered clients
171 if(giIPC_ClientCount > 0)
177 target.IPCType = IPCType;
178 target.Ident = Ident;
179 ret = ⌖ // Abuse ret to get a pointer
181 div = giIPC_ClientCount;
186 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
187 // _SysDebug("Checking against %i gives %i", pos, cmp);
197 return gIPC_Clients[pos];
199 // Adjust pos to be the index where the new client will be placed
204 // - Create a new client entry
205 ident_size = IPCType->GetIdentSize(Ident);
206 // _SysDebug("ident_size = %i", ident_size);
207 ret = malloc( sizeof(tIPC_Client) + ident_size );
208 if(!ret) return NULL;
209 ret->IPCType = IPCType;
210 ret->Ident = ret + 1; // Get the end of the structure
211 memcpy( (void*)ret->Ident, Ident, ident_size );
215 // TODO: Register some way of detecting the client disconnecting
216 // > Wait on the thread / register with kernel somehow
217 // > Sockets are easier, but UDP is harder. Might get rid of it
220 giIPC_ClientCount ++;
221 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
222 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
223 gIPC_Clients[pos] = ret;
228 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
233 if( WindowID >= Client->nWindows ) {
234 // _SysDebug("Window %i out of range for %p (%i)", WindowID, Client, Client->nWindows);
238 return Client->Windows[WindowID];
241 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
243 if( WindowID >= MAX_WINDOWS_PER_APP )
246 if( WindowID >= Client->nWindows )
248 int oldCount = Client->nWindows;
249 Client->nWindows = WindowID + 1;
250 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
251 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
252 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
255 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
256 Client->Windows[WindowID] = WindowPtr;
259 // --- IPC Message Handlers ---
260 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
262 tIPCMsg_SendMsg *info = (void*)Msg->Data;
266 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
268 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
271 src = IPC_int_GetWindow(Client, Msg->Window);
273 dest = IPC_int_GetWindow(Client, info->Dest);
276 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
281 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
283 tIPCMsg_CreateWin *info = (void*)Msg->Data;
284 tWindow *newwin, *parent;
287 // > +1 is for NULL byte on string
288 if( Msg->Size < sizeof(tIPCMsg_CreateWin) + 1 )
290 if( info->Renderer[Msg->Size - sizeof(tIPCMsg_CreateWin)] != '\0' )
293 // - Get the parent window ID
294 parent = IPC_int_GetWindow(Client, Msg->Window);
296 // Catch creating a window with an existing ID
297 if( IPC_int_GetWindow(Client, info->NewWinID) )
300 // - Create the new window, and save its pointer
301 newwin = WM_CreateWindow(parent, info->RendererArg, info->Renderer);
302 IPC_int_SetWindow(Client, info->NewWinID, newwin);
307 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
309 tIPCMsg_ShowWindow *info = (void*)Msg->Data;
312 if( Msg->Size < sizeof(*info) ) return -1;
314 win = IPC_int_GetWindow(Client, Msg->Window);
317 WM_ShowWindow(win, !!info->bShow);
322 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
324 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
327 if(Msg->Size < sizeof(*info)) return -1;
329 win = IPC_int_GetWindow(Client, Msg->Window);
332 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
335 WM_MoveWindow(win, info->X, info->Y);
337 WM_ResizeWindow(win, info->W, info->H);
342 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
347 _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
348 IPCType, Ident, MsgLen, Msg);
350 if( MsgLen < sizeof(tAxWin_IPCMessage) )
352 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
355 client = IPC_int_GetClient(IPCType, Ident);
357 switch((enum eAxWin_IPCMessageTypes) Msg->ID)
359 // --- Ping message (reset timeout and get server version)
361 _SysDebug(" IPC_Handle: IPCMSG_PING");
362 if( Msg->Size < 4 ) return;
363 if( Msg->Flags & IPCMSG_FLAG_RETURN )
365 Msg->ID = IPCMSG_PING;
366 Msg->Size = sizeof(tIPCMsg_Return);
367 ((tIPCMsg_Return*)Msg->Data)->Value = AXWIN_VERSION;
368 IPCType->SendMessage(Ident, sizeof(tIPCMsg_Return), Msg);
372 // --- Send a message
374 _SysDebug(" IPC_Handle: IPCMSG_SENDMSG %i", ((tIPCMsg_SendMsg*)Msg->Data)->ID);
375 rv = IPC_Msg_SendMsg(client, Msg);
379 case IPCMSG_CREATEWIN:
380 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
381 rv = IPC_Msg_CreateWin(client, Msg);
383 // --- Show/Hide a window
384 case IPCMSG_SHOWWINDOW:
385 _SysDebug(" IPC_Handle: IPCMSG_SHOWWINDOW");
386 rv = IPC_Msg_ShowWindow(client, Msg);
388 // --- Move/Resize a window
389 case IPCMSG_SETWINPOS:
390 _SysDebug(" IPC_Handle: IPCMSG_SETWINPOS");
391 rv = IPC_Msg_SetWinPos(client, Msg);
394 // --- Unknown message
396 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
397 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
401 _SysDebug("IPC_Handle: rv = %i", rv);