File: parse_options.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (284 lines) | stat: -rw-r--r-- 9,657 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
/* Copyright (C) 2000-2006 Damir Zucic */

/*=============================================================================

				parse_options.c

Purpose:
	Parse options which require connection to X server: geometry string,
	font name,  cursor name and  color specifications.  These  arguments
	were available before, but connection to X server was required prior
	to parsing.

Input:
	(1) Pointer to GUIS structure, where data will be stored.
	(2) Pointer to ConfigS structure, with configuration data.

Output:
	(1) Some data stored to GUIS structure.
	(2) Return value.

Return value:
	(1) Positive on success.
	(2) Negative on failure.

========includes:============================================================*/

#include <stdio.h>

#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include <X11/cursorfont.h>

#include "defines.h"
#include "typedefs.h"

/*======function prototypes:=================================================*/

void		ErrorMessage_ (char *, char *, char *,
			       char *, char *, char *, char *);
FILE		*CreateLogFile_ (char *);
int		LoadFont_ (WindowS *, Display *, char *);
Cursor		PrepareCursor_ (GUIS *, char *);
int		ParseColor_ (RGBS *, unsigned long *, GUIS *, char *, char *);

/*======parse the remaining options:=========================================*/

int ParseOptions_ (GUIS *guiSP, ConfigS *configSP)
{
size_t		rgb_struct_size;
int		flags;
int		x0, y0;
unsigned int	wx, wy;
RGBS		rgbS;
int		n;
unsigned long	dummyID;

/* The size of RGBS structure: */
rgb_struct_size = sizeof (RGBS);

/* Create (open) the log file, if requested: */
if (configSP->log_fileF)
	{
	configSP->log_fileP = CreateLogFile_ (configSP->log_file_nameA);
	if (configSP->log_fileP == NULL)
		{
		ErrorMessage_ ("garlic", "ParseArgs_",
				configSP->log_file_nameA,
				"Unable to create the log file!\n",
				"", "", "");
		configSP->log_fileF = 0;
		return -1;
		}
	}

/* Main window geometry: */

/** If default geometry should be used: **/
if (strstr (configSP->geometryA, "default"))
	{
	guiSP->main_winS.x0     = configSP->main_margin_left;
	guiSP->main_winS.y0     = configSP->main_margin_top;
	n = (int) guiSP->screen_width - (int) configSP->main_margin_left -
	    (int) configSP->main_margin_right;
	if (n >= 0) guiSP->main_winS.width  = (unsigned int) n;
	n = (int) guiSP->screen_height - (int) configSP->main_margin_top -
	    (int) configSP->main_margin_bottom;
	if (n >= 0) guiSP->main_winS.height = (int) n;
	}

/** If geometry string was specified through **/
/** .garlicrc  file or through command line: **/
else
	{
	/*** Parse geometry string: ***/
	flags = XParseGeometry (configSP->geometryA, &x0, &y0, &wx, &wy);

	/*** Main window width: ***/
	if (WidthValue & flags) guiSP->main_winS.width = wx;

	/*** Main window height: ***/
	if (HeightValue & flags) guiSP->main_winS.height = wy;

	/*** Main window x offset: ***/
	if (XValue & flags)
		{
		if ((XNegative & flags) == 0) guiSP->main_winS.x0 = x0;
		else guiSP->main_winS.x0 = guiSP->screen_width -
					   guiSP->main_winS.width + x0;
		}

	/*** Main window y offset: ***/
	if (YValue & flags)
        	{
        	if ((YNegative & flags) == 0) guiSP->main_winS.y0 = y0;
        	else guiSP->main_winS.y0 = guiSP->screen_height -
					   guiSP->main_winS.height + y0;
        	}
	}

/* Main window background color: */
ParseColor_ (&guiSP->main_winS.bg_rgbS, &guiSP->main_winS.bg_colorID,
	     guiSP, configSP->bg_colorA, "black");

/* Main window foreground color: */
ParseColor_ (&guiSP->main_winS.fg_rgbS, &guiSP->main_winS.fg_colorID,
	     guiSP, configSP->fg_colorA, "white");

/* Text background color: */
ParseColor_ (&guiSP->input_winS.bg_rgbS, &guiSP->input_winS.bg_colorID,
	     guiSP, configSP->text_bg_colorA, "black");

/** Don't forget the output window, it uses the same background color: **/
memcpy (&guiSP->output_winS.bg_rgbS,
	&guiSP->input_winS.bg_rgbS, rgb_struct_size);
guiSP->output_winS.bg_colorID = guiSP->input_winS.bg_colorID;

/* Text foreground color: */
ParseColor_ (&guiSP->input_winS.fg_rgbS, &guiSP->input_winS.fg_colorID,
             guiSP, configSP->text_fg_colorA, "white");

/** The same color is used for output window and control window: **/
memcpy (&guiSP->output_winS.fg_rgbS,
	&guiSP->input_winS.fg_rgbS, rgb_struct_size);
guiSP->output_winS.fg_colorID  = guiSP->input_winS.fg_colorID;
guiSP->control_winS.fg_colorID = guiSP->input_winS.fg_colorID;

/* Default colors for atoms and bonds: */
for (n = 0; n < MAXCOLORSURFACES; n++)
	{
	/** Left color: **/
	ParseColor_ (&rgbS, &dummyID,
		     guiSP, configSP->left_colorAA[n], "white");
	configSP->left_rgbSA[n].red   = rgbS.red;
	configSP->left_rgbSA[n].green = rgbS.green;
	configSP->left_rgbSA[n].blue  = rgbS.blue;

	/** Middle color: **/
	ParseColor_ (&rgbS, &dummyID,
		     guiSP, configSP->middle_colorAA[n], "white");
	configSP->middle_rgbSA[n].red   = rgbS.red;
	configSP->middle_rgbSA[n].green = rgbS.green;
	configSP->middle_rgbSA[n].blue  = rgbS.blue;

	/** Right color: **/
	ParseColor_ (&rgbS, &dummyID,
		     guiSP, configSP->right_colorAA[n], "black");
	configSP->right_rgbSA[n].red    = rgbS.red;
	configSP->right_rgbSA[n].green  = rgbS.green;
	configSP->right_rgbSA[n].blue   = rgbS.blue;
	}

/* Prepare main font: */

/** Load font: **/
n = LoadFont_ (&guiSP->main_winS, guiSP->displaySP, configSP->font_nameA);
if (n < 0)
	{
	ErrorMessage_ ("garlic", "ParseArgs_", "",
		"Failed to load font (after five attempts), exiting ...\n",
		"", "", "");
	return -2;
	}

/** Set this font to all GC's: **/
XSetFont(guiSP->displaySP, guiSP->theGCA[0], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[1], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[2], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[3], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[4], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[5], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[6], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[7], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[8], guiSP->main_winS.fontSP->fid);
XSetFont(guiSP->displaySP, guiSP->theGCA[9], guiSP->main_winS.fontSP->fid);

/** Initialize font heights: **/
n = guiSP->main_winS.fontSP->ascent + guiSP->main_winS.fontSP->descent;

/*** Main window: ***/
guiSP->main_winS.quarter_font_height   = (n + 3) / 4;
guiSP->main_winS.half_font_height      = (n + 1) / 2;
guiSP->main_winS.font_height           = n;
guiSP->main_winS.text_line_height      = guiSP->main_winS.font_height +
					 guiSP->main_winS.quarter_font_height;

/*** Output window: ***/
guiSP->output_winS.quarter_font_height = guiSP->main_winS.quarter_font_height;
guiSP->output_winS.half_font_height    = guiSP->main_winS.half_font_height;
guiSP->output_winS.font_height         = guiSP->main_winS.font_height;
guiSP->output_winS.text_line_height    = guiSP->main_winS.text_line_height;

/*** Input window: ***/
guiSP->input_winS.quarter_font_height  = guiSP->main_winS.quarter_font_height;
guiSP->input_winS.half_font_height     = guiSP->main_winS.half_font_height;
guiSP->input_winS.font_height          = guiSP->main_winS.font_height;
guiSP->input_winS.text_line_height     = guiSP->main_winS.text_line_height;

/* Prepare main window cursor (hope it will work properly): */
guiSP->main_winS.cursorID = PrepareCursor_ (guiSP, configSP->cursor_nameA);
guiSP->main_winS.cursorF = 1;

/* Control window cursor: */
guiSP->control_winS.cursorID = XCreateFontCursor (guiSP->displaySP, XC_hand2);
guiSP->control_winS.cursorF = 1;

/* Input window cursor: */
guiSP->input_winS.cursorID = XCreateFontCursor (guiSP->displaySP, XC_pencil);
guiSP->input_winS.cursorF = 1;

/* Output window cursor: */
guiSP->output_winS.cursorID = XCreateFontCursor (guiSP->displaySP,
						 XC_X_cursor);
guiSP->output_winS.cursorF = 1;

/* Prepare eighteen auxiliary colors: */
ParseColor_ (&rgbS, &guiSP->black_colorID,
	     guiSP, "black", "black");
ParseColor_ (&rgbS, &guiSP->gray_colorID,
	     guiSP, "gray", "white");
ParseColor_ (&rgbS, &guiSP->dark_gray_colorID,
	     guiSP, "RGB:8888/8888/8888", "white");
ParseColor_ (&rgbS, &guiSP->white_colorID,
	     guiSP, "white", "white");
ParseColor_ (&rgbS, &guiSP->red_colorID,
	     guiSP, "red", "white");
ParseColor_ (&rgbS, &guiSP->dark_red_colorID,
	     guiSP, "RGB:8888/0000/0000", "black");
ParseColor_ (&rgbS, &guiSP->green_colorID,
	     guiSP, "green", "white");
ParseColor_ (&rgbS, &guiSP->dark_green_colorID,
	     guiSP, "RGB:0000/8888/0000", "black");
ParseColor_ (&rgbS, &guiSP->blue_colorID,
	     guiSP, "blue", "white");
ParseColor_ (&rgbS, &guiSP->dark_blue_colorID,
	     guiSP, "RGB:0000/0000/8888", "black");
ParseColor_ (&rgbS, &guiSP->yellow_colorID,
	     guiSP, "yellow", "white");
ParseColor_ (&rgbS, &guiSP->dark_yellow_colorID,
	     guiSP, "RGB:8888/8888/0000", "black");
ParseColor_ (&rgbS, &guiSP->cyan_colorID,
	     guiSP, "cyan", "white");
ParseColor_ (&rgbS, &guiSP->dark_cyan_colorID,
	     guiSP, "RGB:0000/8888/8888", "black");
ParseColor_ (&rgbS, &guiSP->magenta_colorID,
	     guiSP, "magenta", "white");
ParseColor_ (&rgbS, &guiSP->dark_magenta_colorID,
	     guiSP, "RGB:8888/0000/8888", "black");
ParseColor_ (&rgbS, &guiSP->orange_colorID,
	     guiSP, "orange", "white");
ParseColor_ (&rgbS, &guiSP->dark_orange_colorID,
	     guiSP, "RGB:8888/4444/0000", "black");

/* Return positive value on success: */
return 1;
}

/*===========================================================================*/