#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//#include <string.h>
#include <X11/Xlib.h>

#define XRANDR 1

#ifdef XRANDR
#include <X11/extensions/Xrandr.h>
#endif

/*
void Usage(void){
	printf("screen-dpi [width pixels] [height pixels] [width mm] [height mm]\n");
}
*/

int main(int argc, char **argv){
/*
	Display *dpy;
	char *displayname = NULL;
	int scr = 0; // Screen number
	double xres, yres;
	double width_dots, height_dots, width_physical, height_physical;
*/

#ifdef XRANDR
	Display *dpy;
	char *displayname = NULL;
	int scr = 0; // Screen number
	int i = 0;
     double xres, yres;
	double xin, yin;
     int ndepths = 0, *depths = NULL;
     unsigned int width, height;
    int event_base, error_base;
    int major, minor;
    XRRScreenResources *res = NULL;
    XRROutputInfo *output;
    XRRCrtcInfo *crtc;

	dpy = XOpenDisplay(displayname);
     printf ("\n");
     printf ("screen #%d:\n", scr);
#ifdef XRANDR
    if (XRRQueryExtension (dpy, &event_base, &error_base) &&
        XRRQueryVersion (dpy, &major, &minor) &&
        (major >= 1 || (major == 1 && minor >= 2)) &&
        (res = XRRGetScreenResources (dpy, RootWindow (dpy, scr))))
    {
        for (i = 0; i < res->noutput; ++i) {
            output = XRRGetOutputInfo (dpy, res, res->outputs[i]);
            if (!output || !output->crtc || output->connection != RR_Connected)
                continue;

            crtc = XRRGetCrtcInfo (dpy, res, output->crtc);
            if (!crtc) {
                XRRFreeOutputInfo (output);
                continue;
            }

            printf ("  output: %s\n", output->name);
            printf ("    dimensions:    %ux%u pixels (%lux%lu millimeters)\n",
                    crtc->width, crtc->height, output->mm_width, output->mm_height);

            xres = ((((double) crtc->width) * 25.4) / ((double) output->mm_width));
            yres = ((((double) crtc->height) * 25.4) / ((double) output->mm_height));
xin = ((((double) output->mm_width)) / ((double) crtc->width * 25.4));
yin = ((((double) output->mm_height)) / ((double) crtc->height * 25.4));
//            printf ("    resolution:    %dx%d dots per inch\n",
  //                  (int) (xres + 0.5), (int) (yres + 0.5));
		printf("  resolution:    %lfx%lf dots per inch\n", xres, yres);
		printf("  dot dimens:    %lfx%lf inch per dot\n", xin, yin);

            XRRFreeCrtcInfo (crtc);
            XRRFreeOutputInfo (output);
        }
        XRRFreeScreenResources (res);
    }
    else
#endif
    {
        printf ("  dimensions:    %dx%d pixels (%dx%d millimeters)\n",
                DisplayWidth (dpy, scr),  DisplayHeight (dpy, scr),
                DisplayWidthMM(dpy, scr), DisplayHeightMM (dpy, scr));

        /*
         * there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
         *
         *     dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
         *         = N pixels / (M inch / 25.4)
         *         = N * 25.4 pixels / M inch
         */
        xres = ((((double) DisplayWidth(dpy,scr)) * 25.4) /
                ((double) DisplayWidthMM(dpy,scr)));
        yres = ((((double) DisplayHeight(dpy,scr)) * 25.4) /
                ((double) DisplayHeightMM(dpy,scr)));
xin = ((((double) output->mm_width)) / ((double) crtc->width * 25.4));
yin = ((((double) output->mm_height)) / ((double) crtc->height * 25.4));

//        printf ("  resolution:    %dx%d dots per inch\n",
//                (int) (xres + 0.5), (int) (yres + 0.5));
	printf ("  resolution:    %lfx%lf dots per inch\n", xres, yres);
	printf("  dot dimens:    %lfx%lf inch per dot\n", xin, yin);
    }

     depths = XListDepths (dpy, scr, &ndepths);
     if (!depths) ndepths = 0;
     printf ("  depths (%d):    \n", ndepths);
#endif
/*
	dpy = XOpenDisplay(displayname);
	width_dots = DisplayWidth(dpy,scr);
	height_dots = DisplayHeight(dpy,scr);
	width_physical = DisplayWidthMM(dpy,scr);
	height_physical = DisplayHeightMM(dpy,scr);
*/
//xres = ((((double) DisplayWidth(dpy,scr)) * 25.4) / 
//        ((double) DisplayWidthMM(dpy,scr)));

//yres = ((((double) DisplayHeight(dpy,scr)) * 25.4) / 
//        ((double) DisplayHeightMM(dpy,scr)));

//*x = (int) (xres + 0.5);
//    *y = (int) (yres + 0.5);

//    XCloseDisplay (dpy);

	//double 
//	if(argc != 5){
//		Usage();
//		exit(1);
//	}

//	width_dots = atof((const char*)argv[1]);
//	height_dots = atof((const char*)argv[2]);
//	width_physical = atof((const char*)argv[3]);
//	height_physical = atof((const char*)argv[4]);

/*
	printf("Dimensions: %lf pixels x %lf pixels\n", width_dots, height_dots);
	printf("Dimensions: %lf mm x %lf mm\n", width_physical, height_physical);
	width_physical /= 25.4;
	height_physical /= 25.4;
	printf("Dimensions: %lf Inches x %lf Inches\n", width_physical, height_physical);
	printf("Resolution DPI: %lf Horizontal %lf Vertical\n", width_dots/width_physical, height_dots/height_physical);
*/

	XCloseDisplay (dpy);
	return 0;
}
