Codebase list i3-gaps / c07936d
no-op refactoring: make ipc_connect find socket path Michael Stapelberg 6 years ago
5 changed file(s) with 28 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
9393 static i3Font font;
9494 static i3Font bold_font;
9595 static int char_width;
96 static char *socket_path;
96 static char *socket_path = NULL;
9797 static xcb_window_t win;
9898 static surface_t surface;
9999 static xcb_key_symbols_t *symbols;
743743
744744 int main(int argc, char *argv[]) {
745745 char *xdg_config_home;
746 socket_path = getenv("I3SOCK");
747746 char *pattern = "pango:monospace 8";
748747 char *patternbold = "pango:monospace bold 8";
749748 int o, option_index = 0;
822821 &xkb_base_event,
823822 &xkb_base_error) != 1)
824823 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";
831824
832825 keysyms = xcb_key_symbols_alloc(conn);
833826 xcb_get_modifier_mapping_cookie_t modmap_cookie;
4040 * the command will be sent to i3 */
4141 static char *format;
4242
43 static char *socket_path;
4443 static int sockfd;
4544 static xcb_key_symbols_t *symbols;
4645 static bool modeswitch_active = false;
373372
374373 int main(int argc, char *argv[]) {
375374 format = sstrdup("%s");
376 socket_path = getenv("I3SOCK");
375 char *socket_path = NULL;
377376 char *pattern = sstrdup("pango:monospace 8");
378377 int o, option_index = 0;
379378
436435 conn = xcb_connect(NULL, &screen);
437436 if (!conn || xcb_connection_has_error(conn))
438437 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";
445438
446439 sockfd = ipc_connect(socket_path);
447440
3737
3838 #include <i3/ipc.h>
3939
40 static char *socket_path;
41
4240 /*
4341 * Having verboselog() and errorlog() is necessary when using libi3.
4442 *
160158 if (pledge("stdio rpath unix", NULL) == -1)
161159 err(EXIT_FAILURE, "pledge");
162160 #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;
168162 int o, option_index = 0;
169163 uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
170164 char *payload = NULL;
182176
183177 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
184178 if (o == 's') {
185 if (socket_path != NULL)
186 free(socket_path);
179 free(socket_path);
187180 socket_path = sstrdup(optarg);
188181 } else if (o == 't') {
189182 if (strcasecmp(optarg, "command") == 0) {
227220 }
228221 }
229222
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
237223 /* Use all arguments, separated by whitespace, as payload.
238224 * This way, you don’t have to do i3-msg 'mark foo', you can use
239225 * i3-msg mark foo */
252238 if (!payload)
253239 payload = sstrdup("");
254240
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);
266242 if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
267243 err(EXIT_FAILURE, "IPC: write()");
268244 free(payload);
2121 *
2222 */
2323 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
2443 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
2544 if (sockfd == -1)
2645 err(EXIT_FAILURE, "Could not create socket");
3049 struct sockaddr_un addr;
3150 memset(&addr, 0, sizeof(struct sockaddr_un));
3251 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);
3453 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);
3756 return sockfd;
3857 }
5454 *
5555 */
5656 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
6157 char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen);
6258 if (pid_from_atom == NULL) {
6359 /* If I3_PID is not set, the running version is older than 4.2-200. */
7066 printf("(Getting version from running i3, press ctrl-c to abort…)");
7167 fflush(stdout);
7268
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);
8570 if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION,
8671 (uint8_t *)"") == -1)
8772 err(EXIT_FAILURE, "IPC: write()");
183168 yajl_free(handle);
184169 free(reply);
185170 free(pid_from_atom);
186 free(socket_path);
187171 }