/*

$Id: xvidmode.c,v 1.3 2002/03/01 16:05:03 roderick Exp $

Roderick Schertler <roderick@argon.org>

*/

/*

Copyright (C) 2002 Roderick Schertler

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

For a copy of the GNU General Public License write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*/

/*

My intention is for this program to allow switching among all your video
modes from the command line.  So far it only switches to the default
mode.  I bind it to a contraol-alt-keypad-enter so I can more easily get
back to the right resolution if a game leaves me in the wrong one.

I don't really know anything about X programming.

*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/xf86vmode.h>

int
main(int argc, char *argv[])
{
    Display *dpy;
    int dotclock, modecount;
    XF86VidModeModeLine ml;
    XF86VidModeModeInfo **all_info;

    char *arg0;

    arg0 = strrchr(argv[0], '/');
    if (arg0)
	arg0++;
    else
	arg0 = argv[0];

    if (argc != 1) {
	fprintf(stderr, "usage: %s\n", arg0);
	exit(1);
    }

     /* XXX When one of the functions fails, how do you get a
        description of the problem? */

    dpy = XOpenDisplay(NULL);
    if (!dpy) {
	fprintf(stderr, "%s: cannot connect to display\n", arg0);
	exit(1);
    }

    if (!XF86VidModeGetModeLine(dpy, DefaultScreen(dpy), &dotclock, &ml)) {
	fprintf(stderr, "%s: XF86VidModeGetModeLine() failed\n", arg0);
	exit(1);
    }

    if (!XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy),
				    &modecount, &all_info)) {
	fprintf(stderr, "%s: XF86VidModeGetAllModeLines() failed\n", arg0);
	exit(1);
    }

    if (!XF86VidModeSwitchToMode(dpy, DefaultScreen(dpy), all_info[0])) {
	fprintf(stderr, "%s: XF86VidModeSwitchToMode() failed\n", arg0);
	exit(1);
    }

    XCloseDisplay(dpy);

    return 0;
}
