File: floatws.c

package info (click to toggle)
ion2 20040729-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,828 kB
  • ctags: 3,466
  • sloc: ansic: 28,432; makefile: 473; sh: 230; perl: 16
file content (744 lines) | stat: -rw-r--r-- 17,662 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
 * ion/floatws/floatws.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2004. 
 *
 * Ion is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */

#include <string.h>

#include <ioncore/common.h>
#include <ioncore/rootwin.h>
#include <ioncore/focus.h>
#include <ioncore/global.h>
#include <ioncore/objp.h>
#include <ioncore/region.h>
#include <ioncore/manage.h>
#include <ioncore/screen.h>
#include <ioncore/names.h>
#include <ioncore/saveload.h>
#include <ioncore/attach.h>
#include <ioncore/regbind.h>
#include <ioncore/genframe-pointer.h>
#include <ioncore/stacking.h>
#include <ioncore/extlconv.h>
#include <ioncore/defer.h>
#include <ioncore/minmax.h>

#include "floatws.h"
#include "floatframe.h"
#include "placement.h"
#include "main.h"


WObjList *floatws_sticky_list=NULL;


/*{{{ region dynfun implementations */


static void floatws_fit(WFloatWS *ws, const WRectangle *geom)
{
    REGION_GEOM(ws)=*geom;
}


static bool reparent_floatws(WFloatWS *ws, WWindow *parent, 
                             const WRectangle *geom)
{
    WRegion *sub, *next;
    bool rs;
    int xdiff, ydiff;
    
    if(!same_rootwin((WRegion*)ws, (WRegion*)parent))
        return FALSE;
    
    region_detach_parent((WRegion*)ws);
    XReparentWindow(wglobal.dpy, ws->dummywin, parent->win, geom->x, geom->h);
    region_attach_parent((WRegion*)ws, (WRegion*)parent);
    
    xdiff=geom->x-REGION_GEOM(ws).x;
    ydiff=geom->y-REGION_GEOM(ws).y;
    
    FOR_ALL_MANAGED_ON_LIST_W_NEXT(ws->managed_list, sub, next){
        WRectangle g=REGION_GEOM(sub);
        g.x+=xdiff;
        g.y+=ydiff;
        if(!region_reparent(sub, parent, &g)){
            warn("Problem: can't reparent a %s managed by a WFloatWS"
                 "being reparented. Detaching from this object.",
                 WOBJ_TYPESTR(sub));
            region_detach_manager(sub);
        }
    }
    
    return TRUE;
}


static void move_sticky(WFloatWS *ws)
{
    WRegion *reg, *par=REGION_PARENT(ws);
    
    if(par==NULL || !WOBJ_IS(par, WMPlex))
        return;
    
    ITERATE_OBJLIST(WRegion*, reg, floatws_sticky_list){
        WFloatWS *rmgr;
        if(REGION_PARENT(reg)!=par)
            continue;
        rmgr=REGION_MANAGER_CHK(reg, WFloatWS);
        if(rmgr==NULL)
            continue;
        if(rmgr->current_managed==reg)
            rmgr->current_managed=NULL;
        region_unset_manager(reg, (WRegion*)rmgr, &(rmgr->managed_list));
        region_set_manager(reg, (WRegion*)ws, &(ws->managed_list));
        if(REGION_IS_ACTIVE(reg) && ws->current_managed==NULL)
            ws->current_managed=reg;
    }
}


static void floatws_map(WFloatWS *ws)
{
    WRegion *reg;

    move_sticky(ws);

    MARK_REGION_MAPPED(ws);
    XMapWindow(wglobal.dpy, ws->dummywin);

    FOR_ALL_MANAGED_ON_LIST(ws->managed_list, reg){
        region_map(reg);
    }
}


static void floatws_unmap(WFloatWS *ws)
{
    WRegion *reg;
    
    MARK_REGION_UNMAPPED(ws);
    XUnmapWindow(wglobal.dpy, ws->dummywin);
    
    FOR_ALL_MANAGED_ON_LIST(ws->managed_list, reg){
        region_unmap(reg);
    }
}


