File: main.c

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

#include <string.h>

#include <libtu/map.h>
#include <libtu/minmax.h>
#include <libextl/readconfig.h>
#include <libmainloop/hooks.h>

#include <ioncore/saveload.h>
#include <ioncore/screen.h>
#include <ioncore/mplex.h>
#include <ioncore/stacking.h>
#include <ioncore/ioncore.h>
#include <ioncore/global.h>
#include <ioncore/framep.h>
#include <ioncore/frame.h>
#include <ioncore/names.h>
#include <ioncore/group.h>
#include <ioncore/group-ws.h>

#include "main.h"
#include "exports.h"


/*{{{ Module information */

#include "../version.h"

char mod_sp_ion_api_version[]=NOTION_API_VERSION;


/*}}}*/


/*{{{ Bindmaps, config, etc. */


#define SP_NAME  "*scratchpad*"
#define SPWS_NAME  "*scratchws*"


/*}}}*/


/*{{{ Exports */

static int sp_width = CF_SCRATCHPAD_DEFAULT_W;
static int sp_height = CF_SCRATCHPAD_DEFAULT_H;

static WRegion *create_frame_scratchpad(WWindow *parent, const WFitParams *fp,
                                        void *UNUSED(unused))
{
    return (WRegion*)create_frame(parent, fp, FRAME_MODE_UNKNOWN, "Scratchpad Frame");
}


static WRegion *create_scratchws(WWindow *parent, const WFitParams *fp,
                                 void *UNUSED(unused))
{
    WRegion *reg;
    WRegionAttachData data;
    WGroupAttachParams par=GROUPATTACHPARAMS_INIT;
    WGroupWS *ws;

    ws=create_groupws(parent, fp);

    if(ws==NULL)
        return NULL;

    region_set_name((WRegion*)ws, SPWS_NAME);

    data.type=REGION_ATTACH_NEW;
    data.u.n.fn=create_frame_scratchpad;
    data.u.n.param=NULL;

    par.szplcy_set=TRUE;
    par.szplcy=SIZEPOLICY_FREE_GLUE;

    par.geom_set=TRUE;
    par.geom.w=MINOF(fp->g.w, sp_width);
    par.geom.h=MINOF(fp->g.h, sp_height);
    par.geom.x=(fp->g.w-par.geom.w)/2;
    par.geom.y=(fp->g.h-par.geom.h)/2;

    par.level_set=TRUE;
    par.level=STACKING_LEVEL_MODAL1;

    par.bottom=TRUE;

    reg=group_do_attach(&ws->grp, &par, &data);

    if(reg==NULL){
        destroy_obj((Obj*)ws);
        return NULL;
    }

    region_set_name((WRegion*)reg, SP_NAME);

    return (WRegion*)ws;
}


static WRegion *create(WMPlex *mplex, int flags)
{
    WRegion *sp;
    WMPlexAttachParams par=MPLEXATTACHPARAMS_INIT;

    par.flags=(flags
               |MPLEX_ATTACH_UNNUMBERED
               |MPLEX_ATTACH_SIZEPOLICY);
    par.szplcy=SIZEPOLICY_FULL_EXACT;

    sp=mplex_do_attach_new(mplex, &par,
                           create_scratchws,
                           NULL);

    if(sp==NULL)
        warn(TR("Unable to create scratchpad."));

    return sp;
}


static bool is_scratchpad(WRegion *reg)
{
    char *nm=reg->ni.name;
    int inst_off=reg->ni.inst_off;

    if(nm==NULL)
        return FALSE;

    return (inst_off<0
            ? (strcmp(nm, SP_NAME)==0 ||
               strcmp(nm, SPWS_NAME)==0)
            : (strncmp(nm, SP_NAME, inst_off)==0 ||
               strncmp(nm, SPWS_NAME, inst_off)==0));
}

/*EXTL_DOC
 * Is \var{reg} a scratchpad?
 */
EXTL_SAFE
EXTL_EXPORT
bool mod_sp_is_scratchpad(WRegion *reg)
{
    return is_scratchpad(reg);
}

/*EXTL_DOC
 * Attempt to create a scratchpad on \var{scr}.
 */
EXTL_EXPORT
bool mod_sp_create_scratchpad(WScreen *scr)
{
    WMPlexIterTmp tmp;
    WRegion *reg;

    FOR_ALL_MANAGED_BY_MPLEX((WMPlex*)scr, reg, tmp){
        if(is_scratchpad(reg))
            return TRUE;
    }

    return create(&scr->mplex, MPLEX_ATTACH_HIDDEN)!=NULL;
}

/*EXTL_DOC
 * Set default dimension for newly created scratchpads
 * with parameters \var{width} and \var{height}.
 */
EXTL_EXPORT
void mod_sp_set_size(int width, int height)
{
    if(width>0)
        sp_width = width;
    if(height>0)
        sp_height = height;
}

/*EXTL_DOC
 * Change displayed status of some scratchpad on \var{mplex} if one is
 * found. The parameter \var{how} is one of (set/unset/toggle).
 */
EXTL_EXPORT
bool mod_sp_set_shown_on(WMPlex *mplex, const char *how)
{
    int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
    WMPlexIterTmp tmp;
    WRegion *reg;
    bool found=FALSE;

    FOR_ALL_MANAGED_BY_MPLEX(mplex, reg, tmp){
        if(is_scratchpad(reg)){
            mplex_set_hidden(mplex, reg, setpar);
            found=TRUE;
        }
    }

    if(!found){
        int sp=libtu_string_to_setparam(how);
        if(sp==SETPARAM_SET || sp==SETPARAM_TOGGLE)
            found=(create(mplex, 0)!=NULL);
    }

    return found;
}


/*EXTL_DOC
 * Toggle displayed status of \var{sp}.
 * The parameter \var{how} is one of (set/unset/toggle).
 */
EXTL_EXPORT
bool mod_sp_set_shown(WFrame *sp, const char *how)
{
    if(sp!=NULL){
        int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
        WMPlex *mplex=OBJ_CAST(REGION_MANAGER(sp), WMPlex);
        if(mplex!=NULL)
            return mplex_set_hidden(mplex, (WRegion*)sp, setpar);
    }

    return FALSE;
}


/*}}}*/


/*{{{ Init & deinit */


void mod_sp_deinit()
{
    mod_sp_unregister_exports();
}


static void check_and_create()
{
    WMPlexIterTmp tmp;
    WScreen *scr;
    WRegion *reg;

    /* No longer needed, free the memory the list uses. */
    hook_remove(ioncore_post_layout_setup_hook, check_and_create);

    FOR_ALL_SCREENS(scr){
        FOR_ALL_MANAGED_BY_MPLEX((WMPlex*)scr, reg, tmp){
            if(is_scratchpad(reg))
                return;
        }

        create(&scr->mplex, MPLEX_ATTACH_HIDDEN);
    }
}


bool mod_sp_init()
{
    if(!mod_sp_register_exports())
        return FALSE;

    extl_read_config("cfg_sp", NULL, FALSE);

    if(ioncore_g.opmode==IONCORE_OPMODE_INIT){
        hook_add(ioncore_post_layout_setup_hook, check_and_create);
    }else{
        check_and_create();
    }

    return TRUE;
}


/*}}}*/