File: videogen.c

package info (click to toggle)
videogen 0.33-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 464 kB
  • sloc: ansic: 2,853; yacc: 853; sh: 124; lex: 118; makefile: 47
file content (321 lines) | stat: -rw-r--r-- 8,780 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
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
/*
 * +-------------------------------------------------------+
 * |                                                       |
 * |     videogen                                          |
 * |                                                       |
 * |     a simple XFree86 Modeline calculator              |
 * |     (c) 1997-2003, Szabolcs Rumi                      |
 * |                                                       |
 * |     http://www.dynaweb.hu/opensource/videogen         |
 * |                                                       |
 * |     the videogen package is distributed under the     |
 * |     GNU General Public License Version 2 (GPLv2)      |
 * |                                                       |
 * +-------------------------------------------------------+
 */





#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <math.h>

#include "config.h"
#include "videogen.h"





extern FILE *yyin;
extern int yyparse (void);
extern int yydebug;





char *cfgfile = CFG_CFGFILE;
unsigned int verbose = CFG_VERBOSE;
unsigned int fbset = CFG_FBSET;
unsigned int nvidia = CFG_NVIDIA;
unsigned int num_modes = 0;
resolution_t modes[256];
double max_dotclk = 0;
double max_hfreq = 0;
double max_vfreq = 0;
double desired_vfreq = 0;
double hvisible = CFG_HORIZ_VISIBLE;
double vvisible = CFG_VERT_VISIBLE;
double hfporch = CFG_HORIZ_FRONT_PORCH;
double hbporch = CFG_HORIZ_BACK_PORCH;
double hsync = CFG_HORIZ_SYNC_PULSE;
double vfporch = CFG_VERT_FRONT_PORCH;
double vbporch = CFG_VERT_BACK_PORCH;
double vsync = CFG_VERT_SYNC_PULSE;





void
banner (void)
{
	pmsg (VL_VERBOSE, "\n       +--------------------------------------------------------------+\n");
	pmsg (VL_VERBOSE, "       |                                                              |\n");
	pmsg (VL_VERBOSE, "       |     videogen %s     simple XFree86 Modeline calculator     |\n", CFG_VIDEOGEN_VERSION);
	pmsg (VL_VERBOSE, "       |                                                              |\n");
	pmsg (VL_VERBOSE, "       |              by Szabolcs Rumi, (c) 1997 - 2003               |\n");
	pmsg (VL_VERBOSE, "       |        THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY        |\n");
	pmsg (VL_VERBOSE, "       |    for details see the GNU General Public License (GPLv2)    |\n");
	pmsg (VL_VERBOSE, "       |                                                              |\n");
	pmsg (VL_VERBOSE, "       +--------------------------------------------------------------+\n\n");
}





void
usage (void)
{
	pmsg (VL_NORMAL, "USAGE: videogen [-h] [-v|-q] [-fb|-nfb] [-nv|-nnv] [-f=<file|->] [-m=<mode>]\n");
	pmsg (VL_NORMAL, "                [-mdc=<n>] [-mhf=<n>] [-mvf=<n>] [-dvf=<n>] [-hv=<n>] [-vv=<n>]\n");
	pmsg (VL_NORMAL, "                [-hfp=<n>] [-hbp=<n>] [-hsp=<n>] [-vfp=<n>] [-vbp=<n>] [-vsp=<n>]\n\n");
}





