1 /* 2 * vim:ts=4:sw=4:expandtab 3 * 4 * i3 - an improved dynamic tiling window manager 5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE) 6 * 7 * ipc.c: UNIX domain socket IPC (initialization, client handling, protocol). 8 * 9 */ 10 #pragma once 11 12 #include <config.h> 13 14 #include <ev.h> 15 #include <yajl/yajl_gen.h> 16 #include <yajl/yajl_parse.h> 17 18 #include "data.h" 19 #include "tree.h" 20 #include "configuration.h" 21 22 #include "i3/ipc.h" 23 24 extern char *current_socketpath; 25 26 typedef struct ipc_client { 27 int fd; 28 29 /* The events which this client wants to receive */ 30 int num_events; 31 char **events; 32 33 /* For clients which subscribe to the tick event: whether the first tick 34 * event has been sent by i3. */ 35 bool first_tick_sent; 36 37 struct ev_io *read_callback; 38 struct ev_io *write_callback; 39 struct ev_timer *timeout; 40 uint8_t *buffer; 41 size_t buffer_size; 42 43 TAILQ_ENTRY(ipc_client) clients; 44 } ipc_client; 45 46 /* 47 * Callback type for the different message types. 48 * 49 * message is the raw packet, as received from the UNIX domain socket. size 50 * is the remaining size of bytes for this packet. 51 * 52 * message_size is the size of the message as the sender specified it. 53 * message_type is the type of the message as the sender specified it. 54 * 55 */ 56 typedef void (*handler_t)(ipc_client *, uint8_t *, int, uint32_t, uint32_t); 57 58 /* Macro to declare a callback */ 59 #define IPC_HANDLER(name) \ 60 static void handle_##name(ipc_client *client, uint8_t *message, \ 61 int size, uint32_t message_size, \ 62 uint32_t message_type) 63 64 /** 65 * Handler for activity on the listening socket, meaning that a new client 66 * has just connected and we should accept() him. Sets up the event handler 67 * for activity on the new connection and inserts the file descriptor into 68 * the list of clients. 69 * 70 */ 71 void ipc_new_client(EV_P_ struct ev_io *w, int revents); 72 73 /** 74 * ipc_new_client_on_fd() only sets up the event handler 75 * for activity on the new connection and inserts the file descriptor into 76 * the list of clients. 77 * 78 * This variant is useful for the inherited IPC connection when restarting. 79 * 80 */ 81 ipc_client *ipc_new_client_on_fd(EV_P_ int fd); 82 83 /** 84 * Sends the specified event to all IPC clients which are currently connected 85 * and subscribed to this kind of event. 86 * 87 */ 88 void ipc_send_event(const char *event, uint32_t message_type, const char *payload); 89 90 /** 91 * Calls to ipc_shutdown() should provide a reason for the shutdown. 92 */ 93 typedef enum { 94 SHUTDOWN_REASON_RESTART, 95 SHUTDOWN_REASON_EXIT 96 } shutdown_reason_t; 97 98 /** 99 * Calls shutdown() on each socket and closes it. This function is to be called 100 * when exiting or restarting only! 101 * 102 * exempt_fd is never closed. Set to -1 to close all fds. 103 * 104 */ 105 void ipc_shutdown(shutdown_reason_t reason, int exempt_fd); 106 107 void dump_node(yajl_gen gen, Con *con, bool inplace_restart); 108 109 /** 110 * Generates a json workspace event. Returns a dynamically allocated yajl 111 * generator. Free with yajl_gen_free(). 112 */ 113 yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old); 114 115 /** 116 * For the workspace events we send, along with the usual "change" field, also 117 * the workspace container in "current". For focus events, we send the 118 * previously focused workspace in "old". 119 */ 120 void ipc_send_workspace_event(const char *change, Con *current, Con *old); 121 122 /** 123 * For the window events we send, along the usual "change" field, 124 * also the window container, in "container". 125 */ 126 void ipc_send_window_event(const char *property, Con *con); 127 128 /** 129 * For the barconfig update events, we send the serialized barconfig. 130 */ 131 void ipc_send_barconfig_update_event(Barconfig *barconfig); 132 133 /** 134 * For the binding events, we send the serialized binding struct. 135 */ 136 void ipc_send_binding_event(const char *event_type, Binding *bind); 137 138 /** 139 * Set the maximum duration that we allow for a connection with an unwriteable 140 * socket. 141 */ 142 void ipc_set_kill_timeout(ev_tstamp new); 143 144 /** 145 * Sends a restart reply to the IPC client on the specified fd. 146 */ 147 void ipc_confirm_restart(ipc_client *client); 148