2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Core window management functions
8 #include <axwin3/axwin.h>
11 #include "include/internal.h"
12 #include "include/ipc.h"
13 #include <wm_messages.h>
15 #define WINDOWS_PER_ALLOC (64-1) // -1 to make it 64 pointers (+ Next)
16 #define MAX_HOTKEYS 32
18 typedef struct sWindowBlock tWindowBlock;
20 typedef struct sAxWin3_Window tWindow;
26 tWindow *Windows[WINDOWS_PER_ALLOC];
30 int giAxWin3_LowestFreeWinID;
31 int giAxWin3_HighestUsedWinID;
32 tWindowBlock gAxWin3_WindowList;
33 tAxWin3_HotkeyCallback gAxWin3_Hotkeys[MAX_HOTKEYS];
36 tWindow *AxWin3_int_CreateWindowStruct(uint32_t ServerID, int ExtraBytes)
40 ret = calloc(sizeof(tWindow) + ExtraBytes, 1);
41 ret->ServerID = ServerID;
46 tWindow *AxWin3_int_GetWindowFromID(uint32_t ServerID)
48 tWindowBlock *block = &gAxWin3_WindowList;
49 while(block && ServerID > WINDOWS_PER_ALLOC) {
51 ServerID -= WINDOWS_PER_ALLOC;
53 if(!block) return NULL;
54 return block->Windows[ServerID];
57 tWindow *AxWin3_int_AllocateWindowInfo(int DataBytes, int *WinID)
60 tWindowBlock *block, *prev = NULL;
63 block = &gAxWin3_WindowList;
64 newWinID = giAxWin3_LowestFreeWinID;
65 for( idx = 0; block; newWinID ++ )
67 if( block->Windows[idx] == NULL )
70 if(idx == WINDOWS_PER_ALLOC) {
79 block = calloc(sizeof(tWindowBlock), 1);
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 ++;
90 if(WinID) *WinID = newWinID;
95 int AxWin3_GetDisplayCount(void)
97 tAxWin_IPCMessage *msg;
98 tIPCMsg_ReturnInt *ret;
101 msg = AxWin3_int_AllocateIPCMessage(NULL, IPCMSG_GETDISPLAYCOUNT, IPCMSG_FLAG_RETURN, 0);
102 AxWin3_int_SendIPCMessage(msg);
105 msg = AxWin3_int_WaitIPCMessage(IPCMSG_GETDISPLAYCOUNT);
106 if(msg->Size < sizeof(*ret)) return -1;
107 ret = (void*)msg->Data;
113 int AxWin3_GetDisplayDims(int Display, int *X, int *Y, int *Width, int *Height)
115 tAxWin_IPCMessage *msg;
116 tIPCMsg_GetDisplayDims *req;
117 tIPCMsg_RetDisplayDims *ret;
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);
125 msg = AxWin3_int_WaitIPCMessage(IPCMSG_GETDISPLAYDIMS);
126 if(msg->Size < sizeof(*ret)) return -1;
127 ret = (void*)msg->Data;
131 if(Width) *Width = ret->W;
132 if(Height) *Height = ret->H;
137 tHWND AxWin3_CreateWindow(
138 tHWND Parent, const char *Renderer, int RendererArg,
139 int DataBytes, tAxWin3_WindowMessageHandler MessageHandler
144 int dataSize = sizeof(tIPCMsg_CreateWin) + strlen(Renderer) + 1;
145 tAxWin_IPCMessage *msg;
146 tIPCMsg_CreateWin *create_win;
148 // Allocate a window ID
149 ret = AxWin3_int_AllocateWindowInfo(DataBytes, &newWinID);
150 if(!ret) return NULL;
151 ret->Handler = MessageHandler;
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);
161 AxWin3_int_SendIPCMessage(msg);
164 _SysDebug("AxWin3_CreateWindow: %i :: '%s'", newWinID, Renderer);
166 // TODO: Detect and handle possible errors
172 void AxWin3_DestroyWindow(tHWND Window)
174 tAxWin_IPCMessage *msg;
176 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DESTROYWIN, 0, 0);
177 AxWin3_int_SendIPCMessage(msg);
181 void *AxWin3_int_GetDataPtr(tHWND Window)
186 void AxWin3_int_HandleMessage(tAxWin_IPCMessage *Msg)
190 dest = AxWin3_int_GetWindowFromID(Msg->Window);
194 case IPCMSG_SENDMSG: {
195 tIPCMsg_SendMsg *info = (void*)Msg->Data;
196 if(Msg->Size < sizeof(*info) || Msg->Size < sizeof(*info) + info->Length) {
197 _SysDebug("Message is undersized (%i < %i + %i)",
198 Msg->Size < sizeof(*info), info->Length);
201 if(!dest || !dest->Handler) {
202 _SysDebug("No handler for destination %p", dest);
205 _SysDebug("IPC Message 0x%x - %i bytes", info->ID, info->Length);
207 if( dest->Handler(dest, info->ID, info->Length, info->Data) )
212 case WNDMSG_HOTKEY: {
213 const struct sWndMsg_Hotkey *mi = (void*)info->Data;
214 if( mi->ID >= MAX_HOTKEYS )
215 ; // TODO: Error when hotkey is out of range
216 else if( gAxWin3_Hotkeys[mi->ID] == 0 )
217 _SysDebug("--- Unmapped hotkey ID %i fired", mi->ID);
219 gAxWin3_Hotkeys[mi->ID]();
222 _SysDebug("--- Unhandled SENDMSG 0x%x win %i", info->ID, Msg->Window);
227 case IPCMSG_DESTROYWIN:
228 // Clean up resources associated with this window
231 _SysDebug("Unknow message ID %i", Msg->ID);
238 void AxWin3_SetWindowTitle(tHWND Window, const char *Title)
240 tAxWin_IPCMessage *msg;
241 int len = strlen(Title);
243 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINTITLE, 0, len+1);
244 strcpy(msg->Data, Title);
246 AxWin3_int_SendIPCMessage(msg);
251 void AxWin3_SendMessage(tHWND Window, tHWND Destination, int Message, int Length, void *Data)
253 tAxWin_IPCMessage *msg;
254 tIPCMsg_SendMsg *info;
256 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SENDMSG, 0, sizeof(*info)+Length);
257 info = (void*)msg->Data;
258 info->Remote = AxWin3_int_GetWindowID(Destination);
260 info->Length = Length;
261 memcpy(info->Data, Data, Length);
263 AxWin3_int_SendIPCMessage(msg);
267 void *AxWin3_WaitMessage(tHWND Window, int MessageID, size_t *Length)
269 tAxWin_IPCMessage *msg;
273 msg = AxWin3_int_WaitIPCMessage(IPCMSG_SENDMSG);
274 if( msg->Window != AxWin3_int_GetWindowID(Window) ) {
275 AxWin3_int_HandleMessage(msg);
278 tIPCMsg_SendMsg *info = (void*)msg->Data;
279 if( info->ID != MessageID ) {
280 AxWin3_int_HandleMessage(msg);
284 *Length = info->Length;
285 void *ret = malloc(info->Length);
286 memcpy(ret, info->Data, info->Length);
293 void AxWin3_SendIPC(tHWND Window, int Message, size_t Length, const void *Data)
295 tAxWin_IPCMessage *msg;
297 msg = AxWin3_int_AllocateIPCMessage(Window, Message, IPCMSG_FLAG_RENDERER, Length);
298 memcpy(msg->Data, Data, Length);
299 AxWin3_int_SendIPCMessage(msg);
303 void *AxWin3_WaitIPCMessage(tHWND Window, int MessageID, size_t *Length)
305 tAxWin_IPCMessage *msg;
308 msg = AxWin3_int_WaitIPCMessage(MessageID);
309 if( !(msg->Flags & IPCMSG_FLAG_RENDERER) || msg->Window != AxWin3_int_GetWindowID(Window) ) {
310 AxWin3_int_HandleMessage(msg);
315 void *ret = malloc(msg->Size);
316 memcpy(ret, msg->Data, msg->Size);
323 void AxWin3_FocusWindow(tHWND Window)
325 tAxWin_IPCMessage *msg;
327 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_FOCUSWINDOW, 0, 0);
329 AxWin3_int_SendIPCMessage(msg);
333 void AxWin3_ShowWindow(tHWND Window, int bShow)
335 tAxWin_IPCMessage *msg;
336 tIPCMsg_Boolean *info;
338 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SHOWWINDOW, 0, sizeof(*info));
339 info = (void*)msg->Data;
340 info->Value = !!bShow;
342 AxWin3_int_SendIPCMessage(msg);
347 void AxWin3_DecorateWindow(tHWND Window, int bDecorate)
349 tAxWin_IPCMessage *msg;
350 tIPCMsg_Boolean *info;
352 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_DECORATEWINDOW, 0, sizeof(*info));
353 info = (void*)msg->Data;
354 info->Value = !!bDecorate;
356 AxWin3_int_SendIPCMessage(msg);
361 void AxWin3_SetWindowPos(tHWND Window, short X, short Y, short W, short H)
363 tAxWin_IPCMessage *msg;
364 tIPCMsg_SetWindowPos *info;
366 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
367 info = (void*)msg->Data;
371 info->X = X; info->Y = Y;
372 info->W = W; info->H = H;
374 AxWin3_int_SendIPCMessage(msg);
378 void AxWin3_MoveWindow(tHWND Window, short X, short Y)
380 tAxWin_IPCMessage *msg;
381 tIPCMsg_SetWindowPos *info;
383 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
384 info = (void*)msg->Data;
391 AxWin3_int_SendIPCMessage(msg);
396 void AxWin3_ResizeWindow(tHWND Window, short W, short H)
398 tAxWin_IPCMessage *msg;
399 tIPCMsg_SetWindowPos *info;
401 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_SETWINPOS, 0, sizeof(*info));
402 info = (void*)msg->Data;
409 AxWin3_int_SendIPCMessage(msg);
414 int AxWin3_RegisterAction(tHWND Window, const char *Action, tAxWin3_HotkeyCallback cb)
417 for( i = 0; i < MAX_HOTKEYS; i ++ )
419 if( gAxWin3_Hotkeys[i] == NULL )
421 tAxWin_IPCMessage *msg;
422 struct sIPCMsg_RegAction *info;
423 gAxWin3_Hotkeys[i] = cb;
425 msg = AxWin3_int_AllocateIPCMessage(Window, IPCMSG_REGACTION, 0, sizeof(*info)+strlen(Action)+1);
426 info = (void*)msg->Data;
429 strcpy(info->Action, Action);
431 AxWin3_int_SendIPCMessage(msg);