2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
6 * - Interprocess communication
12 #include <ipcmessages.h>
15 #include <wm_internals.h>
16 #include <wm_hotkeys.h> // Hotkey registration
17 #include <wm_renderer.h> // Renderer IPC messages
19 #define AXWIN_PORT 4101
21 #define STATICBUF_SIZE 64
22 #define MAX_WINDOWS_PER_APP 128
25 typedef struct sIPC_Type tIPC_Type;
29 int (*GetIdentSize)(const void *Ident);
30 int (*CompareIdent)(const void *Ident1, const void *Ident2);
31 void (*SendMessage)(const void *Ident, size_t Length, const void *Data);
36 const tIPC_Type *IPCType;
37 const void *Ident; // Stored after structure
44 extern tWindow *gpWM_FocusedWindow; // Needed for _FocusWindow
48 void IPC_FillSelect(int *nfds, fd_set *set);
49 void IPC_HandleSelect(fd_set *set);
50 int IPC_Type_Datagram_GetSize(const void *Ident);
51 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
52 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
53 int IPC_Type_Sys_GetSize(const void *Ident);
54 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
55 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
56 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
59 const tIPC_Type gIPC_Type_Datagram = {
60 IPC_Type_Datagram_GetSize,
61 IPC_Type_Datagram_Compare,
62 IPC_Type_Datagram_Send
64 const tIPC_Type gIPC_Type_SysMessage = {
69 int giNetworkFileHandle = -1;
70 int giMessagesFileHandle = -1;
71 int giIPC_ClientCount;
72 tIPC_Client **gIPC_Clients;
79 giNetworkFileHandle = _SysOpen("/Devices/ip/loop/udp", OPENFLAG_READ);
80 if( giNetworkFileHandle != -1 )
83 _SysIOCtl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
87 void IPC_FillSelect(int *nfds, fd_set *set)
89 if( giNetworkFileHandle != -1 )
91 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
92 FD_SET(giNetworkFileHandle, set);
96 void IPC_HandleSelect(fd_set *set)
98 if( giNetworkFileHandle != -1 )
100 if( FD_ISSET(giNetworkFileHandle, set) )
102 char staticBuf[STATICBUF_SIZE];
103 int readlen, identlen;
106 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
108 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
109 msg = staticBuf + identlen;
111 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
112 // _SysDebug("IPC_HandleSelect: UDP handled");
118 while( (len = _SysGetMessage(&tid, 0, NULL)) )
121 _SysGetMessage(NULL, len, data);
123 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
124 // _SysDebug("IPC_HandleSelect: Message handled");
128 int IPC_Type_Datagram_GetSize(const void *Ident)
130 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
133 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
136 // - No need to worry about mis-matching sizes, as the size is computed
137 // from the 3rd/4th bytes, hence it will differ before the size is hit.
138 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
141 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
143 int identlen = IPC_Type_Datagram_GetSize(Ident);
144 char tmpbuf[ identlen + Length ];
145 memcpy(tmpbuf, Ident, identlen); // Header
146 memcpy(tmpbuf + identlen, Data, Length); // Data
147 // TODO: Handle fragmented packets
148 _SysWrite(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
151 int IPC_Type_Sys_GetSize(const void *Ident)
153 return sizeof(pid_t);
156 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
158 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
161 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
163 _SysSendMessage( *(const tid_t*)Ident, Length, Data );
166 // --- Client -> Window Mappings
167 int _CompareClientPtrs(const void *_a, const void *_b)
169 tIPC_Client *a = *(tIPC_Client**)_a;
170 tIPC_Client *b = *(tIPC_Client**)_b;
172 if(a->IPCType < b->IPCType) return -1;
173 if(a->IPCType > b->IPCType) return 1;
175 return a->IPCType->CompareIdent(a->Ident, b->Ident);
178 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
180 int pos = 0; // Position where the new client will be inserted
184 // - Search list of registered clients
185 if(giIPC_ClientCount > 0)
191 target.IPCType = IPCType;
192 target.Ident = Ident;
193 ret = ⌖ // Abuse ret to get a pointer
195 div = giIPC_ClientCount;
200 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
201 // _SysDebug("Checking against %i gives %i", pos, cmp);
211 return gIPC_Clients[pos];
213 // Adjust pos to be the index where the new client will be placed
218 // - Create a new client entry
219 ident_size = IPCType->GetIdentSize(Ident);
220 // _SysDebug("ident_size = %i", ident_size);
221 ret = malloc( sizeof(tIPC_Client) + ident_size );
222 if(!ret) return NULL;
223 ret->IPCType = IPCType;
224 ret->Ident = ret + 1; // Get the end of the structure
225 memcpy( (void*)ret->Ident, Ident, ident_size );
229 // TODO: Register some way of detecting the client disconnecting
230 // > Wait on the thread / register with kernel somehow
231 // > Sockets are easier, but UDP is harder. Might get rid of it
234 giIPC_ClientCount ++;
235 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
236 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
237 gIPC_Clients[pos] = ret;
242 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
247 if( WindowID >= Client->nWindows ) {
251 return Client->Windows[WindowID];
254 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
256 if( WindowID >= MAX_WINDOWS_PER_APP )
259 if( WindowID >= Client->nWindows )
261 int oldCount = Client->nWindows;
262 Client->nWindows = WindowID + 1;
263 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
264 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
265 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
268 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
269 Client->Windows[WindowID] = WindowPtr;
272 // --- IPC Message Handlers ---
273 int IPC_Msg_Ping(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
275 ASSERT(Msg->ID == IPCMSG_PING);
277 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
279 if( Msg->Size < 4 ) return -1;
281 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
282 Msg->ID = IPCMSG_PING;
283 Msg->Size = sizeof(*ret);
284 ret->Value = AXWIN_VERSION;
285 Client->IPCType->SendMessage(Client->Ident, sizeof(*Msg)+sizeof(*ret), Msg);
289 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
291 tAxWin_IPCMessage *ret_hdr;
292 tIPCMsg_ReturnInt *ret;
293 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
295 ASSERT(Msg->ID == IPCMSG_GETDISPLAYCOUNT);
297 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
299 ret_hdr = (void*)buf;
300 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
302 ret_hdr->Window = -1;
303 ret_hdr->Size = sizeof(*ret);
304 ret = (void*)ret_hdr->Data;
305 ret->Value = 1; // HARD CODE - Current version only supports one display
307 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
311 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
313 tIPCMsg_GetDisplayDims *info;
314 tAxWin_IPCMessage *ret_hdr;
315 tIPCMsg_RetDisplayDims *ret;
316 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
318 ASSERT(Msg->ID == IPCMSG_GETDISPLAYDIMS);
320 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
322 info = (void*)Msg->Data;
324 ret_hdr = (void*)buf;
325 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
327 ret_hdr->Window = -1;
328 ret_hdr->Size = sizeof(*ret);
329 ret = (void*)ret_hdr->Data;
331 // HARD CODE! Only one display supported
332 if( info->DisplayID == 0 )
336 ret->W = giScreenWidth;
337 ret->H = giScreenHeight;
341 ret->X = 0; ret->Y = 0;
342 ret->W = 0; ret->H = 0;
345 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
349 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
351 tIPCMsg_SendMsg *info = (void*)Msg->Data;
354 ASSERT(Msg->ID == IPCMSG_SENDMSG);
357 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
359 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
362 src = IPC_int_GetWindow(Client, Msg->Window);
364 dest = IPC_int_GetWindow(Client, info->Remote);
367 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
372 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
374 tIPCMsg_CreateWin *info = (void*)Msg->Data;
375 tWindow *newwin, *parent;
377 ASSERT(Msg->ID == IPCMSG_CREATEWIN);
380 // > +1 is for NULL byte on string
381 if( Msg->Size < sizeof(*info) + 1 ) {
382 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
385 if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
386 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
387 _SysDebug("info = {");
388 _SysDebug(" .NewWinID = %i", info->NewWinID);
389 _SysDebug(" .RendererArg = %i", info->RendererArg);
390 _SysDebug(" .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
395 // - Get the parent window ID
396 parent = IPC_int_GetWindow(Client, Msg->Window);
398 // Catch creating a window with an existing ID
399 if( IPC_int_GetWindow(Client, info->NewWinID) )
402 // - Create the new window, and save its pointer
403 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
404 IPC_int_SetWindow(Client, info->NewWinID, newwin);
409 int IPC_Msg_SetWindowTitle(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
413 ASSERT(Msg->ID == IPCMSG_SETWINTITLE);
415 if( Msg->Size < 1 ) return -1;
416 if( Msg->Data[ Msg->Size-1 ] != '\0' ) return -1;
418 win = IPC_int_GetWindow(Client, Msg->Window);
421 WM_SetWindowTitle(win, Msg->Data);
426 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
428 tIPCMsg_Boolean *info = (void*)Msg->Data;
431 ASSERT(Msg->ID == IPCMSG_SHOWWINDOW);
433 if( Msg->Size < sizeof(*info) ) return -1;
435 win = IPC_int_GetWindow(Client, Msg->Window);
438 WM_ShowWindow(win, !!info->Value);
443 int IPC_Msg_DecorateWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
445 tIPCMsg_Boolean *info = (void*)Msg->Data;
448 if( Msg->Size < sizeof(*info) ) return -1;
450 win = IPC_int_GetWindow(Client, Msg->Window);
453 WM_DecorateWindow(win, !!info->Value);
457 int IPC_Msg_FocusWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
461 ASSERT(Msg->ID == IPCMSG_FOCUSWINDOW);
463 // Don't allow the focus to be changed unless the client has the focus
464 if(!gpWM_FocusedWindow) return 1;
465 if(gpWM_FocusedWindow->Client != Client) return 1;
467 win = IPC_int_GetWindow(Client, Msg->Window);
475 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
477 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
480 ASSERT(Msg->ID == IPCMSG_SETWINPOS);
482 if(Msg->Size < sizeof(*info)) return -1;
484 win = IPC_int_GetWindow(Client, Msg->Window);
487 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
490 WM_MoveWindow(win, info->X, info->Y);
492 WM_ResizeWindow(win, info->W, info->H);
497 int IPC_Msg_RegisterAction(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
499 tIPCMsg_RegAction *info = (void*)Msg->Data;
502 ASSERT(Msg->ID == IPCMSG_REGACTION);
504 if( Msg->Size < sizeof(*info) + 1 )
507 win = IPC_int_GetWindow(Client, Msg->Window);
510 if( strnlen(info->Action, Msg->Size - sizeof(*info)) == Msg->Size - sizeof(*info) )
513 _SysDebug("RegisterAction %p:%i [%i]\"%s\"",
514 Client, Msg->Window, info->Index, info->Action
517 WM_Hotkey_RegisterAction(info->Action, win, info->Index);
522 int (*gIPC_MessageHandlers[])(tIPC_Client *Client, tAxWin_IPCMessage *Msg) = {
524 IPC_Msg_GetDisplayCount,
525 IPC_Msg_GetDisplayDims,
528 NULL, // Destroy window
529 IPC_Msg_SetWindowTitle,
531 IPC_Msg_DecorateWindow,
534 IPC_Msg_RegisterAction
536 const int giIPC_NumMessageHandlers = sizeof(gIPC_MessageHandlers)/sizeof(gIPC_MessageHandlers[0]);
538 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
543 // _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
544 // IPCType, Ident, MsgLen, Msg);
546 if( MsgLen < sizeof(tAxWin_IPCMessage) )
548 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
551 client = IPC_int_GetClient(IPCType, Ident);
556 if( Msg->Flags & IPCMSG_FLAG_RENDERER )
558 tWindow *win = IPC_int_GetWindow(client, Msg->Window);
560 _SysDebug("WARNING: NULL window in message %i", Msg->ID);
563 tWMRenderer *renderer = win->Renderer;
564 if( Msg->ID >= renderer->nIPCHandlers ) {
565 _SysDebug("WARNING: Message %i out of range in %s", Msg->ID, renderer->Name);
568 if( !renderer->IPCHandlers[Msg->ID] ) {
569 _SysDebug("WARNING: Message %i has no handler in %s", Msg->ID, renderer->Name);
572 _SysDebug("IPC_Handle: Call %s-%i", renderer->Name, Msg->ID);
573 rv = renderer->IPCHandlers[Msg->ID](win, Msg->Size, Msg->Data);
575 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
579 if( Msg->ID >= giIPC_NumMessageHandlers ) {
580 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
581 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
585 if( !gIPC_MessageHandlers[ Msg->ID ] ) {
586 fprintf(stderr, "WARNING: Message %i does not have a handler\n", Msg->ID);
587 _SysDebug("WARNING: Message %i does not have a handler", Msg->ID);
591 _SysDebug("IPC_Handle: Call WM-%i", Msg->ID);
592 rv = gIPC_MessageHandlers[Msg->ID](client, Msg);
594 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
598 // Dispatch a message to the client
599 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
601 tAxWin_IPCMessage *hdr;
602 tIPCMsg_SendMsg *msg;
603 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
606 msg = (void*)hdr->Data;
608 hdr->ID = IPCMSG_SENDMSG;
610 hdr->Size = sizeof(*msg) + Len;
616 memcpy(msg->Data, Data, Len);
618 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
621 // --- Server->Client replies
622 void IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data)
624 tAxWin_IPCMessage *hdr;
625 char buf[sizeof(*hdr)+Len];
630 hdr->Flags = IPCMSG_FLAG_RENDERER;
634 memcpy(hdr->Data, Data, Len);
635 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);