412f5832f0582680a90e99b949e1b50683819ed9
[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 #include <wm_messages.h>
14
15 #define WINDOWS_PER_ALLOC       (64-1)  // -1 to make it 64 pointers (+ Next)
16 #define MAX_HOTKEYS     32
17
18 typedef struct sWindowBlock     tWindowBlock;
19
20 typedef struct sAxWin3_Window   tWindow;
21
22 // === STRUCTURES ===
23 struct sWindowBlock
24 {
25         tWindowBlock    *Next;
26         tWindow *Windows[WINDOWS_PER_ALLOC];
27 };
28
29 // === GLOBALS ===
30  int    giAxWin3_LowestFreeWinID;
31  int    giAxWin3_HighestUsedWinID;
32 tWindowBlock    gAxWin3_WindowList;
33 tAxWin3_HotkeyCallback  gAxWin3_Hotkeys[MAX_HOTKEYS];
34
35 // === CODE ===
36 tWindow *AxWin3_int_CreateWindowStruct(uint32_t ServerID, int ExtraBytes)
37 {
38         tWindow *ret;
39         
40         ret = calloc(sizeof(tWindow) + ExtraBytes, 1);
41         ret->ServerID = ServerID;
42
43         return ret;
44 }
45
46 tWindow *AxWin3_int_GetWindowFromID(uint32_t ServerID)
47 {
48         tWindowBlock    *block = &gAxWin3_WindowList;
49         while(block && ServerID > WINDOWS_PER_ALLOC) {
50                 block = block->Next;
51                 ServerID -= WINDOWS_PER_ALLOC;
52         }
53         if(!block)      return NULL;
54         return block->Windows[ServerID];
55 }
56
57 tWindow *AxWin3_int_AllocateWindowInfo(int DataBytes, int *WinID)
58 {
59          int    idx, newWinID;
60         tWindowBlock *block, *prev = NULL;
61         tWindow *ret;   
62
63         block = &gAxWin3_WindowList;
64         newWinID = giAxWin3_LowestFreeWinID;
65         for( idx = 0; block; newWinID ++ )
66         {
67                 if( block->Windows[idx] == NULL )
68                         break;
69                 idx ++;
70                 if(idx == WINDOWS_PER_ALLOC) {
71                         prev = block;
72                         block = block->Next;
73                         idx = 0;
74                 }
75         }
76         
77         if( !block )
78         {
79                 block = calloc(sizeof(tWindowBlock), 1);
80                 prev->Next = block;
81                 idx = 0;
82         }
83         
84         ret = block->Windows[idx] = AxWin3_int_CreateWindowStruct(newWinID, DataBytes);
85         if(newWinID > giAxWin3_HighestUsedWinID)
86                 giAxWin3_HighestUsedWinID = newWinID;
87         if(newWinID == giAxWin3_LowestFreeWinID)
88                 giAxWin3_HighestUsedWinID ++;
89
90         if(WinID)       *WinID = newWinID;
91
92         return ret;
93 }
94
95 int AxWin3_GetDisplayCount(void)
96 {
97         tAxWin_IPCMessage       *msg;
98         tIPCMsg_ReturnInt       *ret;
99          int    rv;
100         
101         msg = AxWin3_int_AllocateIPCMessage(NULL, IPCMSG_GETDISPLAYCOUNT, IPCMSG_FLAG_RETURN, 0);
102         AxWin3_int_SendIPCMessage(msg);
103         free(msg);      
104
105         msg = AxWin3_int_WaitIPCMessage(IPCMSG_GETDISPLAYCOUNT);
106         if(msg->Size < sizeof(*ret))    return -1;
107         ret = (void*)msg->Data;
108         rv = ret->Value;
109         free(msg);
110         return rv;
111 }
112
113 int AxWin3_GetDisplayDims(int Display, int *X, int *Y, int *Width, int *Height)
114 {
115         tAxWin_IPCMessage       *msg;
116         tIPCMsg_GetDisplayDims  *req;
117         tIPCMsg_RetDisplayDims  *ret;
118         
119         msg = AxWin3_int_AllocateIPCMessage(NULL, IPCMSG_GETDISPLAYDIMS, IPCMSG_FLAG_RETURN, sizeof(*req));
120         req = (void*)msg->Data;
121         req->DisplayID = Display;
122         AxWin3_int_SendIPCMessage(msg);
123         free(msg);
124
125         msg = AxWin3_int_WaitIPCMessage(IPCMSG_GETDISPLAYDIMS);
126         if(msg->Size < sizeof(*ret))    return -1;
127         ret = (void*)msg->Data;
128         
129         if(X)   *X = ret->X;
130         if(X)   *X = ret->Y;
131         if(Width)       *Width = ret->W;
132         if(Height)      *Height = ret->H;
133         
134         return 0;
135 }
136
137 tHWND AxWin3_CreateWindow(
138         tHWND Parent, const char *Renderer, int RendererArg,
139         int DataBytes, tAxWin3_WindowMessageHandler MessageHandler
140         )
141 {
142         tWindow *ret;
143          int    newWinID;
144          int    dataSize = sizeof(tIPCMsg_CreateWin) + strlen(Renderer) + 1;
145         tAxWin_IPCMessage       *msg;
146         tIPCMsg_CreateWin       *create_win;
147
148         // Allocate a window ID
149         ret = AxWin3_int_AllocateWindowInfo(DataBytes, &newWinID);
150         if(!ret)        return NULL;
151         ret->Handler = MessageHandler;
152
153         // Create message
154         msg = AxWin3_int_AllocateIPCMessage(Parent, IPCMSG_CREATEWIN, 0, dataSize);
155         create_win = (void*)msg->Data;  
156         create_win->NewWinID = newWinID;
157         create_win->RendererArg = RendererArg;
158         strcpy(create_win->Renderer, Renderer);
159
160         // Send and clean up
161         AxWin3_int_SendIPCMessage(msg);
162         free(msg);
163
164         // TODO: Detect and handle possible errors
165
166         // Return success
167         return ret;
168 }
169
170 void AxWin3_DestroyWindow(tHWND Window)
171 {
172         tAxWin_IPCMessage       *msg;
173         
174         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DESTROYWIN, 0, 0);
175         AxWin3_int_SendIPCMessage(msg);
176         free(msg);
177 }
178
179 void *AxWin3_int_GetDataPtr(tHWND Window)
180 {
181         return Window->Data;
182 }
183
184 void AxWin3_int_HandleMessage(tAxWin_IPCMessage *Msg)
185 {
186         tWindow *dest;
187
188         dest = AxWin3_int_GetWindowFromID(Msg->Window);
189
190         switch(Msg->ID)
191         {
192         case IPCMSG_SENDMSG: {
193                 tIPCMsg_SendMsg *info = (void*)Msg->Data;
194                 if(Msg->Size < sizeof(*info) || Msg->Size < sizeof(*info) + info->Length) {
195                         _SysDebug("Message is undersized (%i < %i + %i)",
196                                 Msg->Size < sizeof(*info), info->Length);
197                         return ;
198                 }
199                 if(!dest || !dest->Handler) {
200                         _SysDebug("No handler for destination %p", dest);
201                         return ;
202                 }
203                 _SysDebug("IPC Message 0x%x - %i bytes", info->ID, info->Length);
204
205                 if( dest->Handler(dest, info->ID, info->Length, info->Data) )
206                         ;
207                 else {
208                         switch( info->ID )
209                         {
210                         case WNDMSG_HOTKEY: {
211                                 const struct sWndMsg_Hotkey *mi = (void*)info->Data;
212                                 if( mi->ID >= MAX_HOTKEYS ) 
213                                         ;       // TODO: Error when hotkey is out of range
214                                 else if( gAxWin3_Hotkeys[mi->ID] == 0 )
215                                         _SysDebug("--- Unmapped hotkey ID %i fired", mi->ID);
216                                 else
217                                         gAxWin3_Hotkeys[mi->ID]();
218                                 }break;
219                         default:
220                                 _SysDebug("--- Unhandled SENDMSG %i", info->ID);
221                                 break;
222                         }
223                 }
224                 break; }
225         default:
226                 _SysDebug("Unknow message ID %i", Msg->ID);
227                 break;
228         }
229         
230         free(Msg);
231 }
232
233 void AxWin3_SetWindowTitle(tHWND Window, const char *Title)
234 {
235         tAxWin_IPCMessage       *msg;
236          int    len = strlen(Title);
237         
238         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINTITLE, 0, len+1);
239         strcpy(msg->Data, Title);
240         
241         AxWin3_int_SendIPCMessage(msg);
242         
243         free(msg);
244 }
245
246 void AxWin3_SendMessage(tHWND Window, tHWND Destination, int Message, int Length, void *Data)
247 {
248         tAxWin_IPCMessage       *msg;
249         tIPCMsg_SendMsg *info;
250         
251         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SENDMSG, 0, sizeof(*info)+Length);
252         info = (void*)msg->Data;
253         info->Remote = AxWin3_int_GetWindowID(Destination);
254         info->ID = Message;
255         info->Length = Length;
256         memcpy(info->Data, Data, Length);
257         
258         AxWin3_int_SendIPCMessage(msg);
259         free(msg);
260 }
261
262 void *AxWin3_WaitMessage(tHWND Window, int MessageID, size_t *Length)
263 {
264         tAxWin_IPCMessage       *msg;
265         
266         for( ;; )
267         {
268                 msg = AxWin3_int_WaitIPCMessage(IPCMSG_SENDMSG);
269                 if( msg->Window != AxWin3_int_GetWindowID(Window) ) {
270                         AxWin3_int_HandleMessage(msg);
271                         continue ;
272                 }
273                 tIPCMsg_SendMsg *info = (void*)msg->Data;
274                 if( info->ID != MessageID ) {
275                         AxWin3_int_HandleMessage(msg);
276                         continue ;
277                 }
278
279                 *Length = info->Length;
280                 void    *ret = malloc(info->Length);    
281                 memcpy(ret, info->Data, info->Length);
282                 free(msg);
283         
284                 return ret;
285         }
286 }
287
288 void AxWin3_FocusWindow(tHWND Window)
289 {
290         tAxWin_IPCMessage       *msg;
291         
292         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_FOCUSWINDOW, 0, 0);
293         
294         AxWin3_int_SendIPCMessage(msg);
295         free(msg);
296 }
297
298 void AxWin3_ShowWindow(tHWND Window, int bShow)
299 {
300         tAxWin_IPCMessage       *msg;
301         tIPCMsg_Boolean *info;
302
303         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SHOWWINDOW, 0, sizeof(*info));
304         info = (void*)msg->Data;
305         info->Value = !!bShow;
306         
307         AxWin3_int_SendIPCMessage(msg);
308         
309         free(msg);
310 }
311
312 void AxWin3_DecorateWindow(tHWND Window, int bDecorate)
313 {
314         tAxWin_IPCMessage       *msg;
315         tIPCMsg_Boolean *info;
316
317         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DECORATEWINDOW, 0, sizeof(*info));
318         info = (void*)msg->Data;
319         info->Value = !!bDecorate;
320         
321         AxWin3_int_SendIPCMessage(msg);
322         
323         free(msg);
324 }
325
326 void AxWin3_SetWindowPos(tHWND Window, short X, short Y, short W, short H)
327 {
328         tAxWin_IPCMessage       *msg;
329         tIPCMsg_SetWindowPos    *info;
330         
331         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
332         info = (void*)msg->Data;
333
334         info->bSetPos = 1;
335         info->bSetDims = 1;
336         info->X = X;    info->Y = Y;
337         info->W = W;    info->H = H;
338
339         AxWin3_int_SendIPCMessage(msg); 
340         free(msg);
341 }
342
343 void AxWin3_MoveWindow(tHWND Window, short X, short Y)
344 {
345         tAxWin_IPCMessage       *msg;
346         tIPCMsg_SetWindowPos    *info;
347         
348         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
349         info = (void*)msg->Data;
350
351         info->bSetPos = 1;
352         info->bSetDims = 0;
353         info->X = X;
354         info->Y = Y;
355         
356         AxWin3_int_SendIPCMessage(msg);
357         
358         free(msg);
359 }
360
361 void AxWin3_ResizeWindow(tHWND Window, short W, short H)
362 {
363         tAxWin_IPCMessage       *msg;
364         tIPCMsg_SetWindowPos    *info;
365         
366         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
367         info = (void*)msg->Data;
368
369         info->bSetPos = 0;
370         info->bSetDims = 1;
371         info->W = W;
372         info->H = H;
373         
374         AxWin3_int_SendIPCMessage(msg);
375         
376         free(msg);
377 }
378
379 int AxWin3_RegisterAction(tHWND Window, const char *Action, tAxWin3_HotkeyCallback cb)
380 {
381          int    i;
382         for( i = 0; i < MAX_HOTKEYS; i ++ )
383         {
384                 if( gAxWin3_Hotkeys[i] == NULL )
385                 {
386                         tAxWin_IPCMessage       *msg;
387                         struct sIPCMsg_RegAction        *info;
388                         gAxWin3_Hotkeys[i] = cb;
389                         
390                         msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_REGACTION, 0, sizeof(*info)+strlen(Action)+1);
391                         info = (void*)msg->Data;
392                 
393                         info->Index = i;
394                         strcpy(info->Action, Action);
395                         
396                         AxWin3_int_SendIPCMessage(msg);
397                         free(msg);
398                         return i;
399                 }
400         }
401         return -1;
402 }
403

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