static WRegion *floatws_get_focus_to(WFloatWS *ws)
{
    WRegion *next=NULL;
    WWindow *par=REGION_PARENT_CHK(ws, WWindow);
    
    if(par!=NULL){
        Window root=None, parent=None, *children=NULL;
        uint nchildren=0;
        WRegion *r;
        XQueryTree(wglobal.dpy, region_x_window((WRegion*)par),
                   &root, &parent, &children, &nchildren);
        while(nchildren>0){
            nchildren--;
            r=find_window(children[nchildren]);
            if(r!=NULL && REGION_MANAGER(r)==(WRegion*)ws){
                next=r;
                break;
            }
        }
        XFree(children);
    }
    
    return (next ? next : ws->managed_list);
}


static void floatws_set_focus_to(WFloatWS *ws, bool warp)
{
    WRegion *r=ws->current_managed;
        
    if(r==NULL)
        r=floatws_get_focus_to(ws);

    if(r==NULL){
        SET_FOCUS(ws->dummywin);
        if(warp)
            do_warp((WRegion*)ws);
        return;
    }
    
    region_set_focus_to(r, warp);
}


static bool floatws_display_managed(WFloatWS *ws, WRegion *reg)
{
    if(!region_is_fully_mapped((WRegion*)ws))
       return FALSE;
    region_map(reg);
    return TRUE;
}


static void floatws_remove_managed(WFloatWS *ws, WRegion *reg)
{
    WRegion *next=NULL;
    bool mcf=region_may_control_focus((WRegion*)ws);
    
    region_unset_manager(reg, (WRegion*)ws, &(ws->managed_list));
    region_remove_bindmap_owned(reg, &floatws_bindmap, (WRegion*)ws);
    
    if(ws->current_managed!=reg)
        return;
    
    ws->current_managed=NULL;
    
    if(mcf){
        if(reg->stacking.above!=NULL)
            next=reg->stacking.above;
        else
            next=floatws_get_focus_to(ws);
        
        do_set_focus(next!=NULL ? next : (WRegion*)ws, FALSE);
    }
}


static void floatws_managed_activated(WFloatWS *ws, WRegion *reg)
{
    ws->current_managed=reg;
}


static Window floatws_x_window(const WFloatWS *ws)
{
    return ws->dummywin;
}


/*}}}*/


/*{{{ Create/destroy */


static bool floatws_init(WFloatWS *ws, WWindow *parent, 
                         const WRectangle *bounds)
{
    if(!WOBJ_IS(parent, WWindow))
        return FALSE;

    ws->dummywin=XCreateWindow(wglobal.dpy, parent->win,
                                bounds->x, bounds->y, 1, 1, 0,
                                CopyFromParent, InputOnly,
                                CopyFromParent, 0, NULL);
    if(ws->dummywin==None)
        return FALSE;

    XSelectInput(wglobal.dpy, ws->dummywin,
                 FocusChangeMask|KeyPressMask|KeyReleaseMask|
                 ButtonPressMask|ButtonReleaseMask);
    XSaveContext(wglobal.dpy, ws->dummywin, wglobal.win_context,
                 (XPointer)ws);
    
    ws->managed_list=NULL;
    ws->current_managed=NULL;

    genws_init(&(ws->genws), parent, bounds);

    region_add_bindmap((WRegion*)ws, &floatws_bindmap);
    
    return TRUE;
}


WFloatWS *create_floatws(WWindow *parent, const WRectangle *bounds)
{
    CREATEOBJ_IMPL(WFloatWS, floatws, (p, parent, bounds));
}


void floatws_deinit(WFloatWS *ws)
{
    while(ws->managed_list!=NULL)
        destroy_obj((WObj*)(ws->managed_list));

    genws_deinit(&(ws->genws));

    XDeleteContext(wglobal.dpy, ws->dummywin, wglobal.win_context);
    XDestroyWindow(wglobal.dpy, ws->dummywin);
}


static bool floatws_do_rescue_clientwins(WFloatWS *ws, WRegion *dst)
{
    return region_do_rescue_managed_clientwins((WRegion*)ws, dst,
                                               ws->managed_list);
}


/*EXTL_DOC
 * Destroys \var{ws} unless this would put the WM in a possibly unusable
 * state.
 */
