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 int IPC_Type_IPCPipe_GetSize(const void *Ident);
57 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2);
58 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data);
59 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident);
60 void IPC_Handle(tIPC_Client *Client, size_t MsgLen, tAxWin_IPCMessage *Msg);
63 const tIPC_Type gIPC_Type_Datagram = {
64 IPC_Type_Datagram_GetSize,
65 IPC_Type_Datagram_Compare,
66 IPC_Type_Datagram_Send
68 const tIPC_Type gIPC_Type_SysMessage = {
73 const tIPC_Type gIPC_Type_IPCPipe = {
74 IPC_Type_IPCPipe_GetSize,
75 IPC_Type_IPCPipe_Compare,
78 int giNetworkFileHandle = -1;
79 int giIPCPipeHandle = -1;
80 int giIPC_ClientCount;
81 tIPC_Client **gIPC_Clients;
88 giNetworkFileHandle = _SysOpen("/Devices/ip/loop/udp", OPENFLAG_READ);
89 if( giNetworkFileHandle != -1 )
92 _SysIOCtl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
95 giIPCPipeHandle = _SysOpen("/Devices/ipcpipe/axwin"/*-$USER*/, OPENFLAG_CREATE);
96 _SysDebug("giIPCPipeHandle = %i", giIPCPipeHandle);
97 if( giIPCPipeHandle == -1 )
98 _SysDebug("ERROR: Can't create IPCPipe handle");
101 void _setfd(int fd, int *nfds, fd_set *set)
105 if( fd >= *nfds ) *nfds = fd+1;
110 void IPC_FillSelect(int *nfds, fd_set *set)
112 _setfd(giNetworkFileHandle, nfds, set);
113 _setfd(giIPCPipeHandle, nfds, set);
114 for( int i = 0; i < giIPC_ClientCount; i ++ )
116 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
117 _setfd( *(int*)(gIPC_Clients[i]->Ident), nfds, set );
121 void IPC_HandleSelect(fd_set *set)
123 if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
125 char staticBuf[STATICBUF_SIZE];
126 int readlen, identlen;
129 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
131 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
132 msg = staticBuf + identlen;
134 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
135 //_SysDebug("IPC_HandleSelect: UDP handled");
138 if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
140 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
141 _SysDebug("newfd = %i");
142 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
145 for( int i = 0; i < giIPC_ClientCount; i ++ )
147 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
149 int fd = *(const int*)gIPC_Clients[i]->Ident;
150 if( FD_ISSET(fd, set) )
152 char staticBuf[STATICBUF_SIZE];
154 len = _SysRead(fd, staticBuf, sizeof(staticBuf));
155 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
162 while( (len = _SysGetMessage(&tid, 0, NULL)) )
165 _SysGetMessage(NULL, len, data);
167 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
168 // _SysDebug("IPC_HandleSelect: Message handled");
172 int IPC_Type_Datagram_GetSize(const void *Ident)
174 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
177 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
180 // - No need to worry about mis-matching sizes, as the size is computed
181 // from the 3rd/4th bytes, hence it will differ before the size is hit.
182 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
185 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
187 int identlen = IPC_Type_Datagram_GetSize(Ident);
188 char tmpbuf[ identlen + Length ];
189 memcpy(tmpbuf, Ident, identlen); // Header
190 memcpy(tmpbuf + identlen, Data, Length); // Data
191 // TODO: Handle fragmented packets
192 _SysWrite(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
195 int IPC_Type_Sys_GetSize(const void *Ident)
197 return sizeof(pid_t);
200 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
202 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
205 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
207 _SysSendMessage( *(const tid_t*)Ident, Length, Data );
210 int IPC_Type_IPCPipe_GetSize(const void *Ident)
214 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
216 return *(const int*)Ident1 - *(const int*)Ident2;
218 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
220 size_t rv = _SysWrite( *(const int*)Ident, Data, Length );
222 _SysDebug("Sent message oversize %x", Length);
226 // --- Client -> Window Mappings
227 int _CompareClientPtrs(const void *_a, const void *_b)
229 tIPC_Client *a = *(tIPC_Client**)_a;
230 tIPC_Client *b = *(tIPC_Client**)_b;
235 if(a->IPCType < b->IPCType) return -1;
236 if(a->IPCType > b->IPCType) return 1;
238 return a->IPCType->CompareIdent(a->Ident, b->Ident);
241 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
243 int pos = 0; // Position where the new client will be inserted
247 // - Search list of registered clients
248 if(giIPC_ClientCount > 0)
254 target.IPCType = IPCType;
255 target.Ident = Ident;
256 ret = ⌖ // Abuse ret to get a pointer
258 div = giIPC_ClientCount;
263 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
264 // _SysDebug("Checking against %i gives %i", pos, cmp);
274 return gIPC_Clients[pos];
276 // Adjust pos to be the index where the new client will be placed
281 // - Create a new client entry
282 ident_size = IPCType->GetIdentSize(Ident);
283 // _SysDebug("ident_size = %i", ident_size);
284 ret = malloc( sizeof(tIPC_Client) + ident_size );
285 if(!ret) return NULL;
286 ret->IPCType = IPCType;
287 ret->Ident = ret + 1; // Get the end of the structure
288 memcpy( (void*)ret->Ident, Ident, ident_size );
292 // TODO: Register some way of detecting the client disconnecting
293 // > Wait on the thread / register with kernel somehow
294 // > Sockets are easier, but UDP is harder. Might get rid of it
297 giIPC_ClientCount ++;
298 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
299 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
300 gIPC_Clients[pos] = ret;
305 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
310 if( WindowID >= Client->nWindows ) {
314 return Client->Windows[WindowID];
317 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
319 if( WindowID >= MAX_WINDOWS_PER_APP )
322 if( WindowID >= Client->nWindows )
324 int oldCount = Client->nWindows;
325 Client->nWindows = WindowID + 1;
326 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
327 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
328 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
331 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
332 Client->Windows[WindowID] = WindowPtr;
335 // --- IPC Message Handlers ---
336 int IPC_Msg_Ping(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
338 ASSERT(Msg->ID == IPCMSG_PING);
340 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
342 if( Msg->Size < 4 ) return -1;
344 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
345 Msg->ID = IPCMSG_PING;
346 Msg->Size = sizeof(*ret);
347 ret->Value = AXWIN_VERSION;
348 Client->IPCType->SendMessage(Client->Ident, sizeof(*Msg)+sizeof(*ret), Msg);
352 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
354 tAxWin_IPCMessage *ret_hdr;
355 tIPCMsg_ReturnInt *ret;
356 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
358 ASSERT(Msg->ID == IPCMSG_GETDISPLAYCOUNT);
360 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
362 ret_hdr = (void*)buf;
363 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
365 ret_hdr->Window = -1;
366 ret_hdr->Size = sizeof(*ret);
367 ret = (void*)ret_hdr->Data;
368 ret->Value = 1; // HARD CODE - Current version only supports one display
370 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
374 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
376 tIPCMsg_GetDisplayDims *info;
377 tAxWin_IPCMessage *ret_hdr;
378 tIPCMsg_RetDisplayDims *ret;
379 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
381 ASSERT(Msg->ID == IPCMSG_GETDISPLAYDIMS);
383 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
385 info = (void*)Msg->Data;
387 ret_hdr = (void*)buf;
388 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
390 ret_hdr->Window = -1;
391 ret_hdr->Size = sizeof(*ret);
392 ret = (void*)ret_hdr->Data;
394 // HARD CODE! Only one display supported
395 if( info->DisplayID == 0 )
399 ret->W = giScreenWidth;
400 ret->H = giScreenHeight;
404 ret->X = 0; ret->Y = 0;
405 ret->W = 0; ret->H = 0;
408 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
412 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
414 tIPCMsg_SendMsg *info = (void*)Msg->Data;
417 ASSERT(Msg->ID == IPCMSG_SENDMSG);
420 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
422 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
425 src = IPC_int_GetWindow(Client, Msg->Window);
427 dest = IPC_int_GetWindow(Client, info->Remote);
430 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
435 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
437 tIPCMsg_CreateWin *info = (void*)Msg->Data;
438 tWindow *newwin, *parent;
440 ASSERT(Msg->ID == IPCMSG_CREATEWIN);
443 // > +1 is for NULL byte on string
444 if( Msg->Size < sizeof(*info) + 1 ) {
445 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
448 if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
449 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
450 _SysDebug("info = {");
451 _SysDebug(" .NewWinID = %i", info->NewWinID);
452 _SysDebug(" .RendererArg = %i", info->RendererArg);
453 _SysDebug(" .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
458 // - Get the parent window ID
459 parent = IPC_int_GetWindow(Client, Msg->Window);
461 // Catch creating a window with an existing ID
462 if( IPC_int_GetWindow(Client, info->NewWinID) )
465 // - Create the new window, and save its pointer
466 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
467 IPC_int_SetWindow(Client, info->NewWinID, newwin);
472 int IPC_Msg_DestroyWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
476 ASSERT(Msg->ID == IPCMSG_DESTROYWIN);
478 win = IPC_int_GetWindow(Client, Msg->Window);
482 WM_DestroyWindow(win);
486 int IPC_Msg_SetWindowTitle(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
490 ASSERT(Msg->ID == IPCMSG_SETWINTITLE);
492 if( Msg->Size < 1 ) return -1;
493 if( Msg->Data[ Msg->Size-1 ] != '\0' ) return -1;
495 win = IPC_int_GetWindow(Client, Msg->Window);
498 WM_SetWindowTitle(win, Msg->Data);
503 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
505 tIPCMsg_Boolean *info = (void*)Msg->Data;
508 ASSERT(Msg->ID == IPCMSG_SHOWWINDOW);
510 if( Msg->Size < sizeof(*info) ) return -1;
512 win = IPC_int_GetWindow(Client, Msg->Window);
515 WM_ShowWindow(win, !!info->Value);
520 int IPC_Msg_DecorateWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
522 tIPCMsg_Boolean *info = (void*)Msg->Data;
525 if( Msg->Size < sizeof(*info) ) return -1;
527 win = IPC_int_GetWindow(Client, Msg->Window);
530 WM_DecorateWindow(win, !!info->Value);
534 int IPC_Msg_FocusWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
538 ASSERT(Msg->ID == IPCMSG_FOCUSWINDOW);
540 // Don't allow the focus to be changed unless the client has the focus
541 // if(!gpWM_FocusedWindow) return 1;
542 // if(gpWM_FocusedWindow->Client != Client) return 1;
544 win = IPC_int_GetWindow(Client, Msg->Window);
552 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
554 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
557 ASSERT(Msg->ID == IPCMSG_SETWINPOS);
559 if(Msg->Size < sizeof(*info)) return -1;
561 win = IPC_int_GetWindow(Client, Msg->Window);
564 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
567 WM_MoveWindow(win, info->X, info->Y);
569 WM_ResizeWindow(win, info->W, info->H);
574 int IPC_Msg_RegisterAction(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
576 tIPCMsg_RegAction *info = (void*)Msg->Data;
579 ASSERT(Msg->ID == IPCMSG_REGACTION);
581 if( Msg->Size < sizeof(*info) + 1 )
584 win = IPC_int_GetWindow(Client, Msg->Window);
587 if( strnlen(info->Action, Msg->Size - sizeof(*info)) == Msg->Size - sizeof(*info) )
590 _SysDebug("RegisterAction %p:%i [%i]\"%s\"",
591 Client, Msg->Window, info->Index, info->Action
594 WM_Hotkey_RegisterAction(info->Action, win, info->Index);
599 int (*gIPC_MessageHandlers[])(tIPC_Client *Client, tAxWin_IPCMessage *Msg) = {
601 IPC_Msg_GetDisplayCount,
602 IPC_Msg_GetDisplayDims,
605 IPC_Msg_DestroyWin, // Destroy window
606 IPC_Msg_SetWindowTitle,
608 IPC_Msg_DecorateWindow,
611 IPC_Msg_RegisterAction
613 const int giIPC_NumMessageHandlers = sizeof(gIPC_MessageHandlers)/sizeof(gIPC_MessageHandlers[0]);
615 void IPC_Handle(tIPC_Client *Client, size_t MsgLen, tAxWin_IPCMessage *Msg)
619 // _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
620 // IPCType, Ident, MsgLen, Msg);
622 if( MsgLen < sizeof(tAxWin_IPCMessage) )
624 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
627 if( Msg->Flags & IPCMSG_FLAG_RENDERER )
629 tWindow *win = IPC_int_GetWindow(Client, Msg->Window);
631 _SysDebug("WARNING: NULL window in message %i", Msg->ID);
634 tWMRenderer *renderer = win->Renderer;
635 if( Msg->ID >= renderer->nIPCHandlers ) {
636 _SysDebug("WARNING: Message %i out of range in %s", Msg->ID, renderer->Name);
639 if( !renderer->IPCHandlers[Msg->ID] ) {
640 _SysDebug("WARNING: Message %i has no handler in %s", Msg->ID, renderer->Name);
643 _SysDebug("IPC_Handle: Call %s-%i", renderer->Name, Msg->ID);
644 rv = renderer->IPCHandlers[Msg->ID](win, Msg->Size, Msg->Data);
646 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
650 if( Msg->ID >= giIPC_NumMessageHandlers ) {
651 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Client);
652 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, Client);
656 if( !gIPC_MessageHandlers[ Msg->ID ] ) {
657 fprintf(stderr, "WARNING: Message %i does not have a handler\n", Msg->ID);
658 _SysDebug("WARNING: Message %i does not have a handler", Msg->ID);
662 _SysDebug("IPC_Handle: Call WM-%i", Msg->ID);
663 rv = gIPC_MessageHandlers[Msg->ID](Client, Msg);
665 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
669 // Dispatch a message to the client
670 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
672 tAxWin_IPCMessage *hdr;
673 tIPCMsg_SendMsg *msg;
674 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
677 msg = (void*)hdr->Data;
679 hdr->ID = IPCMSG_SENDMSG;
681 hdr->Size = sizeof(*msg) + Len;
687 memcpy(msg->Data, Data, Len);
689 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
692 // --- Server->Client replies
693 void IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data)
695 tAxWin_IPCMessage *hdr;
696 char buf[sizeof(*hdr)+Len];
701 hdr->Flags = IPCMSG_FLAG_RENDERER;
705 memcpy(hdr->Data, Data, Len);
706 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);