2 * Acess2 Window Manager v3
3 * - By John Hodge (thePowersGang)
6 * - Window manager core
9 #include <wm_renderer.h>
13 #include <wm_messages.h>
14 #include <decorator.h>
17 extern void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int Msg, int Len, const void *Data);
18 extern void IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data);
19 extern tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t ID);
22 tWMRenderer *gpWM_Renderers;
23 tWindow *gpWM_RootWindow;
24 //! Window which will recieve the next keyboard event
25 tWindow *gpWM_FocusedWindow;
26 //! Hilighted window (owner of the currently focused window)
27 tWindow *gpWM_HilightedWindow;
30 void WM_Initialise(void)
32 WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
33 gpWM_RootWindow->W = giScreenWidth;
34 gpWM_RootWindow->H = giScreenHeight;
35 gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
38 void WM_RegisterRenderer(tWMRenderer *Renderer)
40 // TODO: Catch out duplicates
41 Renderer->Next = gpWM_Renderers;
42 gpWM_Renderers = Renderer;
46 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
48 tWMRenderer *renderer;
52 for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
54 if(strcmp(RendererName, renderer->Name) == 0)
60 // - Call create window function
61 ret = renderer->CreateWindow(RendererArg);
66 ret->Parent = gpWM_RootWindow;
68 ret->Renderer = renderer;
69 ret->Flags |= WINFLAG_CLEAN; // Needed to stop invaildate early exiting
74 if(ret->Parent->LastChild)
75 ret->Parent->LastChild->NextSibling = ret;
77 ret->Parent->FirstChild = ret;
78 ret->PrevSibling = ret->Parent->LastChild;
79 ret->Parent->LastChild = ret;
80 ret->NextSibling = NULL;
84 gpWM_RootWindow = ret;
87 // Don't decorate child windows by default
90 ret->Flags |= WINFLAG_NODECORATE;
97 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
99 return IPC_int_GetWindow(Requester->Client, ID);
102 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
106 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
107 ret->RendererInfo = ret + 1; // Get end of tWindow
112 void WM_SetWindowTitle(tWindow *Window, const char *Title)
116 Window->Title = strdup(Title);
117 _SysDebug("Window %p title set to '%s'", Window, Title);
120 void WM_RaiseWindow(tWindow *Window)
122 tWindow *parent = Window->Parent;
123 if(!Window->Parent) return ;
126 if(Window->PrevSibling)
127 Window->PrevSibling->NextSibling = Window->NextSibling;
128 if(Window->NextSibling)
129 Window->NextSibling->PrevSibling = Window->PrevSibling;
130 if(parent->FirstChild == Window)
131 parent->FirstChild = Window->NextSibling;
132 if(parent->LastChild == Window)
133 parent->LastChild = Window->PrevSibling;
136 if(parent->LastChild)
137 parent->LastChild->NextSibling = Window;
139 parent->FirstChild = Window;
140 Window->PrevSibling = parent->LastChild;
141 Window->NextSibling = NULL;
142 parent->LastChild = Window;
144 _SysDebug("Raised %p", Window);
148 void WM_RaiseWindow(tWindow *Window)
150 // Move to the last render position (move to top)
151 while(Window && Window->Parent)
153 if( Window->NextSibling )
156 if( Window->PrevSibling )
157 Window->PrevSibling->NextSibling = Window->NextSibling;
158 Window->NextSibling->PrevSibling = Window->PrevSibling;
160 Window->PrevSibling = Window->Parent->LastChild;
161 Window->NextSibling = NULL;
163 Window->PrevSibling->NextSibling = Window;
164 Window->Parent->LastChild = Window;
166 _SysDebug("WM_RaiseWindow: Raised %p", Window);
167 Window = Window->Parent;
172 void WM_FocusWindow(tWindow *Destination)
174 struct sWndMsg_Bool _msg;
176 _SysDebug("WM_FocusWindow(%p)", Destination);
178 if( gpWM_FocusedWindow == Destination )
180 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
183 if( gpWM_FocusedWindow )
186 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
191 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
194 WM_Invalidate(gpWM_FocusedWindow);
195 WM_Invalidate(Destination);
197 gpWM_FocusedWindow = Destination;
201 void WM_ShowWindow(tWindow *Window, int bShow)
203 struct sWndMsg_Bool _msg;
205 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
210 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
214 Window->Flags |= WINFLAG_SHOW;
215 _SysDebug("Window %p shown", Window);
219 Window->Flags &= ~WINFLAG_SHOW;
221 if( Window == gpWM_FocusedWindow )
222 WM_FocusWindow(Window->Parent);
224 // Just a little memory saving for large hidden windows
225 if(Window->RenderBuffer) {
226 free(Window->RenderBuffer);
227 Window->RenderBuffer = NULL;
229 _SysDebug("Window %p hidden", Window);
232 WM_Invalidate(Window);
235 void WM_DecorateWindow(tWindow *Window, int bDecorate)
237 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
241 Window->Flags &= ~WINFLAG_NODECORATE;
243 Window->Flags |= WINFLAG_NODECORATE;
245 // Needed because the window size changes
246 if(Window->RenderBuffer) {
247 free(Window->RenderBuffer);
248 Window->RenderBuffer = NULL;
251 WM_Invalidate(Window);
254 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
256 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
257 // No meaning if no parent
258 if( !Window->Parent )
261 // Check that the flag is changing
262 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
265 if( bRelativeToParent ) {
267 Window->Flags |= WINFLAG_RELATIVE;
268 WM_MoveWindow(Window, Window->X, Window->Y);
272 Window->Flags &= ~WINFLAG_RELATIVE;
273 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
277 int WM_MoveWindow(tWindow *Window, int X, int Y)
279 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
281 if(X + Window->W < 0) X = -Window->W + 1;
282 if(Y + Window->H < 0) Y = -Window->H + 1;
283 if(X >= giScreenWidth) X = giScreenWidth - 1;
284 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
286 // If relative to the parent, extra checks
287 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
289 if( X > Window->Parent->W ) return 1;
290 if( Y > Window->Parent->H ) return 1;
294 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
295 Window->X = X; Window->Y = Y;
297 // Mark up the tree that a child window has changed
298 while( (Window = Window->Parent) )
299 Window->Flags &= ~WINFLAG_CHILDCLEAN;
304 int WM_ResizeWindow(tWindow *Window, int W, int H)
306 if(W <= 0 || H <= 0 ) return 1;
307 if(Window->X + W < 0) Window->X = -W + 1;
308 if(Window->Y + H < 0) Window->Y = -H + 1;
310 if( Window->W == W && Window->H == H )
313 _SysDebug("WM_Resizeindow: %ix%i", W, H);
314 Window->W = W; Window->H = H;
316 if(Window->RenderBuffer) {
317 free(Window->RenderBuffer);
318 Window->RenderBuffer = NULL;
320 WM_Invalidate(Window);
323 struct sWndMsg_Resize msg;
326 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
332 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
334 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
336 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
339 if(Length > 0 && Data == NULL) {
340 _SysDebug("WM_SendMessage: non-zero length and NULL data");
344 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
346 // TODO: Catch errors from ->HandleMessage
347 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
351 // ->HandleMessage returns 1 when the message was not handled
352 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
354 // TODO: Catch errors from ->HandleMessage
355 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
359 // TODO: Implement message masking
361 // Dispatch to client
367 else if(Source->Client != Dest->Client) {
368 // TODO: Support different client source windows
369 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
376 // _SysDebug("WM_SendMessage: IPC Dispatch");
377 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
383 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
385 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
389 void WM_Invalidate(tWindow *Window)
392 // _SysDebug("Invalidating %p", Window);
393 // Don't invalidate twice (speedup)
394 // if( !(Window->Flags & WINFLAG_CLEAN) ) return;
396 // Mark for re-render
397 Window->Flags &= ~WINFLAG_CLEAN;
399 // Mark up the tree that a child window has changed
400 while( (Window = Window->Parent) )
401 Window->Flags &= ~WINFLAG_CHILDCLEAN;
404 // --- Rendering / Update
405 void WM_int_UpdateWindow(tWindow *Window)
407 int bDecoratorRedraw = 0;
409 // Ignore hidden windows
410 if( !(Window->Flags & WINFLAG_SHOW) )
413 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
415 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
416 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
420 Window->RealX = Window->X;
421 Window->RealY = Window->Y;
425 if( !(Window->Flags & WINFLAG_CLEAN) )
427 // Calculate RealW/RealH
428 if( !(Window->Flags & WINFLAG_NODECORATE) )
430 //_SysDebug("Applying decorations to %p", Window);
431 Decorator_UpdateBorderSize(Window);
432 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
433 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
434 bDecoratorRedraw = 1;
442 Window->RealW = Window->W;
443 Window->RealH = Window->H;
446 Window->Renderer->Redraw(Window);
447 Window->Flags |= WINFLAG_CLEAN;
451 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
454 for( child = Window->FirstChild; child; child = child->NextSibling )
456 WM_int_UpdateWindow(child);
458 Window->Flags |= WINFLAG_CHILDCLEAN;
461 if( bDecoratorRedraw )
462 Decorator_Redraw(Window);
465 void WM_int_BlitWindow(tWindow *Window)
469 // Ignore hidden windows
470 if( !(Window->Flags & WINFLAG_SHOW) )
473 // Duplicated position update to handle window moving
474 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
476 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
477 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
481 Window->RealX = Window->X;
482 Window->RealY = Window->Y;
485 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
486 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
487 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
489 if( Window == gpWM_FocusedWindow && Window->CursorW )
492 Window->RealX + Window->BorderL + Window->CursorX,
493 Window->RealY + Window->BorderT + Window->CursorY,
494 Window->CursorW, Window->CursorH,
499 for( child = Window->FirstChild; child; child = child->NextSibling )
501 WM_int_BlitWindow(child);
507 // Don't redraw if nothing has changed
508 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
511 // - Iterate through visible windows, updating them as needed
512 WM_int_UpdateWindow(gpWM_RootWindow);
514 // - Draw windows from back to front to the render buffer
515 WM_int_BlitWindow(gpWM_RootWindow);