2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
6 * - Interprocess communication
12 #include <ipcmessages.h>
15 #include <wm_internals.h>
17 #define AXWIN_PORT 4101
19 #define STATICBUF_SIZE 64
20 #define MAX_WINDOWS_PER_APP 128
23 typedef struct sIPC_Type tIPC_Type;
27 int (*GetIdentSize)(const void *Ident);
28 int (*CompareIdent)(const void *Ident1, const void *Ident2);
29 void (*SendMessage)(const void *Ident, size_t Length, const void *Data);
34 const tIPC_Type *IPCType;
35 const void *Ident; // Stored after structure
42 extern tWindow *gpWM_FocusedWindow; // Needed for _FocusWindow
46 void IPC_FillSelect(int *nfds, fd_set *set);
47 void IPC_HandleSelect(fd_set *set);
48 int IPC_Type_Datagram_GetSize(const void *Ident);
49 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
50 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
51 int IPC_Type_Sys_GetSize(const void *Ident);
52 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
53 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
54 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
57 const tIPC_Type gIPC_Type_Datagram = {
58 IPC_Type_Datagram_GetSize,
59 IPC_Type_Datagram_Compare,
60 IPC_Type_Datagram_Send
62 const tIPC_Type gIPC_Type_SysMessage = {
67 int giNetworkFileHandle = -1;
68 int giMessagesFileHandle = -1;
69 int giIPC_ClientCount;
70 tIPC_Client **gIPC_Clients;
77 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
78 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
81 void IPC_FillSelect(int *nfds, fd_set *set)
83 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
84 FD_SET(giNetworkFileHandle, set);
87 void IPC_HandleSelect(fd_set *set)
89 if( FD_ISSET(giNetworkFileHandle, set) )
91 char staticBuf[STATICBUF_SIZE];
92 int readlen, identlen;
95 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
97 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
98 msg = staticBuf + identlen;
100 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
101 // _SysDebug("IPC_HandleSelect: UDP handled");
104 while(SysGetMessage(NULL, NULL))
107 int len = SysGetMessage(&tid, NULL);
109 SysGetMessage(NULL, data);
111 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
112 // _SysDebug("IPC_HandleSelect: Message handled");
116 int IPC_Type_Datagram_GetSize(const void *Ident)
118 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
121 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
124 // - No need to worry about mis-matching sizes, as the size is computed
125 // from the 3rd/4th bytes, hence it will differ before the size is hit.
126 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
129 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
131 int identlen = IPC_Type_Datagram_GetSize(Ident);
132 char tmpbuf[ identlen + Length ];
133 memcpy(tmpbuf, Ident, identlen); // Header
134 memcpy(tmpbuf + identlen, Data, Length); // Data
135 // TODO: Handle fragmented packets
136 write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
139 int IPC_Type_Sys_GetSize(const void *Ident)
141 return sizeof(pid_t);
144 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
146 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
149 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
151 SysSendMessage( *(const tid_t*)Ident, Length, Data );
154 // --- Client -> Window Mappings
155 int _CompareClientPtrs(const void *_a, const void *_b)
157 tIPC_Client *a = *(tIPC_Client**)_a;
158 tIPC_Client *b = *(tIPC_Client**)_b;
160 if(a->IPCType < b->IPCType) return -1;
161 if(a->IPCType > b->IPCType) return 1;
163 return a->IPCType->CompareIdent(a->Ident, b->Ident);
166 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
168 int pos = 0; // Position where the new client will be inserted
172 // - Search list of registered clients
173 if(giIPC_ClientCount > 0)
179 target.IPCType = IPCType;
180 target.Ident = Ident;
181 ret = ⌖ // Abuse ret to get a pointer
183 div = giIPC_ClientCount;
188 cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
189 // _SysDebug("Checking against %i gives %i", pos, cmp);
199 return gIPC_Clients[pos];
201 // Adjust pos to be the index where the new client will be placed
206 // - Create a new client entry
207 ident_size = IPCType->GetIdentSize(Ident);
208 // _SysDebug("ident_size = %i", ident_size);
209 ret = malloc( sizeof(tIPC_Client) + ident_size );
210 if(!ret) return NULL;
211 ret->IPCType = IPCType;
212 ret->Ident = ret + 1; // Get the end of the structure
213 memcpy( (void*)ret->Ident, Ident, ident_size );
217 // TODO: Register some way of detecting the client disconnecting
218 // > Wait on the thread / register with kernel somehow
219 // > Sockets are easier, but UDP is harder. Might get rid of it
222 giIPC_ClientCount ++;
223 gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
224 memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
225 gIPC_Clients[pos] = ret;
230 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
235 if( WindowID >= Client->nWindows ) {
236 // _SysDebug("Window %i out of range for %p (%i)", WindowID, Client, Client->nWindows);
240 return Client->Windows[WindowID];
243 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
245 if( WindowID >= MAX_WINDOWS_PER_APP )
248 if( WindowID >= Client->nWindows )
250 int oldCount = Client->nWindows;
251 Client->nWindows = WindowID + 1;
252 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
253 memset( &Client->Windows[oldCount], 0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
254 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
257 _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);
258 Client->Windows[WindowID] = WindowPtr;
261 // --- IPC Message Handlers ---
262 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
264 tIPCMsg_SendMsg *info = (void*)Msg->Data;
268 if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
270 if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
273 src = IPC_int_GetWindow(Client, Msg->Window);
275 dest = IPC_int_GetWindow(Client, info->Remote);
278 WM_SendMessage(src, dest, info->ID, info->Length, info->Data);
283 int IPC_Msg_FocusWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
287 // Don't allow the focus to be changed unless the client has the focus
288 if(!gpWM_FocusedWindow) return 1;
289 if(gpWM_FocusedWindow->Client != Client) return 1;
291 win = IPC_int_GetWindow(Client, Msg->Window);
299 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
301 tIPCMsg_CreateWin *info = (void*)Msg->Data;
302 tWindow *newwin, *parent;
305 // > +1 is for NULL byte on string
306 if( Msg->Size < sizeof(*info) + 1 ) {
307 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
310 if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
311 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
312 _SysDebug("info = {");
313 _SysDebug(" .NewWinID = %i", info->NewWinID);
314 _SysDebug(" .RendererArg = %i", info->RendererArg);
315 _SysDebug(" .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
320 // - Get the parent window ID
321 parent = IPC_int_GetWindow(Client, Msg->Window);
323 // Catch creating a window with an existing ID
324 if( IPC_int_GetWindow(Client, info->NewWinID) )
327 // - Create the new window, and save its pointer
328 newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
329 IPC_int_SetWindow(Client, info->NewWinID, newwin);
334 int IPC_Msg_SetWindowTitle(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
338 if( Msg->Size < 1 ) return -1;
339 if( Msg->Data[ Msg->Size-1 ] != '\0' ) return -1;
341 win = IPC_int_GetWindow(Client, Msg->Window);
344 WM_SetWindowTitle(win, Msg->Data);
349 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
351 tIPCMsg_Boolean *info = (void*)Msg->Data;
354 if( Msg->Size < sizeof(*info) ) return -1;
356 win = IPC_int_GetWindow(Client, Msg->Window);
359 WM_ShowWindow(win, !!info->Value);
364 int IPC_Msg_DecorateWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
366 tIPCMsg_Boolean *info = (void*)Msg->Data;
369 if( Msg->Size < sizeof(*info) ) return -1;
371 win = IPC_int_GetWindow(Client, Msg->Window);
374 WM_DecorateWindow(win, !!info->Value);
378 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
380 tIPCMsg_SetWindowPos *info = (void*)Msg->Data;
383 if(Msg->Size < sizeof(*info)) return -1;
385 win = IPC_int_GetWindow(Client, Msg->Window);
388 _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
391 WM_MoveWindow(win, info->X, info->Y);
393 WM_ResizeWindow(win, info->W, info->H);
398 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
400 tAxWin_IPCMessage *ret_hdr;
401 tIPCMsg_ReturnInt *ret;
402 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
404 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
406 ret_hdr = (void*)buf;
407 ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
409 ret_hdr->Window = -1;
410 ret_hdr->Size = sizeof(*ret);
411 ret = (void*)ret_hdr->Data;
412 ret->Value = 1; // HARD CODE - Current version only supports one display
414 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
418 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
420 tIPCMsg_GetDisplayDims *info;
421 tAxWin_IPCMessage *ret_hdr;
422 tIPCMsg_RetDisplayDims *ret;
423 char buf[sizeof(*ret_hdr)+sizeof(*ret)];
425 if( !(Msg->Flags & IPCMSG_FLAG_RETURN) ) return 0;
427 info = (void*)Msg->Data;
429 ret_hdr = (void*)buf;
430 ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
432 ret_hdr->Window = -1;
433 ret_hdr->Size = sizeof(*ret);
434 ret = (void*)ret_hdr->Data;
436 // HARD CODE! Only one display supported
437 if( info->DisplayID == 0 )
441 ret->W = giScreenWidth;
442 ret->H = giScreenHeight;
446 ret->X = 0; ret->Y = 0;
447 ret->W = 0; ret->H = 0;
450 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
454 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
459 _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
460 IPCType, Ident, MsgLen, Msg);
462 if( MsgLen < sizeof(tAxWin_IPCMessage) )
464 if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
467 client = IPC_int_GetClient(IPCType, Ident);
469 switch((enum eAxWin_IPCMessageTypes) Msg->ID)
471 // --- Ping message (reset timeout and get server version)
473 _SysDebug(" IPC_Handle: IPCMSG_PING");
474 if( Msg->Size < 4 ) return;
475 if( Msg->Flags & IPCMSG_FLAG_RETURN )
477 tIPCMsg_ReturnInt *ret = (void*)Msg->Data;
478 Msg->ID = IPCMSG_PING;
479 Msg->Size = sizeof(*ret);
480 ret->Value = AXWIN_VERSION;
481 IPCType->SendMessage(Ident, sizeof(*Msg)+sizeof(*ret), Msg);
485 // -- Get display count
486 case IPCMSG_GETDISPLAYCOUNT:
487 rv = IPC_Msg_GetDisplayCount(client, Msg);
490 // --- Get display dimensions
491 case IPCMSG_GETDISPLAYDIMS:
492 rv = IPC_Msg_GetDisplayDims(client, Msg);
495 // --- Send a message
497 _SysDebug(" IPC_Handle: IPCMSG_SENDMSG %i", ((tIPCMsg_SendMsg*)Msg->Data)->ID);
498 rv = IPC_Msg_SendMsg(client, Msg);
502 case IPCMSG_CREATEWIN:
503 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
504 rv = IPC_Msg_CreateWin(client, Msg);
506 // TODO: Destroy window
508 // --- Set window title
509 case IPCMSG_SETWINTITLE:
510 _SysDebug(" IPC_Handle: IPCMSG_SETWINTITLE");
511 rv = IPC_Msg_SetWindowTitle(client, Msg);
514 // --- Give a window focus
515 case IPCMSG_FOCUSWINDOW:
516 _SysDebug(" IPC_Handle: IPCMSG_FOCUSWINDOW");
517 rv = IPC_Msg_FocusWindow(client, Msg);
519 // --- Show/Hide a window
520 case IPCMSG_SHOWWINDOW:
521 _SysDebug(" IPC_Handle: IPCMSG_SHOWWINDOW");
522 rv = IPC_Msg_ShowWindow(client, Msg);
524 case IPCMSG_DECORATEWINDOW:
525 _SysDebug(" IPC_Handle: IPCMSG_DECORATEWINDOW");
526 rv = IPC_Msg_DecorateWindow(client, Msg);
528 // --- Move/Resize a window
529 case IPCMSG_SETWINPOS:
530 _SysDebug(" IPC_Handle: IPCMSG_SETWINPOS");
531 rv = IPC_Msg_SetWinPos(client, Msg);
534 // --- Unknown message
536 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
537 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
541 _SysDebug("IPC_Handle: rv = %i", rv);
544 // --- Server->Client replies
545 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
547 tAxWin_IPCMessage *hdr;
548 tIPCMsg_SendMsg *msg;
549 char buf[sizeof(*hdr)+sizeof(*msg)+Len];
552 msg = (void*)hdr->Data;
554 hdr->ID = IPCMSG_SENDMSG;
556 hdr->Size = sizeof(*msg) + Len;
562 memcpy(msg->Data, Data, Len);
564 Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);