EXTL_EXPORT_MEMBER
bool floatws_relocate_and_close(WFloatWS *ws)
{
    if(!region_may_destroy((WRegion*)ws)){
        warn("Workspace may not be destroyed.");
        return FALSE;
    }
    
    /* TODO: move frames to other workspaces */
    
    if(!region_rescue_clientwins((WRegion*)ws)){
        warn("Failed to rescue some client windows!");
        return FALSE;
    }
    
    defer_destroy((WObj*)ws);
    return TRUE;
}


void floatws_close(WFloatWS *ws)
{
    if(ws->managed_list!=NULL){
        warn("Workspace %s is still managing other objects "
             " -- refusing to close.", region_name((WRegion*)ws));
        return;
    }
    
    floatws_relocate_and_close(ws);
}


/*}}}*/


/*{{{ manage_clientwin/transient */


static void floatws_add_managed(WFloatWS *ws, WRegion *reg)
{
    region_set_manager(reg, (WRegion*)ws, &(ws->managed_list));
    region_add_bindmap_owned(reg, &floatws_bindmap, (WRegion*)ws);

    if(region_is_fully_mapped((WRegion*)ws))
        region_map(reg);
}


static WRegion *floatws_do_attach(WFloatWS *ws, WRegionAttachHandler *fn,
                                  void *fnparams, const WRectangle *geom)
{
    WWindow *par;
    WRegion *reg;

    par=REGION_PARENT_CHK(ws, WWindow);
    assert(par!=NULL);
    
    reg=fn(par, geom, fnparams);

    if(reg!=NULL)
        floatws_add_managed(ws, reg);
    
    return reg;
}


static WRegion *floatws_attach_load(WFloatWS *ws, ExtlTab param)
{
    WRectangle geom;
    
    if(!extl_table_gets_geom(param, "geom", &geom)){
        warn("No geometry specified");
        return NULL;
    }
    
    geom.w=maxof(geom.w, 0);
    geom.h=maxof(geom.h, 0);

    return attach_load_helper((WRegion*)ws, param, 
                              (WRegionDoAttachFn*)floatws_do_attach,
                              &geom);
}


#define REG_OK(R) region_has_manage_clientwin(R)


static WMPlex *find_existing(WFloatWS *ws)
{
    WRegion *r=ws->current_managed;
    
    if(r!=NULL && REG_OK(r))
        return (WMPlex*)r;
    
    FOR_ALL_MANAGED_ON_LIST(ws->managed_list, r){
        if(REG_OK(r))
            return (WMPlex*)r;
    }
    
    return NULL;
}


static bool floatws_do_manage_clientwin(WFloatWS *ws, WClientWin *cwin,
                                        const WManageParams *param, 
                                        bool respectpos)
{
    WMPlex *target=NULL;
    WWindow *par;
    WRectangle fgeom=param->geom;

    par=REGION_PARENT_CHK(ws, WWindow);
    assert(par!=NULL);
    
    initial_to_floatframe_geom(ws, &fgeom, param->gravity);

    if(param->maprq && wglobal.opmode!=OPMODE_INIT){
        /* When the window is mapped by application request, position
         * request is only honoured if the position was given by the user
         * and in case of a transient (the app may know better where to 
         * place them) or if we're initialising.
         */
        respectpos=(param->tfor!=NULL || param->userpos);
    }
    
    /* However, if the requested geometry does not overlap the
     * workspaces's geometry, position request is never honoured.
     */
    if((fgeom.x+fgeom.w<=REGION_GEOM(ws).x) ||
       (fgeom.y+fgeom.h<=REGION_GEOM(ws).y) ||
       (fgeom.x>=REGION_GEOM(ws).x+REGION_GEOM(ws).w) ||
       (fgeom.y>=REGION_GEOM(ws).y+REGION_GEOM(ws).h)){
        respectpos=FALSE;
    }
    
    if(!respectpos)
        floatws_calc_placement(ws, &fgeom);
    
    target=(WMPlex*)create_floatframe(par, &fgeom);
    
    if(target==NULL){
        warn("Failed to create a new WFloatFrame for client window");
        return FALSE;
    }

    assert(same_rootwin((WRegion*)target, (WRegion*)cwin));
    
    if(!mplex_attach_simple(target, (WRegion*)cwin, param->switchto)){
        destroy_obj((WObj*)target);
        return FALSE;
    }

    floatws_add_managed(ws, (WRegion*)target);
    
    /* Don't warp, it is annoying in this case */
    if(param->switchto && region_may_control_focus((WRegion*)ws)){
        set_previous_of((WRegion*)target);
        set_focus((WRegion*)target);
    }
    
    return TRUE;
}


