2 * ion/ioncore/mplexpholder.c
4 * Copyright (c) Tuomo Valkonen 2005-2008.
6 * See the included file LICENSE for details.
9 #include <libtu/objp.h>
10 #include <libtu/obj.h>
11 #include <libtu/pointer.h>
12 #include <libmainloop/defer.h>
16 #include "mplexpholder.h"
18 #include "framedpholder.h"
19 #include "basicpholder.h"
25 static bool on_ph_list(WMPlexPHolder *ll, WMPlexPHolder *ph)
27 return ph->prev!=NULL;
31 static void mplexpholder_do_link(WMPlexPHolder *ph,
36 assert(mplex==(WMPlex*)ph->mplex && mplex!=NULL);
39 assert(after->after==or_after);
41 if(after->after!=NULL){
42 LINK_ITEM_AFTER(after->after->phs, after, ph, next, prev);
44 assert(on_ph_list(mplex->misc_phs, after));
45 LINK_ITEM_AFTER(mplex->misc_phs, after, ph, next, prev);
47 ph->after=after->after;
48 }else if(or_after!=NULL){
49 LINK_ITEM_FIRST(or_after->phs, ph, next, prev);
52 LINK_ITEM_FIRST(mplex->misc_phs, ph, next, prev);
58 static WMPlexPHolder *get_head(WMPlexPHolder *ph)
61 /* ph->prev==NULL should not happen.. */
62 if(ph->prev==NULL || ph->prev->next==NULL)
71 void mplexpholder_do_unlink(WMPlexPHolder *ph, WMPlex *mplex)
73 if(ph->recreate_pholder!=NULL){
75 ph->next->recreate_pholder=ph->recreate_pholder;
77 /* It might be in use in attach chain! So defer. */
78 mainloop_defer_destroy((Obj*)ph->recreate_pholder);
80 ph->recreate_pholder=NULL;
84 UNLINK_ITEM(ph->after->phs, ph, next, prev);
85 }else if(mplex!=NULL && on_ph_list(mplex->misc_phs, ph)){
86 UNLINK_ITEM(mplex->misc_phs, ph, next, prev);
87 }else if(ph->prev!=NULL){
88 WMPlexPHolder *next=ph->next;
94 assert(next->prev==ph);
98 /* ph should not be on a list, if prev pointer is NULL (whereas
99 * next alone can be NULL in our semi-doubly-linked lists).
101 assert(ph->next==NULL);
116 static void mplex_get_attach_params(WMPlex *mplex, WStacking *st,
117 WMPlexAttachParams *param)
119 param->flags=(MPLEX_ATTACH_SIZEPOLICY|
122 (st->hidden ? MPLEX_ATTACH_HIDDEN : 0)|
123 (st->lnode==NULL ? MPLEX_ATTACH_UNNUMBERED : 0));
124 param->szplcy=st->szplcy;
125 param->geom=REGION_GEOM(st->reg);
126 param->level=st->level;
130 bool mplexpholder_init(WMPlexPHolder *ph, WMPlex *mplex, WStacking *st,
131 WMPlexAttachParams *param)
133 WLListNode *or_after=NULL;
134 WMPlexPHolder *after=NULL;
136 pholder_init(&(ph->ph));
143 ph->recreate_pholder=NULL;
146 mplex_get_attach_params(mplex, st, &ph->param);
149 after=LIST_LAST(st->lnode->phs, next, prev);
153 static WMPlexAttachParams dummy_param={0, 0, {0, 0, 0, 0}, 0, 0};
160 if(!(param->flags&MPLEX_ATTACH_UNNUMBERED)){
161 int index=(param->flags&MPLEX_ATTACH_INDEX
163 : mplex_default_index(mplex));
164 or_after=llist_index_to_after(mplex->mx_list,
165 mplex->mx_current, index);
166 after=(index==LLIST_INDEX_LAST
168 ? LIST_LAST(or_after->phs, next, prev)
169 : LIST_LAST(mplex->misc_phs, next, prev))
174 mplexpholder_do_link(ph, mplex, after, or_after);
180 WMPlexPHolder *create_mplexpholder(WMPlex *mplex,
182 WMPlexAttachParams *param)
184 CREATEOBJ_IMPL(WMPlexPHolder, mplexpholder, (p, mplex, st, param));
188 void mplexpholder_deinit(WMPlexPHolder *ph)
190 mplexpholder_do_unlink(ph, ph->mplex);
191 pholder_deinit(&(ph->ph));
198 /*{{{ Move, attach */
202 WMPlexPHolder *ph, *ph_head;
203 WRegionAttachData *data;
209 static WRegion *recreate_handler(WWindow *par,
210 const WFitParams *fp,
214 WMPlexPHolder *ph=rp->ph, *ph_head=rp->ph_head, *phtmp;
215 WFramedParam *param=rp->param;
218 frame=create_frame(par, fp, param->mode);
223 /* Move pholders to frame */
224 frame->mplex.misc_phs=ph_head;
226 for(phtmp=frame->mplex.misc_phs; phtmp!=NULL; phtmp=phtmp->next)
227 phtmp->mplex=&frame->mplex;
230 if(fp->mode&(REGION_FIT_BOUNDS|REGION_FIT_WHATEVER))
231 ph->param.flags|=MPLEX_ATTACH_WHATEVER;
233 rp->reg_ret=mplex_do_attach_pholder(&frame->mplex, ph, rp->data);
235 ph->param.flags&=~MPLEX_ATTACH_WHATEVER;
237 if(rp->reg_ret==NULL){
239 for(phtmp=frame->mplex.misc_phs; phtmp!=NULL; phtmp=phtmp->next)
242 frame->mplex.misc_phs=NULL;
244 destroy_obj((Obj*)frame);
248 frame_adjust_to_initial(frame, fp, param, rp->reg_ret);
250 return (WRegion*)frame;
255 static WFramedPHolder *get_recreate_ph(WMPlexPHolder *ph)
257 return get_head(ph)->recreate_pholder;
261 static WRegion *mplexpholder_attach_recreate(WMPlexPHolder *ph, int flags,
262 WRegionAttachData *data)
264 WRegionAttachData data2;
270 rp.ph_head=get_head(ph);
272 assert(rp.ph_head!=NULL);
274 fph=rp.ph_head->recreate_pholder;
281 rp.param=&fph->param;
284 data2.type=REGION_ATTACH_NEW;
285 data2.u.n.fn=recreate_handler;
288 frame=pholder_do_attach(fph->cont, flags, &data2);
291 rp.ph_head->recreate_pholder=NULL;
292 /* It might be in use in attach chain! So defer. */
293 mainloop_defer_destroy((Obj*)fph);
300 WRegion *mplexpholder_do_attach(WMPlexPHolder *ph, int flags,
301 WRegionAttachData *data)
303 WMPlex *mplex=ph->mplex;
306 return mplexpholder_attach_recreate(ph, flags, data);
308 if(flags&PHOLDER_ATTACH_SWITCHTO)
309 ph->param.flags|=MPLEX_ATTACH_SWITCHTO;
311 ph->param.flags&=~MPLEX_ATTACH_SWITCHTO;
313 return mplex_do_attach_pholder(mplex, ph, data);
317 bool mplexpholder_move(WMPlexPHolder *ph, WMPlex *mplex,
318 WMPlexPHolder *after,
319 WLListNode *or_after)
321 mplexpholder_do_unlink(ph, ph->mplex);
328 mplexpholder_do_link(ph, mplex, after, or_after);
334 bool mplexpholder_do_goto(WMPlexPHolder *ph)
336 WRegion *reg=(WRegion*)ph->mplex;
339 return region_goto(reg);
341 WFramedPHolder *fph=get_recreate_ph(ph);
344 ? pholder_do_goto((WPHolder*)fph)
350 WRegion *mplexpholder_do_target(WMPlexPHolder *ph)
352 WRegion *reg=(WRegion*)ph->mplex;
357 WFramedPHolder *fph=get_recreate_ph(ph);
360 ? pholder_do_target((WPHolder*)fph)
366 bool mplexpholder_stale(WMPlexPHolder *ph)
368 WRegion *reg=(WRegion*)ph->mplex;
373 WFramedPHolder *fph=get_recreate_ph(ph);
375 return (fph==NULL || pholder_stale((WPHolder*)fph));
383 /*{{{ WMPlex routines */
386 void mplex_move_phs(WMPlex *mplex, WLListNode *node,
387 WMPlexPHolder *after,
388 WLListNode *or_after)
392 assert(node!=or_after);
394 while(node->phs!=NULL){
397 mplexpholder_do_unlink(ph, mplex);
398 mplexpholder_do_link(ph, mplex, after, or_after);
404 void mplex_move_phs_before(WMPlex *mplex, WLListNode *node)
406 WMPlexPHolder *after=NULL;
407 WLListNode *or_after;
409 or_after=LIST_PREV(mplex->mx_list, node, next, prev);
411 after=(or_after!=NULL
412 ? LIST_LAST(or_after->phs, next, prev)
413 : LIST_LAST(mplex->misc_phs, next, prev));
415 mplex_move_phs(mplex, node, after, or_after);
419 WMPlexPHolder *mplex_managed_get_pholder(WMPlex *mplex, WRegion *mgd)
421 WStacking *st=mplex_find_stacking(mplex, mgd);
426 return create_mplexpholder(mplex, st, NULL);
430 void mplex_flatten_phs(WMPlex *mplex)
435 FOR_ALL_NODES_ON_LLIST(node, mplex->mx_list, tmp){
436 WMPlexPHolder *last=(mplex->misc_phs==NULL ? NULL : mplex->misc_phs->prev);
437 mplex_move_phs(mplex, node, last, NULL);
442 void mplex_migrate_phs(WMPlex *src, WMPlex *dst)
444 WLListNode *or_after=LIST_LAST(dst->mx_list, next, prev);
445 WMPlexPHolder *after=(or_after!=NULL
446 ? LIST_LAST(or_after->phs, next, prev)
447 : LIST_LAST(dst->misc_phs, next, prev));
449 while(src->misc_phs!=NULL){
450 WMPlexPHolder *ph=src->misc_phs;
451 mplexpholder_move(ph, dst, after, or_after);
463 WRegion *mplex_rescue_attach(WMPlex *mplex, int flags, WRegionAttachData *data)
465 WMPlexAttachParams param;
469 /* Improved attach parametrisation hack for WMPlex source */
470 if(data->type==REGION_ATTACH_REPARENT){
471 WRegion *reg=data->u.reg;
472 WMPlex *src_mplex=REGION_MANAGER_CHK(reg, WMPlex);
474 WStacking *st=ioncore_find_stacking(reg);
476 mplex_get_attach_params(src_mplex, st, ¶m);
480 param.flags|=(MPLEX_ATTACH_INDEX|
481 (flags&PHOLDER_ATTACH_SWITCHTO ? MPLEX_ATTACH_SWITCHTO : 0));
482 param.index=LLIST_INDEX_LAST;
484 return mplex_do_attach(mplex, ¶m, data);
488 WPHolder *mplex_get_rescue_pholder_for(WMPlex *mplex, WRegion *mgd)
491 WStacking *st=mplex_find_stacking(mplex, mgd);
492 WMPlexAttachParams param;
494 param.flags=MPLEX_ATTACH_INDEX;
495 param.index=LLIST_INDEX_LAST;
497 return create_mplexpholder(mplex, st, ¶m);
499 return (WPHolder*)create_basicpholder((WRegion*)mplex,
500 (WBasicPHolderHandler*)mplex_rescue_attach);
508 /*{{{ Class information */
511 static DynFunTab mplexpholder_dynfuntab[]={
512 {(DynFun*)pholder_do_attach,
513 (DynFun*)mplexpholder_do_attach},
515 {(DynFun*)pholder_do_goto,
516 (DynFun*)mplexpholder_do_goto},
518 {(DynFun*)pholder_do_target,
519 (DynFun*)mplexpholder_do_target},
521 {(DynFun*)pholder_stale,
522 (DynFun*)mplexpholder_stale},
528 IMPLCLASS(WMPlexPHolder, WPHolder, mplexpholder_deinit,
529 mplexpholder_dynfuntab);