no-op refactoring: make ipc_connect find socket path
Michael Stapelberg
6 years ago
93 | 93 | static i3Font font; |
94 | 94 | static i3Font bold_font; |
95 | 95 | static int char_width; |
96 | static char *socket_path; | |
96 | static char *socket_path = NULL; | |
97 | 97 | static xcb_window_t win; |
98 | 98 | static surface_t surface; |
99 | 99 | static xcb_key_symbols_t *symbols; |
743 | 743 | |
744 | 744 | int main(int argc, char *argv[]) { |
745 | 745 | char *xdg_config_home; |
746 | socket_path = getenv("I3SOCK"); | |
747 | 746 | char *pattern = "pango:monospace 8"; |
748 | 747 | char *patternbold = "pango:monospace bold 8"; |
749 | 748 | int o, option_index = 0; |
822 | 821 | &xkb_base_event, |
823 | 822 | &xkb_base_error) != 1) |
824 | 823 | errx(EXIT_FAILURE, "Could not setup XKB extension."); |
825 | ||
826 | if (socket_path == NULL) | |
827 | socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen); | |
828 | ||
829 | if (socket_path == NULL) | |
830 | socket_path = "/tmp/i3-ipc.sock"; | |
831 | 824 | |
832 | 825 | keysyms = xcb_key_symbols_alloc(conn); |
833 | 826 | xcb_get_modifier_mapping_cookie_t modmap_cookie; |
40 | 40 | * the command will be sent to i3 */ |
41 | 41 | static char *format; |
42 | 42 | |
43 | static char *socket_path; | |
44 | 43 | static int sockfd; |
45 | 44 | static xcb_key_symbols_t *symbols; |
46 | 45 | static bool modeswitch_active = false; |
373 | 372 | |
374 | 373 | int main(int argc, char *argv[]) { |
375 | 374 | format = sstrdup("%s"); |
376 | socket_path = getenv("I3SOCK"); | |
375 | char *socket_path = NULL; | |
377 | 376 | char *pattern = sstrdup("pango:monospace 8"); |
378 | 377 | int o, option_index = 0; |
379 | 378 | |
436 | 435 | conn = xcb_connect(NULL, &screen); |
437 | 436 | if (!conn || xcb_connection_has_error(conn)) |
438 | 437 | die("Cannot open display\n"); |
439 | ||
440 | if (socket_path == NULL) | |
441 | socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen); | |
442 | ||
443 | if (socket_path == NULL) | |
444 | socket_path = "/tmp/i3-ipc.sock"; | |
445 | 438 | |
446 | 439 | sockfd = ipc_connect(socket_path); |
447 | 440 |
37 | 37 | |
38 | 38 | #include <i3/ipc.h> |
39 | 39 | |
40 | static char *socket_path; | |
41 | ||
42 | 40 | /* |
43 | 41 | * Having verboselog() and errorlog() is necessary when using libi3. |
44 | 42 | * |
160 | 158 | if (pledge("stdio rpath unix", NULL) == -1) |
161 | 159 | err(EXIT_FAILURE, "pledge"); |
162 | 160 | #endif |
163 | char *env_socket_path = getenv("I3SOCK"); | |
164 | if (env_socket_path) | |
165 | socket_path = sstrdup(env_socket_path); | |
166 | else | |
167 | socket_path = NULL; | |
161 | char *socket_path = NULL; | |
168 | 162 | int o, option_index = 0; |
169 | 163 | uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND; |
170 | 164 | char *payload = NULL; |
182 | 176 | |
183 | 177 | while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) { |
184 | 178 | if (o == 's') { |
185 | if (socket_path != NULL) | |
186 | free(socket_path); | |
179 | free(socket_path); | |
187 | 180 | socket_path = sstrdup(optarg); |
188 | 181 | } else if (o == 't') { |
189 | 182 | if (strcasecmp(optarg, "command") == 0) { |
227 | 220 | } |
228 | 221 | } |
229 | 222 | |
230 | if (socket_path == NULL) | |
231 | socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0); | |
232 | ||
233 | /* Fall back to the default socket path */ | |
234 | if (socket_path == NULL) | |
235 | socket_path = sstrdup("/tmp/i3-ipc.sock"); | |
236 | ||
237 | 223 | /* Use all arguments, separated by whitespace, as payload. |
238 | 224 | * This way, you don’t have to do i3-msg 'mark foo', you can use |
239 | 225 | * i3-msg mark foo */ |
252 | 238 | if (!payload) |
253 | 239 | payload = sstrdup(""); |
254 | 240 | |
255 | int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); | |
256 | if (sockfd == -1) | |
257 | err(EXIT_FAILURE, "Could not create socket"); | |
258 | ||
259 | struct sockaddr_un addr; | |
260 | memset(&addr, 0, sizeof(struct sockaddr_un)); | |
261 | addr.sun_family = AF_LOCAL; | |
262 | strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
263 | if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) | |
264 | err(EXIT_FAILURE, "Could not connect to i3 on socket \"%s\"", socket_path); | |
265 | ||
241 | int sockfd = ipc_connect(socket_path); | |
266 | 242 | if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1) |
267 | 243 | err(EXIT_FAILURE, "IPC: write()"); |
268 | 244 | free(payload); |
21 | 21 | * |
22 | 22 | */ |
23 | 23 | int ipc_connect(const char *socket_path) { |
24 | char *path = NULL; | |
25 | if (socket_path != NULL) { | |
26 | path = sstrdup(socket_path); | |
27 | } | |
28 | ||
29 | if (path == NULL) { | |
30 | if ((path = getenv("I3SOCK")) != NULL) { | |
31 | path = sstrdup(path); | |
32 | } | |
33 | } | |
34 | ||
35 | if (path == NULL) { | |
36 | path = root_atom_contents("I3_SOCKET_PATH", NULL, 0); | |
37 | } | |
38 | ||
39 | if (path == NULL) { | |
40 | path = sstrdup("/tmp/i3-ipc.sock"); | |
41 | } | |
42 | ||
24 | 43 | int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); |
25 | 44 | if (sockfd == -1) |
26 | 45 | err(EXIT_FAILURE, "Could not create socket"); |
30 | 49 | struct sockaddr_un addr; |
31 | 50 | memset(&addr, 0, sizeof(struct sockaddr_un)); |
32 | 51 | addr.sun_family = AF_LOCAL; |
33 | strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
52 | strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); | |
34 | 53 | if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) |
35 | err(EXIT_FAILURE, "Could not connect to i3"); | |
36 | ||
54 | err(EXIT_FAILURE, "Could not connect to i3 on socket %s", path); | |
55 | free(path); | |
37 | 56 | return sockfd; |
38 | 57 | } |
54 | 54 | * |
55 | 55 | */ |
56 | 56 | void display_running_version(void) { |
57 | char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen); | |
58 | if (socket_path == NULL) | |
59 | exit(EXIT_SUCCESS); | |
60 | ||
61 | 57 | char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen); |
62 | 58 | if (pid_from_atom == NULL) { |
63 | 59 | /* If I3_PID is not set, the running version is older than 4.2-200. */ |
70 | 66 | printf("(Getting version from running i3, press ctrl-c to abort…)"); |
71 | 67 | fflush(stdout); |
72 | 68 | |
73 | /* TODO: refactor this with the code for sending commands */ | |
74 | int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); | |
75 | if (sockfd == -1) | |
76 | err(EXIT_FAILURE, "Could not create socket"); | |
77 | ||
78 | struct sockaddr_un addr; | |
79 | memset(&addr, 0, sizeof(struct sockaddr_un)); | |
80 | addr.sun_family = AF_LOCAL; | |
81 | strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
82 | if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) | |
83 | err(EXIT_FAILURE, "Could not connect to i3"); | |
84 | ||
69 | int sockfd = ipc_connect(NULL); | |
85 | 70 | if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION, |
86 | 71 | (uint8_t *)"") == -1) |
87 | 72 | err(EXIT_FAILURE, "IPC: write()"); |
183 | 168 | yajl_free(handle); |
184 | 169 | free(reply); |
185 | 170 | free(pid_from_atom); |
186 | free(socket_path); | |
187 | 171 | } |