148 printf(
"this executable doesn't do anything for non-X11 systems currently\n");
153 printf(
"this executable was built with colord support enabled\n");
155 printf(
"this executable was built without colord support\n");
159 printf(
"ansel itself was built with colord support enabled\n");
161 printf(
"ansel itself was built without colord support\n");
167 GList *monitor_list = NULL;
168 const char *disp_name_env;
172 if((disp_name_env = g_getenv(
"DISPLAY")) != NULL)
175 g_strlcpy(disp_name, disp_name_env,
sizeof(disp_name));
176 if((pp = g_strrstr(disp_name,
":")) != NULL)
178 if((pp = g_strstr_len(pp, -1,
".")) == NULL)
179 g_strlcat(disp_name,
".0",
sizeof(disp_name));
182 g_strlcat(disp_name,
"0",
sizeof(disp_name));
192 g_strlcpy(disp_name,
":0.0",
sizeof(disp_name));
194 Display *display = XOpenDisplay(disp_name);
197 fprintf(stderr,
"can't open display `%s'\n", XDisplayName(disp_name));
201 int max_screen = ScreenCount(display);
202 for(
int screen = 0; screen < max_screen; ++screen)
206 Window root = RootWindow(display, screen);
207 XRRScreenResources *rsrc = XRRGetScreenResources(display, root);
210 XID primary = XRRGetOutputPrimary(display, root);
211 gboolean have_primary =
FALSE;
215 for(
int crtc = 0; crtc < rsrc->ncrtc; crtc++)
217 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(display, rsrc, rsrc->crtcs[crtc]);
221 if(crtc_info->mode != None && crtc_info->noutput > 0)
223 for(
int output = 0; output < crtc_info->noutput; output++)
225 if(crtc_info->outputs[output] == primary)
233 XRRFreeCrtcInfo(crtc_info);
236 if (primary_id == -1)
237 printf(
"couldn't locate primary CRTC!\n");
240 printf(
"primary CRTC is at CRTC %d\n", primary_id);
247 for(
int crtc = 0; crtc < rsrc->ncrtc; ++crtc)
249 XRROutputInfo *output_info = NULL;
250 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(display, rsrc, rsrc->crtcs[crtc]);
253 printf(
"can't get CRTC info for screen %d CRTC %d\n", screen, crtc);
257 if(crtc_info->mode == None || crtc_info->noutput <= 0)
259 printf(
"CRTC for screen %d CRTC %d has no mode or no output, skipping\n", screen, crtc);
265 gboolean is_primary =
FALSE;
269 for(
int j = 0; j < crtc_info->noutput; j++)
271 if(crtc_info->outputs[j] == primary)
280 output_info = XRRGetOutputInfo(display, rsrc, crtc_info->outputs[output]);
283 printf(
"can't get output info for screen %d CRTC %d output %d\n", screen, crtc, output);
287 if(output_info->connection == RR_Disconnected)
289 printf(
"screen %d CRTC %d output %d is disconnected, skipping\n", screen, crtc, output);
293 monitor_t *monitor = (monitor_t *)calloc(1,
sizeof(monitor_t));
296 Atom edid_atom = XInternAtom(display,
"EDID", False), actual_type;
298 unsigned long nitems, bytes_after;
300 int res = XRRGetOutputProperty(display, rsrc->outputs[output], edid_atom, 0, G_MAXLONG,
FALSE,
301 FALSE, XA_INTEGER, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
302 if(
res == Success && actual_type == XA_INTEGER && actual_format == 8 && nitems != 0)
304 printf(
"EDID for %s has size %lu\n", output_info->name, nitems);
311 monitor->root = root;
312 monitor->screen = screen;
313 monitor->crtc = crtc;
314 monitor->is_primary = is_primary;
315 monitor->atom_id = atom_id++;
316 monitor->name = g_strdup(output_info->name);
317 monitor_list = g_list_prepend(monitor_list, monitor);
320 XRRFreeCrtcInfo(crtc_info);
321 XRRFreeOutputInfo(output_info);
324 XRRFreeScreenResources(rsrc);
328 monitor_list = g_list_sort(monitor_list, sort_monitor_list);
330 int last_screen = -1;
331 for(GList *iter = monitor_list; iter; iter = g_list_next(iter))
333 monitor_t *monitor = (monitor_t *)iter->data;
334 if(monitor->screen != last_screen) atom_id = 0;
335 last_screen = monitor->screen;
336 monitor->atom_id = atom_id++;
340 for(GList *iter = monitor_list; iter; iter = g_list_next(iter))
342 monitor_t *monitor = (monitor_t *)iter->data;
343 if(monitor->atom_id == 0)
344 monitor->x_atom_name = g_strdup(
"_ICC_PROFILE");
346 monitor->x_atom_name = g_strdup_printf(
"_ICC_PROFILE_%d", monitor->atom_id);
348 Atom atom = XInternAtom(display, monitor->x_atom_name,
FALSE), actual_type;
350 unsigned long nitems, bytes_after;
353 int res = XGetWindowProperty(display, monitor->root, atom, 0, G_MAXLONG,
FALSE, XA_CARDINAL, &actual_type,
354 &actual_format, &nitems, &bytes_after, &prop);
356 if(
res == Success && actual_type == XA_CARDINAL && actual_format == 8)
358 monitor->x_atom_length = nitems;
359 monitor->x_atom_data = prop;
368 CdClient *client = cd_client_new();
369 if(
IS_NULL_PTR(client) || !cd_client_connect_sync(client, NULL, NULL))
371 fprintf(stderr,
"error connecting to colord\n");
374 for(GList *iter = monitor_list; iter; iter = g_list_next(iter))
376 monitor_t *monitor = (monitor_t *)iter->data;
378 CdDevice *device = cd_client_find_device_by_property_sync(client, CD_DEVICE_METADATA_XRANDR_NAME,
379 monitor->name, NULL, NULL);
380 if(device && cd_device_connect_sync(device, NULL, NULL))
382 CdProfile *profile = cd_device_get_default_profile(device);
385 if(cd_profile_connect_sync(profile, NULL, NULL))
387 CdIcc *icc = cd_profile_load_icc(profile, CD_ICC_LOAD_FLAGS_FALLBACK_MD5, NULL, NULL);
390 monitor->colord_filename = g_strdup(cd_icc_get_filename(icc));
394 g_object_unref(profile);
397 if(device) g_object_unref(device);
399 if(client) g_object_unref(client);
404 gboolean any_profile_mismatch =
FALSE, any_unprofiled_monitor =
FALSE;
405 for(GList *iter = monitor_list; iter; iter = g_list_next(iter))
407 monitor_t *monitor = (monitor_t *)iter->data;
408 char *message = NULL;
410 char *monitor_name = monitor->name ? monitor->name :
"(unknown)";
411 char *x_atom_name = monitor->x_atom_name ? monitor->x_atom_name :
"(not found)";
412 char *tmp = get_profile_description(monitor->x_atom_data, monitor->x_atom_length);
413 char *x_atom_description = tmp ? tmp : g_strdup(
"(none)");
416 if(monitor->x_atom_length == 0)
418 message =
"the X atom seems to be missing";
419 any_unprofiled_monitor =
TRUE;
422 char *colord_filename = monitor->colord_filename ? monitor->colord_filename :
"(none)",
425 || g_file_test(monitor->colord_filename, G_FILE_TEST_IS_REGULAR) ==
FALSE)
427 colord_description = g_strdup(
"(file not found)");
428 if(monitor->x_atom_length > 0)
430 any_profile_mismatch =
TRUE;
431 message =
"the X atom and colord returned different profiles";
435 any_unprofiled_monitor =
TRUE;
436 message =
"the X atom and colord returned the same profile";
441 unsigned char *tmp_data = NULL;
443 g_file_get_contents(monitor->colord_filename, (gchar **)&tmp_data, &
size, NULL);
444 gboolean profiles_equal = (
size == monitor->x_atom_length
445 && (
size == 0 || memcmp(monitor->x_atom_data, tmp_data,
size) == 0));
446 if(!profiles_equal) any_profile_mismatch =
TRUE;
447 if(
size == 0 && monitor->x_atom_length == 0) any_unprofiled_monitor =
TRUE;
448 message = profiles_equal ?
"the X atom and colord returned the same profile"
449 :
"the X atom and colord returned different profiles";
450 tmp = get_profile_description(tmp_data,
size);
451 colord_description = tmp ? tmp : g_strdup(
"(none)");
458 printf(
"\n%s", monitor_name);
459 if(message) printf(
"\t%s", message);
460 printf(
"\n\tX atom:\t%s (%" G_GSIZE_FORMAT
" bytes)\n\t\tdescription: %s\n", x_atom_name, monitor->x_atom_length,
463 printf(
"\tcolord:\t\"%s\"\n\t\tdescription: %s\n", colord_filename, colord_description);
475 if(any_profile_mismatch || any_unprofiled_monitor)
477 printf(
"\nBetter check your system setup\n");
478 if(any_profile_mismatch) printf(
" - some monitors reported different profiles\n");
479 if(any_unprofiled_monitor) printf(
" - some monitors lacked a profile\n");
480 printf(
"You may experience inconsistent color rendition between color managed applications\n");
483 printf(
"\nYour system seems to be correctly configured\n");
487 XCloseDisplay(display);
488 for(GList *iter = monitor_list; iter; iter = g_list_next(iter))
490 monitor_t *monitor = (monitor_t *)iter->data;
493 XFree(monitor->x_atom_data);
495 dt_free(monitor->colord_filename);