Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / wm.c
1 /*
2  * Acess2 Window Manager v3
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.c
6  * - Window manager core
7  */
8 #include <common.h>
9 #include <wm_renderer.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <video.h>
13 #include <wm_messages.h>
14 #include <decorator.h>
15
16 // === IMPORTS ===
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);
20
21 // === GLOBALS ===
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;
28
29 // === CODE ===
30 void WM_Initialise(void)
31 {
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;
36 }
37
38 void WM_RegisterRenderer(tWMRenderer *Renderer)
39 {
40         // TODO: Catch out duplicates
41         Renderer->Next = gpWM_Renderers;
42         gpWM_Renderers = Renderer;
43 }
44
45 // --- Manipulation
46 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
47 {
48         tWMRenderer     *renderer;
49         tWindow *ret;
50         
51         // - Get Renderer
52         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
53         {
54                 if(strcmp(RendererName, renderer->Name) == 0)
55                         break;
56         }
57         if(renderer == NULL)
58                 return NULL;
59
60         // - Call create window function
61         ret = renderer->CreateWindow(RendererArg);
62         ret->Client = Client;
63         ret->ID = ID;
64         ret->Parent = Parent;
65         if(!ret->Parent)
66                 ret->Parent = gpWM_RootWindow;
67         ret->Owner = Parent;
68         ret->Renderer = renderer;
69         ret->Flags |= WINFLAG_CLEAN;    // Needed to stop invaildate early exiting
70
71         // Append to parent
72         if(ret->Parent)
73         {
74                 if(ret->Parent->LastChild)
75                         ret->Parent->LastChild->NextSibling = ret;
76                 else
77                         ret->Parent->FirstChild = ret;
78                 ret->PrevSibling = ret->Parent->LastChild;
79                 ret->Parent->LastChild = ret;
80                 ret->NextSibling = NULL;
81         }
82         else
83         {
84                 gpWM_RootWindow = ret;
85         }
86
87         // Don't decorate child windows by default
88         if(Parent)
89         {
90                 ret->Flags |= WINFLAG_NODECORATE;
91         }
92         
93         // - Return!
94         return ret;
95 }
96
97 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
98 {
99         return IPC_int_GetWindow(Requester->Client, ID);
100 }
101
102 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
103 {
104         tWindow *ret;
105         
106         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
107         ret->RendererInfo = ret + 1;    // Get end of tWindow
108         
109         return ret;
110 }
111
112 void WM_SetWindowTitle(tWindow *Window, const char *Title)
113 {
114         if(Window->Title)
115                 free(Window->Title);
116         Window->Title = strdup(Title);
117         _SysDebug("Window %p title set to '%s'", Window, Title);
118 }
119
120 void WM_RaiseWindow(tWindow *Window)
121 {
122         tWindow *parent = Window->Parent;
123         if(!Window->Parent)     return ;
124         
125         // Remove from list
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;
134
135         // Append to end
136         if(parent->LastChild)
137                 parent->LastChild->NextSibling = Window;
138         else
139                 parent->FirstChild = Window;
140         Window->PrevSibling = parent->LastChild;
141         Window->NextSibling = NULL;
142         parent->LastChild = Window;
143 }
144
145 void WM_FocusWindow(tWindow *Destination)
146 {
147         struct sWndMsg_Bool     _msg;
148         
149         if( gpWM_FocusedWindow == Destination )
150                 return ;
151         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
152                 return ;
153         
154         if( gpWM_FocusedWindow )
155         {
156                 _msg.Val = 0;
157                 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
158         }
159         if( Destination )
160         {
161                 _msg.Val = 1;
162                 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
163         }
164                 
165         WM_Invalidate(gpWM_FocusedWindow);
166         WM_Invalidate(Destination);
167         gpWM_FocusedWindow = Destination;
168
169
170         // Get the owner of the focused window  
171 //      while(Destination && Destination->Owner)        Destination = Destination->Owner;
172 //      gpWM_HilightedWindow = Destination;
173 }
174
175
176 void WM_ShowWindow(tWindow *Window, int bShow)
177 {
178         struct sWndMsg_Bool     _msg;
179         
180         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
181                 return ;
182
183         // Message window
184         _msg.Val = !!bShow;
185         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
186
187         // Update the flag
188         if(bShow)
189                 Window->Flags |= WINFLAG_SHOW;
190         else
191         {
192                 Window->Flags &= ~WINFLAG_SHOW;
193
194                 if( Window == gpWM_FocusedWindow )
195                         WM_FocusWindow(Window->Parent);
196                 
197                 // Just a little memory saving for large hidden windows
198                 if(Window->RenderBuffer) {
199                         free(Window->RenderBuffer);
200                         Window->RenderBuffer = NULL;
201                 }
202         }
203         
204         WM_Invalidate(Window);
205 }
206
207 void WM_DecorateWindow(tWindow *Window, int bDecorate)
208 {
209         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
210                 return ;
211         
212         if(bDecorate)
213                 Window->Flags &= ~WINFLAG_NODECORATE;
214         else
215                 Window->Flags |= WINFLAG_NODECORATE;
216         
217         // Needed because the window size changes
218         if(Window->RenderBuffer) {
219                 free(Window->RenderBuffer);
220                 Window->RenderBuffer = NULL;
221         }
222         
223         WM_Invalidate(Window);
224 }
225
226 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
227 {
228 //      _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
229         // No meaning if no parent
230         if( !Window->Parent )
231                 return ;
232
233         // Check that the flag is changing
234         if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
235                 return ;
236
237         if( bRelativeToParent ) {
238                 // Set
239                 Window->Flags |= WINFLAG_RELATIVE;
240                 WM_MoveWindow(Window, Window->X, Window->Y);
241         }
242         else {
243                 // Clear
244                 Window->Flags &= ~WINFLAG_RELATIVE;
245                 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
246         }
247 }
248
249 int WM_MoveWindow(tWindow *Window, int X, int Y)
250 {
251 //      _SysDebug("Move %p to (%i,%i)", Window, X, Y);
252         // Clip coordinates
253         if(X + Window->W < 0)   X = -Window->W + 1;
254         if(Y + Window->H < 0)   Y = -Window->H + 1;
255         if(X >= giScreenWidth)  X = giScreenWidth - 1;
256         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
257         
258         // If relative to the parent, extra checks
259         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
260         {
261                 if( X > Window->Parent->W )     return 1;
262                 if( Y > Window->Parent->H )     return 1;
263         }
264         // TODO: Re-sanitise
265
266         Window->X = X;  Window->Y = Y;
267
268         // TODO: Why invalidate buffer?
269         WM_Invalidate(Window);
270
271         return 0;
272 }
273
274 int WM_ResizeWindow(tWindow *Window, int W, int H)
275 {
276         if(W <= 0 || H <= 0 )   return 1;
277         if(Window->X + W < 0)   Window->X = -W + 1;
278         if(Window->Y + H < 0)   Window->Y = -H + 1;
279
280         if( Window->W == W && Window->H == H )
281                 return 0;
282
283         Window->W = W;  Window->H = H;
284
285         if(Window->RenderBuffer) {
286                 free(Window->RenderBuffer);
287                 Window->RenderBuffer = NULL;
288         }
289         WM_Invalidate(Window);
290
291         {
292                 struct sWndMsg_Resize   msg;
293                 msg.W = W;
294                 msg.H = H;
295                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
296         }
297         
298         return 0;
299 }
300
301 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
302 {
303 //      _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
304         if(Dest == NULL) {
305                 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
306                 return -2;
307         }
308         if(Length > 0 && Data == NULL) {
309                 _SysDebug("WM_SendMessage: non-zero length and NULL data");
310                 return -1;
311         }
312
313         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
314         {
315                 // TODO: Catch errors from ->HandleMessage
316 //              _SysDebug("WM_SendMessage: Decorator grabbed message?");
317                 return 0;
318         }
319         
320         // ->HandleMessage returns 1 when the message was not handled
321         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
322         {
323                 // TODO: Catch errors from ->HandleMessage
324 //              _SysDebug("WM_SendMessage: Renderer grabbed message?");
325                 return 0;
326         }
327
328         // TODO: Implement message masking
329
330         // Dispatch to client
331         if(Dest->Client)
332         {
333                 uint32_t        src_id;
334                 if(!Source)
335                         src_id = -1;
336                 else if(Source->Client != Dest->Client) {
337                         // TODO: Support different client source windows
338                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
339                         return -1;
340                 }
341                 else {
342                         src_id = Source->ID;
343                 }
344                 
345 //              _SysDebug("WM_SendMessage: IPC Dispatch");
346                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
347         }       
348
349         return 1;
350 }
351
352 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
353 {
354         IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
355         return 0;
356 }
357
358 void WM_Invalidate(tWindow *Window)
359 {
360         if(!Window)     return ;
361 //      _SysDebug("Invalidating %p", Window);
362         // Don't invalidate twice (speedup)
363 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
364
365         // Mark for re-render
366         Window->Flags &= ~WINFLAG_CLEAN;
367
368         // Mark up the tree that a child window has changed     
369         while( (Window = Window->Parent) )
370                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
371 }
372
373 // --- Rendering / Update
374 void WM_int_UpdateWindow(tWindow *Window)
375 {
376          int    bDecoratorRedraw = 0;
377
378         // Ignore hidden windows
379         if( !(Window->Flags & WINFLAG_SHOW) )
380                 return ;
381         
382         // Render
383         if( !(Window->Flags & WINFLAG_CLEAN) )
384         {
385                 // Calculate RealW/RealH
386                 if( !(Window->Flags & WINFLAG_NODECORATE) )
387                 {
388                         //_SysDebug("Applying decorations to %p", Window);
389                         Decorator_UpdateBorderSize(Window);
390                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
391                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
392                         bDecoratorRedraw = 1;
393                 }
394                 else
395                 {
396                         Window->BorderL = 0;
397                         Window->BorderR = 0;
398                         Window->BorderT = 0;
399                         Window->BorderB = 0;
400                         Window->RealW = Window->W;
401                         Window->RealH = Window->H;
402                 }
403                 
404                 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
405                 {
406                         Window->RealX = Window->Parent->X + Window->Parent->BorderL + Window->X;
407                         Window->RealY = Window->Parent->Y + Window->Parent->BorderT + Window->Y;
408                 }
409                 else
410                 {
411                         Window->RealX = Window->X;
412                         Window->RealY = Window->Y;
413                 }
414
415                 Window->Renderer->Redraw(Window);
416                 Window->Flags |= WINFLAG_CLEAN;
417         }
418         
419         // Process children
420         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
421         {
422                 tWindow *child;
423                 for( child = Window->FirstChild; child; child = child->NextSibling )
424                 {
425                         WM_int_UpdateWindow(child);
426                 }
427                 Window->Flags |= WINFLAG_CHILDCLEAN;
428         }
429         
430         if( bDecoratorRedraw )
431                 Decorator_Redraw(Window);
432 }
433
434 void WM_int_BlitWindow(tWindow *Window)
435 {
436         tWindow *child;
437
438         // Ignore hidden windows
439         if( !(Window->Flags & WINFLAG_SHOW) )
440                 return ;
441
442 //      _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
443 //              Window->RealX, Window->RealY, Window->RealW, Window->RealH);
444         Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
445         
446         if( Window == gpWM_FocusedWindow && Window->CursorW )
447         {
448                 Video_FillRect(
449                         Window->RealX + Window->BorderL + Window->CursorX,
450                         Window->RealY + Window->BorderT + Window->CursorY,
451                         Window->CursorW, Window->CursorH,
452                         0x000000
453                         );
454         }
455
456         for( child = Window->FirstChild; child; child = child->NextSibling )
457         {
458                 WM_int_BlitWindow(child);
459         }
460 }
461
462 void WM_Update(void)
463 {
464         // Don't redraw if nothing has changed
465         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
466                 return ;        
467
468         // - Iterate through visible windows, updating them as needed
469         WM_int_UpdateWindow(gpWM_RootWindow);
470         
471         // - Draw windows from back to front to the render buffer
472         WM_int_BlitWindow(gpWM_RootWindow);
473
474         Video_Update();
475 }
476

UCC git Repository :: git.ucc.asn.au