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);
27 extern void IPC_int_SetWindow(tIPC_Client *Client, uint32_t ID, tWindow *Window);
30 tWMRenderer *gpWM_Renderers;
31 tWindow *gpWM_RootWindow;
32 //! Window which will recieve the next keyboard event
33 tWindow *gpWM_FocusedWindow;
34 //! Hilighted window (owner of the currently focused window)
35 tWindow *gpWM_HilightedWindow;
38 void WM_Initialise(void)
40 // TODO: Autodetect these
42 Renderer_Widget_Init();
43 Renderer_Background_Init();
44 Renderer_Framebuffer_Init();
45 Renderer_RichText_Init();
47 WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
48 gpWM_RootWindow->W = giScreenWidth;
49 gpWM_RootWindow->H = giScreenHeight;
50 gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
52 // TODO: Move these to config
54 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_r;
55 WM_Hotkey_Register(2, keys, "Interface>Run");
56 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_t;
57 WM_Hotkey_Register(2, keys, "Interface>Terminal");
58 keys[0] = KEYSYM_LEFTGUI; keys[1] = KEYSYM_e;
59 WM_Hotkey_Register(2, keys, "Interface>TextEdit");
62 void WM_RegisterRenderer(tWMRenderer *Renderer)
64 // Catch out duplicates
65 for(tWMRenderer *r = gpWM_Renderers; r; r = r->Next ) {
69 if( strcmp(r->Name, Renderer->Name) == 0 ) {
74 Renderer->Next = gpWM_Renderers;
75 gpWM_Renderers = Renderer;
79 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
81 tWMRenderer *renderer;
85 for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
87 if(strcmp(RendererName, renderer->Name) == 0)
93 // - Call create window function
94 ret = renderer->CreateWindow(RendererArg);
99 ret->Parent = gpWM_RootWindow;
101 ret->Renderer = renderer;
102 ret->Flags |= WINFLAG_CLEAN; // Needed to stop invaildate early exiting
107 if(ret->Parent->LastChild)
108 ret->Parent->LastChild->NextSibling = ret;
110 ret->Parent->FirstChild = ret;
111 ret->PrevSibling = ret->Parent->LastChild;
112 ret->Parent->LastChild = ret;
113 ret->NextSibling = NULL;
117 gpWM_RootWindow = ret;
120 // Don't decorate child windows by default
123 ret->Flags |= WINFLAG_NODECORATE;
130 void WM_DestroyWindow(tWindow *Window)
132 // TODO: Lock window and flag as invalid
134 // - Remove from render tree
136 // TODO: Lock render tree?
137 tWindow *prev = Window->PrevSibling;
138 tWindow *next = Window->NextSibling;
140 prev->NextSibling = next;
142 Window->Parent->FirstChild = next;
144 next->PrevSibling = prev;
146 Window->Parent->LastChild = prev;
149 WM_Invalidate(Window, 0);
150 Window->Parent->Flags &= ~WINFLAG_CLEAN; // Mark parent as unclean, forcing redraw
152 // - Remove from inheritance tree?
154 // - Clean up render children
156 // Lock should not be needed
158 for( win = Window->FirstChild; win; win = next )
160 next = win->NextSibling;
161 ASSERT(Window->FirstChild->Parent == Window);
162 WM_DestroyWindow(win);
166 // - Clean up inheriting children?
168 // - Tell renderer to clean up
169 if( Window->Renderer->DestroyWindow )
170 Window->Renderer->DestroyWindow(Window);
172 _SysDebug("WARN: Renderer '%s' does not have a destroy function", Window->Renderer->Name);
174 // - Tell client to clean up
175 WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
177 // - Clean up render cache and window structure
179 free(Window->RenderBuffer);
181 IPC_int_SetWindow(Window->Client, Window->ID, NULL);
184 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
186 return IPC_int_GetWindow(Requester->Client, ID);
189 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
193 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
194 ret->RendererInfo = ret + 1; // Get end of tWindow
199 void WM_SetWindowTitle(tWindow *Window, const char *Title)
203 Window->Title = strdup(Title);
204 _SysDebug("Window %p title set to '%s'", Window, Title);
207 void WM_RaiseWindow(tWindow *Window)
209 tWindow *parent = Window->Parent;
210 if(!Window->Parent) return ;
213 if(Window->PrevSibling)
214 Window->PrevSibling->NextSibling = Window->NextSibling;
215 if(Window->NextSibling)
216 Window->NextSibling->PrevSibling = Window->PrevSibling;
217 if(parent->FirstChild == Window)
218 parent->FirstChild = Window->NextSibling;
219 if(parent->LastChild == Window)
220 parent->LastChild = Window->PrevSibling;
223 if(parent->LastChild)
224 parent->LastChild->NextSibling = Window;
226 parent->FirstChild = Window;
227 Window->PrevSibling = parent->LastChild;
228 Window->NextSibling = NULL;
229 parent->LastChild = Window;
231 _SysDebug("Raised %p", Window);
235 void WM_RaiseWindow(tWindow *Window)
237 // Move to the last render position (move to top)
238 while(Window && Window->Parent)
240 if( Window->NextSibling )
243 if( Window->PrevSibling )
244 Window->PrevSibling->NextSibling = Window->NextSibling;
245 Window->NextSibling->PrevSibling = Window->PrevSibling;
247 Window->PrevSibling = Window->Parent->LastChild;
248 Window->NextSibling = NULL;
250 Window->PrevSibling->NextSibling = Window;
251 Window->Parent->LastChild = Window;
253 _SysDebug("WM_RaiseWindow: Raised %p", Window);
254 Window = Window->Parent;
259 void WM_FocusWindow(tWindow *Destination)
261 struct sWndMsg_Bool _msg;
263 _SysDebug("WM_FocusWindow(%p)", Destination);
265 if( gpWM_FocusedWindow == Destination )
267 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
270 if( gpWM_FocusedWindow )
273 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
278 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
281 // TODO: Leave it up to the renderer to decide to invalidate
282 WM_Invalidate(gpWM_FocusedWindow, 1);
283 WM_Invalidate(Destination, 1);
285 gpWM_FocusedWindow = Destination;
289 void WM_ShowWindow(tWindow *Window, int bShow)
291 struct sWndMsg_Bool _msg;
293 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
296 // Window is being hidden, invalidate parents
298 WM_Invalidate(Window, 0);
302 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
306 Window->Flags |= WINFLAG_SHOW;
307 _SysDebug("Window %p shown", Window);
311 Window->Flags &= ~WINFLAG_SHOW;
313 if( Window == gpWM_FocusedWindow )
314 WM_FocusWindow(Window->Parent);
316 // Just a little memory saving for large hidden windows
317 if(Window->RenderBuffer) {
318 free(Window->RenderBuffer);
319 Window->RenderBuffer = NULL;
321 _SysDebug("Window %p hidden", Window);
324 // Window has been shown, invalidate everything
326 WM_Invalidate(Window, 1);
329 void WM_DecorateWindow(tWindow *Window, int bDecorate)
331 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
335 Window->Flags &= ~WINFLAG_NODECORATE;
337 Window->Flags |= WINFLAG_NODECORATE;
339 // Needed because the window size changes
340 if(Window->RenderBuffer) {
341 free(Window->RenderBuffer);
342 Window->RenderBuffer = NULL;
345 WM_Invalidate(Window, 1);
348 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
350 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
351 // No meaning if no parent
352 if( !Window->Parent )
355 // Check that the flag is changing
356 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
359 if( bRelativeToParent ) {
361 Window->Flags |= WINFLAG_RELATIVE;
362 WM_MoveWindow(Window, Window->X, Window->Y);
366 Window->Flags &= ~WINFLAG_RELATIVE;
367 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
371 int WM_MoveWindow(tWindow *Window, int X, int Y)
373 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
375 if(X + Window->W < 0) X = -Window->W + 1;
376 if(Y + Window->H < 0) Y = -Window->H + 1;
377 if(X >= giScreenWidth) X = giScreenWidth - 1;
378 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
380 // If relative to the parent, extra checks
381 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
383 if( X > Window->Parent->W ) return 1;
384 if( Y > Window->Parent->H ) return 1;
388 if( Window->X == X && Window->Y == Y ) {
389 _SysDebug("WM_MoveWindow: Equal (%i,%i)", X, Y);
394 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
396 _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
397 Window->X = X; Window->Y = Y;
399 // Mark up the tree that a child window has changed
400 WM_Invalidate(Window, 0);
405 int WM_ResizeWindow(tWindow *Window, int W, int H)
407 if(W <= 0 || H <= 0 ) return 1;
408 if(Window->X + W < 0) Window->X = -W + 1;
409 if(Window->Y + H < 0) Window->Y = -H + 1;
411 if( Window->W == W && Window->H == H )
414 // If the window size has decreased, force the parent to reblit
415 if( Window->Parent && (Window->W > W || Window->H > H) )
416 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
418 _SysDebug("WM_ResizeWindow: %p:%i %ix%i", Window->Client, Window->ID, W, H);
419 Window->W = W; Window->H = H;
421 if(Window->RenderBuffer) {
422 free(Window->RenderBuffer);
423 Window->RenderBuffer = NULL;
425 WM_Invalidate(Window, 1);
428 struct sWndMsg_Resize msg;
431 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
437 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
439 // _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
441 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
444 if(Length > 0 && Data == NULL) {
445 _SysDebug("WM_SendMessage: non-zero length and NULL data");
449 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
451 // TODO: Catch errors from ->HandleMessage
452 // _SysDebug("WM_SendMessage: Decorator grabbed message?");
456 // ->HandleMessage returns 1 when the message was not handled
457 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
459 // TODO: Catch errors from ->HandleMessage
460 // _SysDebug("WM_SendMessage: Renderer grabbed message?");
464 // TODO: Implement message masking
466 // Dispatch to client
472 else if(Source->Client != Dest->Client) {
473 // TODO: Support different client source windows
474 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
481 // _SysDebug("WM_SendMessage: IPC Dispatch");
482 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
488 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
490 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
494 void WM_Invalidate(tWindow *Window, int bClearClean)
498 // Don't bother invalidating if the window isn't shown
499 if( !(Window->Flags & WINFLAG_SHOW) )
502 // Mark for re-render
504 Window->Flags &= ~WINFLAG_CLEAN;
506 // Mark up the tree that a child window has changed
507 while( (Window = Window->Parent) && (Window->Flags & WINFLAG_SHOW) )
508 Window->Flags &= ~WINFLAG_CHILDCLEAN;
511 // --- Rendering / Update
512 void WM_int_UpdateWindow(tWindow *Window)
514 int bDecoratorRedraw = 0;
516 // Ignore hidden windows
517 if( !(Window->Flags & WINFLAG_SHOW) )
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;
532 if( !(Window->Flags & WINFLAG_CLEAN) )
534 // Calculate RealW/RealH
535 if( !(Window->Flags & WINFLAG_NODECORATE) )
537 //_SysDebug("Applying decorations to %p", Window);
538 Decorator_UpdateBorderSize(Window);
539 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
540 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
541 bDecoratorRedraw = 1;
549 Window->RealW = Window->W;
550 Window->RealH = Window->H;
553 Window->Renderer->Redraw(Window);
554 Window->Flags |= WINFLAG_CLEAN|WINFLAG_NEEDREBLIT;
558 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
561 for( child = Window->FirstChild; child; child = child->NextSibling )
563 WM_int_UpdateWindow(child);
565 Window->Flags |= WINFLAG_CHILDCLEAN;
568 if( bDecoratorRedraw )
569 Decorator_Redraw(Window);
572 void WM_int_BlitWindow(tWindow *Window, int bForceReblit)
576 // Ignore hidden windows
577 if( !(Window->Flags & WINFLAG_SHOW) )
580 // Duplicated position update to handle window moving
581 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
583 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
584 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
588 Window->RealX = Window->X;
589 Window->RealY = Window->Y;
592 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
593 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
594 // TODO Don't blit unless:
595 // a) A parent has been reblitted (thus clobbering the existing content)
596 // b) A child has moved (exposing a previously hidden area)
597 if( bForceReblit || (Window->Flags & WINFLAG_NEEDREBLIT) )
599 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
600 Window->Flags &= ~WINFLAG_NEEDREBLIT;
604 if( Window == gpWM_FocusedWindow && Window->CursorW )
607 Window->RealX + Window->BorderL + Window->CursorX,
608 Window->RealY + Window->BorderT + Window->CursorY,
609 Window->CursorW, Window->CursorH,
614 for( child = Window->FirstChild; child; child = child->NextSibling )
616 WM_int_BlitWindow(child, bForceReblit);
622 // Don't redraw if nothing has changed
623 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
626 // - Iterate through visible windows, updating them as needed
627 WM_int_UpdateWindow(gpWM_RootWindow);
629 // - Draw windows from back to front to the render buffer
630 WM_int_BlitWindow(gpWM_RootWindow, 0);