aboutsummaryrefslogtreecommitdiffstats
path: root/wiz.c
blob: 4dab49ba8fb92fc52ec6caeb1598eed7c9110c10 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <wayland-client.h>
#include <wayland-server.h>
#include "ext-idle-notify-v1-protocol.h"

#define LEN(X) (sizeof(X) / sizeof(X[0]))

static struct ext_idle_notifier_v1 *notifier;
static struct wl_compositor *compositor;
static struct wl_display *display;
static struct wl_registry *registry;
static struct wl_seat *seat;
static const struct ext_idle_notification_v1_listener idlelistener;
static const struct wl_registry_listener reglistener;

pid_t cpid;

struct Events {
  uint32_t delay; /* in ms */
  char **idlecmd;
  char **resumecmd;
  struct ext_idle_notification_v1 *notif;
};

#include "config.h"

static void die(const char *fmt, ...);
static void reghandler(void *data, struct wl_registry *registry, uint32_t id,
  const char *interface, uint32_t version);
static void idling(void *data, struct ext_idle_notification_v1 *notif);
static void resuming(void *data, struct ext_idle_notification_v1 *notif);
static void run(char **cmd);

static void
die(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

  if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
    fputc(' ', stderr);
    perror(NULL);
  } else
    fputc('\n', stderr);
  exit(1);
}

static void
reghandler(void *data, struct wl_registry *registry, uint32_t id,
    const char *interface, uint32_t version)
{
  if (strcmp(interface, wl_compositor_interface.name) == 0)
    compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
  else if (strcmp(interface, ext_idle_notifier_v1_interface.name) == 0)
    notifier = wl_registry_bind(registry, id, &ext_idle_notifier_v1_interface, 1);
  else if (strcmp(interface, wl_seat_interface.name) == 0)
    seat = wl_registry_bind(registry, id, &wl_seat_interface, 7);
}

static const struct wl_registry_listener reglistener = {
  .global = reghandler,
};

static void
idling(void *data, struct ext_idle_notification_v1 *notif)
{
  struct Events *event = data;
  run(event->idlecmd);
}

static void
resuming(void *data, struct ext_idle_notification_v1 *notif)
{
  struct Events *event = data;
  run(event->resumecmd);
}

static const struct ext_idle_notification_v1_listener idlelistener = {
  .idled = idling,
  .resumed = resuming,
};

static void
run(char **cmd)
{
  waitpid(-1, NULL, WNOHANG);
  if (killchild && cpid)
    kill(cpid, SIGINT);
  if (cmd && (cpid = fork()) == 0) {
    dup2(STDERR_FILENO, STDOUT_FILENO);
    setsid();
    execvp(cmd[0], cmd);
    die("execvp failed:");
  }
}

int
main(int argc, char *argv[])
{
  int i, c;

  while ((c = getopt(argc, argv, "hv")) != -1) {
    switch (c) {
      case 'v': die("%s-%s", argv[0], VERSION); break;
      case 'h':
      default: die("usage: %s [-hv]", argv[0]); break;
    }
  }

  display = wl_display_connect(NULL);
  if (!display)
    die("%s: failed to connect to wayland display :(", argv[0]);

  registry = wl_display_get_registry(display);
  wl_registry_add_listener(registry, &reglistener, NULL);
  wl_display_roundtrip(display);
  if (!compositor || !notifier || !seat)
    die("%s: compositor doesn't support all necessary protocols", argv[0]);

  /* register all events */
  for (i = 0; i < LEN(events); i++) {
    if (events[i].delay < 0)
      continue;

    events[i].notif = ext_idle_notifier_v1_get_idle_notification(notifier,
      events[i].delay, seat);
    ext_idle_notification_v1_add_listener(events[i].notif, &idlelistener,
      &events[i]);
  }
  wl_display_roundtrip(display);

  while (wl_display_dispatch(display) != -1)
    if (nozombies)
      waitpid(-1, NULL, WUNTRACED);

  ext_idle_notifier_v1_destroy(notifier);
  wl_registry_destroy(registry);
  wl_display_destroy(display);
  wl_compositor_destroy(compositor);
}