| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- * UI on the panel through the s1d13700-tca DRM driver: one dumb buffer, with
- * each change reported as a dirty rectangle.
- */
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <xf86drm.h>
- #include <xf86drmMode.h>
- #include "platform_linux.h"
- #define DRIVER_NAME "s1d13700-tca"
- struct drm_dev {
- int fd;
- uint32_t fb_id, pitch;
- uint8_t *map;
- const char *backlight;
- };
- /* Only ever our own device: never take over the machine's real display */
- static int open_panel(void)
- {
- char path[32];
- int i, fd;
- for (i = 0; i < 16; i++) {
- drmVersionPtr v;
- bool match;
- snprintf(path, sizeof(path), "/dev/dri/card%d", i);
- fd = open(path, O_RDWR | O_CLOEXEC);
- if (fd < 0)
- continue;
- v = drmGetVersion(fd);
- match = v && !strcmp(v->name, DRIVER_NAME);
- drmFreeVersion(v);
- if (match)
- return fd;
- close(fd);
- }
- return -1;
- }
- static int setup_output(struct drm_dev *d, drmModeModeInfo *mode)
- {
- drmModeRes *res = drmModeGetResources(d->fd);
- drmModeConnector *conn;
- uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
- uint32_t handle, pitch, crtc;
- uint64_t size, offset;
- int ret = -1;
- if (!res || res->count_connectors < 1 || res->count_crtcs < 1)
- goto out_res;
- conn = drmModeGetConnector(d->fd, res->connectors[0]);
- if (!conn || conn->count_modes < 1)
- goto out_conn;
- *mode = conn->modes[0];
- crtc = res->crtcs[0];
- if (drmModeCreateDumbBuffer(d->fd, mode->hdisplay, mode->vdisplay, 32, 0,
- &handle, &pitch, &size))
- goto out_conn;
- handles[0] = handle;
- pitches[0] = pitch;
- if (drmModeAddFB2(d->fd, mode->hdisplay, mode->vdisplay,
- 0x34325258 /* XR24 */, handles, pitches, offsets, &d->fb_id, 0))
- goto out_conn;
- if (drmModeMapDumbBuffer(d->fd, handle, &offset))
- goto out_conn;
- d->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, d->fd, (off_t)offset);
- if (d->map == MAP_FAILED)
- goto out_conn;
- memset(d->map, 0xff, size);
- d->pitch = pitch;
- if (drmModeSetCrtc(d->fd, crtc, d->fb_id, 0, 0, &conn->connector_id, 1, mode))
- goto out_conn;
- ret = 0;
- out_conn:
- drmModeFreeConnector(conn);
- out_res:
- drmModeFreeResources(res);
- return ret;
- }
- static void present(void *ctx, const uint32_t *pixels, int stride, int x, int y, int w, int h)
- {
- struct drm_dev *d = ctx;
- drmModeClip clip = { (uint16_t)x, (uint16_t)y, (uint16_t)(x + w), (uint16_t)(y + h) };
- int row;
- for (row = y; row < y + h; row++)
- memcpy(d->map + (size_t)row * d->pitch + (size_t)x * 4,
- (const char *)pixels + row * stride + x * 4, (size_t)w * 4);
- if (drmModeDirtyFB(d->fd, d->fb_id, &clip, 1))
- g_printerr("DIRTYFB failed: %s\n", strerror(errno));
- }
- static void backlight(void *ctx, bool on)
- {
- platform_backlight_set(((struct drm_dev *)ctx)->backlight, on);
- }
- int main(int argc, char **argv)
- {
- static struct drm_dev d;
- static int port = 6600;
- static char *host, *input, *blpath;
- static gboolean kmsg;
- static GOptionEntry entries[] = {
- { "host", 0, 0, G_OPTION_ARG_STRING, &host, "MPD host (localhost)", "NAME" },
- { "port", 0, 0, G_OPTION_ARG_INT, &port, "MPD port (6600)", "N" },
- { "input", 0, 0, G_OPTION_ARG_FILENAME, &input, "Key input device (auto: " DEFAULT_KEYS_NAME ")", "PATH" },
- { "backlight", 0, 0, G_OPTION_ARG_FILENAME, &blpath, "Backlight sysfs brightness file", "PATH" },
- { "kmsg", 0, 0, G_OPTION_ARG_NONE, &kmsg, "Show the kernel log", NULL },
- { NULL, 0, 0, 0, NULL, NULL, NULL },
- };
- GOptionContext *ctx = g_option_context_new("- radio panel over DRM");
- struct app_frontend fe = { &d, present, backlight };
- struct app_options opt;
- drmModeModeInfo mode;
- GError *err = NULL;
- struct app *app;
- g_option_context_add_main_entries(ctx, entries, NULL);
- if (!g_option_context_parse(ctx, &argc, &argv, &err)) {
- g_printerr("%s\n", err->message);
- return 1;
- }
- d.backlight = blpath ? blpath : DEFAULT_BACKLIGHT;
- d.fd = open_panel();
- if (d.fd < 0) {
- g_printerr("no %s DRM device found\n", DRIVER_NAME);
- return 1;
- }
- if (setup_output(&d, &mode)) {
- g_printerr("cannot set up the display: %s\n", strerror(errno));
- return 1;
- }
- opt = (struct app_options){ host ? host : "localhost", port,
- mode.hdisplay, mode.vdisplay, kmsg };
- app = app_new(&opt, &fe);
- if (!platform_keys_start(app, input))
- g_printerr("no key input device, running display-only\n");
- platform_run();
- app_free(app);
- return 0;
- }
|