main_drm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * UI on the panel through the s1d13700-tca DRM driver: one dumb buffer, with
  3. * each change reported as a dirty rectangle.
  4. */
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/mman.h>
  11. #include <unistd.h>
  12. #include <xf86drm.h>
  13. #include <xf86drmMode.h>
  14. #include "platform_linux.h"
  15. #define DRIVER_NAME "s1d13700-tca"
  16. struct drm_dev {
  17. int fd;
  18. uint32_t fb_id, pitch;
  19. uint8_t *map;
  20. const char *backlight;
  21. };
  22. /* Only ever our own device: never take over the machine's real display */
  23. static int open_panel(void)
  24. {
  25. char path[32];
  26. int i, fd;
  27. for (i = 0; i < 16; i++) {
  28. drmVersionPtr v;
  29. bool match;
  30. snprintf(path, sizeof(path), "/dev/dri/card%d", i);
  31. fd = open(path, O_RDWR | O_CLOEXEC);
  32. if (fd < 0)
  33. continue;
  34. v = drmGetVersion(fd);
  35. match = v && !strcmp(v->name, DRIVER_NAME);
  36. drmFreeVersion(v);
  37. if (match)
  38. return fd;
  39. close(fd);
  40. }
  41. return -1;
  42. }
  43. static int setup_output(struct drm_dev *d, drmModeModeInfo *mode)
  44. {
  45. drmModeRes *res = drmModeGetResources(d->fd);
  46. drmModeConnector *conn;
  47. uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
  48. uint32_t handle, pitch, crtc;
  49. uint64_t size, offset;
  50. int ret = -1;
  51. if (!res || res->count_connectors < 1 || res->count_crtcs < 1)
  52. goto out_res;
  53. conn = drmModeGetConnector(d->fd, res->connectors[0]);
  54. if (!conn || conn->count_modes < 1)
  55. goto out_conn;
  56. *mode = conn->modes[0];
  57. crtc = res->crtcs[0];
  58. if (drmModeCreateDumbBuffer(d->fd, mode->hdisplay, mode->vdisplay, 32, 0,
  59. &handle, &pitch, &size))
  60. goto out_conn;
  61. handles[0] = handle;
  62. pitches[0] = pitch;
  63. if (drmModeAddFB2(d->fd, mode->hdisplay, mode->vdisplay,
  64. 0x34325258 /* XR24 */, handles, pitches, offsets, &d->fb_id, 0))
  65. goto out_conn;
  66. if (drmModeMapDumbBuffer(d->fd, handle, &offset))
  67. goto out_conn;
  68. d->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, d->fd, (off_t)offset);
  69. if (d->map == MAP_FAILED)
  70. goto out_conn;
  71. memset(d->map, 0xff, size);
  72. d->pitch = pitch;
  73. if (drmModeSetCrtc(d->fd, crtc, d->fb_id, 0, 0, &conn->connector_id, 1, mode))
  74. goto out_conn;
  75. ret = 0;
  76. out_conn:
  77. drmModeFreeConnector(conn);
  78. out_res:
  79. drmModeFreeResources(res);
  80. return ret;
  81. }
  82. static void present(void *ctx, const uint32_t *pixels, int stride, int x, int y, int w, int h)
  83. {
  84. struct drm_dev *d = ctx;
  85. drmModeClip clip = { (uint16_t)x, (uint16_t)y, (uint16_t)(x + w), (uint16_t)(y + h) };
  86. int row;
  87. for (row = y; row < y + h; row++)
  88. memcpy(d->map + (size_t)row * d->pitch + (size_t)x * 4,
  89. (const char *)pixels + row * stride + x * 4, (size_t)w * 4);
  90. if (drmModeDirtyFB(d->fd, d->fb_id, &clip, 1))
  91. g_printerr("DIRTYFB failed: %s\n", strerror(errno));
  92. }
  93. static void backlight(void *ctx, bool on)
  94. {
  95. platform_backlight_set(((struct drm_dev *)ctx)->backlight, on);
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. static struct drm_dev d;
  100. static int port = 6600;
  101. static char *host, *input, *blpath;
  102. static gboolean kmsg;
  103. static GOptionEntry entries[] = {
  104. { "host", 0, 0, G_OPTION_ARG_STRING, &host, "MPD host (localhost)", "NAME" },
  105. { "port", 0, 0, G_OPTION_ARG_INT, &port, "MPD port (6600)", "N" },
  106. { "input", 0, 0, G_OPTION_ARG_FILENAME, &input, "Key input device (auto: " DEFAULT_KEYS_NAME ")", "PATH" },
  107. { "backlight", 0, 0, G_OPTION_ARG_FILENAME, &blpath, "Backlight sysfs brightness file", "PATH" },
  108. { "kmsg", 0, 0, G_OPTION_ARG_NONE, &kmsg, "Show the kernel log", NULL },
  109. { NULL, 0, 0, 0, NULL, NULL, NULL },
  110. };
  111. GOptionContext *ctx = g_option_context_new("- radio panel over DRM");
  112. struct app_frontend fe = { &d, present, backlight };
  113. struct app_options opt;
  114. drmModeModeInfo mode;
  115. GError *err = NULL;
  116. struct app *app;
  117. g_option_context_add_main_entries(ctx, entries, NULL);
  118. if (!g_option_context_parse(ctx, &argc, &argv, &err)) {
  119. g_printerr("%s\n", err->message);
  120. return 1;
  121. }
  122. d.backlight = blpath ? blpath : DEFAULT_BACKLIGHT;
  123. d.fd = open_panel();
  124. if (d.fd < 0) {
  125. g_printerr("no %s DRM device found\n", DRIVER_NAME);
  126. return 1;
  127. }
  128. if (setup_output(&d, &mode)) {
  129. g_printerr("cannot set up the display: %s\n", strerror(errno));
  130. return 1;
  131. }
  132. opt = (struct app_options){ host ? host : "localhost", port,
  133. mode.hdisplay, mode.vdisplay, kmsg };
  134. app = app_new(&opt, &fe);
  135. if (!platform_keys_start(app, input))
  136. g_printerr("no key input device, running display-only\n");
  137. platform_run();
  138. app_free(app);
  139. return 0;
  140. }