af7c81944d3dd1fcd6dff6a684f6d91f6294b86f
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / messages.c
1 /*
2  * Acess GUI (AxWin) Version 2
3  * By John Hodge (thePowersGang)
4  */
5 #include "common.h"
6 #include <acess/sys.h>
7 #include <net.h>
8 #include <axwin2/messages.h>
9 //#include <sys/select.h>
10
11 #define AXWIN_PORT      4101
12
13 #define STATICBUF_SIZE  64
14
15 // === TYPES ===
16 typedef void tMessages_Handle_Callback(void*, size_t, void*);
17
18 // === PROTOTYPES ===
19 void    Messages_PollIPC();
20 void    Messages_RespondIPC(void *Ident, size_t Length, void *Data);
21 void    Messages_Handle(void *Ident, int MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond);
22
23 // === GLOBALS ===
24  int    giNetworkFileHandle = -1;
25  int    giMessagesFileHandle = -1;
26
27 // === CODE ===
28 void IPC_Init(void)
29 {
30          int    tmp;
31         // TODO: Check this
32         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
33         tmp = AXWIN_PORT;       ioctl(giIPCFileHandle, 4, &tmp);        // TODO: Don't hard-code IOCtl number
34
35         // TODO: Open a handle to something like /Devices/proc/cur/messages to watch for messages
36 //      giMessagesFileHandle = open("/Devices/"
37 }
38
39 void IPC_FillSelect(int *nfds, fd_set *set)
40 {
41         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
42         FD_SET(giNetworkFileHandle, set);
43 }
44
45 void IPC_HandleSelect(fd_set *set)
46 {
47         if( FD_ISSET(giIPCFileHandle, set) )
48         {
49                 char    staticBuf[STATICBUF_SIZE];
50                  int    readlen, identlen;
51                 char    *msg;
52
53                 readlen = read(giIPCFileHandle, sizeof(staticBuf), staticBuf);
54                 
55                 // Assume that all connections are from localhost
56                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
57                 msg = staticBuf + identlen;
58
59                 Messages_Handle(staticBuf, readlen - identlen, (void*)msg, Messages_RespondIPC);
60         }
61 }
62
63 #if 0
64 void Messages_PollIPC()
65 {
66          int    len;
67         pid_t   tid = 0;
68         char    staticBuf[STATICBUF_SIZE];
69         tAxWin_Message  *msg;
70         
71         // Wait for a message
72         while( (len = SysGetMessage(&tid, NULL)) == 0 )
73                 sleep();
74         
75         // Allocate the space for it
76         if( len <= STATICBUF_SIZE )
77                 msg = (void*)staticBuf;
78         else {
79                 msg = malloc( len );
80                 if(!msg) {
81                         fprintf(
82                                 stderr,
83                                 "ERROR - Unable to allocate message buffer, ignoring message from %i\n",
84                                 tid);
85                         SysGetMessage(NULL, GETMSG_IGNORE);
86                         return ;
87                 }
88         }
89         
90         // Get message data
91         SysGetMessage(NULL, msg);
92         
93         Messages_Handle(msg, Messages_RespondIPC, tid);
94 }
95 #endif
96
97 void Messages_RespondIPC(void *Ident, size_t Length, void *Data)
98 {
99         SysSendMessage( *(tid_t*)Ident, Length, Data );
100 }
101
102 void Messages_Handle(void *Ident, int MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond)
103 {
104         switch(Msg->ID)
105         {
106         #if 0
107         case MSG_SREQ_PING:
108                 Msg->ID = MSG_SRSP_VERSION;
109                 Msg->Size = 2;
110                 Msg->Data[0] = 0;
111                 Msg->Data[1] = 1;
112                 *(uint16_t*)&Msg->Data[2] = -1;
113                 Respond(Ident, sizeof(Msg->ID), Msg);
114                 break;
115         #endif
116         default:
117                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
118                 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
119                 break;
120         }
121 }
122

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