0ec25dbe26ff5a159cbf8a92f708fb2e083b586c
[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
20 // === TYPES ===
21 typedef struct sIPC_Type        tIPC_Type;
22 typedef struct sIPC_Client      tIPC_Client;
23
24 struct sIPC_Type
25 {
26          int    (*GetIdentSize)(const void *Ident);
27          int    (*CompareIdent)(const void *Ident1, const void *Ident2);
28         void    (*SendMessage)(const void *Ident, size_t Length, const void *Data);
29 };
30
31 struct sIPC_Client
32 {
33         const tIPC_Type *IPCType;
34         const void      *Ident; // Stored after structure
35
36          int    nWindows;
37         tWindow **Windows;
38 };
39
40
41 // === PROTOTYPES ===
42 void    IPC_Init(void);
43 void    IPC_FillSelect(int *nfds, fd_set *set);
44 void    IPC_HandleSelect(fd_set *set);
45  int    IPC_Type_Datagram_GetSize(const void *Ident);
46  int    IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
47 void    IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
48  int    IPC_Type_Sys_GetSize(const void *Ident);
49  int    IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
50 void    IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
51 void    IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
52
53 // === GLOBALS ===
54 const tIPC_Type gIPC_Type_Datagram = {
55         IPC_Type_Datagram_GetSize,
56         IPC_Type_Datagram_Compare, 
57         IPC_Type_Datagram_Send
58 };
59 const tIPC_Type gIPC_Type_SysMessage = {
60         IPC_Type_Sys_GetSize,
61         IPC_Type_Sys_Compare,
62         IPC_Type_Sys_Send
63 };
64  int    giNetworkFileHandle = -1;
65  int    giMessagesFileHandle = -1;
66  int    giIPC_ClientCount;
67 tIPC_Client     **gIPC_Clients;
68
69 // === CODE ===
70 void IPC_Init(void)
71 {
72          int    tmp;
73         // TODO: Check this
74         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
75         tmp = AXWIN_PORT;       ioctl(giNetworkFileHandle, 4, &tmp);    // TODO: Don't hard-code IOCtl number
76 }
77
78 void IPC_FillSelect(int *nfds, fd_set *set)
79 {
80         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
81         FD_SET(giNetworkFileHandle, set);
82 }
83
84 void IPC_HandleSelect(fd_set *set)
85 {
86         if( FD_ISSET(giNetworkFileHandle, set) )
87         {
88                 char    staticBuf[STATICBUF_SIZE];
89                  int    readlen, identlen;
90                 char    *msg;
91
92                 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
93                 
94                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
95                 msg = staticBuf + identlen;
96
97                 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
98                 _SysDebug("IPC_HandleSelect: UDP handled");
99         }
100
101         while(SysGetMessage(NULL, NULL))
102         {
103                 pid_t   tid;
104                  int    len = SysGetMessage(&tid, NULL);
105                 char    data[len];
106                 SysGetMessage(NULL, data);
107
108                 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
109                 _SysDebug("IPC_HandleSelect: Message handled");
110         }
111 }
112
113 int IPC_Type_Datagram_GetSize(const void *Ident)
114 {
115         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
116 }
117
118 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
119 {
120         // Pass the buck :)
121         // - No need to worry about mis-matching sizes, as the size is computed
122         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
123         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
124 }
125
126 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
127 {
128          int    identlen = IPC_Type_Datagram_GetSize(Ident);
129         char    tmpbuf[ identlen + Length ];
130         memcpy(tmpbuf, Ident, identlen);        // Header
131         memcpy(tmpbuf + identlen, Data, Length);        // Data
132         // TODO: Handle fragmented packets
133         write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
134 }
135
136 int IPC_Type_Sys_GetSize(const void *Ident)
137 {
138         return sizeof(pid_t);
139 }
140
141 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
142 {
143         return *(const int*)Ident1 - *(const int*)Ident2;
144 }
145
146 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
147 {
148         SysSendMessage( *(const tid_t*)Ident, Length, Data );
149 }
150
151 // --- Client -> Window Mappings
152 int _CompareClientPtrs(const void *_a, const void *_b)
153 {
154         tIPC_Client     *a = *(tIPC_Client**)_a;
155         tIPC_Client     *b = *(tIPC_Client**)_b;
156         
157         if(a->IPCType < b->IPCType)     return -1;
158         if(a->IPCType > b->IPCType)     return 1;
159         
160         return a->IPCType->CompareIdent(a->Ident, b->Ident);
161 }
162
163 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
164 {
165          int    pos;    // Position where the new client will be inserted
166          int    ident_size;
167         tIPC_Client     *ret;
168         UNIMPLEMENTED();
169
170         // - Search list of registered clients
171         {
172                 tIPC_Client     target;
173                  int    div;
174                  int    cmp = -1;
175         
176                 target.IPCType = IPCType;
177                 target.Ident = Ident;
178                 ret = &target;  // Abuse ret to get a pointer
179                 
180                 div = giIPC_ClientCount;
181                 pos = div/2;
182                 while(div > 0)
183                 {
184                         div /= 2;
185                         cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
186                         if(cmp == 0)    break;
187                         if(cmp < 0)
188                                 pos -= div;
189                         else
190                                 pos += div;
191                 }
192                 
193                 // - Return if found    
194                 if(cmp == 0)
195                         return gIPC_Clients[pos];
196         
197                 // Adjust pos to be the index where the new client will be placed
198                 if(cmp > 0)     pos ++;
199         }
200
201
202         // - Create a new client entry
203         ident_size = IPCType->GetIdentSize(Ident);
204         ret = malloc( sizeof(tIPC_Client) + ident_size );
205         if(!ret)        return NULL;
206         ret->IPCType = IPCType;
207         ret->Ident = &ret + 1;  // Get the end of the structure
208         memcpy( (void*)ret->Ident, Ident, ident_size );
209         
210         // TODO: Register some way of detecting the client disconnecting
211         //       > Wait on the thread / register with kernel somehow
212         //       > Sockets are easier, but UDP is harder. Might get rid of it
213
214         // - Insert
215         giIPC_ClientCount ++;
216         gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
217         memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
218         gIPC_Clients[pos] = ret;
219
220         return ret;
221 }
222
223 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
224 {
225         if( WindowID == -1 )
226                 return NULL;
227         
228         UNIMPLEMENTED();
229         return NULL;
230 }
231
232 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
233 {
234         UNIMPLEMENTED();
235 }
236
237 // --- IPC Message Handlers ---
238 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
239 {
240         tIPCMsg_CreateWin       *info = (void*)Msg->Data;
241         tWindow *newwin, *parent;
242
243         // - Sanity checks
244         //  > +1 is for NULL byte on string
245         if( Msg->Size < sizeof(tIPCMsg_CreateWin) + 1 )
246                 return -1;
247         if( info->Renderer[Msg->Size - sizeof(tIPCMsg_CreateWin)] != '\0' )
248                 return -1;
249         
250         // - Get the parent window ID
251         parent = IPC_int_GetWindow(Client, Msg->Window);
252
253         // Catch creating a window with an existing ID
254         if( IPC_int_GetWindow(Client, info->NewWinID) )
255                 return 1;
256
257         // - Create the new window, and save its pointer
258         newwin = WM_CreateWindow(parent, info->Flags, info->Renderer);
259         IPC_int_SetWindow(Client, info->NewWinID, newwin);
260
261         return 0;
262 }
263
264 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
265 {
266         tIPC_Client     *client;
267         
268         _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
269                 IPCType, Ident, MsgLen, Msg);
270         
271         if( MsgLen < sizeof(tAxWin_IPCMessage) )
272                 return ;
273         if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
274                 return ;
275         
276         client = IPC_int_GetClient(IPCType, Ident);
277
278         switch((enum eAxWin_IPCMessageTypes) Msg->ID)
279         {
280         // --- Ping message (reset timeout and get server version)
281         case IPCMSG_PING:
282                 _SysDebug(" IPC_Handle: IPCMSG_PING");
283                 if( Msg->Size < 4 )     return;
284                 if( Msg->Flags & IPCMSG_FLAG_RETURN )
285                 {
286                         Msg->ID = IPCMSG_PING;
287                         Msg->Size = sizeof(tIPCMsg_Return);
288                         ((tIPCMsg_Return*)Msg->Data)->Value = AXWIN_VERSION;
289                         IPCType->SendMessage(Ident, sizeof(tIPCMsg_Return), Msg);
290                 }
291                 break;
292
293         // ---  Create window
294         case IPCMSG_CREATEWIN:
295                 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
296                 IPC_Msg_CreateWin(client, Msg);
297                 break;
298
299         // --- Unknown message
300         default:
301                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
302                 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
303                 break;
304         }
305 }
306

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