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
|
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <vali.h>
#include "config.h"
#include "kanshi.h"
#include "ipc.h"
#include "ipc-gen.h"
static struct kanshi_state *state_from_call(struct vali_service_call *call) {
struct vali_service *service = vali_service_call_get_service(call);
return vali_service_get_user_data(service);
}
static void apply_profile_done(void *data, bool success) {
struct vali_service_call *call = data;
if (success) {
vali_service_call_close_with_reply(call, NULL);
} else {
ipc_error_ProfileNotMatched_close_service_call(call, NULL);
}
}
static void handle_status(struct ipc_Status_service_call call, const struct ipc_Status_in *in) {
struct kanshi_state *state = state_from_call(call.base);
ipc_Status_close_with_reply(call, &(struct ipc_Status_out){
.current_profile = state->current_profile ? state->current_profile->name : NULL,
.pending_profile = state->pending_profile ? state->pending_profile->name : NULL,
});
}
static void handle_reload(struct ipc_Reload_service_call call, const struct ipc_Reload_in *in) {
struct kanshi_state *state = state_from_call(call.base);
if (!kanshi_reload_config(state, apply_profile_done, call.base)) {
ipc_error_ProfileNotMatched_close_service_call(call.base, NULL);
}
}
static void handle_switch(struct ipc_Switch_service_call call, const struct ipc_Switch_in *in) {
struct kanshi_state *state = state_from_call(call.base);
struct kanshi_profile *profile;
bool found = false;
bool matched = false;
wl_list_for_each(profile, &state->config->profiles, link) {
if (strcmp(profile->name, in->profile) != 0) {
continue;
}
found = true;
if (kanshi_switch(state, profile, apply_profile_done, call.base)) {
matched = true;
break;
}
}
if (!found) {
ipc_error_ProfileNotFound_close_service_call(call.base, NULL);
return;
}
if (!matched) {
ipc_error_ProfileNotMatched_close_service_call(call.base, NULL);
return;
}
}
static const struct ipc_handler ipc_handler = {
.Reload = handle_reload,
.Switch = handle_switch,
.Status = handle_status,
};
static int set_cloexec(int fd) {
int flags = fcntl(fd, F_GETFD);
if (flags < 0) {
perror("fnctl(F_GETFD) failed");
return -1;
}
if (fcntl(fd, F_SETFD, flags | O_CLOEXEC) < 0) {
perror("fnctl(F_SETFD) failed");
return -1;
}
return 0;
}
int kanshi_init_ipc(struct kanshi_state *state, int listen_fd) {
if (listen_fd >= 0 && set_cloexec(listen_fd) < 0) {
return -1;
}
struct vali_service *service = vali_service_create();
if (service == NULL) {
fprintf(stderr, "Failed to create Varlink service\n");
return -1;
}
struct vali_registry *registry = vali_registry_create(&(struct vali_registry_options){
.vendor = "emersion",
.product = "kanshi",
.version = KANSHI_VERSION,
.url = "https://wayland.emersion.fr/kanshi/",
});
if (registry == NULL) {
fprintf(stderr, "Failed to create Varlink registry\n");
return -1;
}
vali_registry_add(registry, &ipc_interface, ipc_get_call_handler(&ipc_handler));
vali_service_set_call_handler(service, vali_registry_get_call_handler(registry));
vali_service_set_user_data(service, state);
if (listen_fd >= 0) {
if (!vali_service_listen_fd(service, listen_fd)) {
fprintf(stderr, "Failed to start Varlink listener from FD\n");
return -1;
}
} else {
char path[PATH_MAX];
if (get_ipc_path(path, sizeof(path)) < 0) {
return -1;
}
unlink(path);
if (!vali_service_listen_unix(service, path)) {
fprintf(stderr, "Failed to start Varlink listener on %s\n", path);
return -1;
}
}
state->service = service;
state->registry = registry;
return 0;
}
void kanshi_finish_ipc(struct kanshi_state *state) {
if (state->service) {
vali_service_destroy(state->service);
state->service = NULL;
}
if (state->registry) {
vali_registry_destroy(state->registry);
state->registry = NULL;
}
}
|