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>
18 #define AXWIN_PORT 4101
20 #define STATICBUF_SIZE 64
21 #define MAX_WINDOWS_PER_APP 128
24 typedef struct sIPC_Type tIPC_Type;
28 int (*GetIdentSize)(const void *Ident);
29 int (*CompareIdent)(const void *Ident1, const void *Ident2);
30 void (*SendMessage)(const void *Ident, size_t Length, const void *Data);
35 const tIPC_Type *IPCType;
36 const void *Ident; // Stored after structure
43 extern tWindow *gpWM_FocusedWindow; // Needed for _FocusWindow
47 void IPC_FillSelect(int *nfds, fd_set *set);
48 void IPC_HandleSelect(fd_set *set);
49 int IPC_Type_Datagram_GetSize(const void *Ident);
50 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
51 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
52 int IPC_Type_Sys_GetSize(const void *Ident);
53 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
54 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
55 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
58 const tIPC_Type gIPC_Type_Datagram = {
59 IPC_Type_Datagram_GetSize,
60 IPC_Type_Datagram_Compare,
61 IPC_Type_Datagram_Send
63 const tIPC_Type gIPC_Type_SysMessage = {
68 int giNetworkFileHandle = -1;
69 int giMessagesFileHandle = -1;
70 int giIPC_ClientCount;
71 tIPC_Client **gIPC_Clients;
78 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
79 if( giNetworkFileHandle != -1 )
82 ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
86 void IPC_FillSelect(int *nfds, fd_set *set)
88 if( giNetworkFileHandle != -1 )
90 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
91 FD_SET(giNetworkFileHandle, set);
95 void IPC_HandleSelect(fd_set *set)
97 if( giNetworkFileHandle != -1 )
99 if( FD_ISSET(giNetworkFileHandle, set) )
101 char staticBuf[STATICBUF_SIZE];
102 int readlen, identlen;
105 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
107 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
108 msg = staticBuf + identlen;
110 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
111 // _SysDebug("IPC_HandleSelect: UDP handled");
117 while( (len = SysGetMessage(&tid, 0, NULL)) )
120 SysGetMessage(NULL, len, data);
122 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
123 // _SysDebug("IPC_HandleSelect: Message handled");
127 int IPC_Type_Datagram_GetSize(const void *Ident)
129 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
132 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
135 // - No need to worry about mis-matching sizes, as the size is computed
136 // from the 3rd/4th bytes, hence it will differ before the size is hit.
137 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
140 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
142 int identlen = IPC_Type_Datagram_GetSize(Ident);
143 char tmpbuf[ identlen + Length ];
144 memcpy(tmpbuf, Ident, identlen); // Header
145 memcpy(tmpbuf + identlen, Data, Length); // Data
146 // TODO: Handle fragmented packets
147 write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
150 int IPC_Type_Sys_GetSize(const void *Ident)
152 return sizeof(pid_t);
155 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
157 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
160 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
162 SysSendMessage( *(const tid_t*)Ident, Length, Data );
165 // --- Client -> Window Mappings
166 int _CompareClientPtrs(const void *_a, const void *_b)
168 tIPC_Client *a = *(tIPC_Client**)_a;
169 tIPC_Client *b = *(tIPC_Client**)_b;
171 if(a->IPCType < b->IPCType) return -1;
172 if(a->IPCType > b->IPCType) return 1;
174 return a->IPCType->CompareIdent(a->Ident, b->Ident);
177 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
179 int pos = 0; // Position where the new client will be inserted
183 // - Search list of registered clients
184 if(giIPC_ClientCount > 0)
190 target.IPCType = IPCType;
191 target.Ident = Ident;
192 ret = ⌖ // Abuse ret to get a pointer
194 div = giIPC_ClientCount;
199 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
200 // _SysDebug("Checking against %i gives %i", pos, cmp);
210 return gIPC_Clients[pos];
212 // Adjust pos to be the index where the new client will be placed
217 // - Create a new client entry
218 ident_size = IPCType->GetIdentSize(Ident);
219 // _SysDebug("ident_size = %i", ident_size);
220 ret = malloc( sizeof(tIPC_Client) + ident_size );
221 if(!ret) return NULL;
222 ret->IPCType = IPCType;
223 ret->Ident = ret + 1; // Get the end of the structure
224 memcpy( (void*)ret->Ident, Ident, ident_size );
228 // TODO: Register some way of detecting the client disconnecting
229 // > Wait on the thread / register with kernel somehow
230 // > Sockets are easier, but UDP is harder. Might get rid of it
233 giIPC_ClientCount ++;
234 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
235 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
236 gIPC_Clients[pos] = ret;
241 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
246 if( WindowID >= Client->nWindows ) {
250 return Client->Windows[WindowID];
253 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
255 if( WindowID >= MAX_WINDOWS_PER_APP )
258 if( WindowID >= Client->nWindows )
260 int oldCount = Client->nWindows;
261 Client->nWindows = WindowID + 1;
262 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
263 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
264 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
267 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
268 Client->Windows[WindowID] = WindowPtr;
271 // --- IPC Message Handlers ---
272 int IPC_Msg_Ping(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
274 ASSERT(Msg->ID == IPCMSG_PING);
276 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
278 if( Msg->Size < 4 ) return -1;
280 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
281 Msg->ID = IPCMSG_PING;
282 Msg->Size = sizeof(*ret);
283 ret->Value = AXWIN_VERSION;
284 Client->IPCType->SendMessage(Client->Ident, sizeof(*Msg)+sizeof(*ret), Msg);
288 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
290 tAxWin_IPCMessage *ret_hdr;
291 tIPCMsg_ReturnInt *ret;
292 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
294 ASSERT(Msg->ID == IPCMSG_GETDISPLAYCOUNT);
296 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
298 ret_hdr = (void*)buf;
299 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
301 ret_hdr->Window = -1;
302 ret_hdr->Size = sizeof(*ret);
303 ret = (void*)ret_hdr->Data;
304 ret->Value = 1; // HARD CODE - Current version only supports one display
306 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
310 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
312 tIPCMsg_GetDisplayDims *info;
313 tAxWin_IPCMessage *ret_hdr;
314 tIPCMsg_RetDisplayDims *ret;
315 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
317 ASSERT(Msg->ID == IPCMSG_GETDISPLAYDIMS);
319 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
321 info = (void*)Msg->Data;
323 ret_hdr = (void*)buf;
324 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
326 ret_hdr->Window = -1;
327 ret_hdr->Size = sizeof(*ret);
328 ret = (void*)ret_hdr->Data;
330 // HARD CODE! Only one display supported
331 if( info->DisplayID == 0 )
335 ret->W = giScreenWidth;
336 ret->H = giScreenHeight;
340 ret->X = 0; ret->Y = 0;
341 ret->W = 0; ret->H = 0;
344 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
348 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
350 tIPCMsg_SendMsg *info = (void*)Msg->Data;
353 ASSERT(Msg->ID == IPCMSG_SENDMSG);
356 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
358 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
361 src = IPC_int_GetWindow(Client, Msg->Window);
363 dest = IPC_int_GetWindow(Client, info->Remote);
366 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
371 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
373 tIPCMsg_CreateWin *info = (void*)Msg->Data;
374 tWindow *newwin, *parent;
376 ASSERT(Msg->ID == IPCMSG_CREATEWIN);
379 // > +1 is for NULL byte on string
380 if( Msg->Size < sizeof(*info) + 1 ) {
381 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
384 if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
385 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
386 _SysDebug("info = {");
387 _SysDebug(" .NewWinID = %i", info->NewWinID);
388 _SysDebug(" .RendererArg = %i", info->RendererArg);
389 _SysDebug(" .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
394 // - Get the parent window ID
395 parent = IPC_int_GetWindow(Client, Msg->Window);
397 // Catch creating a window with an existing ID
398 if( IPC_int_GetWindow(Client, info->NewWinID) )
401 // - Create the new window, and save its pointer
402 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
403 IPC_int_SetWindow(Client, info->NewWinID, newwin);
408 int IPC_Msg_SetWindowTitle(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
412 ASSERT(Msg->ID == IPCMSG_SETWINTITLE);
414 if( Msg->Size < 1 ) return -1;
415 if( Msg->Data[ Msg->Size-1 ] != '\0' ) return -1;
417 win = IPC_int_GetWindow(Client, Msg->Window);
420 WM_SetWindowTitle(win, Msg->Data);
425 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
427 tIPCMsg_Boolean *info = (void*)Msg->Data;
430 ASSERT(Msg->ID == IPCMSG_SHOWWINDOW);
432 if( Msg->Size < sizeof(*info) ) return -1;
434 win = IPC_int_GetWindow(Client, Msg->Window);
437 WM_ShowWindow(win, !!info->Value);
442 int IPC_Msg_DecorateWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
444 tIPCMsg_Boolean *info = (void*)Msg->Data;
447 if( Msg->Size < sizeof(*info) ) return -1;
449 win = IPC_int_GetWindow(Client, Msg->Window);
452 WM_DecorateWindow(win, !!info->Value);
456 int IPC_Msg_FocusWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
460 ASSERT(Msg->ID == IPCMSG_FOCUSWINDOW);
462 // Don't allow the focus to be changed unless the client has the focus
463 if(!gpWM_FocusedWindow) return 1;
464 if(gpWM_FocusedWindow->Client != Client) return 1;
466 win = IPC_int_GetWindow(Client, Msg->Window);
474 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
476 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
479 ASSERT(Msg->ID == IPCMSG_SETWINPOS);
481 if(Msg->Size < sizeof(*info)) return -1;
483 win = IPC_int_GetWindow(Client, Msg->Window);
486 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
489 WM_MoveWindow(win, info->X, info->Y);
491 WM_ResizeWindow(win, info->W, info->H);
496 int IPC_Msg_RegisterAction(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
498 tIPCMsg_RegAction *info = (void*)Msg->Data;
501 ASSERT(Msg->ID == IPCMSG_REGACTION);
503 if( Msg->Size < sizeof(*info) + 1 )
506 win = IPC_int_GetWindow(Client, Msg->Window);
509 if( strnlen(info->Action, Msg->Size - sizeof(*info)) == Msg->Size - sizeof(*info) )
512 _SysDebug("RegisterAction %p:%i [%i]\"%s\"",
513 Client, Msg->Window, info->Index, info->Action
516 WM_Hotkey_RegisterAction(info->Action, win, info->Index);
521 int (*gIPC_MessageHandlers[])(tIPC_Client *Client, tAxWin_IPCMessage *Msg) = {
523 IPC_Msg_GetDisplayCount,
524 IPC_Msg_GetDisplayDims,
527 NULL, // Destroy window
528 IPC_Msg_SetWindowTitle,
530 IPC_Msg_DecorateWindow,
533 IPC_Msg_RegisterAction
535 const int giIPC_NumMessageHandlers = sizeof(gIPC_MessageHandlers)/sizeof(gIPC_MessageHandlers[0]);
537 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
542 _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
543 IPCType, Ident, MsgLen, Msg);
545 if( MsgLen < sizeof(tAxWin_IPCMessage) )
547 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
550 client = IPC_int_GetClient(IPCType, Ident);
552 if( Msg->ID >= giIPC_NumMessageHandlers ) {
553 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
554 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
558 if( !gIPC_MessageHandlers[ Msg->ID ] ) {
559 fprintf(stderr, "WARNING: Message %i does not have a handler\n", Msg->ID);
560 _SysDebug("WARNING: Message %i does not have a handler", Msg->ID);
564 _SysDebug("IPC_Handle: Msg->ID = %i", Msg->ID);
565 rv = gIPC_MessageHandlers[Msg->ID](client, Msg);
566 _SysDebug("IPC_Handle: rv = %i", rv);
569 // --- Server->Client replies
570 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
572 tAxWin_IPCMessage *hdr;
573 tIPCMsg_SendMsg *msg;
574 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
577 msg = (void*)hdr->Data;
579 hdr->ID = IPCMSG_SENDMSG;
581 hdr->Size = sizeof(*msg) + Len;
587 memcpy(msg->Data, Data, Len);
589 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);