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 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
99 return IPC_int_GetWindow(Requester->Client, ID);
102 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
106 ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
107 ret->RendererInfo = ret + 1; // Get end of tWindow
112 void WM_SetWindowTitle(tWindow *Window, const char *Title)
116 Window->Title = strdup(Title);
117 _SysDebug("Window %p title set to '%s'", Window, Title);
120 void WM_RaiseWindow(tWindow *Window)
122 tWindow *parent = Window->Parent;
123 if(!Window->Parent) return ;
126 if(Window->PrevSibling)
127 Window->PrevSibling->NextSibling = Window->NextSibling;
128 if(Window->NextSibling)
129 Window->NextSibling->PrevSibling = Window->PrevSibling;
130 if(parent->FirstChild == Window)
131 parent->FirstChild = Window->NextSibling;
132 if(parent->LastChild == Window)
133 parent->LastChild = Window->PrevSibling;
136 if(parent->LastChild)
137 parent->LastChild->NextSibling = Window;
139 parent->FirstChild = Window;
140 Window->PrevSibling = parent->LastChild;
141 Window->NextSibling = NULL;
142 parent->LastChild = Window;
145 void WM_FocusWindow(tWindow *Destination)
147 struct sWndMsg_Bool _msg;
149 if( gpWM_FocusedWindow == Destination )
151 if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
155 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
157 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
159 WM_Invalidate(gpWM_FocusedWindow);
160 WM_Invalidate(Destination);
161 gpWM_FocusedWindow = Destination;
164 // Get the owner of the focused window
165 // while(Destination && Destination->Owner) Destination = Destination->Owner;
166 // gpWM_HilightedWindow = Destination;
170 void WM_ShowWindow(tWindow *Window, int bShow)
172 struct sWndMsg_Bool _msg;
174 if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
179 WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
183 Window->Flags |= WINFLAG_SHOW;
186 Window->Flags &= ~WINFLAG_SHOW;
188 if( Window == gpWM_FocusedWindow )
189 WM_FocusWindow(Window->Parent);
191 // Just a little memory saving for large hidden windows
192 if(Window->RenderBuffer) {
193 free(Window->RenderBuffer);
194 Window->RenderBuffer = NULL;
198 WM_Invalidate(Window);
201 void WM_DecorateWindow(tWindow *Window, int bDecorate)
203 if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
207 Window->Flags &= ~WINFLAG_NODECORATE;
209 Window->Flags |= WINFLAG_NODECORATE;
211 // Needed because the window size changes
212 if(Window->RenderBuffer) {
213 free(Window->RenderBuffer);
214 Window->RenderBuffer = NULL;
217 WM_Invalidate(Window);
220 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
222 // _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
223 // No meaning if no parent
224 if( !Window->Parent )
227 // Check that the flag is changing
228 if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
231 if( bRelativeToParent ) {
233 Window->Flags |= WINFLAG_RELATIVE;
234 WM_MoveWindow(Window, Window->X, Window->Y);
238 Window->Flags &= ~WINFLAG_RELATIVE;
239 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
243 int WM_MoveWindow(tWindow *Window, int X, int Y)
245 // _SysDebug("Move %p to (%i,%i)", Window, X, Y);
247 if(X + Window->W < 0) X = -Window->W + 1;
248 if(Y + Window->H < 0) Y = -Window->H + 1;
249 if(X >= giScreenWidth) X = giScreenWidth - 1;
250 if(Y >= giScreenHeight) Y = giScreenHeight - 1;
252 // If relative to the parent, extra checks
253 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
255 if( X > Window->Parent->W ) return 1;
256 if( Y > Window->Parent->H ) return 1;
260 Window->X = X; Window->Y = Y;
262 // TODO: Why invalidate buffer?
263 WM_Invalidate(Window);
268 int WM_ResizeWindow(tWindow *Window, int W, int H)
270 if(W <= 0 || H <= 0 ) return 1;
271 if(Window->X + W < 0) Window->X = -W + 1;
272 if(Window->Y + H < 0) Window->Y = -H + 1;
274 if( Window->W == W && Window->H == H )
277 Window->W = W; Window->H = H;
279 if(Window->RenderBuffer) {
280 free(Window->RenderBuffer);
281 Window->RenderBuffer = NULL;
283 WM_Invalidate(Window);
286 struct sWndMsg_Resize msg;
289 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
295 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
297 if(Dest == NULL) return -2;
298 if(Length > 0 && Data == NULL) return -1;
300 if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
302 // TODO: Catch errors from ->HandleMessage
306 // ->HandleMessage returns 1 when the message was not handled
307 if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
309 // TODO: Catch errors from ->HandleMessage
313 // TODO: Implement message masking
320 else if(Source->Client != Dest->Client) {
321 // TODO: Support different client source windows
322 _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
329 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
335 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
337 IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
341 void WM_Invalidate(tWindow *Window)
344 // _SysDebug("Invalidating %p", Window);
345 // Don't invalidate twice (speedup)
346 // if( !(Window->Flags & WINFLAG_CLEAN) ) return;
348 // Mark for re-render
349 Window->Flags &= ~WINFLAG_CLEAN;
351 // Mark up the tree that a child window has changed
352 while( (Window = Window->Parent) )
353 Window->Flags &= ~WINFLAG_CHILDCLEAN;
356 // --- Rendering / Update
357 void WM_int_UpdateWindow(tWindow *Window)
359 int bDecoratorRedraw = 0;
361 // Ignore hidden windows
362 if( !(Window->Flags & WINFLAG_SHOW) )
366 if( !(Window->Flags & WINFLAG_CLEAN) )
368 // Calculate RealW/RealH
369 if( !(Window->Flags & WINFLAG_NODECORATE) )
371 //_SysDebug("Applying decorations to %p", Window);
372 Decorator_UpdateBorderSize(Window);
373 Window->RealW = Window->BorderL + Window->W + Window->BorderR;
374 Window->RealH = Window->BorderT + Window->H + Window->BorderB;
375 bDecoratorRedraw = 1;
383 Window->RealW = Window->W;
384 Window->RealH = Window->H;
387 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
389 Window->RealX = Window->Parent->X + Window->Parent->BorderL + Window->X;
390 Window->RealY = Window->Parent->Y + Window->Parent->BorderT + Window->Y;
394 Window->RealX = Window->X;
395 Window->RealY = Window->Y;
398 Window->Renderer->Redraw(Window);
399 Window->Flags |= WINFLAG_CLEAN;
403 if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
406 for( child = Window->FirstChild; child; child = child->NextSibling )
408 WM_int_UpdateWindow(child);
410 Window->Flags |= WINFLAG_CHILDCLEAN;
413 if( bDecoratorRedraw )
414 Decorator_Redraw(Window);
417 void WM_int_BlitWindow(tWindow *Window)
421 // Ignore hidden windows
422 if( !(Window->Flags & WINFLAG_SHOW) )
425 // _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
426 // Window->RealX, Window->RealY, Window->RealW, Window->RealH);
427 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
429 if( Window == gpWM_FocusedWindow && Window->CursorW )
432 Window->RealX + Window->BorderL + Window->CursorX,
433 Window->RealY + Window->BorderT + Window->CursorY,
434 Window->CursorW, Window->CursorH,
439 for( child = Window->FirstChild; child; child = child->NextSibling )
441 WM_int_BlitWindow(child);
447 // Don't redraw if nothing has changed
448 if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
451 // - Iterate through visible windows, updating them as needed
452 WM_int_UpdateWindow(gpWM_RootWindow);
454 // - Draw windows from back to front to the render buffer
455 WM_int_BlitWindow(gpWM_RootWindow);