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_int_DropClient(tIPC_Client *Client);
61 void IPC_Handle(tIPC_Client *Client, size_t MsgLen, tAxWin_IPCMessage *Msg);
64 const tIPC_Type gIPC_Type_Datagram = {
65 IPC_Type_Datagram_GetSize,
66 IPC_Type_Datagram_Compare,
67 IPC_Type_Datagram_Send
69 const tIPC_Type gIPC_Type_SysMessage = {
74 const tIPC_Type gIPC_Type_IPCPipe = {
75 IPC_Type_IPCPipe_GetSize,
76 IPC_Type_IPCPipe_Compare,
79 int giNetworkFileHandle = -1;
80 int giIPCPipeHandle = -1;
81 int giIPC_ClientCount;
82 tIPC_Client **gIPC_Clients;
89 giNetworkFileHandle = _SysOpen("/Devices/ip/loop/udp", OPENFLAG_READ);
90 if( giNetworkFileHandle != -1 )
93 _SysIOCtl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
96 giIPCPipeHandle = _SysOpen("/Devices/ipcpipe/axwin"/*-$USER*/, OPENFLAG_CREATE);
97 _SysDebug("giIPCPipeHandle = %i", giIPCPipeHandle);
98 if( giIPCPipeHandle == -1 )
99 _SysDebug("ERROR: Can't create IPCPipe handle");
102 void _setfd(int fd, int *nfds, fd_set *set)
106 if( fd >= *nfds ) *nfds = fd+1;
111 void IPC_FillSelect(int *nfds, fd_set *set)
113 _setfd(giNetworkFileHandle, nfds, set);
114 _setfd(giIPCPipeHandle, nfds, set);
115 for( int i = 0; i < giIPC_ClientCount; i ++ )
117 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
118 _setfd( *(int*)(gIPC_Clients[i]->Ident), nfds, set );
122 void IPC_HandleSelect(fd_set *set)
124 if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
126 char staticBuf[STATICBUF_SIZE];
127 int readlen, identlen;
130 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
132 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
133 msg = staticBuf + identlen;
135 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
136 //_SysDebug("IPC_HandleSelect: UDP handled");
139 if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
141 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
142 _SysDebug("newfd = %i", newfd);
143 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
146 for( int i = 0; i < giIPC_ClientCount; i ++ )
148 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
150 int fd = *(const int*)gIPC_Clients[i]->Ident;
151 if( FD_ISSET(fd, set) )
153 char staticBuf[STATICBUF_SIZE];
155 len = _SysRead(fd, staticBuf, sizeof(staticBuf));
156 if( len == (size_t)-1 ) {
157 // TODO: Check errno for EINTR
158 IPC_int_DropClient(gIPC_Clients[i]);
161 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
168 while( (len = _SysGetMessage(&tid, 0, NULL)) )
171 _SysGetMessage(NULL, len, data);
173 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
174 // _SysDebug("IPC_HandleSelect: Message handled");
178 int IPC_Type_Datagram_GetSize(const void *Ident)
180 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
183 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
186 // - No need to worry about mis-matching sizes, as the size is computed
187 // from the 3rd/4th bytes, hence it will differ before the size is hit.
188 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
191 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
193 int identlen = IPC_Type_Datagram_GetSize(Ident);
194 char tmpbuf[ identlen + Length ];
195 memcpy(tmpbuf, Ident, identlen); // Header
196 memcpy(tmpbuf + identlen, Data, Length); // Data
197 // TODO: Handle fragmented packets
198 _SysWrite(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
201 int IPC_Type_Sys_GetSize(const void *Ident)
203 return sizeof(pid_t);
206 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
208 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
211 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
213 _SysSendMessage( *(const tid_t*)Ident, Length, Data );
216 int IPC_Type_IPCPipe_GetSize(const void *Ident)
220 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
222 return *(const int*)Ident1 - *(const int*)Ident2;
224 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
226 size_t rv = _SysWrite( *(const int*)Ident, Data, Length );
228 _SysDebug("Sent message oversize %x", Length);
232 // --- Client -> Window Mappings
233 int _CompareClientPtrs(const void *_a, const void *_b)
235 tIPC_Client *a = *(tIPC_Client**)_a;
236 tIPC_Client *b = *(tIPC_Client**)_b;
241 if(a->IPCType < b->IPCType) return -1;
242 if(a->IPCType > b->IPCType) return 1;
244 return a->IPCType->CompareIdent(a->Ident, b->Ident);
247 int IPC_int_BSearchClients(const tIPC_Client *TargetClient, int *Pos)
253 div = giIPC_ClientCount;
258 _SysDebug("Cmp with %i [%i] (%p)", pos, div, gIPC_Clients[pos]);
259 cmp = _CompareClientPtrs(&TargetClient, &gIPC_Clients[pos]);
260 // _SysDebug("Checking against %i gives %i", pos, cmp);
274 // Adjust pos to be the index where the new client will be placed
280 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
282 int pos = 0; // Position where the new client will be inserted
286 // - Search list of registered clients
287 if(giIPC_ClientCount > 0)
290 target.IPCType = IPCType;
291 target.Ident = Ident;
292 if( IPC_int_BSearchClients(&target, &pos) )
293 return gIPC_Clients[pos];
297 // - Create a new client entry
298 ident_size = IPCType->GetIdentSize(Ident);
299 // _SysDebug("ident_size = %i", ident_size);
300 ret = malloc( sizeof(tIPC_Client) + ident_size );
301 if(!ret) return NULL;
302 ret->IPCType = IPCType;
303 ret->Ident = ret + 1; // Get the end of the structure
304 memcpy( (void*)ret->Ident, Ident, ident_size );
308 // TODO: Register some way of detecting the client disconnecting
309 // > Wait on the thread / register with kernel somehow
310 // > Sockets are easier, but UDP is harder. Might get rid of it
313 giIPC_ClientCount ++;
314 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
315 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
316 gIPC_Clients[pos] = ret;
321 void IPC_int_DropClient(tIPC_Client *Client)
323 // Remove from client list
325 if( !IPC_int_BSearchClients(Client, &pos) ) {
326 _SysDebug("IPC_int_DropClient: Can't find client %p", Client);
330 giIPC_ClientCount --;
331 memmove(&gIPC_Clients[pos], &gIPC_Clients[pos+1], (giIPC_ClientCount-pos)*sizeof(tIPC_Client*));
333 // Terminate client's windows
334 // - If there were active windows, show an error?
335 int nWindowsDestroyed = 0;
336 for(int i = 0; i < Client->nWindows; i ++)
338 if( Client->Windows[i] )
340 _SysDebug("Window %p:%i %p still exists", Client, i, Client->Windows[i]);
341 WM_DestroyWindow(Client->Windows[i]);
342 nWindowsDestroyed ++;
345 if( nWindowsDestroyed )
347 _SysDebug("TODO: Show notice that application exited without destroying windows");
350 // Free client structure
352 _SysDebug("Dropped client %p", Client);
355 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
360 if( WindowID >= Client->nWindows ) {
364 return Client->Windows[WindowID];
367 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
369 if( WindowID >= MAX_WINDOWS_PER_APP )
372 if( WindowID >= Client->nWindows )
374 int oldCount = Client->nWindows;
375 Client->nWindows = WindowID + 1;
376 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
377 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
378 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
381 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
382 Client->Windows[WindowID] = WindowPtr;
385 // --- IPC Message Handlers ---
386 int IPC_Msg_Ping(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
388 ASSERT(Msg->ID == IPCMSG_PING);
390 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
392 if( Msg->Size < 4 ) return -1;
394 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
395 Msg->ID = IPCMSG_PING;
396 Msg->Size = sizeof(*ret);
397 ret->Value = AXWIN_VERSION;
398 Client->IPCType->SendMessage(Client->Ident, sizeof(*Msg)+sizeof(*ret), Msg);
402 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
404 tAxWin_IPCMessage *ret_hdr;
405 tIPCMsg_ReturnInt *ret;
406 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
408 ASSERT(Msg->ID == IPCMSG_GETDISPLAYCOUNT);
410 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
412 ret_hdr = (void*)buf;
413 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
415 ret_hdr->Window = -1;
416 ret_hdr->Size = sizeof(*ret);
417 ret = (void*)ret_hdr->Data;
418 ret->Value = 1; // HARD CODE - Current version only supports one display
420 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
424 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
426 tIPCMsg_GetDisplayDims *info;
427 tAxWin_IPCMessage *ret_hdr;
428 tIPCMsg_RetDisplayDims *ret;
429 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
431 ASSERT(Msg->ID == IPCMSG_GETDISPLAYDIMS);
433 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
435 info = (void*)Msg->Data;
437 ret_hdr = (void*)buf;
438 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
440 ret_hdr->Window = -1;
441 ret_hdr->Size = sizeof(*ret);
442 ret = (void*)ret_hdr->Data;
444 // HARD CODE! Only one display supported
445 if( info->DisplayID == 0 )
449 ret->W = giScreenWidth;
450 ret->H = giScreenHeight;
454 ret->X = 0; ret->Y = 0;
455 ret->W = 0; ret->H = 0;
458 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
462 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
464 tIPCMsg_SendMsg *info = (void*)Msg->Data;
467 ASSERT(Msg->ID == IPCMSG_SENDMSG);
470 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
472 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
475 src = IPC_int_GetWindow(Client, Msg->Window);
477 dest = IPC_int_GetWindow(Client, info->Remote);
480 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
485 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
487 tIPCMsg_CreateWin *info = (void*)Msg->Data;
488 tWindow *newwin, *parent;
490 ASSERT(Msg->ID == IPCMSG_CREATEWIN);
493 // > +1 is for NULL byte on string
494 if( Msg->Size < sizeof(*info) + 1 ) {
495 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
498 if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
499 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
500 _SysDebug("info = {");
501 _SysDebug(" .NewWinID = %i", info->NewWinID);
502 _SysDebug(" .RendererArg = %i", info->RendererArg);
503 _SysDebug(" .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
508 // - Get the parent window ID
509 parent = IPC_int_GetWindow(Client, Msg->Window);
511 // Catch creating a window with an existing ID
512 if( IPC_int_GetWindow(Client, info->NewWinID) )
515 // - Create the new window, and save its pointer
516 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
517 IPC_int_SetWindow(Client, info->NewWinID, newwin);
522 int IPC_Msg_DestroyWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
526 ASSERT(Msg->ID == IPCMSG_DESTROYWIN);
528 win = IPC_int_GetWindow(Client, Msg->Window);
532 WM_DestroyWindow(win);
533 IPC_int_SetWindow(Client, Msg->Window, NULL);
537 int IPC_Msg_SetWindowTitle(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
541 ASSERT(Msg->ID == IPCMSG_SETWINTITLE);
543 if( Msg->Size < 1 ) return -1;
544 if( Msg->Data[ Msg->Size-1 ] != '\0' ) return -1;
546 win = IPC_int_GetWindow(Client, Msg->Window);
549 WM_SetWindowTitle(win, Msg->Data);
554 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
556 tIPCMsg_Boolean *info = (void*)Msg->Data;
559 ASSERT(Msg->ID == IPCMSG_SHOWWINDOW);
561 if( Msg->Size < sizeof(*info) ) return -1;
563 win = IPC_int_GetWindow(Client, Msg->Window);
566 WM_ShowWindow(win, !!info->Value);
571 int IPC_Msg_DecorateWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
573 tIPCMsg_Boolean *info = (void*)Msg->Data;
576 if( Msg->Size < sizeof(*info) ) return -1;
578 win = IPC_int_GetWindow(Client, Msg->Window);
581 WM_DecorateWindow(win, !!info->Value);
585 int IPC_Msg_FocusWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
589 ASSERT(Msg->ID == IPCMSG_FOCUSWINDOW);
591 // Don't allow the focus to be changed unless the client has the focus
592 // if(!gpWM_FocusedWindow) return 1;
593 // if(gpWM_FocusedWindow->Client != Client) return 1;
595 win = IPC_int_GetWindow(Client, Msg->Window);
603 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
605 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
608 ASSERT(Msg->ID == IPCMSG_SETWINPOS);
610 if(Msg->Size < sizeof(*info)) return -1;
612 win = IPC_int_GetWindow(Client, Msg->Window);
615 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
618 WM_MoveWindow(win, info->X, info->Y);
620 WM_ResizeWindow(win, info->W, info->H);
625 int IPC_Msg_RegisterAction(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
627 tIPCMsg_RegAction *info = (void*)Msg->Data;
630 ASSERT(Msg->ID == IPCMSG_REGACTION);
632 if( Msg->Size < sizeof(*info) + 1 )
635 win = IPC_int_GetWindow(Client, Msg->Window);
638 if( strnlen(info->Action, Msg->Size - sizeof(*info)) == Msg->Size - sizeof(*info) )
641 _SysDebug("RegisterAction %p:%i [%i]\"%s\"",
642 Client, Msg->Window, info->Index, info->Action
645 WM_Hotkey_RegisterAction(info->Action, win, info->Index);
650 int (*gIPC_MessageHandlers[])(tIPC_Client *Client, tAxWin_IPCMessage *Msg) = {
652 IPC_Msg_GetDisplayCount,
653 IPC_Msg_GetDisplayDims,
656 IPC_Msg_DestroyWin, // Destroy window
657 IPC_Msg_SetWindowTitle,
659 IPC_Msg_DecorateWindow,
662 IPC_Msg_RegisterAction
664 const int giIPC_NumMessageHandlers = sizeof(gIPC_MessageHandlers)/sizeof(gIPC_MessageHandlers[0]);
666 void IPC_Handle(tIPC_Client *Client, size_t MsgLen, tAxWin_IPCMessage *Msg)
670 // _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
671 // IPCType, Ident, MsgLen, Msg);
673 if( MsgLen < sizeof(tAxWin_IPCMessage) )
675 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
678 if( Msg->Flags & IPCMSG_FLAG_RENDERER )
680 tWindow *win = IPC_int_GetWindow(Client, Msg->Window);
682 _SysDebug("WARNING: NULL window in message %i (%x)", Msg->ID, Msg->Window);
685 tWMRenderer *renderer = win->Renderer;
686 if( Msg->ID >= renderer->nIPCHandlers ) {
687 _SysDebug("WARNING: Message %i out of range in %s", Msg->ID, renderer->Name);
690 if( !renderer->IPCHandlers[Msg->ID] ) {
691 _SysDebug("WARNING: Message %i has no handler in %s", Msg->ID, renderer->Name);
694 _SysDebug("IPC_Handle: Call %s-%i", renderer->Name, Msg->ID);
695 rv = renderer->IPCHandlers[Msg->ID](win, Msg->Size, Msg->Data);
697 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
701 if( Msg->ID >= giIPC_NumMessageHandlers ) {
702 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Client);
703 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, Client);
707 if( !gIPC_MessageHandlers[ Msg->ID ] ) {
708 fprintf(stderr, "WARNING: Message %i does not have a handler\n", Msg->ID);
709 _SysDebug("WARNING: Message %i does not have a handler", Msg->ID);
713 _SysDebug("IPC_Handle: Call WM-%i", Msg->ID);
714 rv = gIPC_MessageHandlers[Msg->ID](Client, Msg);
716 _SysDebug("IPC_Handle: rv != 0 (%i)", rv);
720 // Dispatch a message to the client
721 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
723 tAxWin_IPCMessage *hdr;
724 tIPCMsg_SendMsg *msg;
725 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
728 msg = (void*)hdr->Data;
730 hdr->ID = IPCMSG_SENDMSG;
732 hdr->Size = sizeof(*msg) + Len;
738 memcpy(msg->Data, Data, Len);
740 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
743 // --- Server->Client replies
744 void IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data)
746 tAxWin_IPCMessage *hdr;
747 char buf[sizeof(*hdr)+Len];
752 hdr->Flags = IPCMSG_FLAG_RENDERER;
756 memcpy(hdr->Data, Data, Len);
757 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);