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 // TODO: Catch out duplicates
64 Renderer->Next = gpWM_Renderers;
65 gpWM_Renderers = Renderer;
69 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
71 tWMRenderer *renderer;
75 for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
77 if(strcmp(RendererName, renderer->Name) == 0)
83 // - Call create window function
84 ret = renderer->CreateWindow(RendererArg);
89 ret->Parent = gpWM_RootWindow;
91 ret->Renderer = renderer;
92 ret->Flags |= WINFLAG_CLEAN; // Needed to stop invaildate early exiting
97 if(ret->Parent->LastChild)
98 ret->Parent->LastChild->NextSibling = ret;
100 ret->Parent->FirstChild = ret;
101 ret->PrevSibling = ret->Parent->LastChild;
102 ret->Parent->LastChild = ret;
103 ret->NextSibling = NULL;
107 gpWM_RootWindow = ret;
110 // Don't decorate child windows by default
113 ret->Flags |= WINFLAG_NODECORATE;
120 void WM_DestroyWindow(tWindow *Window)
122 // TODO: Lock window and flag as invalid
124 // - Remove from render tree
126 // TODO: Lock render tree?
127 tWindow *prev = Window->PrevSibling;
128 tWindow *next = Window->NextSibling;
130 prev->NextSibling = next;
132 Window->Parent->FirstChild = next;
134 next->PrevSibling = prev;
136 Window->Parent->LastChild = prev;
139 WM_Invalidate(Window, 1);
141 // - Remove from inheritance tree?
143 // - Clean up render children
145 // Lock should not be needed
147 for( win = Window->FirstChild; win; win = next )
149 next = win->NextSibling;
150 ASSERT(Window->FirstChild->Parent == Window);
151 WM_DestroyWindow(win);
155 // - Clean up inheriting children?
157 // - Tell renderer to clean up
158 if( Window->Renderer->DestroyWindow )
159 Window->Renderer->DestroyWindow(Window);
161 _SysDebug("WARN: Renderer '%s' does not have a destroy function", Window->Renderer->Name);
163 // - Tell client to clean up
164 WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
166 // - Clean up render cache and window structure
168 free(Window->RenderBuffer);
172 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
174 return IPC_int_GetWindow(Requester->Client, ID);
177 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
181 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
182 ret->RendererInfo = ret + 1; // Get end of tWindow
187 void WM_SetWindowTitle(tWindow *Window, const char *Title)
191 Window->Title = strdup(Title);
192 _SysDebug("Window %p title set to '%s'", Window, Title);
195 void WM_RaiseWindow(tWindow *Window)
197 tWindow *parent = Window->Parent;
198 if(!Window->Parent) return ;
201 if(Window->PrevSibling)
202 Window->PrevSibling->NextSibling = Window->NextSibling;
203 if(Window->NextSibling)
204 Window->NextSibling->PrevSibling = Window->PrevSibling;
205 if(parent->FirstChild == Window)
206 parent->FirstChild = Window->NextSibling;
207 if(parent->LastChild == Window)
208 parent->LastChild = Window->PrevSibling;
211 if(parent->LastChild)
212 parent->LastChild->NextSibling = Window;
214 parent->FirstChild = Window;
215 Window->PrevSibling = parent->LastChild;
216 Window->NextSibling = NULL;
217 parent->LastChild = Window;
219 _SysDebug("Raised %p", Window);
223 void WM_RaiseWindow(tWindow *Window)
225 // Move to the last render position (move to top)
226 while(Window && Window->Parent)
228 if( Window->NextSibling )
231 if( Window->PrevSibling )
232 Window->PrevSibling->NextSibling = Window->NextSibling;
233 Window->NextSibling->PrevSibling = Window->PrevSibling;
235 Window->PrevSibling = Window->Parent->LastChild;
236 Window->NextSibling = NULL;
238 Window->PrevSibling->NextSibling = Window;
239 Window->Parent->LastChild = Window;
241 _SysDebug("WM_RaiseWindow: Raised %p", Window);
242 Window = Window->Parent;
247 void WM_FocusWindow(tWindow *Destination)
249 struct sWndMsg_Bool _msg;
251 _SysDebug("WM_FocusWindow(%p)", Destination);
253 if( gpWM_FocusedWindow == Destination )
255 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
258 if( gpWM_FocusedWindow )
261 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
266 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
269 // TODO: Leave it up to the renderer to decide to invalidate
270 WM_Invalidate(gpWM_FocusedWindow, 1);
271 WM_Invalidate(Destination, 1);
273 gpWM_FocusedWindow = Destination;
277 void WM_ShowWindow(tWindow *Window, int bShow)
279 struct sWndMsg_Bool _msg;
281 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
284 // Window is being hidden, invalidate parents
286 WM_Invalidate(Window, 0);
290 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
294 Window->Flags |= WINFLAG_SHOW;
295 _SysDebug("Window %p shown", Window);
299 Window->Flags &= ~WINFLAG_SHOW;
301 if( Window == gpWM_FocusedWindow )
302 WM_FocusWindow(Window->Parent);
304 // Just a little memory saving for large hidden windows
305 if(Window->RenderBuffer) {
306 free(Window->RenderBuffer);
307 Window->RenderBuffer = NULL;
309 _SysDebug("Window %p hidden", Window);
312 // Window has been shown, invalidate everything
314 WM_Invalidate(Window, 1);
317 void WM_DecorateWindow(tWindow *Window, int bDecorate)
319 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
323 Window->Flags &= ~WINFLAG_NODECORATE;
325 Window->Flags |= WINFLAG_NODECORATE;
327 // Needed because the window size changes
328 if(Window->RenderBuffer) {
329 free(Window->RenderBuffer);
330 Window->RenderBuffer = NULL;
333 WM_Invalidate(Window, 1);
336 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
338 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
339 // No meaning if no parent
340 if( !Window->Parent )
343 // Check that the flag is changing
344 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
347 if( bRelativeToParent ) {
349 Window->Flags |= WINFLAG_RELATIVE;
350 WM_MoveWindow(Window, Window->X, Window->Y);
354 Window->Flags &= ~WINFLAG_RELATIVE;
355 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
359 int WM_MoveWindow(tWindow *Window, int X, int Y)
361 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
363 if(X + Window->W < 0) X = -Window->W + 1;
364 if(Y + Window->H < 0) Y = -Window->H + 1;
365 if(X >= giScreenWidth) X = giScreenWidth - 1;
366 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
368 // If relative to the parent, extra checks
369 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
371 if( X > Window->Parent->W ) return 1;
372 if( Y > Window->Parent->H ) return 1;
376 if( Window->X == X && Window->Y == Y ) {
377 _SysDebug("WM_MoveWindow: Equal (%i,%i)", X, Y);
382 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
384 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
385 Window->X = X; Window->Y = Y;
387 // Mark up the tree that a child window has changed
388 WM_Invalidate(Window, 0);
393 int WM_ResizeWindow(tWindow *Window, int W, int H)
395 if(W <= 0 || H <= 0 ) return 1;
396 if(Window->X + W < 0) Window->X = -W + 1;
397 if(Window->Y + H < 0) Window->Y = -H + 1;
399 if( Window->W == W && Window->H == H )
402 // If the window size has decreased, force the parent to reblit
403 if( Window->Parent && (Window->W > W || Window->H > H) )
404 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
406 _SysDebug("WM_ResizeWindow: %p:%i %ix%i", Window->Client, Window->ID, W, H);
407 Window->W = W; Window->H = H;
409 if(Window->RenderBuffer) {
410 free(Window->RenderBuffer);
411 Window->RenderBuffer = NULL;
413 WM_Invalidate(Window, 1);
416 struct sWndMsg_Resize msg;
419 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
425 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
427 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
429 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
432 if(Length > 0 && Data == NULL) {
433 _SysDebug("WM_SendMessage: non-zero length and NULL data");
437 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
439 // TODO: Catch errors from ->HandleMessage
440 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
444 // ->HandleMessage returns 1 when the message was not handled
445 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
447 // TODO: Catch errors from ->HandleMessage
448 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
452 // TODO: Implement message masking
454 // Dispatch to client
460 else if(Source->Client != Dest->Client) {
461 // TODO: Support different client source windows
462 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
469 // _SysDebug("WM_SendMessage: IPC Dispatch");
470 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
476 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
478 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
482 void WM_Invalidate(tWindow *Window, int bClearClean)
486 // Don't bother invalidating if the window isn't shown
487 if( !(Window->Flags & WINFLAG_SHOW) )
490 // Mark for re-render
492 Window->Flags &= ~WINFLAG_CLEAN;
494 // Mark up the tree that a child window has changed
495 while( (Window = Window->Parent) && (Window->Flags & WINFLAG_SHOW) )
496 Window->Flags &= ~WINFLAG_CHILDCLEAN;
499 // --- Rendering / Update
500 void WM_int_UpdateWindow(tWindow *Window)
502 int bDecoratorRedraw = 0;
504 // Ignore hidden windows
505 if( !(Window->Flags & WINFLAG_SHOW) )
508 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
510 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
511 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
515 Window->RealX = Window->X;
516 Window->RealY = Window->Y;
520 if( !(Window->Flags & WINFLAG_CLEAN) )
522 // Calculate RealW/RealH
523 if( !(Window->Flags & WINFLAG_NODECORATE) )
525 //_SysDebug("Applying decorations to %p", Window);
526 Decorator_UpdateBorderSize(Window);
527 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
528 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
529 bDecoratorRedraw = 1;
537 Window->RealW = Window->W;
538 Window->RealH = Window->H;
541 Window->Renderer->Redraw(Window);
542 Window->Flags |= WINFLAG_CLEAN|WINFLAG_NEEDREBLIT;
546 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
549 for( child = Window->FirstChild; child; child = child->NextSibling )
551 WM_int_UpdateWindow(child);
553 Window->Flags |= WINFLAG_CHILDCLEAN;
556 if( bDecoratorRedraw )
557 Decorator_Redraw(Window);
560 void WM_int_BlitWindow(tWindow *Window, int bForceReblit)
564 // Ignore hidden windows
565 if( !(Window->Flags & WINFLAG_SHOW) )
568 // Duplicated position update to handle window moving
569 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
571 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
572 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
576 Window->RealX = Window->X;
577 Window->RealY = Window->Y;
580 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
581 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
582 // TODO Don't blit unless:
583 // a) A parent has been reblitted (thus clobbering the existing content)
584 // b) A child has moved (exposing a previously hidden area)
585 if( bForceReblit || (Window->Flags & WINFLAG_NEEDREBLIT) )
587 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
588 Window->Flags &= ~WINFLAG_NEEDREBLIT;
592 if( Window == gpWM_FocusedWindow && Window->CursorW )
595 Window->RealX + Window->BorderL + Window->CursorX,
596 Window->RealY + Window->BorderT + Window->CursorY,
597 Window->CursorW, Window->CursorH,
602 for( child = Window->FirstChild; child; child = child->NextSibling )
604 WM_int_BlitWindow(child, bForceReblit);
610 // Don't redraw if nothing has changed
611 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
614 // - Iterate through visible windows, updating them as needed
615 WM_int_UpdateWindow(gpWM_RootWindow);
617 // - Draw windows from back to front to the render buffer
618 WM_int_BlitWindow(gpWM_RootWindow, 0);