Revert "link mute/volumeup/volumedown keys to respective amixer commands"
[notion/jeffpc.git] / ioncore / manage.c
blob0cfba7a73730136ec953787c21fc507454eaa2e4
1 /*
2 * ion/ioncore/manage.c
4 * Copyright (c) Tuomo Valkonen 1999-2009.
6 * See the included file LICENSE for details.
7 */
9 #include <libtu/objp.h>
10 #include <libextl/extl.h>
11 #include "global.h"
12 #include "common.h"
13 #include "region.h"
14 #include "manage.h"
15 #include "names.h"
16 #include "fullscreen.h"
17 #include "pointer.h"
18 #include "netwm.h"
19 #include "extlconv.h"
20 #include "return.h"
21 #include "conf.h"
22 #include "detach.h"
23 #include "group-ws.h"
26 /*{{{ Add */
29 WScreen *clientwin_find_suitable_screen(WClientWin *cwin,
30 const WManageParams *param)
32 WScreen *scr=NULL, *found=NULL;
33 bool respectpos=(param->tfor!=NULL || param->userpos);
35 FOR_ALL_SCREENS(scr){
36 if(!region_same_rootwin((WRegion*)scr, (WRegion*)cwin))
37 continue;
38 if(REGION_IS_ACTIVE(scr)){
39 found=scr;
40 if(!respectpos)
41 break;
44 if(rectangle_contains(&REGION_GEOM(scr),
45 param->geom.x, param->geom.y)){
46 found=scr;
47 if(respectpos)
48 break;
51 if(found==NULL)
52 found=scr;
55 return found;
59 /*extern WRegion *ioncore_newly_created;*/
62 static WPHolder *try_target(WClientWin *cwin, const WManageParams *param,
63 const char *target)
65 WRegion *r=ioncore_lookup_region(target, NULL);
67 if(r==NULL)
68 return NULL;
70 if(!region_same_rootwin(r, (WRegion*)cwin))
71 return NULL;
73 return region_prepare_manage(r, cwin, param, MANAGE_PRIORITY_NONE);
77 static bool handle_target_winprops(WClientWin *cwin, const WManageParams *param,
78 WScreen *scr, WPHolder **ph_ret)
80 char *layout=NULL, *target=NULL;
81 WPHolder *ph=NULL;
82 bool mgd=FALSE;
84 if(extl_table_gets_s(cwin->proptab, "target", &target))
85 ph=try_target(cwin, param, target);
87 if(ph==NULL && extl_table_gets_s(cwin->proptab, "new_group", &layout)){
88 ExtlTab lo=ioncore_get_layout(layout);
90 free(layout);
92 if(lo!=extl_table_none()){
93 WMPlexAttachParams par=MPLEXATTACHPARAMS_INIT;
94 int mask=MPLEX_ATTACH_SWITCHTO;
95 WRegion *reg;
97 if(param->switchto)
98 par.flags|=MPLEX_ATTACH_SWITCHTO;
100 /*ioncore_newly_created=(WRegion*)cwin;*/
102 reg=mplex_attach_new_(&scr->mplex, &par, mask, lo);
104 extl_unref_table(lo);
106 /*ioncore_newly_created=NULL;*/
108 mgd=(region_manager((WRegion*)cwin)!=NULL);
110 if(reg!=NULL && !mgd){
111 if(target!=NULL)
112 ph=try_target(cwin, param, target);
114 if(ph==NULL){
115 ph=region_prepare_manage(reg, cwin, param,
116 MANAGE_PRIORITY_NONE);
118 if(ph==NULL)
119 destroy_obj((Obj*)reg);
125 if(target!=NULL)
126 free(target);
128 *ph_ret=ph;
130 return mgd;
134 static bool try_fullscreen(WClientWin *cwin, WScreen *dflt,
135 const WManageParams *param)
137 WScreen *fs_scr=NULL;
138 bool fs=FALSE, tmp;
140 /* Check fullscreen mode. (This is intentionally not done
141 * for transients and windows with target winprops.)
143 if(extl_table_gets_b(cwin->proptab, "fullscreen", &tmp)){
144 if(!tmp)
145 return FALSE;
146 fs_scr=dflt;
149 if(fs_scr==NULL && netwm_check_initial_fullscreen(cwin))
150 fs_scr=dflt;
152 if(fs_scr==NULL)
153 fs_scr=clientwin_fullscreen_chkrq(cwin, param->geom.w, param->geom.h);
155 if(fs_scr!=NULL){
156 WPHolder *fs_ph=region_prepare_manage((WRegion*)fs_scr, cwin, param,
157 MANAGE_PRIORITY_NOREDIR);
159 if(fs_ph!=NULL){
160 int swf=(param->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
162 cwin->flags|=CLIENTWIN_FS_RQ;
164 fs=pholder_attach(fs_ph, swf, (WRegion*)cwin);
166 if(!fs)
167 cwin->flags&=~CLIENTWIN_FS_RQ;
169 destroy_obj((Obj*)fs_ph);
173 return fs;
177 bool clientwin_do_manage_default(WClientWin *cwin,
178 const WManageParams *param)
180 WScreen *scr=NULL;
181 WPHolder *ph=NULL;
182 int swf=(param->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
183 bool ok, uq=FALSE;
184 WRegion *createroot=NULL;
186 /* Find a suitable screen */
187 scr=clientwin_find_suitable_screen(cwin, param);
188 if(scr==NULL){
189 warn(TR("Unable to find a screen for a new client window."));
190 return FALSE;
193 if(handle_target_winprops(cwin, param, scr, &ph))
194 return TRUE;
196 /* Check if param->tfor or any of its managers want to manage cwin. */
197 if(ph==NULL && param->tfor!=NULL){
198 assert(param->tfor!=cwin);
199 ph=region_prepare_manage_transient((WRegion*)param->tfor, cwin,
200 param, 0);
201 uq=TRUE;
204 if(ph==NULL){
205 /* Find a placeholder for non-fullscreen state */
206 ph=region_prepare_manage((WRegion*)scr, cwin, param,
207 MANAGE_PRIORITY_NONE);
209 if(try_fullscreen(cwin, scr, param)){
210 if(pholder_target(ph)!=(WRegion*)region_screen_of((WRegion*)cwin)){
211 WRegion *grp=region_groupleader_of((WRegion*)cwin);
212 if(region_do_set_return(grp, ph))
213 return TRUE;
215 destroy_obj((Obj*)ph);
216 return TRUE;
221 if(ph==NULL)
222 return FALSE;
224 /* Not in full-screen mode; use the placeholder to attach. */
226 WRegionAttachData data;
227 data.type=REGION_ATTACH_REPARENT;
228 data.u.reg=(WRegion*)cwin;
230 createroot=pholder_do_attach(ph,
231 swf|PHOLDER_ATTACH_RETURN_CREATEROOT,
232 &data);
235 destroy_obj((Obj*)ph);
237 if(uq && createroot!=NULL)
238 ioncore_unsqueeze(createroot, FALSE);
240 return (createroot!=NULL);
244 /*}}}*/
247 /*{{{ region_prepare_manage/region_manage_clientwin/etc. */
250 WPHolder *region_prepare_manage(WRegion *reg, const WClientWin *cwin,
251 const WManageParams *param, int priority)
253 WPHolder *ret=NULL;
254 CALL_DYN_RET(ret, WPHolder*, region_prepare_manage, reg,
255 (reg, cwin, param, priority));
256 return ret;
260 WPHolder *region_prepare_manage_default(WRegion *reg, const WClientWin *cwin,
261 const WManageParams *param, int priority)
263 int cpriority=MANAGE_PRIORITY_SUB(priority, MANAGE_PRIORITY_NONE);
264 WRegion *curr=region_current(reg);
266 if(curr==NULL)
267 return NULL;
269 return region_prepare_manage(curr, cwin, param, cpriority);
273 WPHolder *region_prepare_manage_transient(WRegion *reg,
274 const WClientWin *cwin,
275 const WManageParams *param,
276 int unused)
278 WPHolder *ret=NULL;
279 CALL_DYN_RET(ret, WPHolder*, region_prepare_manage_transient, reg,
280 (reg, cwin, param, unused));
281 return ret;
285 WPHolder *region_prepare_manage_transient_default(WRegion *reg,
286 const WClientWin *cwin,
287 const WManageParams *param,
288 int unused)
290 WRegion *mgr=REGION_MANAGER(reg);
292 if(mgr!=NULL)
293 return region_prepare_manage_transient(mgr, cwin, param, unused);
294 else
295 return NULL;
299 bool region_manage_clientwin(WRegion *reg, WClientWin *cwin,
300 const WManageParams *par, int priority)
302 bool ret;
303 WPHolder *ph=region_prepare_manage(reg, cwin, par, priority);
304 int swf=(par->switchto ? PHOLDER_ATTACH_SWITCHTO : 0);
306 if(ph==NULL)
307 return FALSE;
309 ret=pholder_attach(ph, swf, (WRegion*)cwin);
311 destroy_obj((Obj*)ph);
313 return ret;
317 /*}}}*/
320 /*{{{ Rescue */
323 DECLSTRUCT(WRescueInfo){
324 WPHolder *ph;
325 WRegion *get_rescue;
326 bool failed_get;
327 bool test;
328 int flags;
332 static WRegion *iter_children(void *st)
334 WRegion **nextp=(WRegion**)st;
335 WRegion *next=*nextp;
336 *nextp=(next==NULL ? NULL : next->p_next);
337 return next;
341 bool region_rescue_child_clientwins(WRegion *reg, WRescueInfo *info)
343 WRegion *next=reg->children;
344 return region_rescue_some_clientwins(reg, info, iter_children, &next);
348 WPHolder *rescueinfo_pholder(WRescueInfo *info)
350 if(info->test)
351 return NULL;
353 if(info->ph==NULL){
354 info->ph=region_get_rescue_pholder(info->get_rescue);
355 if(info->ph==NULL){
356 info->failed_get=TRUE;
357 return NULL;
361 return info->ph;
365 /* Bah, unsplitissä oikestaan pitäisi tehä non-deep rescue */
367 bool region_do_rescue_this(WRegion *tosave_, WRescueInfo *info, int ph_flags)
369 WClientWin *cwin=OBJ_CAST(tosave_, WClientWin);
370 WRegion *tosave=NULL;
372 if(cwin!=NULL){
373 if(cwin->flags&CLIENTWIN_UNMAP_RQ)
374 return TRUE;
375 tosave=(WRegion*)cwin;
376 }else if(info->flags&REGION_RESCUE_NODEEP){
377 tosave=tosave_;
378 }else{
379 /* Try to rescue whole groups. */
380 /*tosave=(WRegion*)OBJ_CAST(tosave_, WGroupCW);*/
383 if(tosave==NULL){
384 return region_rescue_clientwins(tosave_, info);
385 }else{
386 int phf=(info->flags&REGION_RESCUE_PHFLAGS_OK ? ph_flags : 0);
387 WPHolder *ph=rescueinfo_pholder(info);
389 return (ph==NULL
390 ? FALSE
391 : pholder_attach(info->ph, phf, tosave));
396 bool region_rescue_some_clientwins(WRegion *reg, WRescueInfo *info,
397 WRegionIterator *iter, void *st)
399 bool res=FALSE;
400 int fails=0;
402 if(info->failed_get)
403 return FALSE;
405 reg->flags|=REGION_CWINS_BEING_RESCUED;
407 while(TRUE){
408 WRegion *tosave=iter(st);
410 if(tosave==NULL)
411 break;
413 if(!region_do_rescue_this(tosave, info, 0)){
414 fails++;
415 if(info->failed_get)
416 break;
420 reg->flags&=~REGION_CWINS_BEING_RESCUED;
422 return (fails==0 && !info->failed_get);
426 bool region_rescue_clientwins(WRegion *reg, WRescueInfo *info)
428 bool ret=FALSE;
429 CALL_DYN_RET(ret, bool, region_rescue_clientwins, reg, (reg, info));
430 return ret;
434 bool region_rescue(WRegion *reg, WPHolder *ph, int flags)
436 WRescueInfo info;
437 bool ret;
439 info.ph=ph;
440 info.flags=flags;
441 info.test=FALSE;
442 info.get_rescue=reg;
443 info.failed_get=FALSE;
445 ret=region_rescue_clientwins(reg, &info);
447 if(info.ph!=ph)
448 destroy_obj((Obj*)info.ph);
450 return ret;
454 bool region_rescue_needed(WRegion *reg)
456 WRescueInfo info;
458 info.ph=NULL;
459 info.flags=0;
460 info.test=TRUE;
461 info.get_rescue=reg;
462 info.failed_get=FALSE;
464 return !region_rescue_clientwins(reg, &info);
468 /*}}}*/
471 /*{{{ Misc. */
474 ExtlTab manageparams_to_table(const WManageParams *mp)
476 ExtlTab t=extl_create_table();
477 extl_table_sets_b(t, "switchto", mp->switchto);
478 extl_table_sets_b(t, "jumpto", mp->jumpto);
479 extl_table_sets_b(t, "userpos", mp->userpos);
480 extl_table_sets_b(t, "dockapp", mp->dockapp);
481 extl_table_sets_b(t, "maprq", mp->maprq);
482 extl_table_sets_i(t, "gravity", mp->gravity);
483 extl_table_sets_rectangle(t, "geom", &(mp->geom));
484 extl_table_sets_o(t, "tfor", (Obj*)(mp->tfor));
486 return t;
490 /*}}}*/