main.c (2466B)
1 /* Copyright (c), Niclas Meyer <niclas@countingsort.com> 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at https://mozilla.org/MPL/2.0/. 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include <wayland-client.h> 13 14 #include "kiwmi-ipc-client-protocol.h" 15 16 static void 17 command_done( 18 void *data, 19 struct kiwmi_command *UNUSED(kiwmi_command), 20 uint32_t error, 21 const char *message) 22 { 23 int *exit_code = data; 24 FILE *out; 25 26 if (error == KIWMI_COMMAND_ERROR_SUCCESS) { 27 *exit_code = EXIT_SUCCESS; 28 out = stdout; 29 } else { 30 *exit_code = EXIT_FAILURE; 31 out = stderr; 32 } 33 34 if (message[0] != '\0') { 35 fprintf(out, "%s\n", message); 36 } 37 } 38 39 static const struct kiwmi_command_listener command_listener = { 40 .done = command_done, 41 }; 42 43 static void 44 registry_global( 45 void *data, 46 struct wl_registry *registry, 47 uint32_t name, 48 const char *interface, 49 uint32_t UNUSED(version)) 50 { 51 struct kiwmi_ipc **ipc = data; 52 if (strcmp(interface, kiwmi_ipc_interface.name) == 0) { 53 *ipc = wl_registry_bind(registry, name, &kiwmi_ipc_interface, 1); 54 } 55 } 56 57 static void 58 registry_global_remove( 59 void *UNUSED(data), 60 struct wl_registry *UNUSED(registry), 61 uint32_t UNUSED(name)) 62 { 63 // EMPTY 64 } 65 66 static const struct wl_registry_listener registry_listener = { 67 .global = registry_global, 68 .global_remove = registry_global_remove, 69 }; 70 71 int 72 main(int argc, char **argv) 73 { 74 if (argc < 2) { 75 fprintf(stderr, "Usage: kiwmic COMMAND\n"); 76 exit(EXIT_FAILURE); 77 } 78 79 struct wl_display *display = wl_display_connect(NULL); 80 if (!display) { 81 fprintf(stderr, "Failed to connect to display\n"); 82 exit(EXIT_FAILURE); 83 } 84 85 struct wl_registry *registry = wl_display_get_registry(display); 86 struct kiwmi_ipc *ipc = NULL; 87 88 wl_registry_add_listener(registry, ®istry_listener, &ipc); 89 wl_display_roundtrip(display); 90 91 if (!ipc) { 92 fprintf(stderr, "Failed to bind to kiwmi_ipc\n"); 93 exit(EXIT_FAILURE); 94 } 95 96 struct kiwmi_command *command = kiwmi_ipc_eval(ipc, argv[1]); 97 int exit_code; 98 kiwmi_command_add_listener(command, &command_listener, &exit_code); 99 wl_display_roundtrip(display); 100 wl_display_disconnect(display); 101 102 exit(exit_code); 103 }