int main(void) { fd_set descriptors; char socket_path[BUFSIZ]; int sock_fd, ret_fd, dpy_fd, sel, n; struct sockaddr_un sock_address; char msg[BUFSIZ] = {0}; char rsp[BUFSIZ] = {0}; xcb_generic_event_t *event; running = true; dpy = xcb_connect(NULL, &default_screen); if (xcb_connection_has_error(dpy)) die("error: cannot open display\n"); setup(); register_events(); dpy_fd = xcb_get_file_descriptor(dpy); char *sp = getenv(SOCKET_ENV_VAR); strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path)); sock_address.sun_family = AF_UNIX; strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path)); unlink(socket_path); sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (sock_fd == -1) die("error: could not create socket\n"); bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)); listen(sock_fd, SOMAXCONN); sel = MAX(sock_fd, dpy_fd) + 1; load_settings(); run_autostart(); ewmh_update_wm_name(); update_root_dimensions(); while (running) { xcb_flush(dpy); FD_ZERO(&descriptors); FD_SET(sock_fd, &descriptors); FD_SET(dpy_fd, &descriptors); if (select(sel, &descriptors, NULL, NULL, NULL)) { if (FD_ISSET(sock_fd, &descriptors)) { ret_fd = accept(sock_fd, NULL, 0); if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) { msg[n] = '\0'; process_message(msg, rsp); send(ret_fd, rsp, strlen(rsp), 0); close(ret_fd); } } if (FD_ISSET(dpy_fd, &descriptors)) { while ((event = xcb_poll_for_event(dpy)) != NULL) { handle_event(event); free(event); } } } if (xcb_connection_has_error(dpy)) { die("connection has errors\n"); } } close(sock_fd); xcb_ewmh_connection_wipe(ewmh); free(ewmh); xcb_flush(dpy); xcb_disconnect(dpy); return 0; }
int main(int argc, char *argv[]) { if (argc == 2 && strcmp(argv[1], "-v") == 0) { printf("%s\n", VERSION); exit(EXIT_SUCCESS); } fd_set descriptors; char socket_path[MAXLEN]; int sock_fd, ret_fd, dpy_fd, sel, n; struct sockaddr_un sock_address; size_t rsplen = 0; char msg[BUFSIZ] = {0}; char rsp[BUFSIZ] = {0}; xcb_generic_event_t *event; running = true; dpy = xcb_connect(NULL, &default_screen); if (xcb_connection_has_error(dpy)) err("error: cannot open display\n"); setup(); register_events(); dpy_fd = xcb_get_file_descriptor(dpy); char *sp = getenv(SOCKET_ENV_VAR); strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path)); sock_address.sun_family = AF_UNIX; strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path)); unlink(socket_path); sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (sock_fd == -1) err("error: could not create socket\n"); bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)); listen(sock_fd, SOMAXCONN); sel = MAX(sock_fd, dpy_fd) + 1; load_settings(); run_autostart(); grab_buttons(); ewmh_update_wm_name(); while (running) { xcb_flush(dpy); FD_ZERO(&descriptors); FD_SET(sock_fd, &descriptors); FD_SET(dpy_fd, &descriptors); if (select(sel, &descriptors, NULL, NULL, NULL)) { if (FD_ISSET(sock_fd, &descriptors)) { ret_fd = accept(sock_fd, NULL, 0); if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) { msg[n] = '\0'; process_message(msg, rsp); rsplen = strlen(rsp); if (rsp[rsplen - 1] == '\n') rsp[--rsplen] = '\0'; send(ret_fd, rsp, rsplen, 0); close(ret_fd); rsp[0] = '\0'; } } if (FD_ISSET(dpy_fd, &descriptors)) { while ((event = xcb_poll_for_event(dpy)) != NULL) { handle_event(event); free(event); } } } if (xcb_connection_has_error(dpy)) { err("connection has errors\n"); } } close(sock_fd); xcb_ewmh_connection_wipe(ewmh); free(ewmh); xcb_flush(dpy); xcb_disconnect(dpy); return 0; }