summaryrefslogtreecommitdiff
path: root/main.c
blob: b0129fea0617726fa3ec4247b5559278146a1902 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* This program was created using this as a reference: https://jan.newmarch.name/Wayland/ProgrammingClient/
 *
 * wayland-run-on-new-display runs a command each time a new display is attached.
 *
 * Copyright (C) 2021 Robby Zambito 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#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;
}