static bool floatws_manage_clientwin(WFloatWS *ws, WClientWin *cwin,
                                     const WManageParams *param)
{
    return floatws_do_manage_clientwin(ws, cwin, param, TRUE);
}


static bool floatws_handle_drop(WFloatWS *ws, int x, int y,
                                WRegion *dropped)
{
    WRectangle fgeom;
    WFloatFrame *target;
    WWindow *par;
    
    par=REGION_PARENT_CHK(ws, WWindow);
    
    if(par==NULL)
        return FALSE;
    
    fgeom=REGION_GEOM(dropped);
    managed_to_floatframe_geom(ROOTWIN_OF(ws), &fgeom);
    /* The x and y arguments are in root coordinate space */
    region_rootpos((WRegion*)par, &fgeom.x, &fgeom.y);
    fgeom.x=x-fgeom.x;
    fgeom.y=y-fgeom.y;

    target=create_floatframe(par, &fgeom);
    
    if(target==NULL){
        warn("Failed to create a new WFloatFrame.");
        return FALSE;
    }
    
    if(!mplex_attach_simple((WMPlex*)target, dropped, TRUE)){
        destroy_obj((WObj*)target);
        warn("Failed to attach dropped region to created WFloatFrame");
        return FALSE;
    }
    
    floatws_add_managed(ws, (WRegion*)target);

    return TRUE;
}

/*EXTL_DOC
 * Attach client window \var{cwin} on \var{ws}.
 * At least the following fields in \var{p} are supported:
 * 
 * \begin{tabularx}{\linewidth}{lX}
 *  \hline
 *  Field & Description \\
 *  \hline
 *  \var{switchto} & Should the region be switched to (boolean)? Optional. \\
 *  \var{geom} & Geometry; \var{x} and \var{y}, if set, indicates top-left of 
 *   the frame to be created while \var{width} and \var{height}, if set, indicate
 *   the size of the client window within that frame. Optional.
 * \end{tabularx}
 */
EXTL_EXPORT_MEMBER
bool floatws_attach(WFloatWS *ws, WClientWin *cwin, ExtlTab p)
{
    int posok=0;
    WManageParams param=INIT_WMANAGEPARAMS;
    ExtlTab g;
    
    param.gravity=ForgetGravity;
    param.geom.w=REGION_GEOM(cwin).w;
    param.geom.h=REGION_GEOM(cwin).h;
    
    extl_table_gets_b(p, "switchto", &(param.switchto));
    
    if(extl_table_gets_t(p, "geom", &g)){
        if(extl_table_gets_i(g, "x", &(param.geom.x)))
            posok++;
        if(extl_table_gets_i(g, "y", &(param.geom.y)))
            posok++;
    
        extl_table_gets_i(p, "w", &(param.geom.w));
        extl_table_gets_i(p, "h", &(param.geom.h));
        
        extl_unref_table(g);
    }
    
    param.geom.w=maxof(0, param.geom.w);
    param.geom.h=maxof(0, param.geom.h);

    /*if(posok==2)
        param.userpos=TRUE;*/
    
    return floatws_do_manage_clientwin(ws, cwin, &param, posok==2);
}


/* A handler for add_clientwin_alt hook to handle transients for windows
 * on WFloatWS:s differently from the normal behaviour.
 */
bool add_clientwin_floatws_transient(WClientWin *cwin, 
                                     const WManageParams *param)
{
    WRegion *stack_above;
    WFloatWS *ws;
    
    if(param->tfor==NULL)
        return FALSE;

    stack_above=(WRegion*)REGION_PARENT_CHK(param->tfor, WFloatFrame);
    if(stack_above==NULL)
        return FALSE;
    
    ws=REGION_MANAGER_CHK(stack_above, WFloatWS);
    if(ws==NULL)
        return FALSE;
    
    if(!floatws_manage_clientwin(ws, cwin, param))
        return FALSE;

    region_stack_above(REGION_MANAGER(cwin), stack_above);

    return TRUE;
}


/*}}}*/


/*{{{ Circulate */


