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
121 // Lock should not be needed
123 for( win = Window->FirstChild; win; win = next )
125 next = win->NextSibling;
126 ASSERT(Window->FirstChild->Parent == Window);
127 WM_DestroyWindow(win);
131 // - Clean up inheriting children?
133 // - Tell renderer to clean up
134 if( Window->Renderer->DestroyWindow )
135 Window->Renderer->DestroyWindow(Window);
137 _SysDebug("WARN: Renderer %s does not have a destroy function", Window->Renderer->Name);
139 // - Tell client to clean up
140 WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
142 // - Clean up render cache and window structure
144 free(Window->RenderBuffer);
148 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
150 return IPC_int_GetWindow(Requester->Client, ID);
153 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
157 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
158 ret->RendererInfo = ret + 1; // Get end of tWindow
163 void WM_SetWindowTitle(tWindow *Window, const char *Title)
167 Window->Title = strdup(Title);
168 _SysDebug("Window %p title set to '%s'", Window, Title);
171 void WM_RaiseWindow(tWindow *Window)
173 tWindow *parent = Window->Parent;
174 if(!Window->Parent) return ;
177 if(Window->PrevSibling)
178 Window->PrevSibling->NextSibling = Window->NextSibling;
179 if(Window->NextSibling)
180 Window->NextSibling->PrevSibling = Window->PrevSibling;
181 if(parent->FirstChild == Window)
182 parent->FirstChild = Window->NextSibling;
183 if(parent->LastChild == Window)
184 parent->LastChild = Window->PrevSibling;
187 if(parent->LastChild)
188 parent->LastChild->NextSibling = Window;
190 parent->FirstChild = Window;
191 Window->PrevSibling = parent->LastChild;
192 Window->NextSibling = NULL;
193 parent->LastChild = Window;
195 _SysDebug("Raised %p", Window);
199 void WM_RaiseWindow(tWindow *Window)
201 // Move to the last render position (move to top)
202 while(Window && Window->Parent)
204 if( Window->NextSibling )
207 if( Window->PrevSibling )
208 Window->PrevSibling->NextSibling = Window->NextSibling;
209 Window->NextSibling->PrevSibling = Window->PrevSibling;
211 Window->PrevSibling = Window->Parent->LastChild;
212 Window->NextSibling = NULL;
214 Window->PrevSibling->NextSibling = Window;
215 Window->Parent->LastChild = Window;
217 _SysDebug("WM_RaiseWindow: Raised %p", Window);
218 Window = Window->Parent;
223 void WM_FocusWindow(tWindow *Destination)
225 struct sWndMsg_Bool _msg;
227 _SysDebug("WM_FocusWindow(%p)", Destination);
229 if( gpWM_FocusedWindow == Destination )
231 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
234 if( gpWM_FocusedWindow )
237 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
242 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
245 WM_Invalidate(gpWM_FocusedWindow);
246 WM_Invalidate(Destination);
248 gpWM_FocusedWindow = Destination;
252 void WM_ShowWindow(tWindow *Window, int bShow)
254 struct sWndMsg_Bool _msg;
256 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
261 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
265 Window->Flags |= WINFLAG_SHOW;
266 _SysDebug("Window %p shown", Window);
270 Window->Flags &= ~WINFLAG_SHOW;
272 if( Window == gpWM_FocusedWindow )
273 WM_FocusWindow(Window->Parent);
275 // Just a little memory saving for large hidden windows
276 if(Window->RenderBuffer) {
277 free(Window->RenderBuffer);
278 Window->RenderBuffer = NULL;
280 _SysDebug("Window %p hidden", Window);
283 WM_Invalidate(Window);
286 void WM_DecorateWindow(tWindow *Window, int bDecorate)
288 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
292 Window->Flags &= ~WINFLAG_NODECORATE;
294 Window->Flags |= WINFLAG_NODECORATE;
296 // Needed because the window size changes
297 if(Window->RenderBuffer) {
298 free(Window->RenderBuffer);
299 Window->RenderBuffer = NULL;
302 WM_Invalidate(Window);
305 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
307 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
308 // No meaning if no parent
309 if( !Window->Parent )
312 // Check that the flag is changing
313 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
316 if( bRelativeToParent ) {
318 Window->Flags |= WINFLAG_RELATIVE;
319 WM_MoveWindow(Window, Window->X, Window->Y);
323 Window->Flags &= ~WINFLAG_RELATIVE;
324 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
328 int WM_MoveWindow(tWindow *Window, int X, int Y)
330 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
332 if(X + Window->W < 0) X = -Window->W + 1;
333 if(Y + Window->H < 0) Y = -Window->H + 1;
334 if(X >= giScreenWidth) X = giScreenWidth - 1;
335 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
337 // If relative to the parent, extra checks
338 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
340 if( X > Window->Parent->W ) return 1;
341 if( Y > Window->Parent->H ) return 1;
345 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
346 Window->X = X; Window->Y = Y;
348 // Mark up the tree that a child window has changed
349 while( (Window = Window->Parent) )
350 Window->Flags &= ~WINFLAG_CHILDCLEAN;
355 int WM_ResizeWindow(tWindow *Window, int W, int H)
357 if(W <= 0 || H <= 0 ) return 1;
358 if(Window->X + W < 0) Window->X = -W + 1;
359 if(Window->Y + H < 0) Window->Y = -H + 1;
361 if( Window->W == W && Window->H == H )
364 _SysDebug("WM_ResizeWindow: %ix%i", W, H);
365 Window->W = W; Window->H = H;
367 if(Window->RenderBuffer) {
368 free(Window->RenderBuffer);
369 Window->RenderBuffer = NULL;
371 WM_Invalidate(Window);
374 struct sWndMsg_Resize msg;
377 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
383 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
385 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
387 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
390 if(Length > 0 && Data == NULL) {
391 _SysDebug("WM_SendMessage: non-zero length and NULL data");
395 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
397 // TODO: Catch errors from ->HandleMessage
398 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
402 // ->HandleMessage returns 1 when the message was not handled
403 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
405 // TODO: Catch errors from ->HandleMessage
406 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
410 // TODO: Implement message masking
412 // Dispatch to client
418 else if(Source->Client != Dest->Client) {
419 // TODO: Support different client source windows
420 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
427 // _SysDebug("WM_SendMessage: IPC Dispatch");
428 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
434 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
436 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
440 void WM_Invalidate(tWindow *Window)
443 // _SysDebug("Invalidating %p", Window);
444 // Don't invalidate twice (speedup)
445 // if( !(Window->Flags & WINFLAG_CLEAN) ) return;
447 // Mark for re-render
448 Window->Flags &= ~WINFLAG_CLEAN;
450 // Mark up the tree that a child window has changed
451 while( (Window = Window->Parent) )
452 Window->Flags &= ~WINFLAG_CHILDCLEAN;
455 // --- Rendering / Update
456 void WM_int_UpdateWindow(tWindow *Window)
458 int bDecoratorRedraw = 0;
460 // Ignore hidden windows
461 if( !(Window->Flags & WINFLAG_SHOW) )
464 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
466 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
467 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
471 Window->RealX = Window->X;
472 Window->RealY = Window->Y;
476 if( !(Window->Flags & WINFLAG_CLEAN) )
478 // Calculate RealW/RealH
479 if( !(Window->Flags & WINFLAG_NODECORATE) )
481 //_SysDebug("Applying decorations to %p", Window);
482 Decorator_UpdateBorderSize(Window);
483 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
484 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
485 bDecoratorRedraw = 1;
493 Window->RealW = Window->W;
494 Window->RealH = Window->H;
497 Window->Renderer->Redraw(Window);
498 Window->Flags |= WINFLAG_CLEAN;
502 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
505 for( child = Window->FirstChild; child; child = child->NextSibling )
507 WM_int_UpdateWindow(child);
509 Window->Flags |= WINFLAG_CHILDCLEAN;
512 if( bDecoratorRedraw )
513 Decorator_Redraw(Window);
516 void WM_int_BlitWindow(tWindow *Window)
520 // Ignore hidden windows
521 if( !(Window->Flags & WINFLAG_SHOW) )
524 // Duplicated position update to handle window moving
525 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
527 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
528 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
532 Window->RealX = Window->X;
533 Window->RealY = Window->Y;
536 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
537 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
538 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
540 if( Window == gpWM_FocusedWindow && Window->CursorW )
543 Window->RealX + Window->BorderL + Window->CursorX,
544 Window->RealY + Window->BorderT + Window->CursorY,
545 Window->CursorW, Window->CursorH,
550 for( child = Window->FirstChild; child; child = child->NextSibling )
552 WM_int_BlitWindow(child);
558 // Don't redraw if nothing has changed
559 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
562 // - Iterate through visible windows, updating them as needed
563 WM_int_UpdateWindow(gpWM_RootWindow);
565 // - Draw windows from back to front to the render buffer
566 WM_int_BlitWindow(gpWM_RootWindow);