2 * Acess GUI (AxWin) Version 2
3 * By John Hodge (thePowersGang)
8 #include <axwin2/messages.h>
11 #define AXWIN_PORT 4101
13 #define STATICBUF_SIZE 64
19 void IPC_FillSelect(int *nfds, fd_set *set);
20 void IPC_HandleSelect(fd_set *set);
21 void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg);
22 void IPC_ReturnValue(tIPC_Type *IPCType, void *Ident, int MessageID, uint32_t Value);
23 int IPC_Type_Datagram_GetSize(void *Ident);
24 int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2);
25 void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data);
26 int IPC_Type_Sys_GetSize(void *Ident);
27 int IPC_Type_Sys_Compare(void *Ident1, void *Ident2);
28 void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data);
31 int giNetworkFileHandle = -1;
32 int giMessagesFileHandle = -1;
33 tIPC_Type gIPC_Type_Datagram = {
34 IPC_Type_Datagram_GetSize,
35 IPC_Type_Datagram_Compare,
36 IPC_Type_Datagram_Send
38 tIPC_Type gIPC_Type_SysMessage = {
49 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
50 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
53 void IPC_FillSelect(int *nfds, fd_set *set)
55 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
56 FD_SET(giNetworkFileHandle, set);
59 void IPC_HandleSelect(fd_set *set)
61 if( FD_ISSET(giNetworkFileHandle, set) )
63 char staticBuf[STATICBUF_SIZE];
64 int readlen, identlen;
67 readlen = read(giNetworkFileHandle, sizeof(staticBuf), staticBuf);
69 // Assume that all connections are from localhost
70 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
71 msg = staticBuf + identlen;
73 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
76 while(SysGetMessage(NULL, NULL))
79 int len = SysGetMessage(&tid, NULL);
81 SysGetMessage(NULL, data);
83 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
87 void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg)
92 if( MsgLen < sizeof(tAxWin_Message) )
94 if( MsgLen < sizeof(tAxWin_Message) + Msg->Size )
97 app = AxWin_GetClient(IPCType, Ident);
99 switch((enum eAxWin_Messages) Msg->ID)
101 // --- Ping message (reset timeout and get server version)
103 if( MsgLen < sizeof(tAxWin_Message) + 4 ) return;
104 Msg->ID = MSG_SRSP_VERSION;
108 *(uint16_t*)&Msg->Data[2] = -1;
109 IPCType->SendMessage(Ident, sizeof(Msg->ID), Msg);
113 // --- Register an application
114 case MSG_SREQ_REGISTER:
115 if( Msg->Data[Msg->Size-1] != '\0' ) {
121 _SysDebug("Notice: Duplicate registration (%s)\n", Msg->Data);
125 // TODO: Should this function be implemented here?
126 AxWin_RegisterClient(IPCType, Ident, Msg->Data);
129 // --- Create a window
130 case MSG_SREQ_ADDWIN:
131 if( Msg->Data[Msg->Size-1] != '\0' ) {
136 ele = AxWin_CreateAppWindow(app, Msg->Data);
137 IPC_ReturnValue(IPCType, Ident, MSG_SREQ_ADDWIN, ele->ApplicationID);
140 // --- Set a window's icon
141 case MSG_SREQ_SETICON:
142 // TODO: Find a good way of implementing this
145 // --- Create an element
146 case MSG_SREQ_INSERT: {
147 struct sAxWin_SReq_NewElement *info = (void *)Msg->Data;
149 if( Msg->Size != sizeof(*info) ) return;
151 if( !app || info->Parent > app->MaxElementIndex ) return ;
153 ele = AxWin_CreateElement( app->EleIndex[info->Parent], info->Type, info->Flags, NULL );
154 IPC_ReturnValue(IPCType, Ident, MSG_SREQ_ADDWIN, ele->ApplicationID);
157 // --- Unknown message
159 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
160 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
165 void IPC_ReturnValue(tIPC_Type *IPCType, void *Ident, int MessageID, uint32_t Value)
167 char data[sizeof(tAxWin_Message) + sizeof(tAxWin_RetMsg)];
168 tAxWin_Message *msg = (void *)data;
169 tAxWin_RetMsg *ret_msg = (void *)msg->Data;
171 msg->Source = 0; // 0 = Server
172 msg->ID = MSG_SRSP_RETURN;
173 msg->Size = sizeof(tAxWin_RetMsg);
174 ret_msg->ReqID = MessageID;
176 ret_msg->Value = Value;
178 IPCType->SendMessage(Ident, sizeof(data), data);
181 int IPC_Type_Datagram_GetSize(void *Ident)
183 return 4 + Net_GetAddressSize( ((uint16_t*)Ident)[1] );
186 int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2)
189 // - No need to worry about mis-matching sizes, as the size is computed
190 // from the 3rd/4th bytes, hence it will differ before the size is hit.
191 return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
194 void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data)
196 int identlen = IPC_Type_Datagram_GetSize(Ident);
197 char tmpbuf[ identlen + Length ];
198 memcpy(tmpbuf, Ident, identlen); // Header
199 memcpy(tmpbuf + identlen, Data, Length); // Data
200 // TODO: Handle fragmented packets
201 write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf);
204 int IPC_Type_Sys_GetSize(void *Ident)
206 return sizeof(pid_t);
209 int IPC_Type_Sys_Compare(void *Ident1, void *Ident2)
211 return *(int*)Ident1 - *(int*)Ident2;
214 void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data)
216 SysSendMessage( *(tid_t*)Ident, Length, Data );