File: xinit.c

package info (click to toggle)
xzip 1%3A1.8.2-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 656 kB
  • ctags: 1,556
  • sloc: ansic: 11,942; makefile: 171; sh: 11
file content (312 lines) | stat: -rw-r--r-- 7,068 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <ctype.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "ztypes.h"
#include "xio.h"
#include "arguments.h"

#define DEFAULTGEOMETRY "500x600+100+100"
#define DEFAULTSTATGEOMETRY "80x24+100+50"

static char *defaultfonts[NUMFONTS] = {
    FND0, FND0,
    FND1, FND1,
    FND2, FND2,
    FND3, FND3,
    FND4, FND4,
    FND5, FND5,
    FND6, FND6,
    FND7, FND7
};

static char *attrnames[NUMFONTS] = {
    "n",
    "r",
    "b",
    "rb",
    "i",
    "ri",
    "bi",
    "rbi",
    "f",
    "rf",
    "bf",
    "rbf",
    "if",
    "rif",
    "bif",
    "rbif"
};

#ifdef __STDC__
static char *findoption(int key);
static int str_to_attr(char *cx);
static int str_to_bool(char *cx);
#else
static char *findoption();
static int str_to_attr();
static int str_to_bool();
#endif

#ifdef __STDC__
void xinit_defaultprefs()
#else
void xinit_defaultprefs()
#endif
{
    int ix;
    strictz_report_mode = STRICTZ_DEFAULT_REPORT_MODE;
    strictz_declare_spec = FALSE;
    prefs.leading = 3;
    prefs.marginx = 4;
    prefs.marginy = 4;
    prefs.inputattr = BOLD;
    prefs.historylength = 20;
    prefs.paging = FALSE;
    prefs.fulljustify = TRUE;
    prefs.buffersize = 4000;
    prefs.bufferslack = 1000;
    for (ix=0; ix<NUMFONTS; ix++)
	prefs.font[ix] = NULL;
    prefs.statwid = 80;
    prefs.stathgt = 24;
    prefs.autoresize = TRUE;
    prefs.resizeupward = FALSE;
    prefs.autoclear = TRUE;
    /* can't set up colors yet */

    xkey_init();
}

