File: screen-notify.c

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (505 lines) | stat: -rw-r--r-- 12,134 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
/*
 * ion/ioncore/screen-notify.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2007.
 *
 * See the included file LICENSE for details.
 */

#include <string.h>

#include <libtu/minmax.h>
#include <libmainloop/defer.h>
#include <libmainloop/signal.h>

#include "common.h"
#include "global.h"
#include "infowin.h"
#include "activity.h"
#include "tags.h"
#include "gr.h"
#include "gr-util.h"
#include "stacking.h"
#include "names.h"
#include "screen.h"
#include "screen-notify.h"
#include "strings.h"


/*{{{ Generic stuff */


static WInfoWin *do_get_popup_win(WScreen *scr, Watch *watch, uint pos,
                                  char *style)
{

    WInfoWin *iw=(WInfoWin*)(watch->obj);

    if(iw==NULL){
        WMPlexAttachParams param=MPLEXATTACHPARAMS_INIT;

        param.flags=(MPLEX_ATTACH_UNNUMBERED|
                     MPLEX_ATTACH_SIZEPOLICY|
                     MPLEX_ATTACH_GEOM|
                     MPLEX_ATTACH_LEVEL);
        param.level=STACKING_LEVEL_ON_TOP;

        param.geom.x=0;
        param.geom.y=0;
        param.geom.w=1;
        param.geom.h=1;

        switch(pos){
        case MPLEX_STDISP_TL:
            param.szplcy=SIZEPOLICY_GRAVITY_NORTHWEST;
            param.geom.x=0;
            break;

        case MPLEX_STDISP_TR:
            param.szplcy=SIZEPOLICY_GRAVITY_NORTHEAST;
            param.geom.x=REGION_GEOM(scr).w-1;
            break;

        case MPLEX_STDISP_BL:
            param.szplcy=SIZEPOLICY_GRAVITY_SOUTHWEST;
            param.geom.x=0;
            param.geom.y=REGION_GEOM(scr).h-1;
            break;

        case MPLEX_STDISP_BR:
            param.szplcy=SIZEPOLICY_GRAVITY_SOUTHEAST;
            param.geom.x=REGION_GEOM(scr).w-1;
            param.geom.y=REGION_GEOM(scr).h-1;
            break;
        }


        iw=(WInfoWin*)mplex_do_attach_new(&scr->mplex, &param,
                                          (WRegionCreateFn*)create_infowin,
                                          style);

        if(iw!=NULL)
            watch_setup(watch, (Obj*)iw, NULL);
    }

    return iw;
}


static void do_unnotify(Watch *watch)
{
    Obj *iw=watch->obj;
    if(iw!=NULL){
        watch_reset(watch);
        mainloop_defer_destroy((Obj*)iw);
    }
}

// returns the position or the stdisp, or -1 if there isn't one
static int get_stdisp_pos(WScreen *scr)
{
    WRegion* stdisp=NULL;
    WMPlexSTDispInfo info;

    mplex_get_stdisp(&scr->mplex, &stdisp, &info);

    return (stdisp!=NULL) ? info.pos : -1;
}

/*}}}*/


/*{{{ Notifywin */


static WInfoWin *get_notifywin(WScreen *scr)
{
    int stdisp_pos = get_stdisp_pos(scr);
    return do_get_popup_win(scr,
                            &scr->notifywin_watch,
                            (stdisp_pos >= 0) ? stdisp_pos : MPLEX_STDISP_TL,
                            "actnotify");
}


void screen_notify(WScreen *scr, const char *str)
{
    WInfoWin *iw=get_notifywin(scr);

    if(iw!=NULL){
        int maxw=REGION_GEOM(scr).w/3;
        infowin_set_text(iw, str, maxw);
    }
}


void screen_unnotify_notifywin(WScreen *scr)
{
    do_unnotify(&scr->notifywin_watch);
}


static bool ws_mapped(WScreen *scr, WRegion *reg)
{
    while(1){
        WRegion *mgr=REGION_MANAGER(reg);

        if(mgr==NULL)
            return FALSE;

        if(mgr==(WRegion*)scr)
            return REGION_IS_MAPPED(reg);

        reg=mgr;
    }
}


