Merge branch 'master' of git://localhost/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         _SysDebug("Raised %p", Window);
145 }
146
147 /*
148 void WM_RaiseWindow(tWindow *Window)
149 {
150         // Move to the last render position (move to top)
151         while(Window && Window->Parent)
152         {
153                 if( Window->NextSibling )
154                 {
155                         // remove
156                         if( Window->PrevSibling )
157                                 Window->PrevSibling->NextSibling = Window->NextSibling;
158                         Window->NextSibling->PrevSibling = Window->PrevSibling;
159                         // Mutate self
160                         Window->PrevSibling = Window->Parent->LastChild;
161                         Window->NextSibling = NULL;
162                         // re-add
163                         Window->PrevSibling->NextSibling = Window;
164                         Window->Parent->LastChild = Window;
165                 }
166                 _SysDebug("WM_RaiseWindow: Raised %p", Window);
167                 Window = Window->Parent;
168         }
169 }
170 */
171
172 void WM_FocusWindow(tWindow *Destination)
173 {
174         struct sWndMsg_Bool     _msg;
175         
176         _SysDebug("WM_FocusWindow(%p)", Destination);
177
178         if( gpWM_FocusedWindow == Destination )
179                 return ;
180         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
181                 return ;
182         
183         if( gpWM_FocusedWindow )
184         {
185                 _msg.Val = 0;
186                 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
187         }
188         if( Destination )
189         {
190                 _msg.Val = 1;
191                 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
192         }
193                 
194         WM_Invalidate(gpWM_FocusedWindow);
195         WM_Invalidate(Destination);
196
197         gpWM_FocusedWindow = Destination;
198 }
199
200
201 void WM_ShowWindow(tWindow *Window, int bShow)
202 {
203         struct sWndMsg_Bool     _msg;
204         
205         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
206                 return ;
207
208         // Message window
209         _msg.Val = !!bShow;
210         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
211
212         // Update the flag
213         if(bShow) {
214                 Window->Flags |= WINFLAG_SHOW;
215                 _SysDebug("Window %p shown", Window);
216         }
217         else
218         {
219                 Window->Flags &= ~WINFLAG_SHOW;
220
221                 if( Window == gpWM_FocusedWindow )
222                         WM_FocusWindow(Window->Parent);
223                 
224                 // Just a little memory saving for large hidden windows
225                 if(Window->RenderBuffer) {
226                         free(Window->RenderBuffer);
227                         Window->RenderBuffer = NULL;
228                 }
229                 _SysDebug("Window %p hidden", Window);
230         }
231         
232         WM_Invalidate(Window);
233 }
234
235 void WM_DecorateWindow(tWindow *Window, int bDecorate)
236 {
237         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
238                 return ;
239         
240         if(bDecorate)
241                 Window->Flags &= ~WINFLAG_NODECORATE;
242         else
243                 Window->Flags |= WINFLAG_NODECORATE;
244         
245         // Needed because the window size changes
246         if(Window->RenderBuffer) {
247                 free(Window->RenderBuffer);
248                 Window->RenderBuffer = NULL;
249         }
250         
251         WM_Invalidate(Window);
252 }
253
254 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
255 {
256 //      _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
257         // No meaning if no parent
258         if( !Window->Parent )
259                 return ;
260
261         // Check that the flag is changing
262         if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
263                 return ;
264
265         if( bRelativeToParent ) {
266                 // Set
267                 Window->Flags |= WINFLAG_RELATIVE;
268                 WM_MoveWindow(Window, Window->X, Window->Y);
269         }
270         else {
271                 // Clear
272                 Window->Flags &= ~WINFLAG_RELATIVE;
273                 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
274         }
275 }
276
277 int WM_MoveWindow(tWindow *Window, int X, int Y)
278 {
279 //      _SysDebug("Move %p to (%i,%i)", Window, X, Y);
280         // Clip coordinates
281         if(X + Window->W < 0)   X = -Window->W + 1;
282         if(Y + Window->H < 0)   Y = -Window->H + 1;
283         if(X >= giScreenWidth)  X = giScreenWidth - 1;
284         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
285         
286         // If relative to the parent, extra checks
287         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
288         {
289                 if( X > Window->Parent->W )     return 1;
290                 if( Y > Window->Parent->H )     return 1;
291         }
292         // TODO: Re-sanitise
293
294         _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
295         Window->X = X;  Window->Y = Y;
296
297         // Mark up the tree that a child window has changed     
298         while( (Window = Window->Parent) )
299                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
300
301         return 0;
302 }
303
304 int WM_ResizeWindow(tWindow *Window, int W, int H)
305 {
306         if(W <= 0 || H <= 0 )   return 1;
307         if(Window->X + W < 0)   Window->X = -W + 1;
308         if(Window->Y + H < 0)   Window->Y = -H + 1;
309
310         if( Window->W == W && Window->H == H )
311                 return 0;
312
313         _SysDebug("WM_Resizeindow: %ix%i", W, H);
314         Window->W = W;  Window->H = H;
315
316         if(Window->RenderBuffer) {
317                 free(Window->RenderBuffer);
318                 Window->RenderBuffer = NULL;
319         }
320         WM_Invalidate(Window);
321
322         {
323                 struct sWndMsg_Resize   msg;
324                 msg.W = W;
325                 msg.H = H;
326                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
327         }
328         
329         return 0;
330 }
331
332 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
333 {
334 //      _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
335         if(Dest == NULL) {
336                 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
337                 return -2;
338         }
339         if(Length > 0 && Data == NULL) {
340                 _SysDebug("WM_SendMessage: non-zero length and NULL data");
341                 return -1;
342         }
343
344         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
345         {
346                 // TODO: Catch errors from ->HandleMessage
347 //              _SysDebug("WM_SendMessage: Decorator grabbed message?");
348                 return 0;
349         }
350         
351         // ->HandleMessage returns 1 when the message was not handled
352         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
353         {
354                 // TODO: Catch errors from ->HandleMessage
355 //              _SysDebug("WM_SendMessage: Renderer grabbed message?");
356                 return 0;
357         }
358
359         // TODO: Implement message masking
360
361         // Dispatch to client
362         if(Dest->Client)
363         {
364                 uint32_t        src_id;
365                 if(!Source)
366                         src_id = -1;
367                 else if(Source->Client != Dest->Client) {
368                         // TODO: Support different client source windows
369                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
370                         return -1;
371                 }
372                 else {
373                         src_id = Source->ID;
374                 }
375                 
376 //              _SysDebug("WM_SendMessage: IPC Dispatch");
377                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
378         }       
379
380         return 1;
381 }
382
383 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
384 {
385         IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
386         return 0;
387 }
388
389 void WM_Invalidate(tWindow *Window)
390 {
391         if(!Window)     return ;
392 //      _SysDebug("Invalidating %p", Window);
393         // Don't invalidate twice (speedup)
394 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
395
396         // Mark for re-render
397         Window->Flags &= ~WINFLAG_CLEAN;
398
399         // Mark up the tree that a child window has changed     
400         while( (Window = Window->Parent) )
401                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
402 }
403
404 // --- Rendering / Update
405 void WM_int_UpdateWindow(tWindow *Window)
406 {
407          int    bDecoratorRedraw = 0;
408
409         // Ignore hidden windows
410         if( !(Window->Flags & WINFLAG_SHOW) )
411                 return ;
412         
413         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
414         {
415                 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
416                 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
417         }
418         else
419         {
420                 Window->RealX = Window->X;
421                 Window->RealY = Window->Y;
422         }
423         
424         // Render
425         if( !(Window->Flags & WINFLAG_CLEAN) )
426         {
427                 // Calculate RealW/RealH
428                 if( !(Window->Flags & WINFLAG_NODECORATE) )
429                 {
430                         //_SysDebug("Applying decorations to %p", Window);
431                         Decorator_UpdateBorderSize(Window);
432                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
433                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
434                         bDecoratorRedraw = 1;
435                 }
436                 else
437                 {
438                         Window->BorderL = 0;
439                         Window->BorderR = 0;
440                         Window->BorderT = 0;
441                         Window->BorderB = 0;
442                         Window->RealW = Window->W;
443                         Window->RealH = Window->H;
444                 }
445
446                 Window->Renderer->Redraw(Window);
447                 Window->Flags |= WINFLAG_CLEAN;
448         }
449         
450         // Process children
451         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
452         {
453                 tWindow *child;
454                 for( child = Window->FirstChild; child; child = child->NextSibling )
455                 {
456                         WM_int_UpdateWindow(child);
457                 }
458                 Window->Flags |= WINFLAG_CHILDCLEAN;
459         }
460         
461         if( bDecoratorRedraw )
462                 Decorator_Redraw(Window);
463 }
464
465 void WM_int_BlitWindow(tWindow *Window)
466 {
467         tWindow *child;
468
469         // Ignore hidden windows
470         if( !(Window->Flags & WINFLAG_SHOW) )
471                 return ;
472         
473         // Duplicated position update to handle window moving
474         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
475         {
476                 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
477                 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
478         }
479         else
480         {
481                 Window->RealX = Window->X;
482                 Window->RealY = Window->Y;
483         }
484
485 //      _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
486 //              Window->RealX, Window->RealY, Window->RealW, Window->RealH);
487         Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
488         
489         if( Window == gpWM_FocusedWindow && Window->CursorW )
490         {
491                 Video_FillRect(
492                         Window->RealX + Window->BorderL + Window->CursorX,
493                         Window->RealY + Window->BorderT + Window->CursorY,
494                         Window->CursorW, Window->CursorH,
495                         0x000000
496                         );
497         }
498
499         for( child = Window->FirstChild; child; child = child->NextSibling )
500         {
501                 WM_int_BlitWindow(child);
502         }
503 }
504
505 void WM_Update(void)
506 {
507         // Don't redraw if nothing has changed
508         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
509                 return ;        
510
511         // - Iterate through visible windows, updating them as needed
512         WM_int_UpdateWindow(gpWM_RootWindow);
513         
514         // - Draw windows from back to front to the render buffer
515         WM_int_BlitWindow(gpWM_RootWindow);
516
517         Video_Update();
518 }
519

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