/* set xiodpy, xioscn, xiodepth, fontstr[] */
#ifdef __STDC__
void xinit_openconnection()
#else
void xinit_openconnection()
#endif
{
    int ix;
    int direction;
    int ascent, descent;
    XCharStruct overall;
    XColor col, realcol;
    Colormap colormap;
    char *cx;

    xiodpy = XOpenDisplay(NULL);
    if (!xiodpy) {
	fprintf(stderr, "%s: could not open display.\n", PROGRAMNAME);
	exit(-1);
    }

    /* init all the X stuff */
    xioscn = DefaultScreen(xiodpy);
    xiodepth = DefaultDepth(xiodpy, xioscn);
    if (DoesBackingStore(ScreenOfDisplay(xiodpy, xioscn)) == NotUseful)
	xiobackstore = FALSE;
    else
	xiobackstore = TRUE;
    colormap = DefaultColormap(xiodpy, xioscn);

    xinit_defaultprefs(); /* ### should be before argv[] parsing? */
    prefs.forecolor = BlackPixel(xiodpy, xioscn);
    prefs.backcolor = WhitePixel(xiodpy, xioscn);
    for (ix=0; ix<NUMFONTS; ix++) {
	prefs.textcolor[ix] = prefs.forecolor;
    }

    /* suck in the preferences */
    cx = findoption(OPT_BINDINGS);
    if (cx) {
	xkey_parse_bindings(cx);
    }
    cx = findoption(OPT_GEOMETRY);
    if (!cx) {
	cx = DEFAULTGEOMETRY;
    }
    XGeometry(xiodpy, xioscn, cx, DEFAULTGEOMETRY, 1, 1, 1, 0, 0, &prefs.winx, &prefs.winy, &prefs.winw, &prefs.winh);
    cx = findoption(OPT_STATGEOMETRY);
    if (!cx) {
	cx = DEFAULTSTATGEOMETRY;
    }
    XGeometry(xiodpy, xioscn, cx, DEFAULTSTATGEOMETRY, 1, 1, 1, 0, 0, &prefs.statwinx, &prefs.statwiny, &prefs.statwid, &prefs.stathgt);
    cx = findoption(OPT_STRICTZ);
    if (cx) {
        ix = atoi(cx);
        if (ix >= STRICTZ_REPORT_NEVER && ix <= STRICTZ_REPORT_FATAL)
	    strictz_report_mode = ix;
    }
    cx = findoption(OPT_SPEC);
    if (cx)
	strictz_declare_spec = str_to_bool(cx);
    cx = findoption(OPT_INPUTSTYLE);
    if (cx)
	prefs.inputattr = str_to_attr(cx);
    cx = findoption(OPT_MARGINX);
    if (cx)
	prefs.marginx = atoi(cx);
    cx = findoption(OPT_LEADING);
    if (cx)
	prefs.leading = atoi(cx);
    cx = findoption(OPT_JUSTIFY);
    if (cx)
	prefs.fulljustify = str_to_bool(cx);
    cx = findoption(OPT_AUTORESIZE);
    if (cx)
	prefs.autoresize = str_to_bool(cx);
    cx = findoption(OPT_RESIZEUPWARD);
    if (cx)
	prefs.resizeupward = str_to_bool(cx);
    cx = findoption(OPT_AUTOCLEAR);
    if (cx)
	prefs.autoclear = str_to_bool(cx);
    cx = findoption(OPT_HISTORY);
    if (cx)
	prefs.historylength = atoi(cx);
    cx = findoption(OPT_BUFFER);
    if (cx)
	prefs.buffersize = atol(cx);

    cx = findoption(OPT_BACKGROUND);
    if (cx
	&& XAllocNamedColor(xiodpy, colormap, cx, &col, &realcol)) {
	prefs.backcolor = col.pixel;
    }
    cx = findoption(OPT_FOREGROUND);
    if (cx
	&& XAllocNamedColor(xiodpy, colormap, cx, &col, &realcol)) {
	prefs.forecolor = col.pixel;
	for (ix=0; ix<NUMFONTS; ix++) 
	    prefs.textcolor[ix] = col.pixel;
    }
    cx = findoption(OPT_GREYCOLOR);
    if (!cx)
	cx = "grey60";
    if (cx
	&& XAllocNamedColor(xiodpy, colormap, cx, &col, &realcol)) {
	prefs.greycolor = col.pixel;
    }
    else {
	prefs.greycolor = prefs.forecolor;
    }
    if (xiodepth > 1) {
	for (ix=0; ix<NUMFONTS; ix++) {
	    cx = findoption(OPT_STYLECOLOR+ix);
	    if (cx
		&& XAllocNamedColor(xiodpy, colormap, cx, &col, &realcol)) {
		prefs.textcolor[ix] = col.pixel;
	    }
	}
    }
    for (ix=0; ix<NUMFONTS; ix++) {
	cx = findoption(OPT_STYLEFONT+ix);
	if (cx) {
	    prefs.font[ix] = cx;
	}
    }

    for (ix=0; ix<NUMFONTS; ix++) {
	if (!(ix & REVERSE)) {
	    fontstr[ix] = NULL;
	    if (prefs.font[ix])
		fontstr[ix] = XLoadQueryFont(xiodpy, prefs.font[ix]);
	    if (!fontstr[ix]) {
		if (prefs.font[ix]) {
		    fprintf(stderr, "%s: unable to load font <%s>, defaulting to <%s>...\n", PROGRAMNAME, prefs.font[ix], defaultfonts[ix]);
		}
		fontstr[ix] = XLoadQueryFont(xiodpy, defaultfonts[ix]);
		if (!fontstr[ix]) {
		    fprintf(stderr, "%s: fatal error: unable to load font <%s>\n", PROGRAMNAME, defaultfonts[ix]);
		    exit(1);
		}
	    }
	}
	else
	    fontstr[ix] = fontstr[ix & (~REVERSE)];
    }

    /* figure out font foo */
    for (ix=0; ix<NUMFONTS; ix++) {
	XTextExtents(fontstr[ix], " ", 1, &direction, &ascent, &descent, &overall);
	spacewidth[ix] = overall.width;
	if (fontstr[ix]->ascent + fontstr[ix]->descent > lineheight)
	    lineheight = fontstr[ix]->ascent + fontstr[ix]->descent;
	if (fontstr[ix]->ascent > lineheightoff)
	    lineheightoff = fontstr[ix]->ascent;
    }
    lineheight += prefs.leading;

    /* ensure all cut buffers exist */
    for (ix=0; ix<8; ix++) {
	int size;
	char *cx = XFetchBuffer(xiodpy, &size, ix);
	if (!cx) {
	    XStoreBuffer(xiodpy, "#", sizeof(char), ix);
	}
	else {
	    free(cx);
	}
    }

}

#ifdef __STDC__
static char *findoption(int key)
#else
static char *findoption(key)
int key;
#endif
{
    char *cx;
    unixoption *opt;

    opt = (&unixoptionlist[key]);
    if (!opt->name) {
	fprintf(stderr, "%s: internal option error on key %d\n", PROGRAMNAME, key);
	exit(1);
    }

    if (opt->val)
	cx = opt->val;
    else
	cx = XGetDefault(xiodpy, PROGRAMNAME, opt->name);

    return cx;
}

#ifdef __STDC__
static int str_to_attr(char *cx)
#else
static int str_to_attr(cx)
char *cx;
#endif
{
    int ix;
    for (ix=0; ix<NUMFONTS; ix++) {
	if (!strcmp(attrnames[ix], cx))
	    return ix;
    }
    return 0;
}

#ifdef __STDC__
static int str_to_bool(char *cx)
#else
static int str_to_bool(cx)
char *cx;
#endif
{
    if (*cx=='y' || *cx=='Y' || *cx=='t' || *cx=='T')
	return TRUE;

    if (*cx=='o' || *cx=='O') {
	cx++;
	if (*cx=='n' || *cx=='N')
	    return TRUE;
    }

    return FALSE;
}