Usermode/AxWin3 - Added window invalidation, sub-windows render now
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / ipc.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  * 
5  * ipc.c
6  * - Interprocess communication
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10 #include <net.h>
11 #include <string.h>
12 #include <ipcmessages.h>
13 #include <stdio.h>
14 #include <wm.h>
15
16 #define AXWIN_PORT      4101
17
18 #define STATICBUF_SIZE  64
19 #define MAX_WINDOWS_PER_APP     128
20
21 // === TYPES ===
22 typedef struct sIPC_Type        tIPC_Type;
23 typedef struct sIPC_Client      tIPC_Client;
24
25 struct sIPC_Type
26 {
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);
30 };
31
32 struct sIPC_Client
33 {
34         const tIPC_Type *IPCType;
35         const void      *Ident; // Stored after structure
36
37          int    nWindows;
38         tWindow **Windows;
39 };
40
41
42 // === PROTOTYPES ===
43 void    IPC_Init(void);
44 void    IPC_FillSelect(int *nfds, fd_set *set);
45 void    IPC_HandleSelect(fd_set *set);
46  int    IPC_Type_Datagram_GetSize(const void *Ident);
47  int    IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
48 void    IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
49  int    IPC_Type_Sys_GetSize(const void *Ident);
50  int    IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
51 void    IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
52 void    IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
53
54 // === GLOBALS ===
55 const tIPC_Type gIPC_Type_Datagram = {
56         IPC_Type_Datagram_GetSize,
57         IPC_Type_Datagram_Compare, 
58         IPC_Type_Datagram_Send
59 };
60 const tIPC_Type gIPC_Type_SysMessage = {
61         IPC_Type_Sys_GetSize,
62         IPC_Type_Sys_Compare,
63         IPC_Type_Sys_Send
64 };
65  int    giNetworkFileHandle = -1;
66  int    giMessagesFileHandle = -1;
67  int    giIPC_ClientCount;
68 tIPC_Client     **gIPC_Clients;
69
70 // === CODE ===
71 void IPC_Init(void)
72 {
73          int    tmp;
74         // TODO: Check this
75         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
76         tmp = AXWIN_PORT;       ioctl(giNetworkFileHandle, 4, &tmp);    // TODO: Don't hard-code IOCtl number
77 }
78
79 void IPC_FillSelect(int *nfds, fd_set *set)
80 {
81         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
82         FD_SET(giNetworkFileHandle, set);
83 }
84
85 void IPC_HandleSelect(fd_set *set)
86 {
87         if( FD_ISSET(giNetworkFileHandle, set) )
88         {
89                 char    staticBuf[STATICBUF_SIZE];
90                  int    readlen, identlen;
91                 char    *msg;
92
93                 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
94                 
95                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
96                 msg = staticBuf + identlen;
97
98                 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
99                 _SysDebug("IPC_HandleSelect: UDP handled");
100         }
101
102         while(SysGetMessage(NULL, NULL))
103         {
104                 pid_t   tid;
105                  int    len = SysGetMessage(&tid, NULL);
106                 char    data[len];
107                 SysGetMessage(NULL, data);
108
109                 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
110                 _SysDebug("IPC_HandleSelect: Message handled");
111         }
112 }
113
114 int IPC_Type_Datagram_GetSize(const void *Ident)
115 {
116         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
117 }
118
119 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
120 {
121         // Pass the buck :)
122         // - No need to worry about mis-matching sizes, as the size is computed
123         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
124         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
125 }
126
127 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
128 {
129          int    identlen = IPC_Type_Datagram_GetSize(Ident);
130         char    tmpbuf[ identlen + Length ];
131         memcpy(tmpbuf, Ident, identlen);        // Header
132         memcpy(tmpbuf + identlen, Data, Length);        // Data
133         // TODO: Handle fragmented packets
134         write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
135 }
136
137 int IPC_Type_Sys_GetSize(const void *Ident)
138 {
139         return sizeof(pid_t);
140 }
141
142 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
143 {
144         return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
145 }
146
147 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
148 {
149         SysSendMessage( *(const tid_t*)Ident, Length, Data );
150 }
151
152 // --- Client -> Window Mappings
153 int _CompareClientPtrs(const void *_a, const void *_b)
154 {
155         tIPC_Client     *a = *(tIPC_Client**)_a;
156         tIPC_Client     *b = *(tIPC_Client**)_b;
157         
158         if(a->IPCType < b->IPCType)     return -1;
159         if(a->IPCType > b->IPCType)     return 1;
160         
161         return a->IPCType->CompareIdent(a->Ident, b->Ident);
162 }
163
164 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
165 {
166          int    pos = 0;        // Position where the new client will be inserted
167          int    ident_size;
168         tIPC_Client     *ret;
169
170         // - Search list of registered clients
171         if(giIPC_ClientCount > 0)
172         {
173                 tIPC_Client     target;
174                  int    div;
175                  int    cmp = -1;
176         
177                 target.IPCType = IPCType;
178                 target.Ident = Ident;
179                 ret = &target;  // Abuse ret to get a pointer
180                 
181                 div = giIPC_ClientCount;
182                 pos = div/2;
183                 while(div > 0)
184                 {
185                         div /= 2;
186                         cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
187 //                      _SysDebug("Checking against %i gives %i", pos, cmp);
188                         if(cmp == 0)    break;
189                         if(cmp < 0)
190                                 pos -= div;
191                         else
192                                 pos += div;
193                 }
194                 
195                 // - Return if found    
196                 if(cmp == 0)
197                         return gIPC_Clients[pos];
198         
199                 // Adjust pos to be the index where the new client will be placed
200                 if(cmp > 0)     pos ++;
201         }
202
203
204         // - Create a new client entry
205         ident_size = IPCType->GetIdentSize(Ident);
206 //      _SysDebug("ident_size = %i", ident_size);
207         ret = malloc( sizeof(tIPC_Client) + ident_size );
208         if(!ret)        return NULL;
209         ret->IPCType = IPCType;
210         ret->Ident = ret + 1;   // Get the end of the structure
211         memcpy( (void*)ret->Ident, Ident, ident_size );
212         ret->nWindows = 0;
213         ret->Windows = NULL;
214         
215         // TODO: Register some way of detecting the client disconnecting
216         //       > Wait on the thread / register with kernel somehow
217         //       > Sockets are easier, but UDP is harder. Might get rid of it
218
219         // - Insert
220         giIPC_ClientCount ++;
221         gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
222         memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
223         gIPC_Clients[pos] = ret;
224
225         return ret;
226 }
227
228 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
229 {
230         if( WindowID == -1 )
231                 return NULL;
232
233         if( WindowID >= Client->nWindows ) {
234 //              _SysDebug("Window %i out of range for %p (%i)", WindowID, Client, Client->nWindows);
235                 return NULL;
236         }
237
238         return Client->Windows[WindowID];
239 }
240
241 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
242 {
243         if( WindowID >= MAX_WINDOWS_PER_APP )
244                 return ;
245
246         if( WindowID >= Client->nWindows )
247         {
248                  int    oldCount = Client->nWindows;
249                 Client->nWindows = WindowID + 1;
250                 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
251                 memset( &Client->Windows[oldCount],  0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
252                 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
253         }
254
255         _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);      
256         Client->Windows[WindowID] = WindowPtr;
257 }
258
259 // --- IPC Message Handlers ---
260 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
261 {
262         tIPCMsg_CreateWin       *info = (void*)Msg->Data;
263         tWindow *newwin, *parent;
264
265         // - Sanity checks
266         //  > +1 is for NULL byte on string
267         if( Msg->Size < sizeof(tIPCMsg_CreateWin) + 1 )
268                 return -1;
269         if( info->Renderer[Msg->Size - sizeof(tIPCMsg_CreateWin)] != '\0' )
270                 return -1;
271         
272         // - Get the parent window ID
273         parent = IPC_int_GetWindow(Client, Msg->Window);
274
275         // Catch creating a window with an existing ID
276         if( IPC_int_GetWindow(Client, info->NewWinID) )
277                 return 1;
278
279         // - Create the new window, and save its pointer
280         newwin = WM_CreateWindow(parent, info->RendererArg, info->Renderer);
281         IPC_int_SetWindow(Client, info->NewWinID, newwin);
282
283         return 0;
284 }
285
286 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
287 {
288         tIPCMsg_ShowWindow      *info = (void*)Msg->Data;
289         tWindow *win;
290         
291         if( Msg->Size < sizeof(*info) ) return -1;
292         
293         win = IPC_int_GetWindow(Client, Msg->Window);
294         if(!win)        return 1;
295
296         WM_ShowWindow(win, !!info->bShow);
297         
298         return 0;
299 }
300
301 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
302 {
303         tIPCMsg_SetWindowPos    *info = (void*)Msg->Data;
304         tWindow *win;
305         
306         if(Msg->Size < sizeof(*info))   return -1;
307         
308         win = IPC_int_GetWindow(Client, Msg->Window);
309         if(!win)        return 1;
310         
311         _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
312         
313         if(info->bSetPos)
314                 WM_MoveWindow(win, info->X, info->Y);
315         if(info->bSetDims)
316                 WM_ResizeWindow(win, info->W, info->H);
317         
318         return 0;
319 }
320
321 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
322 {
323         tIPC_Client     *client;
324          int    rv = 0;
325         
326         _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
327                 IPCType, Ident, MsgLen, Msg);
328         
329         if( MsgLen < sizeof(tAxWin_IPCMessage) )
330                 return ;
331         if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
332                 return ;
333         
334         client = IPC_int_GetClient(IPCType, Ident);
335
336         switch((enum eAxWin_IPCMessageTypes) Msg->ID)
337         {
338         // --- Ping message (reset timeout and get server version)
339         case IPCMSG_PING:
340                 _SysDebug(" IPC_Handle: IPCMSG_PING");
341                 if( Msg->Size < 4 )     return;
342                 if( Msg->Flags & IPCMSG_FLAG_RETURN )
343                 {
344                         Msg->ID = IPCMSG_PING;
345                         Msg->Size = sizeof(tIPCMsg_Return);
346                         ((tIPCMsg_Return*)Msg->Data)->Value = AXWIN_VERSION;
347                         IPCType->SendMessage(Ident, sizeof(tIPCMsg_Return), Msg);
348                 }
349                 break;
350
351         // --- Create window
352         case IPCMSG_CREATEWIN:
353                 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
354                 rv = IPC_Msg_CreateWin(client, Msg);
355                 break;
356         // --- Show/Hide a window
357         case IPCMSG_SHOWWINDOW:
358                 _SysDebug(" IPC_Handle: IPCMSG_SHOWWINDOW");
359                 rv = IPC_Msg_ShowWindow(client, Msg);
360                 break;
361         // --- Move/Resize a window
362         case IPCMSG_SETWINPOS:
363                 _SysDebug(" IPC_Handle: IPCMSG_SETWINPOS");
364                 rv = IPC_Msg_SetWinPos(client, Msg);
365                 break;
366
367         // --- Unknown message
368         default:
369                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
370                 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
371                 break;
372         }
373         if(rv)
374                 _SysDebug("IPC_Handle: rv = %i", rv);
375 }
376

UCC git Repository :: git.ucc.asn.au