diff options
author | Leo (Sunpeng) Li <sunpeng.li@amd.com> | 2018-03-27 15:02:52 -0400 |
---|---|---|
committer | Leo (Sunpeng) Li <sunpeng.li@amd.com> | 2018-05-02 10:13:31 -0400 |
commit | 26d8404fb3ea871be9488d31a2e5da0f67df1488 (patch) | |
tree | 8ffcb29e644e51aac3d97a720b0dee5f29d3864c | |
parent | 675b5bd3debd2806d4b4ee3f6c58a7591bb404b5 (diff) |
Open X display via X libraries
And also get screen resources using libxrandr library. Use it to open a
hard-coded DisplayPort-0 output for now.
Add headers and update makefile.
Signed-off-by: Leo (Sunpeng) Li <sunpeng.li@amd.com>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | demo.c | 68 |
2 files changed, 58 insertions, 12 deletions
@@ -2,7 +2,7 @@ CFLAGS=-g -Wall CC=gcc LDFLAGS=$(shell pkg-config --cflags libdrm) -LDLIBS = $(shell pkg-config --libs libdrm) -lm +LDLIBS = $(shell pkg-config --libs libdrm x11 xrandr) -lm # All sources SOURCES=demo.c @@ -33,6 +33,9 @@ #include <xf86drm.h> #include <xf86drmMode.h> +#include <X11/Xlib.h> +#include <X11/extensions/Xrandr.h> + #define LUT_SIZE 4096 @@ -200,6 +203,36 @@ static void load_table(struct color3d *coeffs, int lut_size, double *exps) } /** + * Find the output on the RandR screen resource by name. + * + * dpy: The X display + * res: The RandR screen resource + * name: The output name to search for. + * + * Return: The RROutput X-id if found, 0 (None) otherwise. + */ +static RROutput find_output_by_name(Display *dpy, XRRScreenResources *res, + const char *name) +{ + int i; + RROutput ret; + XRROutputInfo *output_info; + + for (i = 0; i < res->noutput; i++) { + ret = res->outputs[i]; + output_info = XRRGetOutputInfo (dpy, res, ret); + + if (!strcmp(name, output_info->name)) { + XRRFreeOutputInfo(output_info); + return ret; + } + + XRRFreeOutputInfo(output_info); + } + return 0; +} + +/** * Create a DRM color LUT blob using the given coefficients, and set the * output's CRTC to use it. Since setting degamma and regamma follows similar * procedures, a flag is used to determine which one is set. Also note the @@ -560,10 +593,13 @@ int main(int argc, char *const argv[]) struct color3d regamma_coeffs[LUT_SIZE]; int drm_fd; - int ret; + int ret = 0; + + Display *dpy; + Window root; + XRRScreenResources *res; + RROutput output; - char cmd_enable_color_mgmt[] = - "xrandr --output DisplayPort-0 --set use_color_mgmt 1"; /* * Parse arguments @@ -617,27 +653,37 @@ int main(int argc, char *const argv[]) return -1; } - /* Ensure non-legacy color management is enabled in xrandr */ - printf("# %s\n", cmd_enable_color_mgmt); - system(cmd_enable_color_mgmt); + /* Open the default display and window*/ + dpy = XOpenDisplay(NULL); + root = DefaultRootWindow(dpy); + res = XRRGetScreenResourcesCurrent(dpy, root); + + output = find_output_by_name(dpy, res, "DisplayPort-0"); + if (!output) { + printf("Cannot find output!\n"); + goto done; + } if (degamma_changed) { ret = set_gamma(drm_fd, degamma_coeffs, degamma_is_srgb, 1); if (ret) - return ret; + goto done; } if (ctm_changed) { ret = set_ctm(drm_fd, ctm_coeffs); if (ret) - return ret; + goto done; } if (regamma_changed) { ret = set_gamma(drm_fd, regamma_coeffs, regamma_is_srgb, 0); if (ret) - return ret; + goto done; } +done: + XRRFreeScreenResources(res); + XCloseDisplay(dpy); close(drm_fd); - printf("Done!\n"); - return 0; + + return ret; } |