int
main (int argc, char **argv)
{
	char pathname_buf[4096];
	arg_parse_t ap = { 0, 0, 0, NULL, &opts };
	unsigned int i;
	double calc_dc, calc_hf, calc_vf;
	unsigned int calc_hfl, calc_vfl, calc_hfp, calc_hsp, calc_hbp, calc_vfp, calc_vsp, calc_vbp;



	/*
	 * reading command line parameters
	 */

	ap.argc = argc;
	ap.argv = argv;

	if (arg_parse (&ap, &arg_action) < 0)
	{
		pmsg (VL_NORMAL, "command line syntax error\n");
		usage();
		exit (ERR_CMDLINE);
	}



	/*
	 * reading config file
	 */

	if (strcmp (cfgfile, "-"))
	{
		yyin = fopen (tpathexp (cfgfile, (char *)&pathname_buf), "r");
		if (yyin == NULL)
		{
			pmsg (VL_NORMAL, "could not open configuration file \"%s\" (errno=%i)\n", cfgfile, errno);
		}
		else
		{
			pmsg (VL_VERBOSE, "reading configuration from file %s\n", cfgfile);
			if (yyparse () != 0)
				exit (ERR_CFGFILE);
		}
	}
	else
	{
		yyin = stdin;
		if (yyin == NULL)
		{
			pmsg (VL_DEBUG, "[main] stdin not open\n");
		}
		else
		{
			pmsg (VL_VERBOSE, "reading configuration from standard input\n");
			if (yyparse () != 0)
				exit (ERR_CFGFILE);
		}
	}



	/*
	 * print out some information about the program itself (in verbose mode only)
	 */

	banner ();



	/*
	 * check if the mandatory parameters are present
	 */

	if (num_modes == 0)
	{
		pmsg (VL_NORMAL, "error: no modes (resolutions) have been specified\n");
		exit (ERR_RES);
	}

	if (max_dotclk == 0)
	{
		pmsg (VL_NORMAL, "error: the maximum dot clock rate has not been specified\n");
		exit (ERR_MDC);
	}

	if (max_hfreq == 0)
	{
		pmsg (VL_NORMAL, "error: the maximum horizontal refresh frequency has not been specified\n");
		exit (ERR_MHF);
	}

	if (max_vfreq == 0)
	{
		pmsg (VL_NORMAL, "error: the maximum vertical refresh frequency has not been specified\n");
		exit (ERR_MVF);
	}

	if ((desired_vfreq == 0) || (desired_vfreq > max_vfreq))
		desired_vfreq = max_vfreq;



	/*
	 * do our main job
	 */

	for (i = 0; i < num_modes; i++)
	{
		calc_vf = desired_vfreq;
		calc_hfl = (unsigned int)floor (modes[i].hres * 100 / hvisible / 8) * 8;

		if ((nvidia > 0) && (calc_hfl - modes[i].hres > CFG_NV_MAX_HBLANK))
		{
			pmsg (VL_DEBUG, "[main] hblank: %u > %u\n", calc_hfl - modes[i].hres, CFG_NV_MAX_HBLANK);
			hvisible = 100 - ((100 - hvisible) * (CFG_NV_MAX_HBLANK / (calc_hfl - modes[i].hres)));
			calc_hfl = (unsigned int)floor (modes[i].hres * 100 / hvisible);
			calc_hfl = (unsigned int)floor (calc_hfl / 8) * 8;
		}

		calc_hfp = (unsigned int)floor ((calc_hfl - modes[i].hres) / 64) * 8;
		calc_hbp = (unsigned int)floor ((calc_hfl - modes[i].hres - calc_hfp) / 8) * 8;

		calc_hsp = floor (hsync * max_dotclk / 8) * 8;

		if ((nvidia > 0) && (calc_hsp > CFG_NV_MAX_HSP))
		{
			/* calc_hsp is assumed to be a multiple of 8 because all values it is
			 * calculated from are assumed to be multiples of 8.
			 */
			pmsg (VL_DEBUG, "[main] hsp: %u > %u\n", calc_hsp, CFG_NV_MAX_HSP);
			calc_hsp = CFG_NV_MAX_HSP;
		}

		calc_hbp -= calc_hsp;

		calc_vfl = (unsigned int)floor (modes[i].vres * 100 / vvisible + 0.5);

		if ((nvidia > 0) && (calc_vfl - modes[i].vres > CFG_NV_MAX_VBLANK))
		{
			pmsg (VL_DEBUG, "[main] vblank: %u > %u\n", calc_vfl - modes[i].vres, CFG_NV_MAX_VBLANK);
			vvisible = 100 - ((100 - vvisible) * (CFG_NV_MAX_VBLANK / (calc_vfl - modes[i].vres)));
			calc_vfl = (unsigned int)floor (modes[i].vres * 100 / vvisible);
		}

		calc_dc = calc_vf * calc_hfl * calc_vfl / 1000000;
		calc_hf = (1000 * calc_dc) / calc_hfl;

		calc_vfp = (unsigned int)vfporch;
		calc_vbp = calc_vfl - modes[i].vres - calc_vfp;

		calc_vsp = floor (vsync * calc_hf / 1000);

		if ((nvidia > 0) && (calc_vsp > CFG_NV_MAX_VSP))
		{
			/* framelength must be the same after the correction
			 */
			pmsg (VL_DEBUG, "[main] vsp: %u > %u\n", calc_vsp, CFG_NV_MAX_VSP);
			calc_vsp = CFG_NV_MAX_VSP;
		}

		calc_vbp -= calc_vsp;

		pmsg (VL_DEBUG, "[main] dump 1: visible=%ux%u frame=%ux%u\n", modes[i].hres, modes[i].vres, calc_hfl, calc_vfl);
		pmsg (VL_DEBUG, "[main] dump 1: dc=%f hf=%f vf=%f\n", calc_dc, calc_hf, calc_vf);
		pmsg (VL_DEBUG, "[main] dump 1: hsp=%u vsp=%u\n\n", calc_hsp, calc_vsp);

		if (calc_dc > max_dotclk)
		{
			pmsg (VL_DEBUG, "[main] dc: %f > %f\n", calc_dc, max_dotclk);
			calc_dc = max_dotclk;
			calc_vf = (1000000 * calc_dc) / (calc_hfl * calc_vfl);
		}

		calc_hf = (1000 * calc_dc) / calc_hfl;

		pmsg (VL_DEBUG, "[main] dump 2: visible=%ux%u frame=%ux%u\n", modes[i].hres, modes[i].vres, calc_hfl, calc_vfl);
		pmsg (VL_DEBUG, "[main] dump 2: dc=%f hf=%f vf=%f\n", calc_dc, calc_hf, calc_vf);
		pmsg (VL_DEBUG, "[main] dump 2: hsp=%u vsp=%u\n\n", calc_hsp, calc_vsp);

		if (calc_hf > max_hfreq)
		{
			pmsg (VL_DEBUG, "[main] hf: %f > %f\n", calc_hf, max_hfreq);
			calc_hf = max_hfreq;
			calc_dc = calc_hf * calc_hfl / 1000;
			calc_vf = (1000000 * calc_dc) / (calc_hfl * calc_vfl);
		}

		pmsg (VL_DEBUG, "[main] dump 3: visible=%ux%u frame=%ux%u\n", modes[i].hres, modes[i].vres, calc_hfl, calc_vfl);
		pmsg (VL_DEBUG, "[main] dump 3: dc=%f hf=%f vf=%f\n", calc_dc, calc_hf, calc_vf);
		pmsg (VL_DEBUG, "[main] dump 3: hsp=%u vsp=%u\n\n", calc_hsp, calc_vsp);

		/*
		 * printing out the results
		 */

		if (fbset == 0)
		{
			fprintf (stdout, "Modeline \"%ux%u\" %0.2f %u %u %u %u %u %u %u %u  # %0.0f MHz, %0.1f kHz, %0.1f Hz\n",
				modes[i].hres, modes[i].vres,
				calc_dc,
				modes[i].hres,
				modes[i].hres + calc_hfp,
				modes[i].hres + calc_hfp + calc_hsp,
				modes[i].hres + calc_hfp + calc_hsp + calc_hbp,
				modes[i].vres,
				modes[i].vres + calc_vfp,
				modes[i].vres + calc_vfp + calc_vsp,
				modes[i].vres + calc_vfp + calc_vsp + calc_vbp,
				calc_dc, calc_hf, calc_vf);
		}
		else
		{
			fprintf (stdout, "timings %0.0f %u %u %u %u %u %u  # %0.0f MHz, %0.1f kHz, %0.1f Hz\n",
				1000000 / calc_dc, calc_hbp, calc_hfp, calc_vbp, calc_vfp, calc_hsp, calc_vsp,
				calc_dc, calc_hf, calc_vf);
		}
	}

	return (0);
}





/* EOF */