/*
 * Parses /var/log/Xorg.0.log and returns the driver name and chip family
 *
 * Compile this as follows:
 *   gcc xorg_driver.c -o xorg_driver -lX11 -lXxf86misc -lXrandr
 *
 * Copyright (c) 2008  Bryce Harrington <bryce@canonical.com>
 *
 * 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.
 *
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/extensions/xf86misc.h>
#include <X11/extensions/Xrandr.h>

char *logfile_default = "/var/log/Xorg.0.log";
char *driver_line = "(II) Loading /usr/lib/xorg/modules/drivers/";
char *chipset_line = "(--) Chipset ";

#define MAXLEN (255)
char driver[MAXLEN];
char chipset[MAXLEN];
char logfile[MAXLEN];
XF86MiscFilePaths paths;

int parse_xorg_log(const char* logfile) {
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen(logfile, "r");
    if (fp == NULL) {
        printf("error:  Could not open %s\n", logfile);
        return 1;
    }

    while ((read = getline(&line, &len, fp)) != -1) {
        char *p;
        char *e;

        /* Looking for:  (II) Loading $DRIVER_PATH/*.*_drv.so */
        if (strncmp(line, driver_line, strlen(driver_line)) == 0) {
            p = line+strlen(driver_line);
            while (*p == '/') ++p;

            e = strstr(p, "_drv.");
            if (e != NULL) {
                *e = '\0';
            }

            strncpy(driver, p, MAXLEN);
        }

        /* Looking for:  (--) Chipset .* found */
        if (strncmp(line, chipset_line, strlen(chipset_line)) == 0) {
            p = line+strlen(chipset_line);
            while (isspace(*p)) ++p;

            e = strstr(p, " found");
            if (e != NULL) {
                *e = '\0';
            }

            strncpy(chipset, p, MAXLEN);
        }

    }
    if (line)
        free(line);

    fclose(fp);

    return 0;
}



int main(int argc, char *argv[]) {
    int dummy;
    int major, minor;
    Display *dpy;

    strncpy(driver, "unknown", MAXLEN);
    strncpy(chipset, "unknown", MAXLEN);
    strncpy(logfile, logfile_default, MAXLEN);

    dpy = XOpenDisplay(NULL);
    if (XF86MiscQueryExtension(dpy, &dummy, &dummy) &&
        XF86MiscQueryVersion(dpy, &major, &minor) &&
        ((major > 0) || (major == 0 && minor >= 7)) &&
        XF86MiscGetFilePaths(dpy, &paths)) {
        printf("Config file:  %s\n", paths.configfile);
        printf("Modules path: %s\n", paths.modulepath);
        printf("Log file:     %s\n", paths.logfile);
        strncpy(logfile, paths.logfile, MAXLEN);
    }

    if (parse_xorg_log(logfile) == 0) {
        printf("Chipset:      %s\n", chipset);
        printf("Driver:       %s\n", driver);
    }

    if (XRRQueryExtension(dpy, &dummy, &dummy) &&
        XRRQueryVersion (dpy, &major, &minor)) {
        printf("Xrandr:       %d.%d\n", major, minor);
    }

    return 0;
}
