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>
15 #include <axwin3/keysyms.h>
18 extern int Renderer_Menu_Init(void);
19 extern int Renderer_Widget_Init(void);
20 extern int Renderer_Background_Init(void);
21 extern int Renderer_Framebuffer_Init(void);
22 extern int Renderer_RichText_Init(void);
24 extern void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int Msg, int Len, const void *Data);
25 extern void IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data);
26 extern tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t ID);
29 tWMRenderer *gpWM_Renderers;
30 tWindow *gpWM_RootWindow;
31 //! Window which will recieve the next keyboard event
32 tWindow *gpWM_FocusedWindow;
33 //! Hilighted window (owner of the currently focused window)
34 tWindow *gpWM_HilightedWindow;
37 void WM_Initialise(void)
39 // TODO: Autodetect these
41 Renderer_Widget_Init();
42 Renderer_Background_Init();
43 Renderer_Framebuffer_Init();
44 Renderer_RichText_Init();
46 WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
47 gpWM_RootWindow->W = giScreenWidth;
48 gpWM_RootWindow->H = giScreenHeight;
49 gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
51 // TODO: Move these to config
53 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_r;
54 WM_Hotkey_Register(2, keys, "Interface>Run");
55 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_t;
56 WM_Hotkey_Register(2, keys, "Interface>Terminal");
57 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_e;
58 WM_Hotkey_Register(2, keys, "Interface>TextEdit");
61 void WM_RegisterRenderer(tWMRenderer *Renderer)
63 // Catch out duplicates
64 for(tWMRenderer *r = gpWM_Renderers; r; r = r->Next ) {
68 if( strcmp(r->Name, Renderer->Name) == 0 ) {
73 Renderer->Next = gpWM_Renderers;
74 gpWM_Renderers = Renderer;
78 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
80 tWMRenderer *renderer;
84 for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
86 if(strcmp(RendererName, renderer->Name) == 0)
92 // - Call create window function
93 ret = renderer->CreateWindow(RendererArg);
98 ret->Parent = gpWM_RootWindow;
100 ret->Renderer = renderer;
101 ret->Flags |= WINFLAG_CLEAN; // Needed to stop invaildate early exiting
106 if(ret->Parent->LastChild)
107 ret->Parent->LastChild->NextSibling = ret;
109 ret->Parent->FirstChild = ret;
110 ret->PrevSibling = ret->Parent->LastChild;
111 ret->Parent->LastChild = ret;
112 ret->NextSibling = NULL;
116 gpWM_RootWindow = ret;
119 // Don't decorate child windows by default
122 ret->Flags |= WINFLAG_NODECORATE;
129 void WM_DestroyWindow(tWindow *Window)
131 // TODO: Lock window and flag as invalid
133 // - Remove from render tree
135 // TODO: Lock render tree?
136 tWindow *prev = Window->PrevSibling;
137 tWindow *next = Window->NextSibling;
139 prev->NextSibling = next;
141 Window->Parent->FirstChild = next;
143 next->PrevSibling = prev;
145 Window->Parent->LastChild = prev;
148 WM_Invalidate(Window, 1);
150 // - Remove from inheritance tree?
152 // - Clean up render children
154 // Lock should not be needed
156 for( win = Window->FirstChild; win; win = next )
158 next = win->NextSibling;
159 ASSERT(Window->FirstChild->Parent == Window);
160 WM_DestroyWindow(win);
164 // - Clean up inheriting children?
166 // - Tell renderer to clean up
167 if( Window->Renderer->DestroyWindow )
168 Window->Renderer->DestroyWindow(Window);
170 _SysDebug("WARN: Renderer '%s' does not have a destroy function", Window->Renderer->Name);
172 // - Tell client to clean up
173 WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
175 // - Clean up render cache and window structure
177 free(Window->RenderBuffer);
181 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
183 return IPC_int_GetWindow(Requester->Client, ID);
186 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
190 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
191 ret->RendererInfo = ret + 1; // Get end of tWindow
196 void WM_SetWindowTitle(tWindow *Window, const char *Title)
200 Window->Title = strdup(Title);
201 _SysDebug("Window %p title set to '%s'", Window, Title);
204 void WM_RaiseWindow(tWindow *Window)
206 tWindow *parent = Window->Parent;
207 if(!Window->Parent) return ;
210 if(Window->PrevSibling)
211 Window->PrevSibling->NextSibling = Window->NextSibling;
212 if(Window->NextSibling)
213 Window->NextSibling->PrevSibling = Window->PrevSibling;
214 if(parent->FirstChild == Window)
215 parent->FirstChild = Window->NextSibling;
216 if(parent->LastChild == Window)
217 parent->LastChild = Window->PrevSibling;
220 if(parent->LastChild)
221 parent->LastChild->NextSibling = Window;
223 parent->FirstChild = Window;
224 Window->PrevSibling = parent->LastChild;
225 Window->NextSibling = NULL;
226 parent->LastChild = Window;
228 _SysDebug("Raised %p", Window);
232 void WM_RaiseWindow(tWindow *Window)
234 // Move to the last render position (move to top)
235 while(Window && Window->Parent)
237 if( Window->NextSibling )
240 if( Window->PrevSibling )
241 Window->PrevSibling->NextSibling = Window->NextSibling;
242 Window->NextSibling->PrevSibling = Window->PrevSibling;
244 Window->PrevSibling = Window->Parent->LastChild;
245 Window->NextSibling = NULL;
247 Window->PrevSibling->NextSibling = Window;
248 Window->Parent->LastChild = Window;
250 _SysDebug("WM_RaiseWindow: Raised %p", Window);
251 Window = Window->Parent;
256 void WM_FocusWindow(tWindow *Destination)
258 struct sWndMsg_Bool _msg;
260 _SysDebug("WM_FocusWindow(%p)", Destination);
262 if( gpWM_FocusedWindow == Destination )
264 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
267 if( gpWM_FocusedWindow )
270 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
275 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
278 // TODO: Leave it up to the renderer to decide to invalidate
279 WM_Invalidate(gpWM_FocusedWindow, 1);
280 WM_Invalidate(Destination, 1);
282 gpWM_FocusedWindow = Destination;
286 void WM_ShowWindow(tWindow *Window, int bShow)
288 struct sWndMsg_Bool _msg;
290 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
293 // Window is being hidden, invalidate parents
295 WM_Invalidate(Window, 0);
299 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
303 Window->Flags |= WINFLAG_SHOW;
304 _SysDebug("Window %p shown", Window);
308 Window->Flags &= ~WINFLAG_SHOW;
310 if( Window == gpWM_FocusedWindow )
311 WM_FocusWindow(Window->Parent);
313 // Just a little memory saving for large hidden windows
314 if(Window->RenderBuffer) {
315 free(Window->RenderBuffer);
316 Window->RenderBuffer = NULL;
318 _SysDebug("Window %p hidden", Window);
321 // Window has been shown, invalidate everything
323 WM_Invalidate(Window, 1);
326 void WM_DecorateWindow(tWindow *Window, int bDecorate)
328 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
332 Window->Flags &= ~WINFLAG_NODECORATE;
334 Window->Flags |= WINFLAG_NODECORATE;
336 // Needed because the window size changes
337 if(Window->RenderBuffer) {
338 free(Window->RenderBuffer);
339 Window->RenderBuffer = NULL;
342 WM_Invalidate(Window, 1);
345 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
347 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
348 // No meaning if no parent
349 if( !Window->Parent )
352 // Check that the flag is changing
353 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
356 if( bRelativeToParent ) {
358 Window->Flags |= WINFLAG_RELATIVE;
359 WM_MoveWindow(Window, Window->X, Window->Y);
363 Window->Flags &= ~WINFLAG_RELATIVE;
364 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
368 int WM_MoveWindow(tWindow *Window, int X, int Y)
370 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
372 if(X + Window->W < 0) X = -Window->W + 1;
373 if(Y + Window->H < 0) Y = -Window->H + 1;
374 if(X >= giScreenWidth) X = giScreenWidth - 1;
375 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
377 // If relative to the parent, extra checks
378 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
380 if( X > Window->Parent->W ) return 1;
381 if( Y > Window->Parent->H ) return 1;
385 if( Window->X == X && Window->Y == Y ) {
386 _SysDebug("WM_MoveWindow: Equal (%i,%i)", X, Y);
391 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
393 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
394 Window->X = X; Window->Y = Y;
396 // Mark up the tree that a child window has changed
397 WM_Invalidate(Window, 0);
402 int WM_ResizeWindow(tWindow *Window, int W, int H)
404 if(W <= 0 || H <= 0 ) return 1;
405 if(Window->X + W < 0) Window->X = -W + 1;
406 if(Window->Y + H < 0) Window->Y = -H + 1;
408 if( Window->W == W && Window->H == H )
411 // If the window size has decreased, force the parent to reblit
412 if( Window->Parent && (Window->W > W || Window->H > H) )
413 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
415 _SysDebug("WM_ResizeWindow: %p:%i %ix%i", Window->Client, Window->ID, W, H);
416 Window->W = W; Window->H = H;
418 if(Window->RenderBuffer) {
419 free(Window->RenderBuffer);
420 Window->RenderBuffer = NULL;
422 WM_Invalidate(Window, 1);
425 struct sWndMsg_Resize msg;
428 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
434 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
436 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
438 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
441 if(Length > 0 && Data == NULL) {
442 _SysDebug("WM_SendMessage: non-zero length and NULL data");
446 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
448 // TODO: Catch errors from ->HandleMessage
449 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
453 // ->HandleMessage returns 1 when the message was not handled
454 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
456 // TODO: Catch errors from ->HandleMessage
457 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
461 // TODO: Implement message masking
463 // Dispatch to client
469 else if(Source->Client != Dest->Client) {
470 // TODO: Support different client source windows
471 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
478 // _SysDebug("WM_SendMessage: IPC Dispatch");
479 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
485 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
487 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
491 void WM_Invalidate(tWindow *Window, int bClearClean)
495 // Don't bother invalidating if the window isn't shown
496 if( !(Window->Flags & WINFLAG_SHOW) )
499 // Mark for re-render
501 Window->Flags &= ~WINFLAG_CLEAN;
503 // Mark up the tree that a child window has changed
504 while( (Window = Window->Parent) && (Window->Flags & WINFLAG_SHOW) )
505 Window->Flags &= ~WINFLAG_CHILDCLEAN;
508 // --- Rendering / Update
509 void WM_int_UpdateWindow(tWindow *Window)
511 int bDecoratorRedraw = 0;
513 // Ignore hidden windows
514 if( !(Window->Flags & WINFLAG_SHOW) )
517 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
519 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
520 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
524 Window->RealX = Window->X;
525 Window->RealY = Window->Y;
529 if( !(Window->Flags & WINFLAG_CLEAN) )
531 // Calculate RealW/RealH
532 if( !(Window->Flags & WINFLAG_NODECORATE) )
534 //_SysDebug("Applying decorations to %p", Window);
535 Decorator_UpdateBorderSize(Window);
536 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
537 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
538 bDecoratorRedraw = 1;
546 Window->RealW = Window->W;
547 Window->RealH = Window->H;
550 Window->Renderer->Redraw(Window);
551 Window->Flags |= WINFLAG_CLEAN|WINFLAG_NEEDREBLIT;
555 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
558 for( child = Window->FirstChild; child; child = child->NextSibling )
560 WM_int_UpdateWindow(child);
562 Window->Flags |= WINFLAG_CHILDCLEAN;
565 if( bDecoratorRedraw )
566 Decorator_Redraw(Window);
569 void WM_int_BlitWindow(tWindow *Window, int bForceReblit)
573 // Ignore hidden windows
574 if( !(Window->Flags & WINFLAG_SHOW) )
577 // Duplicated position update to handle window moving
578 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
580 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
581 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
585 Window->RealX = Window->X;
586 Window->RealY = Window->Y;
589 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
590 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
591 // TODO Don't blit unless:
592 // a) A parent has been reblitted (thus clobbering the existing content)
593 // b) A child has moved (exposing a previously hidden area)
594 if( bForceReblit || (Window->Flags & WINFLAG_NEEDREBLIT) )
596 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
597 Window->Flags &= ~WINFLAG_NEEDREBLIT;
601 if( Window == gpWM_FocusedWindow && Window->CursorW )
604 Window->RealX + Window->BorderL + Window->CursorX,
605 Window->RealY + Window->BorderT + Window->CursorY,
606 Window->CursorW, Window->CursorH,
611 for( child = Window->FirstChild; child; child = child->NextSibling )
613 WM_int_BlitWindow(child, bForceReblit);
619 // Don't redraw if nothing has changed
620 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
623 // - Iterate through visible windows, updating them as needed
624 WM_int_UpdateWindow(gpWM_RootWindow);
626 // - Draw windows from back to front to the render buffer
627 WM_int_BlitWindow(gpWM_RootWindow, 0);