summaryrefslogtreecommitdiff
path: root/hw/xfree86/utils/cvt/cvt.c
blob: fff500b43facf1245ee4a0d629c606bee1ece249 (plain)
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
/*
 * Copyright 2005-2006 Luc Verhaegen.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/* Standalone VESA CVT standard timing modelines generator. */

#include "xf86.h"

/* Error implementation used by the server code we built in */
void
Error(const char *str)
{
    perror(str);
}

/* FatalError implementation used by the server code we built in */
void
FatalError(const char *f, ...)
{
    va_list args;
    va_start(args, f);
    vfprintf(stderr, f, args);
    va_end(args);
    exit(1);
}

/* xnfalloc implementation used by the server code we built in */
pointer
XNFalloc(unsigned long n)
{
    pointer r;

    r = malloc(n);
    if (!r) {
        perror("malloc failed");
        exit(1);
    }
    return r;
}

/* xnfcalloc implementation used by the server code we built in */
pointer
XNFcalloc(unsigned long n)
{
    pointer r;

    r = calloc(1, n);
    if (!r) {
        perror("calloc failed");
        exit(1);
    }
    return r;
}

/*
 * Quickly check wether this is a CVT standard mode.
 */
static Bool
CVTCheckStandard(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
                     Bool Verbose)
{
    Bool  IsCVT = TRUE;

    if ((!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay)) ||
        (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay)) ||
        (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay)) ||
        (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay)) ||
        (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay)))
        ;
    else {
        if (Verbose)
            fprintf(stderr, "Warning: Aspect Ratio is not CVT standard.\n");
        IsCVT = FALSE;
    }
    
    if ((VRefresh != 50.0) && (VRefresh != 60.0) &&
        (VRefresh != 75.0) && (VRefresh != 85.0)) {
        if (Verbose)
            fprintf(stderr, "Warning: Refresh Rate is not CVT standard "
                    "(50, 60, 75 or 85Hz).\n");
        IsCVT = FALSE;
    }
    
    return IsCVT;
}


/*
 * I'm not documenting --interlaced for obvious reasons, even though I did
 * implement it. I also can't deny having looked at gtf here.
 */
static void
PrintUsage(char *Name)
{
    fprintf(stderr, "\n");
    fprintf(stderr, "usage: %s [-v|--verbose] [-r|--reduced] X Y [refresh]\n",
            Name);
    fprintf(stderr, "\n");
    fprintf(stderr, " -v|--verbose : Warn about CVT standard adherance.\n");
    fprintf(stderr, " -r|--reduced : Create a mode with reduced blanking "
            "(default: normal blanking).\n");
    fprintf(stderr, "            X : Desired horizontal resolution "
            "(multiple of 8, required).\n");
    fprintf(stderr, "            Y : Desired vertical resolution (required).\n");
    fprintf(stderr, "      refresh : Desired refresh rate (default: 60.0Hz).\n");
    fprintf(stderr, "\n");

    fprintf(stderr, "Calculates VESA CVT (Coordinated Video Timing) modelines"
	    " for use with X.\n");
}


/*
 *
 */
static void
PrintComment(DisplayModeRec *Mode, Bool CVT, Bool Reduced)
{
    printf("# %dx%d %.2f Hz ", Mode->HDisplay, Mode->VDisplay, Mode->VRefresh);

    if (CVT) {
        printf("(CVT %.2fM",
               ((float) Mode->HDisplay * Mode->VDisplay) / 1000000.0);

        if (!(Mode->VDisplay % 3) &&
            ((Mode->VDisplay * 4 / 3) == Mode->HDisplay))
            printf("3");
        else if (!(Mode->VDisplay % 9) &&
                 ((Mode->VDisplay * 16 / 9) == Mode->HDisplay))
            printf("9");
        else if (!(Mode->VDisplay % 10) &&
                 ((Mode->VDisplay * 16 / 10) == Mode->HDisplay))
            printf("A");
        else if (!(Mode->VDisplay % 4) &&
                 ((Mode->VDisplay * 5 / 4) == Mode->HDisplay))
            printf("4");
        else if (!(Mode->VDisplay % 9) &&
                 ((Mode->VDisplay * 15 / 9) == Mode->HDisplay))
            printf("9");

        if (Reduced)
            printf("-R");

        printf(") ");
    } else
        printf("(CVT) ");

    printf("hsync: %.2f kHz; ", Mode->HSync);
    printf("pclk: %.2f MHz", ((float ) Mode->Clock) / 1000.0);

    printf("\n");
}


