Initial commit
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / utils / cvt / cvt.c
blobfee4f7c3e508395288fdf8a82d48862e9c7d4d67
1 /*
2 * Copyright 2005-2006 Luc Verhaegen.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 /* Standalone VESA CVT standard timing modelines generator. */
26 #include "xf86.h"
29 * Quickly check wether this is a CVT standard mode.
31 static Bool
32 CVTCheckStandard(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
33 Bool Verbose)
35 Bool IsCVT = TRUE;
37 if ((!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay)) ||
38 (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay)) ||
39 (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay)) ||
40 (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay)) ||
41 (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay)))
43 else {
44 if (Verbose)
45 fprintf(stderr, "Warning: Aspect Ratio is not CVT standard.\n");
46 IsCVT = FALSE;
49 if ((VRefresh != 50.0) && (VRefresh != 60.0) &&
50 (VRefresh != 75.0) && (VRefresh != 85.0)) {
51 if (Verbose)
52 fprintf(stderr, "Warning: Refresh Rate is not CVT standard "
53 "(50, 60, 75 or 85Hz).\n");
54 IsCVT = FALSE;
57 return IsCVT;
62 * I'm not documenting --interlaced for obvious reasons, even though I did
63 * implement it. I also can't deny having looked at gtf here.
65 static void
66 PrintUsage(char *Name)
68 fprintf(stderr, "\n");
69 fprintf(stderr, "usage: %s [-v|--verbose] [-r|--reduced] X Y [refresh]\n",
70 Name);
71 fprintf(stderr, "\n");
72 fprintf(stderr, " -v|--verbose : Warn about CVT standard adherance.\n");
73 fprintf(stderr, " -r|--reduced : Create a mode with reduced blanking "
74 "(default: normal blanking).\n");
75 fprintf(stderr, " X : Desired horizontal resolution "
76 "(multiple of 8, required).\n");
77 fprintf(stderr, " Y : Desired vertical resolution (required).\n");
78 fprintf(stderr, " refresh : Desired refresh rate (default: 60.0Hz).\n");
79 fprintf(stderr, "\n");
81 fprintf(stderr, "Calculates VESA CVT (Coordinated Video Timing) modelines"
82 " for use with X.\n");
89 static void
90 PrintComment(DisplayModeRec *Mode, Bool CVT, Bool Reduced)
92 printf("# %dx%d %.2f Hz ", Mode->HDisplay, Mode->VDisplay, Mode->VRefresh);
94 if (CVT) {
95 printf("(CVT %.2fM",
96 ((float) Mode->HDisplay * Mode->VDisplay) / 1000000.0);
98 if (!(Mode->VDisplay % 3) &&
99 ((Mode->VDisplay * 4 / 3) == Mode->HDisplay))
100 printf("3");
101 else if (!(Mode->VDisplay % 9) &&
102 ((Mode->VDisplay * 16 / 9) == Mode->HDisplay))
103 printf("9");
104 else if (!(Mode->VDisplay % 10) &&
105 ((Mode->VDisplay * 16 / 10) == Mode->HDisplay))
106 printf("A");
107 else if (!(Mode->VDisplay % 4) &&
108 ((Mode->VDisplay * 5 / 4) == Mode->HDisplay))
109 printf("4");
110 else if (!(Mode->VDisplay % 9) &&
111 ((Mode->VDisplay * 15 / 9) == Mode->HDisplay))
112 printf("9");
114 if (Reduced)
115 printf("-R");
117 printf(") ");
118 } else
119 printf("(CVT) ");
121 printf("hsync: %.2f kHz; ", Mode->HSync);
122 printf("pclk: %.2f MHz", ((float ) Mode->Clock) / 1000.0);
124 printf("\n");
129 * Originally grabbed from xf86Mode.c.
131 * Ignoring the actual Mode->name, as the user will want something solid
132 * to grab hold of.
134 static void
135 PrintModeline(DisplayModePtr Mode, int HDisplay, int VDisplay, float VRefresh,
136 Bool Reduced)
138 if (Reduced)
139 printf("Modeline \"%dx%dR\" ", HDisplay, VDisplay);
140 else
141 printf("Modeline \"%dx%d_%.2f\" ", HDisplay, VDisplay, VRefresh);
143 printf("%6.2f %i %i %i %i %i %i %i %i", Mode->Clock/1000., Mode->HDisplay,
144 Mode->HSyncStart, Mode->HSyncEnd, Mode->HTotal, Mode->VDisplay,
145 Mode->VSyncStart, Mode->VSyncEnd, Mode->VTotal);
147 if (Mode->Flags & V_INTERLACE)
148 printf(" interlace");
149 if (Mode->Flags & V_PHSYNC)
150 printf(" +hsync");
151 if (Mode->Flags & V_NHSYNC)
152 printf(" -hsync");
153 if (Mode->Flags & V_PVSYNC)
154 printf(" +vsync");
155 if (Mode->Flags & V_NVSYNC)
156 printf(" -vsync");
158 printf("\n");
166 main (int argc, char *argv[])
168 DisplayModeRec *Mode;
169 int HDisplay = 0, VDisplay = 0;
170 float VRefresh = 0.0;
171 Bool Reduced = FALSE, Verbose = FALSE, IsCVT;
172 Bool Interlaced = FALSE;
173 int n;
175 if ((argc < 3) || (argc > 7)) {
176 PrintUsage(argv[0]);
177 return 0;
180 /* This doesn't filter out bad flags properly. Bad flags get passed down
181 * to atoi/atof, which then return 0, so that these variables can get
182 * filled next time round. So this is just a cosmetic problem.
184 for (n = 1; n < argc; n++) {
185 if (!strcmp(argv[n], "-r") || !strcmp(argv[n], "--reduced"))
186 Reduced = TRUE;
187 else if (!strcmp(argv[n], "-i") || !strcmp(argv[n], "--interlaced"))
188 Interlaced = TRUE;
189 else if (!strcmp(argv[n], "-v") || !strcmp(argv[n], "--verbose"))
190 Verbose = TRUE;
191 else if (!strcmp(argv[n], "-h") || !strcmp(argv[n], "--help")) {
192 PrintUsage(argv[0]);
193 return 0;
194 } else if (!HDisplay)
195 HDisplay = atoi(argv[n]);
196 else if (!VDisplay)
197 VDisplay = atoi(argv[n]);
198 else if (!VRefresh)
199 VRefresh = atof(argv[n]);
200 else {
201 PrintUsage(argv[0]);
202 return 0;
206 if (!HDisplay || !VDisplay) {
207 PrintUsage(argv[0]);
208 return 0;
211 /* Default to 60.0Hz */
212 if (!VRefresh)
213 VRefresh = 60.0;
215 /* Horizontal timing is always a multiple of 8: round up. */
216 if (HDisplay & 0x07) {
217 HDisplay &= ~0x07;
218 HDisplay += 8;
221 if (Reduced && (VRefresh != 60.0)) {
222 fprintf(stderr, "\nERROR: 60Hz refresh rate required for reduced"
223 " blanking.\n");
224 PrintUsage(argv[0]);
225 return 0;
228 IsCVT = CVTCheckStandard(HDisplay, VDisplay, VRefresh, Reduced, Verbose);
230 Mode = xf86CVTMode(HDisplay, VDisplay, VRefresh, Reduced, Interlaced);
232 PrintComment(Mode, IsCVT, Reduced);
233 PrintModeline(Mode, HDisplay, VDisplay, VRefresh, Reduced);
235 return 0;