You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * i3 - an improved dynamic tiling window manager
  5. * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  6. *
  7. */
  8. #include "dpi.h"
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <xcb/xcb_xrm.h>
  13. #include "xcb.h"
  14. #include "i3lock.h"
  15. extern bool debug_mode;
  16. static long dpi;
  17. extern xcb_screen_t *screen;
  18. static long init_dpi_fallback(void) {
  19. return (double)screen->height_in_pixels * 25.4 / (double)screen->height_in_millimeters;
  20. }
  21. /*
  22. * Initialize the DPI setting.
  23. * This will use the 'Xft.dpi' X resource if available and fall back to
  24. * guessing the correct value otherwise.
  25. */
  26. void init_dpi(void) {
  27. xcb_xrm_database_t *database = NULL;
  28. char *resource = NULL;
  29. if (conn == NULL) {
  30. goto init_dpi_end;
  31. }
  32. database = xcb_xrm_database_from_default(conn);
  33. if (database == NULL) {
  34. DEBUG("Failed to open the resource database.\n");
  35. goto init_dpi_end;
  36. }
  37. xcb_xrm_resource_get_string(database, "Xft.dpi", NULL, &resource);
  38. if (resource == NULL) {
  39. DEBUG("Resource Xft.dpi not specified, skipping.\n");
  40. goto init_dpi_end;
  41. }
  42. char *endptr;
  43. double in_dpi = strtod(resource, &endptr);
  44. if (in_dpi == HUGE_VAL || dpi < 0 || *endptr != '\0' || endptr == resource) {
  45. DEBUG("Xft.dpi = %s is an invalid number and couldn't be parsed.\n", resource);
  46. dpi = 0;
  47. goto init_dpi_end;
  48. }
  49. dpi = (long)round(in_dpi);
  50. DEBUG("Found Xft.dpi = %ld.\n", dpi);
  51. init_dpi_end:
  52. if (resource != NULL) {
  53. free(resource);
  54. }
  55. if (database != NULL) {
  56. xcb_xrm_database_free(database);
  57. }
  58. if (dpi == 0) {
  59. DEBUG("Using fallback for calculating DPI.\n");
  60. dpi = init_dpi_fallback();
  61. DEBUG("Using dpi = %ld\n", dpi);
  62. }
  63. }
  64. /*
  65. * This function returns the value of the DPI setting.
  66. *
  67. */
  68. long get_dpi_value(void) {
  69. return dpi;
  70. }
  71. /*
  72. * Convert a logical amount of pixels (e.g. 2 pixels on a standard 96 DPI
  73. * screen) to a corresponding amount of physical pixels on a standard or retina
  74. * screen, e.g. 5 pixels on a 227 DPI MacBook Pro 13" Retina screen.
  75. *
  76. */
  77. int logical_px(const int logical) {
  78. if (screen == NULL) {
  79. /* Dpi info may not be available when parsing a config without an X
  80. * server, such as for config file validation. */
  81. return logical;
  82. }
  83. /* There are many misconfigurations out there, i.e. systems with screens
  84. * whose dpi is in fact higher than 96 dpi, but not significantly higher,
  85. * so software was never adapted. We could tell people to reconfigure their
  86. * systems to 96 dpi in order to get the behavior they expect/are used to,
  87. * but since we can easily detect this case in code, lets do it for them.
  88. */
  89. if ((dpi / 96.0) < 1.25)
  90. return logical;
  91. return ceil((dpi / 96.0) * logical);
  92. }