static void screen_managed_activity(WScreen *scr)
{
    char *notstr=NULL;
    WRegion *reg;
    ObjListIterTmp tmp;
    PtrListIterTmp tmp2;
    ObjList *actlist=ioncore_activity_list();
    WInfoWin *iw=NULL;
    PtrList *found=NULL;
    int nfound=0, nadded=0;
    int w=0, maxw=REGION_GEOM(scr).w/4;

    /* Lisäksi minimipituus (10ex tms.), ja sen yli menevät jätetään
     * pois (+ n)
     */
    FOR_ALL_ON_OBJLIST(WRegion*, reg, actlist, tmp){
        if(!ioncore_g.activity_notification_on_all_screens &&
           (region_screen_of(reg)!=scr || ws_mapped(scr, reg)))
            continue;
        if(region_name(reg)==NULL)
            continue;
        if(ptrlist_insert_last(&found, reg))
            nfound++;
    }

    if(found==NULL)
        goto unnotify;

    iw=get_notifywin(scr);

    if(iw==NULL)
        return;

    if(iw->brush==NULL)
        goto unnotify;

    notstr=scopy(TR("act: "));

    if(notstr==NULL)
        goto unnotify;

    FOR_ALL_ON_PTRLIST(WRegion*, reg, found, tmp2){
        const char *nm=region_name(reg);
        char *nstr=NULL, *label=NULL;

        w=grbrush_get_text_width(iw->brush, notstr, strlen(notstr));

        if(w>=maxw)
            break;

        label=grbrush_make_label(iw->brush, nm, maxw-w);
        if(label!=NULL){
            nstr=(nadded>0
                  ? scat3(notstr, ", ", label)
                  : scat(notstr, label));

            if(nstr!=NULL){
                free(notstr);
                notstr=nstr;
                nadded++;
            }
            free(label);
        }
    }

    if(nfound > nadded){
        char *nstr=NULL;

        libtu_asprintf(&nstr, "%s  +%d", notstr, nfound-nadded);

        if(nstr!=NULL){
            free(notstr);
            notstr=nstr;
        }
    }

    ptrlist_clear(&found);

    infowin_set_text(iw, notstr, 0);

    free(notstr);

    return;

unnotify:
    screen_unnotify_notifywin(scr);
}


static void _screen_do_update_notifywin(WScreen *scr)
{
    if(ioncore_g.screen_notify)
        screen_managed_activity(scr);
    else
        screen_unnotify_notifywin(scr);
}

static void screen_do_update_notifywin(WScreen *scr)
{
    if(ioncore_g.activity_notification_on_all_screens) {
        WScreen* scr_iterated;
        FOR_ALL_SCREENS(scr_iterated)
        {
            _screen_do_update_notifywin(scr_iterated);
        }
    }else{
        _screen_do_update_notifywin(scr);
    }
}

/*}}}*/


/*{{{ Infowin */


static WInfoWin *get_infowin(WScreen *scr)
{
    int stdisp_pos = get_stdisp_pos(scr);
    return do_get_popup_win(scr,
                            &scr->infowin_watch,
                            (stdisp_pos == MPLEX_STDISP_TR) ? MPLEX_STDISP_BR : MPLEX_STDISP_TR,
                            "tab-info");
}


void screen_unnotify_infowin(WScreen *scr)
{
    do_unnotify(&scr->infowin_watch);
}


GR_DEFATTR(active);
GR_DEFATTR(inactive);
GR_DEFATTR(selected);
GR_DEFATTR(tagged);
GR_DEFATTR(not_tagged);
GR_DEFATTR(not_dragged);
GR_DEFATTR(activity);
GR_DEFATTR(no_activity);


static void init_attr()
{
    GR_ALLOCATTR_BEGIN;
    GR_ALLOCATTR(active);
    GR_ALLOCATTR(inactive);
    GR_ALLOCATTR(selected);
    GR_ALLOCATTR(tagged);
    GR_ALLOCATTR(not_tagged);
    GR_ALLOCATTR(not_dragged);
    GR_ALLOCATTR(no_activity);
    GR_ALLOCATTR(activity);
    GR_ALLOCATTR_END;
}


static void screen_do_update_infowin(WScreen *scr)
{
    WRegion *reg=mplex_mx_current(&(scr->mplex));
    bool tag=(reg!=NULL && region_is_tagged(reg));
    bool act=(reg!=NULL && region_is_activity_r(reg) && !REGION_IS_ACTIVE(scr));
    bool sac=REGION_IS_ACTIVE(scr);

    if(tag || act){
        const char *n=region_displayname(reg);
        WInfoWin *iw=get_infowin(scr);

        if(iw!=NULL){
            int maxw=REGION_GEOM(scr).w/3;
            infowin_set_text(iw, n, maxw);

            GrStyleSpec *spec=infowin_stylespec(iw);

            init_attr();

            gr_stylespec_unalloc(spec);

            gr_stylespec_set(spec, GR_ATTR(selected));
            gr_stylespec_set(spec, GR_ATTR(not_dragged));
            gr_stylespec_set(spec, sac ? GR_ATTR(active) : GR_ATTR(inactive));
            gr_stylespec_set(spec, tag ? GR_ATTR(tagged) : GR_ATTR(not_tagged));
            gr_stylespec_set(spec, act ? GR_ATTR(activity) : GR_ATTR(no_activity));
        }

    }else{
        screen_unnotify_infowin(scr);
    }
}



