]> git.decadent.org.uk Git - ion3.git/blob - ioncore/manage.c
2eb6f38b8f97e02bb2cfb54402cc6c417867338a
[ion3.git] / ioncore / manage.c
1 /*
2  * ion/ioncore/manage.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <libtu/objp.h>
13 #include <libextl/extl.h>
14 #include "global.h"
15 #include "common.h"
16 #include "region.h"
17 #include "manage.h"
18 #include "names.h"
19 #include "fullscreen.h"
20 #include "pointer.h"
21 #include "netwm.h"
22 #include "extlconv.h"
23 #include "return.h"
24 #include "conf.h"
25 #include "detach.h"
26 #include "group-ws.h"
27
28
29 /*{{{ Add */
30
31
32 WScreen *clientwin_find_suitable_screen(WClientWin *cwin, 
33                                         const WManageParams *param)
34 {
35     WScreen *scr=NULL, *found=NULL;
36     bool respectpos=(param->tfor!=NULL || param->userpos);
37     
38     FOR_ALL_SCREENS(scr){
39         if(!region_same_rootwin((WRegion*)scr, (WRegion*)cwin))
40             continue;
41             
42         if(!OBJ_IS(scr, WRootWin)){
43             /* The root window itself is only a fallback */
44             
45             if(REGION_IS_ACTIVE(scr)){
46                 found=scr;
47                 if(!respectpos)
48                     break;
49             }
50             
51             if(rectangle_contains(&REGION_GEOM(scr), 
52                                   param->geom.x, param->geom.y)){
53                 found=scr;
54                 if(respectpos)
55                     break;
56             }
57         }
58         
59         if(found==NULL)
60             found=scr;
61     }
62     
63     return found;
64 }
65
66
67 /*extern WRegion *ioncore_newly_created;*/
68
69
70 static WPHolder *try_target(WClientWin *cwin, const WManageParams *param, 
71                             const char *target)
72 {
73     WRegion *r=ioncore_lookup_region(target, NULL);
74         
75     if(r==NULL)
76         return NULL;
77             
78     return region_prepare_manage(r, cwin, param, MANAGE_REDIR_PREFER_NO);
79 }
80
81
82 static bool handle_target_winprops(WClientWin *cwin, const WManageParams *param,
83                                    WScreen *scr, WPHolder **ph_ret)
84 {
85     char *layout=NULL, *target=NULL;
86     WPHolder *ph=NULL;
87     bool mgd=FALSE;
88     
89     if(extl_table_gets_s(cwin->proptab, "target", &target))
90         ph=try_target(cwin, param, target);
91     
92     if(ph==NULL && extl_table_gets_s(cwin->proptab, "new_group", &layout)){
93         ExtlTab lo=ioncore_get_layout(layout);
94         
95         free(layout);
96         
97         if(lo!=extl_table_none()){
98             WMPlexAttachParams par=MPLEXATTACHPARAMS_INIT;
99             int mask=MPLEX_ATTACH_SWITCHTO;
100             WRegion *reg;
101             
102             if(param->switchto)
103                 par.flags|=MPLEX_ATTACH_SWITCHTO;
104             
105             /*ioncore_newly_created=(WRegion*)cwin;*/
106             
107             reg=mplex_attach_new_(&scr->mplex, &par, mask, lo);
108             
109             extl_unref_table(lo);
110             
111             /*ioncore_newly_created=NULL;*/
112             
113             mgd=(region_manager((WRegion*)cwin)!=NULL);
114             
115             if(reg!=NULL && !mgd){
116                 if(target!=NULL)
117                     ph=try_target(cwin, param, target);
118                 
119                 if(ph==NULL){
120                     ph=region_prepare_manage(reg, cwin, param, 
121                                              MANAGE_REDIR_PREFER_YES);
122                     
123                     if(ph==NULL)
124                         destroy_obj((Obj*)reg);
125                 }
126             }
127         }
128     }
129     
130     if(target!=NULL)
131         free(target);
132     
133     *ph_ret=ph;
134     
135     return mgd;
136 }
137
138
139 static bool try_fullscreen(WClientWin *cwin, WScreen *dflt, 
140                            const WManageParams *param)
141 {
142     WScreen *fs_scr=NULL;
143     bool fs=FALSE, tmp;
144     
145     /* Check fullscreen mode. (This is intentionally not done
146      * for transients and windows with target winprops.)
147      */
148     if(extl_table_gets_b(cwin->proptab, "fullscreen", &tmp)){
149         if(!tmp)
150             return FALSE;
151         fs_scr=dflt;
152     }
153
154     if(fs_scr==NULL)
155         fs_scr=netwm_check_initial_fullscreen(cwin);
156
157     if(fs_scr==NULL)
158         fs_scr=clientwin_fullscreen_chkrq(cwin, param->geom.w, param->geom.h);
159     
160     if(fs_scr!=NULL){
161         WPHolder *fs_ph=region_prepare_manage((WRegion*)fs_scr, cwin, param,
162                                               MANAGE_REDIR_STRICT_NO);
163         
164         if(fs_ph!=NULL){
165             int swf=(param->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
166             
167             cwin->flags|=CLIENTWIN_FS_RQ;
168             
169             fs=pholder_attach(fs_ph, swf, (WRegion*)cwin);
170             
171             if(!fs)
172                 cwin->flags&=~CLIENTWIN_FS_RQ;
173                 
174             destroy_obj((Obj*)fs_ph);
175         }
176     }
177
178     return fs;
179 }
180
181
182 bool clientwin_do_manage_default(WClientWin *cwin, 
183                                  const WManageParams *param)
184 {
185     WScreen *scr=NULL;
186     WPHolder *ph=NULL;
187     int swf=(param->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
188     bool ok, uq=FALSE;
189
190     /* Find a suitable screen */
191     scr=clientwin_find_suitable_screen(cwin, param);
192     if(scr==NULL){
193         warn(TR("Unable to find a screen for a new client window."));
194         return FALSE;
195     }
196     
197     if(handle_target_winprops(cwin, param, scr, &ph))
198         return TRUE;
199         
200     /* Check if param->tfor or any of its managers want to manage cwin. */
201     if(ph==NULL && param->tfor!=NULL){
202         assert(param->tfor!=cwin);
203         ph=region_prepare_manage_transient((WRegion*)param->tfor, cwin, 
204                                            param, 0);
205         uq=TRUE;
206     }
207
208     if(ph==NULL){
209         /* Find a placeholder for non-fullscreen state */
210         ph=region_prepare_manage((WRegion*)scr, cwin, param,
211                                  MANAGE_REDIR_PREFER_YES);
212
213         if(try_fullscreen(cwin, scr, param)){
214             if(pholder_target(ph)!=(WRegion*)region_screen_of((WRegion*)cwin)){
215                 WRegion *grp=region_groupleader_of((WRegion*)cwin);
216                 if(region_do_set_return(grp, ph))
217                     return TRUE;
218             }
219             destroy_obj((Obj*)ph);
220             return TRUE;
221         }
222         
223     }
224
225     if(ph==NULL)
226         return FALSE;
227     
228     /* Not in full-screen mode; use the placeholder to attach. */
229     
230     ok=pholder_attach(ph, swf, (WRegion*)cwin);
231     
232     destroy_obj((Obj*)ph);
233     
234     if(uq && ok)
235         ioncore_unsqueeze((WRegion*)cwin, FALSE);
236     
237     return ok;
238 }
239
240
241 /*}}}*/
242
243
244 /*{{{ region_prepare_manage/region_manage_clientwin/etc. */
245
246
247 WPHolder *region_prepare_manage(WRegion *reg, const WClientWin *cwin,
248                                 const WManageParams *param, int redir)
249 {
250     WPHolder *ret=NULL;
251     CALL_DYN_RET(ret, WPHolder*, region_prepare_manage, reg, 
252                  (reg, cwin, param, redir));
253     return ret;
254 }
255
256
257 WPHolder *region_prepare_manage_default(WRegion *reg, const WClientWin *cwin,
258                                         const WManageParams *param, int redir)
259 {
260     WRegion *curr;
261     
262     if(redir==MANAGE_REDIR_STRICT_NO)
263         return NULL;
264     
265     curr=region_current(reg);
266     
267     if(curr==NULL)
268         return NULL;
269         
270     return region_prepare_manage(curr, cwin, param, MANAGE_REDIR_PREFER_YES);
271 }
272
273
274 WPHolder *region_prepare_manage_transient(WRegion *reg, 
275                                           const WClientWin *cwin,
276                                           const WManageParams *param,
277                                           int unused)
278 {
279     WPHolder *ret=NULL;
280     CALL_DYN_RET(ret, WPHolder*, region_prepare_manage_transient, reg, 
281                  (reg, cwin, param, unused));
282     return ret;
283 }
284
285
286 WPHolder *region_prepare_manage_transient_default(WRegion *reg, 
287                                                   const WClientWin *cwin,
288                                                   const WManageParams *param,
289                                                   int unused)
290 {
291     WRegion *mgr=REGION_MANAGER(reg);
292     
293     if(mgr!=NULL)
294         return region_prepare_manage_transient(mgr, cwin, param, unused);
295     else
296         return NULL;
297 }
298
299
300 bool region_manage_clientwin(WRegion *reg, WClientWin *cwin,
301                              const WManageParams *par, int redir)
302 {
303     bool ret;
304     WPHolder *ph=region_prepare_manage(reg, cwin, par, redir);
305     int swf=(par->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
306     
307     if(ph==NULL)
308         return FALSE;
309     
310     ret=pholder_attach(ph, swf, (WRegion*)cwin);
311     
312     destroy_obj((Obj*)ph);
313     
314     return ret;
315 }
316
317
318 /*}}}*/
319
320
321 /*{{{ Rescue */
322
323
324 DECLSTRUCT(WRescueInfo){
325     WPHolder *ph;
326     WRegion *get_rescue;
327     bool failed_get;
328     bool test;
329 };
330
331
332 static WRegion *iter_children(void *st)
333 {
334     WRegion **nextp=(WRegion**)st;
335     WRegion *next=*nextp;
336     *nextp=(next==NULL ? NULL : next->p_next);
337     return next;   
338 }
339
340
341 bool region_rescue_child_clientwins(WRegion *reg, WRescueInfo *info)
342 {
343     WRegion *next=reg->children;
344     return region_rescue_some_clientwins(reg, info, iter_children, &next);
345 }
346
347
348 bool region_rescue_some_clientwins(WRegion *reg, WRescueInfo *info,
349                                    WRegionIterator *iter, void *st)
350 {
351     bool res=FALSE;
352     int fails=0;
353
354     if(info->failed_get)
355         return FALSE;
356     
357     reg->flags|=REGION_CWINS_BEING_RESCUED;
358     
359     while(TRUE){
360         WRegion *tosave=iter(st);
361         WClientWin *cwin;
362         
363         if(tosave==NULL)
364             break;
365         
366         cwin=OBJ_CAST(tosave, WClientWin);
367         
368         if(cwin==NULL){
369             if(!region_rescue_clientwins(tosave, info)){
370                 fails++;
371                 if(info->failed_get)
372                     break;
373             }
374         }else if(info->test){
375             fails++;
376             break;
377         }else if(!(cwin->flags&CLIENTWIN_UNMAP_RQ)){
378             if(info->ph==NULL){
379                 info->ph=region_get_rescue_pholder(info->get_rescue);
380                 if(info->ph==NULL){
381                     info->failed_get=TRUE;
382                     break;
383                 }
384             }
385             if(!pholder_attach(info->ph, 0, (WRegion*)cwin))
386                 fails++;
387         }
388     }
389     
390     reg->flags&=~REGION_CWINS_BEING_RESCUED;
391
392     return (fails==0 && !info->failed_get);
393 }
394
395
396 bool region_rescue_clientwins(WRegion *reg, WRescueInfo *info)
397 {
398     bool ret=FALSE;
399     CALL_DYN_RET(ret, bool, region_rescue_clientwins, reg, (reg, info));
400     return ret;
401 }
402
403
404 bool region_rescue(WRegion *reg, WPHolder *ph_param)
405 {
406     WRescueInfo info;
407     bool ret;
408     
409     info.ph=ph_param;
410     info.test=FALSE;
411     info.get_rescue=reg;
412     info.failed_get=FALSE;
413     
414     ret=region_rescue_clientwins(reg, &info);
415     
416     if(info.ph!=ph_param)
417         destroy_obj((Obj*)info.ph);
418     
419     return ret;
420 }
421
422
423 bool region_rescue_needed(WRegion *reg)
424 {
425     WRescueInfo info;
426     
427     info.ph=NULL;
428     info.test=TRUE;
429     info.get_rescue=reg;
430     info.failed_get=FALSE;
431     
432     return !region_rescue_clientwins(reg, &info);
433 }
434
435
436 /*}}}*/
437
438
439 /*{{{ Misc. */
440
441
442 ExtlTab manageparams_to_table(const WManageParams *mp)
443 {
444     ExtlTab t=extl_create_table();
445     extl_table_sets_b(t, "switchto", mp->switchto);
446     extl_table_sets_b(t, "jumpto", mp->jumpto);
447     extl_table_sets_b(t, "userpos", mp->userpos);
448     extl_table_sets_b(t, "dockapp", mp->dockapp);
449     extl_table_sets_b(t, "maprq", mp->maprq);
450     extl_table_sets_i(t, "gravity", mp->gravity);
451     extl_table_sets_rectangle(t, "geom", &(mp->geom));
452     extl_table_sets_o(t, "tfor", (Obj*)(mp->tfor));
453     
454     return t;
455 }
456
457
458 /*}}}*/