summaryrefslogtreecommitdiff
path: root/main.c
blob: 2d4ecf3f038d66994806bc466ea094e074bf5c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-client.h>

#define STREQ(a, b) (strcmp(a, b) == 0)

static char **command_argv;

static void global_registry_handler(void *data, struct wl_registry *registry,
                                    uint32_t id, const char *interface,
                                    uint32_t version) {
  /* A new display has been attached */
  if (STREQ(interface, "wl_output")) {
    errno = 0;
    switch (fork()) {
    case -1: /* Handle error */
      perror("Unable to fork");
      break;
    case 0: /* In child */
      execvp(command_argv[0], command_argv);
      /* No need to break, the exec leaves the current program. */
    default:;
    }
  }
}

static void global_registry_remover(void *data, struct wl_registry *registry,
                                    uint32_t id) {
  /* Do nothing. */
}

int main(int argc, char **argv) {
  command_argv = argv + 1;

  struct wl_display *display = wl_display_connect(NULL);

  if (display == NULL) {
    fputs("Failed to connect to the Wayland socket", stderr);
    exit(EXIT_FAILURE);
  }

  const struct wl_registry_listener registry_listener = {
      global_registry_handler,
      global_registry_remover,
  };

  struct wl_registry *registry = wl_display_get_registry(display);

  wl_registry_add_listener(registry, &registry_listener, NULL);

  for (;;) {
    wl_display_dispatch(display);
    wl_display_roundtrip(display);
  }

  wl_display_disconnect(display);
  return EXIT_SUCCESS;
}