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 void WM_DestroyWindow(tWindow *Window)
99 // TODO: Lock window and flag as invalid
101 // - Remove from render tree
103 // TODO: Lock render tree?
104 tWindow *prev = Window->PrevSibling;
105 tWindow *next = Window->NextSibling;
107 prev->NextSibling = next;
109 Window->Parent->FirstChild = next;
111 next->PrevSibling = prev;
113 Window->Parent->LastChild = prev;
115 WM_Invalidate(Window->Parent);
117 // - Remove from inheritance tree?
119 // - Clean up render children
120 // Lock should not be needed
122 for( win = Window->FirstChild; win; win = next )
124 next = win->NextSibling;
125 ASSERT(Window->FirstChild->Parent == Window);
126 WM_DestroyWindow(win);
129 // - Clean up inheriting children?
131 // - Tell renderer to clean up
132 if( Window->Renderer->DestroyWindow )
133 Window->Renderer->DestroyWindow(Window);
135 _SysDebug("WARN: Renderer %s does not have a destroy function", Window->Renderer->Name);
137 // - Clean up render cache and window structure
139 free(Window->RenderBuffer);
143 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
145 return IPC_int_GetWindow(Requester->Client, ID);
148 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
152 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
153 ret->RendererInfo = ret + 1; // Get end of tWindow
158 void WM_SetWindowTitle(tWindow *Window, const char *Title)
162 Window->Title = strdup(Title);
163 _SysDebug("Window %p title set to '%s'", Window, Title);
166 void WM_RaiseWindow(tWindow *Window)
168 tWindow *parent = Window->Parent;
169 if(!Window->Parent) return ;
172 if(Window->PrevSibling)
173 Window->PrevSibling->NextSibling = Window->NextSibling;
174 if(Window->NextSibling)
175 Window->NextSibling->PrevSibling = Window->PrevSibling;
176 if(parent->FirstChild == Window)
177 parent->FirstChild = Window->NextSibling;
178 if(parent->LastChild == Window)
179 parent->LastChild = Window->PrevSibling;
182 if(parent->LastChild)
183 parent->LastChild->NextSibling = Window;
185 parent->FirstChild = Window;
186 Window->PrevSibling = parent->LastChild;
187 Window->NextSibling = NULL;
188 parent->LastChild = Window;
190 _SysDebug("Raised %p", Window);
194 void WM_RaiseWindow(tWindow *Window)
196 // Move to the last render position (move to top)
197 while(Window && Window->Parent)
199 if( Window->NextSibling )
202 if( Window->PrevSibling )
203 Window->PrevSibling->NextSibling = Window->NextSibling;
204 Window->NextSibling->PrevSibling = Window->PrevSibling;
206 Window->PrevSibling = Window->Parent->LastChild;
207 Window->NextSibling = NULL;
209 Window->PrevSibling->NextSibling = Window;
210 Window->Parent->LastChild = Window;
212 _SysDebug("WM_RaiseWindow: Raised %p", Window);
213 Window = Window->Parent;
218 void WM_FocusWindow(tWindow *Destination)
220 struct sWndMsg_Bool _msg;
222 _SysDebug("WM_FocusWindow(%p)", Destination);
224 if( gpWM_FocusedWindow == Destination )
226 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
229 if( gpWM_FocusedWindow )
232 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
237 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
240 WM_Invalidate(gpWM_FocusedWindow);
241 WM_Invalidate(Destination);
243 gpWM_FocusedWindow = Destination;
247 void WM_ShowWindow(tWindow *Window, int bShow)
249 struct sWndMsg_Bool _msg;
251 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
256 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
260 Window->Flags |= WINFLAG_SHOW;
261 _SysDebug("Window %p shown", Window);
265 Window->Flags &= ~WINFLAG_SHOW;
267 if( Window == gpWM_FocusedWindow )
268 WM_FocusWindow(Window->Parent);
270 // Just a little memory saving for large hidden windows
271 if(Window->RenderBuffer) {
272 free(Window->RenderBuffer);
273 Window->RenderBuffer = NULL;
275 _SysDebug("Window %p hidden", Window);
278 WM_Invalidate(Window);
281 void WM_DecorateWindow(tWindow *Window, int bDecorate)
283 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
287 Window->Flags &= ~WINFLAG_NODECORATE;
289 Window->Flags |= WINFLAG_NODECORATE;
291 // Needed because the window size changes
292 if(Window->RenderBuffer) {
293 free(Window->RenderBuffer);
294 Window->RenderBuffer = NULL;
297 WM_Invalidate(Window);
300 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
302 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
303 // No meaning if no parent
304 if( !Window->Parent )
307 // Check that the flag is changing
308 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
311 if( bRelativeToParent ) {
313 Window->Flags |= WINFLAG_RELATIVE;
314 WM_MoveWindow(Window, Window->X, Window->Y);
318 Window->Flags &= ~WINFLAG_RELATIVE;
319 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
323 int WM_MoveWindow(tWindow *Window, int X, int Y)
325 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
327 if(X + Window->W < 0) X = -Window->W + 1;
328 if(Y + Window->H < 0) Y = -Window->H + 1;
329 if(X >= giScreenWidth) X = giScreenWidth - 1;
330 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
332 // If relative to the parent, extra checks
333 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
335 if( X > Window->Parent->W ) return 1;
336 if( Y > Window->Parent->H ) return 1;
340 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
341 Window->X = X; Window->Y = Y;
343 // Mark up the tree that a child window has changed
344 while( (Window = Window->Parent) )
345 Window->Flags &= ~WINFLAG_CHILDCLEAN;
350 int WM_ResizeWindow(tWindow *Window, int W, int H)
352 if(W <= 0 || H <= 0 ) return 1;
353 if(Window->X + W < 0) Window->X = -W + 1;
354 if(Window->Y + H < 0) Window->Y = -H + 1;
356 if( Window->W == W && Window->H == H )
359 _SysDebug("WM_Resizeindow: %ix%i", W, H);
360 Window->W = W; Window->H = H;
362 if(Window->RenderBuffer) {
363 free(Window->RenderBuffer);
364 Window->RenderBuffer = NULL;
366 WM_Invalidate(Window);
369 struct sWndMsg_Resize msg;
372 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
378 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
380 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
382 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
385 if(Length > 0 && Data == NULL) {
386 _SysDebug("WM_SendMessage: non-zero length and NULL data");
390 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
392 // TODO: Catch errors from ->HandleMessage
393 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
397 // ->HandleMessage returns 1 when the message was not handled
398 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
400 // TODO: Catch errors from ->HandleMessage
401 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
405 // TODO: Implement message masking
407 // Dispatch to client
413 else if(Source->Client != Dest->Client) {
414 // TODO: Support different client source windows
415 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
422 // _SysDebug("WM_SendMessage: IPC Dispatch");
423 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
429 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
431 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
435 void WM_Invalidate(tWindow *Window)
438 // _SysDebug("Invalidating %p", Window);
439 // Don't invalidate twice (speedup)
440 // if( !(Window->Flags & WINFLAG_CLEAN) ) return;
442 // Mark for re-render
443 Window->Flags &= ~WINFLAG_CLEAN;
445 // Mark up the tree that a child window has changed
446 while( (Window = Window->Parent) )
447 Window->Flags &= ~WINFLAG_CHILDCLEAN;
450 // --- Rendering / Update
451 void WM_int_UpdateWindow(tWindow *Window)
453 int bDecoratorRedraw = 0;
455 // Ignore hidden windows
456 if( !(Window->Flags & WINFLAG_SHOW) )
459 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
461 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
462 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
466 Window->RealX = Window->X;
467 Window->RealY = Window->Y;
471 if( !(Window->Flags & WINFLAG_CLEAN) )
473 // Calculate RealW/RealH
474 if( !(Window->Flags & WINFLAG_NODECORATE) )
476 //_SysDebug("Applying decorations to %p", Window);
477 Decorator_UpdateBorderSize(Window);
478 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
479 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
480 bDecoratorRedraw = 1;
488 Window->RealW = Window->W;
489 Window->RealH = Window->H;
492 Window->Renderer->Redraw(Window);
493 Window->Flags |= WINFLAG_CLEAN;
497 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
500 for( child = Window->FirstChild; child; child = child->NextSibling )
502 WM_int_UpdateWindow(child);
504 Window->Flags |= WINFLAG_CHILDCLEAN;
507 if( bDecoratorRedraw )
508 Decorator_Redraw(Window);
511 void WM_int_BlitWindow(tWindow *Window)
515 // Ignore hidden windows
516 if( !(Window->Flags & WINFLAG_SHOW) )
519 // Duplicated position update to handle window moving
520 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
522 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
523 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
527 Window->RealX = Window->X;
528 Window->RealY = Window->Y;
531 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
532 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
533 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
535 if( Window == gpWM_FocusedWindow && Window->CursorW )
538 Window->RealX + Window->BorderL + Window->CursorX,
539 Window->RealY + Window->BorderT + Window->CursorY,
540 Window->CursorW, Window->CursorH,
545 for( child = Window->FirstChild; child; child = child->NextSibling )
547 WM_int_BlitWindow(child);
553 // Don't redraw if nothing has changed
554 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
557 // - Iterate through visible windows, updating them as needed
558 WM_int_UpdateWindow(gpWM_RootWindow);
560 // - Draw windows from back to front to the render buffer
561 WM_int_BlitWindow(gpWM_RootWindow);