File: init.c

package info (click to toggle)
ion2 20040729-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,828 kB
  • ctags: 3,466
  • sloc: ansic: 28,432; makefile: 473; sh: 230; perl: 16
file content (318 lines) | stat: -rw-r--r-- 7,066 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
/*
 * ion/de/init.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/global.h>
#include <ioncore/rootwin.h>
#include <ioncore/extl.h>
#include <ioncore/extlconv.h>
#include <ioncore/ioncore.h>
#include <ioncore/readconfig.h>

#include "brush.h"
#include "font.h"
#include "colour.h"
#include "misc.h"
#include "init.h"


/*{{{ Borders */


#define CF_BORDER_VAL_SANITY_CHECK 16

void de_get_border_val(uint *val, ExtlTab tab, const char *what)
{
    int g;
    
    if(extl_table_gets_i(tab, what, &g)){
        if(g>CF_BORDER_VAL_SANITY_CHECK || g<0)
            warn("Border attribute %s sanity check failed.", what);
        else
            *val=g;
    }
}


void de_get_border_style(uint *ret, ExtlTab tab)
{
    char *style=NULL;
    
    if(!extl_table_gets_s(tab, "border_style", &style))
        return;
    
    if(strcmp(style, "inlaid")==0)
        *ret=DEBORDER_INLAID;
    else if(strcmp(style, "elevated")==0)
        *ret=DEBORDER_ELEVATED;
    else if(strcmp(style, "groove")==0)
        *ret=DEBORDER_GROOVE;
    else if(strcmp(style, "ridge")==0)
        *ret=DEBORDER_RIDGE;
    else
        warn("Unknown border style \"%s\".", style);
    
    free(style);
}


void de_get_border(DEBorder *border, ExtlTab tab)
{
    de_get_border_val(&(border->sh), tab, "shadow_pixels");
    de_get_border_val(&(border->hl), tab, "highlight_pixels");
    de_get_border_val(&(border->pad), tab, "padding_pixels");
    de_get_border_style(&(border->style), tab);
}


/*}}}*/


/*{{{ Colours */


bool de_get_colour(WRootWin *rootwin, DEColour *ret, 
                   ExtlTab tab, const char *what, DEColour substitute)
{
    char *name=NULL;
    bool ok=FALSE;
    
    if(extl_table_gets_s(tab, what, &name)){
        ok=de_alloc_colour(rootwin, ret, name);
    
        if(!ok)
            warn("Unable to allocate colour \"%s\".", name);

        free(name);
    }
    
    if(!ok)
        return de_duplicate_colour(rootwin, substitute, ret);
    
    return ok;
}


void de_get_colour_group(WRootWin *rootwin, DEColourGroup *cg, 
                         ExtlTab tab)
{
    de_get_colour(rootwin, &(cg->hl), tab, "highlight_colour",
                  DE_WHITE(rootwin));
    de_get_colour(rootwin, &(cg->sh), tab, "shadow_colour",
                  DE_WHITE(rootwin));
    de_get_colour(rootwin, &(cg->bg), tab, "background_colour",
                  DE_BLACK(rootwin));
    de_get_colour(rootwin, &(cg->fg), tab, "foreground_colour",
                  DE_WHITE(rootwin));
    de_get_colour(rootwin, &(cg->pad), tab, "padding_colour", cg->bg);
}


void de_get_extra_cgrps(WRootWin *rootwin, DEStyle *style, ExtlTab tab)
{
    
    uint i=0, nfailed=0, n=extl_table_get_n(tab);
    char *name;
    ExtlTab sub;
    
    if(n==0)
        return;
    
    style->extra_cgrps=ALLOC_N(DEColourGroup, n);
    
    if(style->extra_cgrps==NULL){
        warn_err();
        return;
    }

    for(i=0; i<n-nfailed; i++){
        if(!extl_table_geti_t(tab, i+1, &sub))
            goto err;
        if(!extl_table_gets_s(sub, "substyle_pattern", &name)){
            extl_unref_table(sub);
            goto err;
        }
        
        /*de_init_colour_group(rootwin, style->extra_cgrps+i-nfailed);*/
        style->extra_cgrps[i-nfailed].spec=name;
        de_get_colour_group(rootwin, style->extra_cgrps+i-nfailed, sub);
        
        extl_unref_table(sub);
        continue;
        
    err:
        warn("Corrupt substyle table %d.", i);
        nfailed++;
    }
    
    if(n-nfailed==0){
        free(style->extra_cgrps);
        style->extra_cgrps=NULL;
    }
    
    style->n_extra_cgrps=n-nfailed;
}


