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;
26 int (*GetIdentSize)(const void *Ident);
27 int (*CompareIdent)(const void *Ident1, const void *Ident2);
28 void (*SendMessage)(const void *Ident, size_t Length, const void *Data);
33 const tIPC_Type *IPCType;
34 const void *Ident; // Stored after structure
43 void IPC_FillSelect(int *nfds, fd_set *set);
44 void IPC_HandleSelect(fd_set *set);
45 int IPC_Type_Datagram_GetSize(const void *Ident);
46 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
47 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
48 int IPC_Type_Sys_GetSize(const void *Ident);
49 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
50 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
51 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
54 const tIPC_Type gIPC_Type_Datagram = {
55 IPC_Type_Datagram_GetSize,
56 IPC_Type_Datagram_Compare,
57 IPC_Type_Datagram_Send
59 const tIPC_Type gIPC_Type_SysMessage = {
64 int giNetworkFileHandle = -1;
65 int giMessagesFileHandle = -1;
66 int giIPC_ClientCount;
67 tIPC_Client **gIPC_Clients;
74 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
75 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
78 void IPC_FillSelect(int *nfds, fd_set *set)
80 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
81 FD_SET(giNetworkFileHandle, set);
84 void IPC_HandleSelect(fd_set *set)
86 if( FD_ISSET(giNetworkFileHandle, set) )
88 char staticBuf[STATICBUF_SIZE];
89 int readlen, identlen;
92 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
94 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
95 msg = staticBuf + identlen;
97 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
98 // _SysDebug("IPC_HandleSelect: UDP handled");
101 while(SysGetMessage(NULL, NULL))
104 int len = SysGetMessage(&tid, NULL);
106 SysGetMessage(NULL, data);
108 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
109 // _SysDebug("IPC_HandleSelect: Message handled");
113 int IPC_Type_Datagram_GetSize(const void *Ident)
115 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
118 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
121 // - No need to worry about mis-matching sizes, as the size is computed
122 // from the 3rd/4th bytes, hence it will differ before the size is hit.
123 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
126 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
128 int identlen = IPC_Type_Datagram_GetSize(Ident);
129 char tmpbuf[ identlen + Length ];
130 memcpy(tmpbuf, Ident, identlen); // Header
131 memcpy(tmpbuf + identlen, Data, Length); // Data
132 // TODO: Handle fragmented packets
133 write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
136 int IPC_Type_Sys_GetSize(const void *Ident)
138 return sizeof(pid_t);
141 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
143 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
146 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
148 SysSendMessage( *(const tid_t*)Ident, Length, Data );
151 // --- Client -> Window Mappings
152 int _CompareClientPtrs(const void *_a, const void *_b)
154 tIPC_Client *a = *(tIPC_Client**)_a;
155 tIPC_Client *b = *(tIPC_Client**)_b;
157 if(a->IPCType < b->IPCType) return -1;
158 if(a->IPCType > b->IPCType) return 1;
160 return a->IPCType->CompareIdent(a->Ident, b->Ident);
163 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
165 int pos = 0; // Position where the new client will be inserted
169 // - Search list of registered clients
170 if(giIPC_ClientCount > 0)
176 target.IPCType = IPCType;
177 target.Ident = Ident;
178 ret = ⌖ // Abuse ret to get a pointer
180 div = giIPC_ClientCount;
185 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
186 // _SysDebug("Checking against %i gives %i", pos, cmp);
196 return gIPC_Clients[pos];
198 // Adjust pos to be the index where the new client will be placed
203 // - Create a new client entry
204 ident_size = IPCType->GetIdentSize(Ident);
205 // _SysDebug("ident_size = %i", ident_size);
206 ret = malloc( sizeof(tIPC_Client) + ident_size );
207 if(!ret) return NULL;
208 ret->IPCType = IPCType;
209 ret->Ident = ret + 1; // Get the end of the structure
210 memcpy( (void*)ret->Ident, Ident, ident_size );
214 // TODO: Register some way of detecting the client disconnecting
215 // > Wait on the thread / register with kernel somehow
216 // > Sockets are easier, but UDP is harder. Might get rid of it
219 giIPC_ClientCount ++;
220 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
221 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
222 gIPC_Clients[pos] = ret;
227 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
232 if( WindowID >= Client->nWindows ) {
233 // _SysDebug("Window %i out of range for %p (%i)", WindowID, Client, Client->nWindows);
237 return Client->Windows[WindowID];
240 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
242 if( WindowID >= MAX_WINDOWS_PER_APP )
245 if( WindowID >= Client->nWindows )
247 int oldCount = Client->nWindows;
248 Client->nWindows = WindowID + 1;
249 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
250 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
251 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
254 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
255 Client->Windows[WindowID] = WindowPtr;
258 // --- IPC Message Handlers ---
259 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
261 tIPCMsg_SendMsg *info = (void*)Msg->Data;
265 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
267 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
270 src = IPC_int_GetWindow(Client, Msg->Window);
272 dest = IPC_int_GetWindow(Client, info->Remote);
275 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
280 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
282 tIPCMsg_CreateWin *info = (void*)Msg->Data;
283 tWindow *newwin, *parent;
286 // > +1 is for NULL byte on string
287 if( Msg->Size < sizeof(tIPCMsg_CreateWin) + 1 )
289 if( info->Renderer[Msg->Size - sizeof(tIPCMsg_CreateWin)] != '\0' )
292 // - Get the parent window ID
293 parent = IPC_int_GetWindow(Client, Msg->Window);
295 // Catch creating a window with an existing ID
296 if( IPC_int_GetWindow(Client, info->NewWinID) )
299 // - Create the new window, and save its pointer
300 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
301 IPC_int_SetWindow(Client, info->NewWinID, newwin);
306 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
308 tIPCMsg_ShowWindow *info = (void*)Msg->Data;
311 if( Msg->Size < sizeof(*info) ) return -1;
313 win = IPC_int_GetWindow(Client, Msg->Window);
316 WM_ShowWindow(win, !!info->bShow);
321 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
323 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
326 if(Msg->Size < sizeof(*info)) return -1;
328 win = IPC_int_GetWindow(Client, Msg->Window);
331 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
334 WM_MoveWindow(win, info->X, info->Y);
336 WM_ResizeWindow(win, info->W, info->H);
341 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
343 tAxWin_IPCMessage *ret_hdr;
344 tIPCMsg_ReturnInt *ret;
345 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
347 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
349 ret_hdr = (void*)buf;
350 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
352 ret_hdr->Window = -1;
353 ret_hdr->Size = sizeof(*ret);
354 ret = (void*)ret_hdr->Data;
355 ret->Value = 1; // HARD CODE - Current version only supports one display
357 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
361 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
363 tIPCMsg_GetDisplayDims *info;
364 tAxWin_IPCMessage *ret_hdr;
365 tIPCMsg_RetDisplayDims *ret;
366 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
368 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
370 info = (void*)Msg->Data;
372 ret_hdr = (void*)buf;
373 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
375 ret_hdr->Window = -1;
376 ret_hdr->Size = sizeof(*ret);
377 ret = (void*)ret_hdr->Data;
379 // HARD CODE! Only one display supported
380 if( info->DisplayID == 0 )
384 ret->W = giScreenWidth;
385 ret->H = giScreenHeight;
389 ret->X = 0; ret->Y = 0;
390 ret->W = 0; ret->H = 0;
393 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
397 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
402 _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
403 IPCType, Ident, MsgLen, Msg);
405 if( MsgLen < sizeof(tAxWin_IPCMessage) )
407 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
410 client = IPC_int_GetClient(IPCType, Ident);
412 switch((enum eAxWin_IPCMessageTypes) Msg->ID)
414 // --- Ping message (reset timeout and get server version)
416 _SysDebug(" IPC_Handle: IPCMSG_PING");
417 if( Msg->Size < 4 ) return;
418 if( Msg->Flags & IPCMSG_FLAG_RETURN )
420 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
421 Msg->ID = IPCMSG_PING;
422 Msg->Size = sizeof(*ret);
423 ret->Value = AXWIN_VERSION;
424 IPCType->SendMessage(Ident, sizeof(*Msg)+sizeof(*ret), Msg);
428 // -- Get display count
429 case IPCMSG_GETDISPLAYCOUNT:
430 rv = IPC_Msg_GetDisplayCount(client, Msg);
433 // --- Get display dimensions
434 case IPCMSG_GETDISPLAYDIMS:
435 rv = IPC_Msg_GetDisplayDims(client, Msg);
438 // --- Send a message
440 _SysDebug(" IPC_Handle: IPCMSG_SENDMSG %i", ((tIPCMsg_SendMsg*)Msg->Data)->ID);
441 rv = IPC_Msg_SendMsg(client, Msg);
445 case IPCMSG_CREATEWIN:
446 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
447 rv = IPC_Msg_CreateWin(client, Msg);
449 // --- Show/Hide a window
450 case IPCMSG_SHOWWINDOW:
451 _SysDebug(" IPC_Handle: IPCMSG_SHOWWINDOW");
452 rv = IPC_Msg_ShowWindow(client, Msg);
454 // --- Move/Resize a window
455 case IPCMSG_SETWINPOS:
456 _SysDebug(" IPC_Handle: IPCMSG_SETWINPOS");
457 rv = IPC_Msg_SetWinPos(client, Msg);
460 // --- Unknown message
462 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
463 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
467 _SysDebug("IPC_Handle: rv = %i", rv);
470 // --- Server->Client replies
471 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
473 tAxWin_IPCMessage *hdr;
474 tIPCMsg_SendMsg *msg;
475 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
478 msg = (void*)hdr->Data;
480 hdr->ID = IPCMSG_SENDMSG;
482 hdr->Size = sizeof(*msg) + Len;
488 memcpy(msg->Data, Data, Len);
490 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);