Usermode/AxWin3 - Implementing SendMessage for client
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / wm.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.c
6  * - Core window management functions
7  */
8 #include <axwin3/axwin.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "include/internal.h"
12 #include "include/ipc.h"
13
14 #define WINDOWS_PER_ALLOC       (63)
15
16 typedef struct sWindowBlock     tWindowBlock;
17
18 typedef struct sAxWin3_Window   tWindow;
19
20 // === STRUCTURES ===
21 struct sWindowBlock
22 {
23         tWindowBlock    *Next;
24         tWindow *Windows[WINDOWS_PER_ALLOC];
25 };
26
27 // === GLOBALS ===
28  int    giAxWin3_LowestFreeWinID;
29  int    giAxWin3_HighestUsedWinID;
30 tWindowBlock    gAxWin3_WindowList;
31
32 // === CODE ===
33 tWindow *AxWin3_int_CreateWindowStruct(uint32_t ServerID, int ExtraBytes)
34 {
35         tWindow *ret;
36         
37         ret = calloc(sizeof(tWindow) + ExtraBytes, 1);
38         ret->ServerID = ServerID;
39
40         return ret;
41 }
42
43 tWindow *AxWin3_int_AllocateWindowInfo(int DataBytes, int *WinID)
44 {
45          int    idx, newWinID;
46         tWindowBlock *block, *prev;
47         tWindow *ret;   
48
49         block = &gAxWin3_WindowList;
50         newWinID = giAxWin3_LowestFreeWinID;
51         for( idx = 0; block; newWinID ++ )
52         {
53                 if( block->Windows[idx] == NULL )
54                         break;
55                 idx ++;
56                 if(idx == WINDOWS_PER_ALLOC) {
57                         prev = block;
58                         block = block->Next;
59                         idx = 0;
60                 }
61         }
62         
63         if( !block )
64         {
65                 block = calloc(sizeof(tWindowBlock), 1);
66                 prev->Next = block;
67                 idx = 0;
68         }
69         
70         ret = block->Windows[idx] = AxWin3_int_CreateWindowStruct(newWinID, DataBytes);
71         if(newWinID > giAxWin3_HighestUsedWinID)
72                 giAxWin3_HighestUsedWinID = newWinID;
73         if(newWinID == giAxWin3_LowestFreeWinID)
74                 giAxWin3_HighestUsedWinID ++;
75
76         if(WinID)       *WinID = newWinID;
77
78         return ret;
79 }
80
81 tHWND AxWin3_CreateWindow(
82         tHWND Parent, const char *Renderer, int RendererArg,
83         int DataBytes, tAxWin3_WindowMessageHandler MessageHandler
84         )
85 {
86         tWindow *ret;
87          int    newWinID;
88          int    dataSize = sizeof(tIPCMsg_CreateWin) + strlen(Renderer) + 1;
89         tAxWin_IPCMessage       *msg;
90         tIPCMsg_CreateWin       *create_win;
91
92         // Allocate a window ID
93         ret = AxWin3_int_AllocateWindowInfo(DataBytes, &newWinID);
94         if(!ret)        return NULL;
95         ret->Handler = MessageHandler;
96
97         // Create message
98         msg = AxWin3_int_AllocateIPCMessage(Parent, IPCMSG_CREATEWIN, 0, dataSize);
99         create_win = (void*)msg->Data;  
100         create_win->NewWinID = newWinID;
101         create_win->RendererArg = RendererArg;
102         strcpy(create_win->Renderer, Renderer);
103
104         // Send and clean up
105         AxWin3_int_SendIPCMessage(msg);
106         free(msg);
107
108         // TODO: Detect and handle possible errors
109
110         // Return success
111         return ret;
112 }
113
114 void AxWin3_DestroyWindow(tHWND Window)
115 {
116         tAxWin_IPCMessage       *msg;
117         
118         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DESTROYWIN, 0, 0);
119         AxWin3_int_SendIPCMessage(msg);
120         free(msg);
121 }
122
123 void *AxWin3_int_GetDataPtr(tHWND Window)
124 {
125         return &Window->Data;
126 }
127
128 void AxWin3_SendMessage(tHWND Window, tHWND Destination, int Message, int Length, void *Data)
129 {
130         tAxWin_IPCMessage       *msg;
131         tIPCMsg_SendMsg *info;
132         
133         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SENDMSG, 0, sizeof(*info)+Length);
134         info = (void*)msg->Data;
135         info->Dest = AxWin3_int_GetWindowID(Destination);
136 }
137
138 void AxWin3_ShowWindow(tHWND Window, int bShow)
139 {
140         tAxWin_IPCMessage       *msg;
141         tIPCMsg_ShowWindow      *info;
142
143         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SHOWWINDOW, 0, sizeof(*info));
144         info = (void*)msg->Data;
145         info->bShow = !!bShow;
146         
147         AxWin3_int_SendIPCMessage(msg);
148         
149         free(msg);
150 }
151
152 void AxWin3_SetWindowPos(tHWND Window, short X, short Y, short W, short H)
153 {
154         tAxWin_IPCMessage       *msg;
155         tIPCMsg_SetWindowPos    *info;
156         
157         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
158         info = (void*)msg->Data;
159
160         info->bSetPos = 1;
161         info->bSetDims = 1;
162         info->X = X;    info->Y = Y;
163         info->W = W;    info->H = H;
164
165         AxWin3_int_SendIPCMessage(msg); 
166         free(msg);
167 }
168
169 void AxWin3_MoveWindow(tHWND Window, short X, short Y)
170 {
171         tAxWin_IPCMessage       *msg;
172         tIPCMsg_SetWindowPos    *info;
173         
174         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
175         info = (void*)msg->Data;
176
177         info->bSetPos = 1;
178         info->bSetDims = 0;
179         info->X = X;
180         info->Y = Y;
181         
182         AxWin3_int_SendIPCMessage(msg);
183         
184         free(msg);
185 }
186
187 void AxWin3_ResizeWindow(tHWND Window, short W, short H)
188 {
189         tAxWin_IPCMessage       *msg;
190         tIPCMsg_SetWindowPos    *info;
191         
192         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
193         info = (void*)msg->Data;
194
195         info->bSetPos = 0;
196         info->bSetDims = 1;
197         info->W = W;
198         info->H = H;
199         
200         AxWin3_int_SendIPCMessage(msg);
201         
202         free(msg);
203 }
204

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