/*
 * Originally grabbed from xf86Mode.c.
 *
 * Ignoring the actual Mode->name, as the user will want something solid
 * to grab hold of.
 */
static void
PrintModeline(DisplayModePtr Mode, int HDisplay, int VDisplay, float VRefresh,
              Bool Reduced)
{
    if (Reduced)
        printf("Modeline \"%dx%dR\"  ", HDisplay, VDisplay);
    else
        printf("Modeline \"%dx%d_%.2f\"  ", HDisplay, VDisplay, VRefresh);

    printf("%6.2f  %i %i %i %i  %i %i %i %i", Mode->Clock/1000., Mode->HDisplay,
           Mode->HSyncStart, Mode->HSyncEnd, Mode->HTotal, Mode->VDisplay,
           Mode->VSyncStart, Mode->VSyncEnd, Mode->VTotal);

    if (Mode->Flags & V_INTERLACE)
        printf(" interlace");
    if (Mode->Flags & V_PHSYNC)
        printf(" +hsync");
    if (Mode->Flags & V_NHSYNC)
        printf(" -hsync");
    if (Mode->Flags & V_PVSYNC)
        printf(" +vsync");
    if (Mode->Flags & V_NVSYNC)
        printf(" -vsync");

    printf("\n");
}


/*
 *
 */
int
main (int argc, char *argv[])
{
    DisplayModeRec  *Mode;
    int  HDisplay = 0, VDisplay = 0;
    float  VRefresh = 0.0;
    Bool  Reduced = FALSE, Verbose = FALSE, IsCVT;
    Bool  Interlaced = FALSE;
    int  n;

    if ((argc < 3) || (argc > 7)) {
        PrintUsage(argv[0]);
        return 1;
    }

    /* This doesn't filter out bad flags properly. Bad flags get passed down
     * to atoi/atof, which then return 0, so that these variables can get
     * filled next time round. So this is just a cosmetic problem.
     */
    for (n = 1; n < argc; n++) {
        if (!strcmp(argv[n], "-r") || !strcmp(argv[n], "--reduced"))
            Reduced = TRUE;
        else if (!strcmp(argv[n], "-i") || !strcmp(argv[n], "--interlaced"))
            Interlaced = TRUE;
        else if (!strcmp(argv[n], "-v") || !strcmp(argv[n], "--verbose"))
            Verbose = TRUE;
        else if (!strcmp(argv[n], "-h") || !strcmp(argv[n], "--help")) {
            PrintUsage(argv[0]);
            return 0;
        } else if (!HDisplay) {
            HDisplay = atoi(argv[n]);
	    if (!HDisplay) {
		PrintUsage(argv[0]);
		return 1;
	    }
	}
        else if (!VDisplay) {
            VDisplay = atoi(argv[n]);
	    if (!VDisplay) {
		PrintUsage(argv[0]);
		return 1;
	    }
	}
        else if (!VRefresh) {
            VRefresh = atof(argv[n]);
	    if (!VRefresh) {
		PrintUsage(argv[0]);
		return 1;
	    }
	}
        else {
            PrintUsage(argv[0]);
            return 1;
        }
    }

    if (!HDisplay || !VDisplay) {
        PrintUsage(argv[0]);
        return 0;
    }

    /* Default to 60.0Hz */
    if (!VRefresh)
        VRefresh = 60.0;

    /* Horizontal timing is always a multiple of 8: round up. */
    if (HDisplay & 0x07) {
        HDisplay &= ~0x07;
        HDisplay += 8;
    }

    if (Reduced) {
	if ((VRefresh / 60.0) != floor(VRefresh / 60.0)) {
	    fprintf(stderr,
		    "\nERROR: Multiple of 60Hz refresh rate required for "
		    " reduced blanking.\n");
	    PrintUsage(argv[0]);
	    return 0;
	}
    }

    IsCVT = CVTCheckStandard(HDisplay, VDisplay, VRefresh, Reduced, Verbose);

    Mode = xf86CVTMode(HDisplay, VDisplay, VRefresh, Reduced, Interlaced);

    PrintComment(Mode, IsCVT, Reduced);
    PrintModeline(Mode, HDisplay, VDisplay, VRefresh, Reduced);
    
    return 0;
}