/*}}}*/


/*{{{ workspace indicator win */

// A single timer for ALL the screens
static WTimer*   workspace_indicator_remove_timer   = NULL;
static WScreen*  workspace_indicator_active_screen  = NULL;
static WGroupWS* workspace_indicator_last_workspace = NULL;

static WInfoWin *get_workspace_indicatorwin(WScreen *scr)
{
    return do_get_popup_win(scr,
                            &scr->workspace_indicatorwin_watch,
                            MPLEX_STDISP_BL,
                            "tab-info");
}


void screen_unnotify_workspace_indicatorwin(void)
{
    if( workspace_indicator_active_screen != NULL)
    {
        do_unnotify(&workspace_indicator_active_screen->workspace_indicatorwin_watch);
        workspace_indicator_active_screen    = NULL;
    }

    if( workspace_indicator_remove_timer )
        timer_reset( workspace_indicator_remove_timer );
}

static void timer_expired__workspace_indicator_remove(WTimer* UNUSED(dummy1), Obj* UNUSED(dummy2))
{
    if( workspace_indicator_active_screen != NULL )
        screen_unnotify_workspace_indicatorwin();
}

// Called when a region is focused. In response this function may either put up
// a new workspace-indicator or remove an existing one
void screen_update_workspace_indicatorwin(WRegion* reg_focused)
{
    if( ioncore_g.workspace_indicator_timeout <= 0 )
        return;

    WScreen* scr = region_screen_of(reg_focused);
    WRegion* mplex;
    if( scr == NULL ||
        (mplex = mplex_mx_current(&(scr->mplex))) == NULL ||
        !OBJ_IS(mplex, WGroupWS) ||
        mplex->ni.name == NULL)
    {
        return;
    }

    // remove the indicator from the other screen that it is on (if it IS on
    // another screen)
    if( workspace_indicator_active_screen != NULL &&
        workspace_indicator_active_screen != scr )
    {
        screen_unnotify_workspace_indicatorwin();
    }
    else if( (WGroupWS*)mplex == workspace_indicator_last_workspace )
        // If I'm already on this workspace, do nothing
        return;

    const char* name = mplex->ni.name;

    WInfoWin *iw=get_workspace_indicatorwin(scr);

    if(iw!=NULL){
        int maxw=REGION_GEOM(scr).w/3;
        infowin_set_text(iw, name, maxw);

        GrStyleSpec *spec=infowin_stylespec(iw);
        init_attr();
        gr_stylespec_unalloc(spec);

        gr_stylespec_set(spec, GR_ATTR(selected));
        gr_stylespec_set(spec, GR_ATTR(not_dragged));
        gr_stylespec_set(spec, GR_ATTR(active));
    }

    // set up the indicator on THIS screen
    workspace_indicator_active_screen  = scr;
    workspace_indicator_last_workspace = (WGroupWS*)mplex;

    // create and set the timer
    if( workspace_indicator_remove_timer == NULL )
        workspace_indicator_remove_timer = create_timer();
    if( workspace_indicator_remove_timer == NULL )
        return;

    timer_set(workspace_indicator_remove_timer, ioncore_g.workspace_indicator_timeout,
              (WTimerHandler*)timer_expired__workspace_indicator_remove, NULL);
}

void screen_unnotify_if_screen( WScreen* reg )
{
    if( reg == workspace_indicator_active_screen )
        screen_unnotify_workspace_indicatorwin();
}

void screen_unnotify_if_workspace( WGroupWS* reg)
{
    if( reg == workspace_indicator_last_workspace )
    {
        screen_unnotify_workspace_indicatorwin();
        workspace_indicator_last_workspace = NULL;
    }
}

/*}}}*/


/*{{{ Notification callbacks and deferred updates*/


void screen_update_infowin(WScreen *scr)
{
    mainloop_defer_action((Obj*)scr,
                          (WDeferredAction*)screen_do_update_infowin);
}


void screen_update_notifywin(WScreen *scr)
{
    mainloop_defer_action((Obj*)scr,
                          (WDeferredAction*)screen_do_update_notifywin);
}


void screen_managed_notify(WScreen *scr, WRegion *UNUSED(reg), WRegionNotify how)
{
    if(how==ioncore_g.notifies.tag)
        screen_update_infowin(scr);
}


void ioncore_screen_activity_notify(WRegion *reg, WRegionNotify how)
{
    WScreen *scr=region_screen_of(reg);
    if(scr!=NULL){
        if(how==ioncore_g.notifies.activity){
            screen_update_notifywin(region_screen_of(reg));
        }else if(how==ioncore_g.notifies.name){
            if(region_is_activity(reg))
                screen_update_notifywin(scr);
            if((WRegion*)scr==REGION_MANAGER(reg))
                screen_do_update_infowin(scr);
        }
    }
}


/*}}}*/