/*}}}*/


/*{{{ Misc. */


void de_get_text_align(int *alignret, ExtlTab tab)
{
    char *align=NULL;
    
    if(!extl_table_gets_s(tab, "text_align", &align))
        return;
    
    if(strcmp(align, "left")==0)
        *alignret=DEALIGN_LEFT;
    else if(strcmp(align, "right")==0)
        *alignret=DEALIGN_RIGHT;
    else if(strcmp(align, "center")==0)
        *alignret=DEALIGN_CENTER;
    else
        warn("Unknown text alignment \"%s\".", align);
    
    free(align);
}


void de_get_transparent_background(uint *mode, ExtlTab tab)
{
    if(extl_table_is_bool_set(tab, "transparent_background"))
        *mode=GR_TRANSPARENCY_YES;
    else
        *mode=GR_TRANSPARENCY_NO;
}


/*}}}*/


/*{{{ de_define_style */


void de_get_nonfont(WRootWin *rootwin, DEStyle *style, ExtlTab tab)
{
    style->data_table=extl_ref_table(tab);
    
    de_get_border(&(style->border), tab);
    de_get_border_val(&(style->spacing), tab, "spacing");

    de_get_text_align(&(style->textalign), tab);

    de_get_transparent_background(&(style->transparency_mode), tab);

    style->cgrp_alloced=TRUE;
    de_get_colour_group(rootwin, &(style->cgrp), tab);
    de_get_extra_cgrps(rootwin, style, tab);
}


/*EXTL_DOC
 * Define a style for the root window \var{rootwin}. Use
 * \fnref{de_define_style} instead to define for all root windows.
 */
EXTL_EXPORT
bool de_do_define_style(WRootWin *rootwin, const char *name, ExtlTab tab)
{
    DEStyle *style;
    char *fnt;
    uint n;

    if(name==NULL)
        return FALSE;
    
    style=de_create_style(rootwin, name);

    if(style==NULL)
        return FALSE;

    de_get_nonfont(rootwin, style, tab);

    if(extl_table_gets_s(tab, "font", &fnt)){
        de_load_font_for_style(style, fnt);
        free(fnt);
    }else{
        de_load_font_for_style(style, CF_FALLBACK_FONT_NAME);
    }
    
    return TRUE;
}

    
/*}}}*/


/*{{{ Module initialisation */


#include "../version.h"

char de_module_ion_api_version[]=ION_API_VERSION;


extern bool de_module_register_exports();
extern void de_module_unregister_exports();


bool de_module_init()
{
    WRootWin *rootwin;
    DEStyle *style;
    
    if(!de_module_register_exports())
        return FALSE;
    
    if(!read_config("delib"))
        goto fail;
    
    if(!gr_register_engine("de", (GrGetBrushFn*)&de_get_brush,
                           (GrGetValuesFn*)&de_get_brush_values)){
        warn("DE module", "Failed to register the drawing engine");
        goto fail;
    }
    
    /* Create fallback brushes */
    FOR_ALL_ROOTWINS(rootwin){
        style=de_create_style(rootwin, "*");
        if(style==NULL){
            warn_obj("DE module", "Could not initialise fallback style for "
                     "root window %d.\n", rootwin->xscr);
        }else{
            style->is_fallback=TRUE;
            de_load_font_for_style(style, CF_FALLBACK_FONT_NAME);
        }
    }
    
    return TRUE;
    
fail:
    de_module_unregister_exports();
    return FALSE;
}


void de_module_deinit()
{
    gr_unregister_engine("de");
    de_module_unregister_exports();
    de_deinit_styles();
}


/*}}}*/