static void accept_loop() { while (!stopping) { int client_fd; struct pollfd pfd; int bufsize; w_stm_t stm; #ifdef HAVE_LIBGIMLI_H if (hb) { gimli_heartbeat_set(hb, GIMLI_HB_RUNNING); } #endif pfd.events = POLLIN; pfd.fd = listener_fd; if (poll(&pfd, 1, 60000) < 1 || (pfd.revents & POLLIN) == 0) { if (stopping) { break; } // Timed out, or error. // Arrange to sanity check that we're working w_check_my_sock(); continue; } #ifdef HAVE_ACCEPT4 client_fd = accept4(listener_fd, NULL, 0, SOCK_CLOEXEC); #else client_fd = accept(listener_fd, NULL, 0); #endif if (client_fd == -1) { continue; } w_set_cloexec(client_fd); bufsize = WATCHMAN_IO_BUF_SIZE; setsockopt(client_fd, SOL_SOCKET, SO_SNDBUF, (void*)&bufsize, sizeof(bufsize)); stm = w_stm_fdopen(client_fd); if (!stm) { w_log(W_LOG_ERR, "Failed to allocate stm for fd: %s\n", strerror(errno)); close(client_fd); continue; } make_new_client(stm); } }
static void named_pipe_accept_loop(const char *path) { HANDLE handles[2]; OVERLAPPED olap; HANDLE connected_event = CreateEvent(NULL, FALSE, TRUE, NULL); if (!connected_event) { w_log(W_LOG_ERR, "named_pipe_accept_loop: CreateEvent failed: %s\n", win32_strerror(GetLastError())); return; } listener_thread_event = CreateEvent(NULL, FALSE, TRUE, NULL); handles[0] = connected_event; handles[1] = listener_thread_event; memset(&olap, 0, sizeof(olap)); olap.hEvent = connected_event; w_log(W_LOG_ERR, "waiting for pipe clients on %s\n", path); while (!stopping) { w_stm_t stm; HANDLE client_fd; DWORD res; client_fd = CreateNamedPipe( path, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE| PIPE_REJECT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, WATCHMAN_IO_BUF_SIZE, 512, 0, NULL); if (client_fd == INVALID_HANDLE_VALUE) { w_log(W_LOG_ERR, "CreateNamedPipe(%s) failed: %s\n", path, win32_strerror(GetLastError())); continue; } ResetEvent(connected_event); if (!ConnectNamedPipe(client_fd, &olap)) { res = GetLastError(); if (res == ERROR_PIPE_CONNECTED) { goto good_client; } if (res != ERROR_IO_PENDING) { w_log(W_LOG_ERR, "ConnectNamedPipe: %s\n", win32_strerror(GetLastError())); CloseHandle(client_fd); continue; } res = WaitForMultipleObjectsEx(2, handles, false, INFINITE, true); if (res == WAIT_OBJECT_0 + 1) { // Signalled to stop CancelIoEx(client_fd, &olap); CloseHandle(client_fd); continue; } if (res == WAIT_OBJECT_0) { goto good_client; } w_log(W_LOG_ERR, "WaitForMultipleObjectsEx: ConnectNamedPipe: " "unexpected status %u\n", res); CancelIoEx(client_fd, &olap); CloseHandle(client_fd); } else { good_client: stm = w_stm_handleopen(client_fd); if (!stm) { w_log(W_LOG_ERR, "Failed to allocate stm for pipe handle: %s\n", strerror(errno)); CloseHandle(client_fd); continue; } make_new_client(stm); } } }
static void setup_display(void) { XGCValues gv; XSetWindowAttributes attr; XColor dummy; XModifierKeymap *modmap; /* used in scanning windows (XQueryTree) */ unsigned int i, j, nwins; Window dw1, dw2, *wins; XWindowAttributes winattr; LOG_ENTER("setup_display()"); dpy = XOpenDisplay(opt_display); if (!dpy) { LOG_ERROR("can't open display %s\n", opt_display); exit(1); } XSetErrorHandler(handle_xerror); /* XSynchronize(dpy, True); */ /* Standard & EWMH atoms */ ewmh_init(); font = XLoadQueryFont(dpy, opt_font); if (!font) font = XLoadQueryFont(dpy, DEF_FONT); if (!font) { LOG_ERROR("couldn't find a font to use: try starting with -fn fontname\n"); exit(1); } move_curs = XCreateFontCursor(dpy, XC_fleur); resize_curs = XCreateFontCursor(dpy, XC_plus); /* find out which modifier is NumLock - we'll use this when grabbing * every combination of modifiers we can think of */ modmap = XGetModifierMapping(dpy); for (i = 0; i < 8; i++) { for (j = 0; j < (unsigned int)modmap->max_keypermod; j++) { if (modmap->modifiermap[i*modmap->max_keypermod+j] == XKeysymToKeycode(dpy, XK_Num_Lock)) { numlockmask = (1<<i); LOG_DEBUG("XK_Num_Lock is (1<<0x%02x)\n", i); } } } XFreeModifiermap(modmap); /* set up GC parameters - same for each screen */ gv.function = GXinvert; gv.subwindow_mode = IncludeInferiors; gv.line_width = 1; /* opt_bw */ gv.font = font->fid; /* set up root window attributes - same for each screen */ attr.event_mask = ChildMask | EnterWindowMask | ColormapChangeMask; /* SHAPE extension? */ #ifdef SHAPE { int e_dummy; have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy); } #endif /* Xrandr extension? */ #ifdef RANDR { int e_dummy; have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy); if (!have_randr) { LOG_DEBUG("XRandR is not supported on this display.\n"); } } #endif /* now set up each screen in turn */ num_screens = ScreenCount(dpy); if (num_screens < 0) { LOG_ERROR("Can't count screens\n"); exit(1); } screens = xmalloc(num_screens * sizeof(ScreenInfo)); for (i = 0; i < (unsigned int)num_screens; i++) { char *ds, *colon, *dot; ds = DisplayString(dpy); /* set up DISPLAY environment variable to use */ colon = strrchr(ds, ':'); screens[i].display = xmalloc(14 + strlen(ds)); strcpy(screens[i].display, "DISPLAY="); strcat(screens[i].display, ds); if (colon && num_screens > 1) { colon = strrchr(screens[i].display, ':'); dot = strchr(colon, '.'); if (!dot) dot = colon + strlen(colon); snprintf(dot, 5, ".%d", i); } screens[i].screen = i; screens[i].root = RootWindow(dpy, i); #ifdef RANDR if (have_randr) { XRRSelectInput(dpy, screens[i].root, RRScreenChangeNotifyMask); } #endif #ifdef VWM screens[i].vdesk = KEY_TO_VDESK(XK_1); #endif XAllocNamedColor(dpy, DefaultColormap(dpy, i), opt_fg, &screens[i].fg, &dummy); XAllocNamedColor(dpy, DefaultColormap(dpy, i), opt_bg, &screens[i].bg, &dummy); #ifdef VWM XAllocNamedColor(dpy, DefaultColormap(dpy, i), opt_fc, &screens[i].fc, &dummy); #endif screens[i].invert_gc = XCreateGC(dpy, screens[i].root, GCFunction | GCSubwindowMode | GCLineWidth | GCFont, &gv); XChangeWindowAttributes(dpy, screens[i].root, CWEventMask, &attr); grab_keys_for_screen(&screens[i]); screens[i].docks_visible = 1; /* scan all the windows on this screen */ LOG_XENTER("XQueryTree(screen=%d)", i); XQueryTree(dpy, screens[i].root, &dw1, &dw2, &wins, &nwins); LOG_XDEBUG("%d windows\n", nwins); LOG_XLEAVE(); for (j = 0; j < nwins; j++) { XGetWindowAttributes(dpy, wins[j], &winattr); if (!winattr.override_redirect && winattr.map_state == IsViewable) make_new_client(wins[j], &screens[i]); } XFree(wins); ewmh_init_screen(&screens[i]); } ewmh_set_net_active_window(NULL); LOG_LEAVE(); }