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;
116 WM_Invalidate(Window, 1);
118 // - Remove from inheritance tree?
120 // - Clean up render children
122 // Lock should not be needed
124 for( win = Window->FirstChild; win; win = next )
126 next = win->NextSibling;
127 ASSERT(Window->FirstChild->Parent == Window);
128 WM_DestroyWindow(win);
132 // - Clean up inheriting children?
134 // - Tell renderer to clean up
135 if( Window->Renderer->DestroyWindow )
136 Window->Renderer->DestroyWindow(Window);
138 _SysDebug("WARN: Renderer '%s' does not have a destroy function", Window->Renderer->Name);
140 // - Tell client to clean up
141 WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
143 // - Clean up render cache and window structure
145 free(Window->RenderBuffer);
149 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
151 return IPC_int_GetWindow(Requester->Client, ID);
154 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
158 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
159 ret->RendererInfo = ret + 1; // Get end of tWindow
164 void WM_SetWindowTitle(tWindow *Window, const char *Title)
168 Window->Title = strdup(Title);
169 _SysDebug("Window %p title set to '%s'", Window, Title);
172 void WM_RaiseWindow(tWindow *Window)
174 tWindow *parent = Window->Parent;
175 if(!Window->Parent) return ;
178 if(Window->PrevSibling)
179 Window->PrevSibling->NextSibling = Window->NextSibling;
180 if(Window->NextSibling)
181 Window->NextSibling->PrevSibling = Window->PrevSibling;
182 if(parent->FirstChild == Window)
183 parent->FirstChild = Window->NextSibling;
184 if(parent->LastChild == Window)
185 parent->LastChild = Window->PrevSibling;
188 if(parent->LastChild)
189 parent->LastChild->NextSibling = Window;
191 parent->FirstChild = Window;
192 Window->PrevSibling = parent->LastChild;
193 Window->NextSibling = NULL;
194 parent->LastChild = Window;
196 _SysDebug("Raised %p", Window);
200 void WM_RaiseWindow(tWindow *Window)
202 // Move to the last render position (move to top)
203 while(Window && Window->Parent)
205 if( Window->NextSibling )
208 if( Window->PrevSibling )
209 Window->PrevSibling->NextSibling = Window->NextSibling;
210 Window->NextSibling->PrevSibling = Window->PrevSibling;
212 Window->PrevSibling = Window->Parent->LastChild;
213 Window->NextSibling = NULL;
215 Window->PrevSibling->NextSibling = Window;
216 Window->Parent->LastChild = Window;
218 _SysDebug("WM_RaiseWindow: Raised %p", Window);
219 Window = Window->Parent;
224 void WM_FocusWindow(tWindow *Destination)
226 struct sWndMsg_Bool _msg;
228 _SysDebug("WM_FocusWindow(%p)", Destination);
230 if( gpWM_FocusedWindow == Destination )
232 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
235 if( gpWM_FocusedWindow )
238 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
243 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
246 // TODO: Leave it up to the renderer to decide to invalidate
247 WM_Invalidate(gpWM_FocusedWindow, 1);
248 WM_Invalidate(Destination, 1);
250 gpWM_FocusedWindow = Destination;
254 void WM_ShowWindow(tWindow *Window, int bShow)
256 struct sWndMsg_Bool _msg;
258 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
261 // Window is being hidden, invalidate parents
263 WM_Invalidate(Window, 0);
267 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
271 Window->Flags |= WINFLAG_SHOW;
272 _SysDebug("Window %p shown", Window);
276 Window->Flags &= ~WINFLAG_SHOW;
278 if( Window == gpWM_FocusedWindow )
279 WM_FocusWindow(Window->Parent);
281 // Just a little memory saving for large hidden windows
282 if(Window->RenderBuffer) {
283 free(Window->RenderBuffer);
284 Window->RenderBuffer = NULL;
286 _SysDebug("Window %p hidden", Window);
289 // Window has been shown, invalidate everything
291 WM_Invalidate(Window, 1);
294 void WM_DecorateWindow(tWindow *Window, int bDecorate)
296 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
300 Window->Flags &= ~WINFLAG_NODECORATE;
302 Window->Flags |= WINFLAG_NODECORATE;
304 // Needed because the window size changes
305 if(Window->RenderBuffer) {
306 free(Window->RenderBuffer);
307 Window->RenderBuffer = NULL;
310 WM_Invalidate(Window, 1);
313 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
315 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
316 // No meaning if no parent
317 if( !Window->Parent )
320 // Check that the flag is changing
321 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
324 if( bRelativeToParent ) {
326 Window->Flags |= WINFLAG_RELATIVE;
327 WM_MoveWindow(Window, Window->X, Window->Y);
331 Window->Flags &= ~WINFLAG_RELATIVE;
332 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
336 int WM_MoveWindow(tWindow *Window, int X, int Y)
338 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
340 if(X + Window->W < 0) X = -Window->W + 1;
341 if(Y + Window->H < 0) Y = -Window->H + 1;
342 if(X >= giScreenWidth) X = giScreenWidth - 1;
343 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
345 // If relative to the parent, extra checks
346 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
348 if( X > Window->Parent->W ) return 1;
349 if( Y > Window->Parent->H ) return 1;
353 if( Window->X == X && Window->Y == Y ) {
354 _SysDebug("WM_MoveWindow: Equal (%i,%i)", X, Y);
359 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
361 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
362 Window->X = X; Window->Y = Y;
364 // Mark up the tree that a child window has changed
365 WM_Invalidate(Window, 0);
370 int WM_ResizeWindow(tWindow *Window, int W, int H)
372 if(W <= 0 || H <= 0 ) return 1;
373 if(Window->X + W < 0) Window->X = -W + 1;
374 if(Window->Y + H < 0) Window->Y = -H + 1;
376 if( Window->W == W && Window->H == H )
379 // If the window size has decreased, force the parent to reblit
380 if( Window->Parent && (Window->W > W || Window->H > H) )
381 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
383 _SysDebug("WM_ResizeWindow: %p:%i %ix%i", Window->Client, Window->ID, W, H);
384 Window->W = W; Window->H = H;
386 if(Window->RenderBuffer) {
387 free(Window->RenderBuffer);
388 Window->RenderBuffer = NULL;
390 WM_Invalidate(Window, 1);
393 struct sWndMsg_Resize msg;
396 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
402 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
404 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
406 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
409 if(Length > 0 && Data == NULL) {
410 _SysDebug("WM_SendMessage: non-zero length and NULL data");
414 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
416 // TODO: Catch errors from ->HandleMessage
417 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
421 // ->HandleMessage returns 1 when the message was not handled
422 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
424 // TODO: Catch errors from ->HandleMessage
425 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
429 // TODO: Implement message masking
431 // Dispatch to client
437 else if(Source->Client != Dest->Client) {
438 // TODO: Support different client source windows
439 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
446 // _SysDebug("WM_SendMessage: IPC Dispatch");
447 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
453 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
455 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
459 void WM_Invalidate(tWindow *Window, int bClearClean)
463 // Don't bother invalidating if the window isn't shown
464 if( !(Window->Flags & WINFLAG_SHOW) )
467 // Mark for re-render
469 Window->Flags &= ~WINFLAG_CLEAN;
471 // Mark up the tree that a child window has changed
472 while( (Window = Window->Parent) && (Window->Flags & WINFLAG_SHOW) )
473 Window->Flags &= ~WINFLAG_CHILDCLEAN;
476 // --- Rendering / Update
477 void WM_int_UpdateWindow(tWindow *Window)
479 int bDecoratorRedraw = 0;
481 // Ignore hidden windows
482 if( !(Window->Flags & WINFLAG_SHOW) )
485 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
487 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
488 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
492 Window->RealX = Window->X;
493 Window->RealY = Window->Y;
497 if( !(Window->Flags & WINFLAG_CLEAN) )
499 // Calculate RealW/RealH
500 if( !(Window->Flags & WINFLAG_NODECORATE) )
502 //_SysDebug("Applying decorations to %p", Window);
503 Decorator_UpdateBorderSize(Window);
504 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
505 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
506 bDecoratorRedraw = 1;
514 Window->RealW = Window->W;
515 Window->RealH = Window->H;
518 Window->Renderer->Redraw(Window);
519 Window->Flags |= WINFLAG_CLEAN|WINFLAG_NEEDREBLIT;
523 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
526 for( child = Window->FirstChild; child; child = child->NextSibling )
528 WM_int_UpdateWindow(child);
530 Window->Flags |= WINFLAG_CHILDCLEAN;
533 if( bDecoratorRedraw )
534 Decorator_Redraw(Window);
537 void WM_int_BlitWindow(tWindow *Window, int bForceReblit)
541 // Ignore hidden windows
542 if( !(Window->Flags & WINFLAG_SHOW) )
545 // Duplicated position update to handle window moving
546 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
548 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
549 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
553 Window->RealX = Window->X;
554 Window->RealY = Window->Y;
557 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
558 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
559 // TODO Don't blit unless:
560 // a) A parent has been reblitted (thus clobbering the existing content)
561 // b) A child has moved (exposing a previously hidden area)
562 if( bForceReblit || (Window->Flags & WINFLAG_NEEDREBLIT) )
564 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
565 Window->Flags &= ~WINFLAG_NEEDREBLIT;
569 if( Window == gpWM_FocusedWindow && Window->CursorW )
572 Window->RealX + Window->BorderL + Window->CursorX,
573 Window->RealY + Window->BorderT + Window->CursorY,
574 Window->CursorW, Window->CursorH,
579 for( child = Window->FirstChild; child; child = child->NextSibling )
581 WM_int_BlitWindow(child, bForceReblit);
587 // Don't redraw if nothing has changed
588 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
591 // - Iterate through visible windows, updating them as needed
592 WM_int_UpdateWindow(gpWM_RootWindow);
594 // - Draw windows from back to front to the render buffer
595 WM_int_BlitWindow(gpWM_RootWindow, 0);