/*EXTL_DOC
 * Activate next object on \var{ws}.
 */
EXTL_EXPORT_MEMBER
WRegion *floatws_circulate(WFloatWS *ws)
{
    WRegion *r=NEXT_MANAGED_WRAP(ws->managed_list, ws->current_managed);
    if(r!=NULL)
        region_goto(r);
    return r;
}


/*EXTL_DOC
 * Activate previous object on \var{ws}.
 */
EXTL_EXPORT_MEMBER
WRegion *floatws_backcirculate(WFloatWS *ws)
{
    WRegion *r=PREV_MANAGED_WRAP(ws->managed_list, ws->current_managed);
    if(r!=NULL)
        region_goto(r);
    return r;
}


/*}}}*/


/*{{{ Misc. */


/*EXTL_DOC
 * Returns a list of regions managed by the workspace (frames, mostly).
 */
EXTL_EXPORT_MEMBER
ExtlTab floatws_managed_list(WFloatWS *ws)
{
    return managed_list_to_table(ws->managed_list, NULL);
}


/*EXTL_DOC
 * Returns the object that currently has or previously had focus on \var{ws}
 * (if no other object on the workspace currently has focus).
 */
EXTL_EXPORT_MEMBER
WRegion* floatws_current(WFloatWS *ws)
{
    return ws->current_managed;
}


/*}}}*/


/*{{{ Save/load */


static bool floatws_save_to_file(WFloatWS *ws, FILE *file, int lvl)
{
    WRegion *mgd;
    
    begin_saved_region((WRegion*)ws, file, lvl);
    save_indent_line(file, lvl);
    fprintf(file, "managed = {\n");
    FOR_ALL_MANAGED_ON_LIST(ws->managed_list, mgd){
        save_indent_line(file, lvl+1);
        fprintf(file, "{\n");
        if(region_save_to_file((WRegion*)mgd, file, lvl+2))
            save_geom(&REGION_GEOM(mgd), file, lvl+2);
        save_indent_line(file, lvl+1);
        fprintf(file, "},\n");
    }
    save_indent_line(file, lvl);
    fprintf(file, "},\n");
    
    return TRUE;
}


WRegion *floatws_load(WWindow *par, const WRectangle *geom, ExtlTab tab)
{
    WFloatWS *ws;
    ExtlTab substab, subtab;
    int i, n;
    
    ws=create_floatws(par, geom);
    
    if(ws==NULL)
        return NULL;
        
    if(!extl_table_gets_t(tab, "managed", &substab))
        return (WRegion*)ws;

    n=extl_table_get_n(substab);
    for(i=1; i<=n; i++){
        if(extl_table_geti_t(substab, i, &subtab)){
            floatws_attach_load(ws, subtab);
            extl_unref_table(subtab);
        }
    }
    
    extl_unref_table(substab);

    return (WRegion*)ws;
}


/*}}}*/


/*{{{ Dynamic function table and class implementation */


static DynFunTab floatws_dynfuntab[]={
    {region_fit, 
     floatws_fit},
    {(DynFun*)region_reparent,
     (DynFun*)reparent_floatws},

    {region_map, 
     floatws_map},
    {region_unmap, 
     floatws_unmap},
    {(DynFun*)region_display_managed, 
     (DynFun*)floatws_display_managed},

    {region_set_focus_to, 
     floatws_set_focus_to},
    {region_managed_activated, 
     floatws_managed_activated},
    
    {(DynFun*)region_manage_clientwin, 
     (DynFun*)floatws_manage_clientwin},
    {(DynFun*)region_handle_drop,
     (DynFun*)floatws_handle_drop},
    {region_remove_managed,
     floatws_remove_managed},
    
    {(DynFun*)region_save_to_file, 
     (DynFun*)floatws_save_to_file},

    {(DynFun*)region_x_window,
     (DynFun*)floatws_x_window},

    {region_close,
     floatws_close},

    {(DynFun*)region_current,
     (DynFun*)floatws_current},
    
    {(DynFun*)region_do_rescue_clientwins,
     (DynFun*)floatws_do_rescue_clientwins},
    
    END_DYNFUNTAB
};


IMPLOBJ(WFloatWS, WGenWS, floatws_deinit, floatws_dynfuntab);


/*}}}*/