//- helpers internal void x11_button_update(PlatformButton *button, b32 down) { if(button->ended_down != down) { button->ended_down = down; ++button->half_transition_count; } } internal u32 x11_mods(u32 state) { u32 mods = 0; if(state & ShiftMask) mods |= PLATFORM_MOD_SHIFT; if(state & ControlMask) mods |= PLATFORM_MOD_CTRL; if(state & Mod1Mask) mods |= PLATFORM_MOD_ALT; return mods; } internal u32 x11_scale_for_dpi(f32 dpi) { u32 scale = (u32)(dpi / 96.0f + 0.5f); return scale ? scale : 1; } // NOTE: Xft.dpi from the resource database is what every toolkit uses for hidpi on x11 internal f32 x11_read_dpi(Display *display) { const char *resources = XResourceManagerString(display); if(resources) { const char *at = strstr(resources, "Xft.dpi:"); if(at) { f32 dpi = (f32)atof(at + 8); if(dpi >= 48.0f && dpi <= 480.0f) return dpi; } } return 96.0f; } internal void x11_update_size(X11State *state, u32 physical_width, u32 physical_height) { u32 width = physical_width / state->scale; u32 height = physical_height / state->scale; if(physical_width != state->physical_width || physical_height != state->physical_height) { state->physical_width = physical_width; state->physical_height = physical_height; state->width = width; state->height = height; state->resized = true; } } internal void x11_send_client_message(X11State *state, Window target, Atom type, long l0, long l1, long l2, long l3, long l4) { XEvent event = {}; event.xclient.type = ClientMessage; event.xclient.window = target; event.xclient.message_type = type; event.xclient.format = 32; event.xclient.data.l[0] = l0; event.xclient.data.l[1] = l1; event.xclient.data.l[2] = l2; event.xclient.data.l[3] = l3; event.xclient.data.l[4] = l4; XSendEvent(state->display, target, False, NoEventMask, &event); } // NOTE: window manager requests (maximize, move) go to the root window internal void x11_send_wm_message(X11State *state, Atom type, long l0, long l1, long l2, long l3, long l4) { XEvent event = {}; event.xclient.type = ClientMessage; event.xclient.window = state->window; event.xclient.message_type = type; event.xclient.format = 32; event.xclient.data.l[0] = l0; event.xclient.data.l[1] = l1; event.xclient.data.l[2] = l2; event.xclient.data.l[3] = l3; event.xclient.data.l[4] = l4; XSendEvent(state->display, state->root, False, SubstructureNotifyMask | SubstructureRedirectMask, &event); } //- drag and drop (XDND v5) internal void x11_xdnd_enter(X11State *state, XClientMessageEvent *message) { state->xdnd_source = (Window)message->data.l[0]; state->xdnd_version = (u32)((unsigned long)message->data.l[1] >> 24); state->xdnd_accept = false; if(message->data.l[1] & 1) { // NOTE: more than three types, the full list is a property on the source window Atom type; int format; unsigned long count = 0, remaining = 0; unsigned char *data = NULL; if(XGetWindowProperty(state->display, state->xdnd_source, state->xdnd_type_list, 0, 1024, False, XA_ATOM, &type, &format, &count, &remaining, &data) == Success && data) { Atom *atoms = (Atom *)data; for(unsigned long index = 0; index < count; ++index) if(atoms[index] == state->uri_list) state->xdnd_accept = true; XFree(data); } } else { for(u32 index = 2; index <= 4; ++index) if((Atom)message->data.l[index] == state->uri_list) state->xdnd_accept = true; } } internal void x11_xdnd_position(X11State *state, XClientMessageEvent *message) { int root_x = (int)((unsigned long)message->data.l[2] >> 16), root_y = (int)(message->data.l[2] & 0xFFFF); int x = 0, y = 0; Window child; XTranslateCoordinates(state->display, state->root, state->window, root_x, root_y, &x, &y, &child); state->xdnd_x = (f32)x / (f32)state->scale; state->xdnd_y = (f32)y / (f32)state->scale; x11_send_client_message(state, state->xdnd_source, state->xdnd_status, (long)state->window, state->xdnd_accept ? 1 : 0, 0, 0, state->xdnd_accept ? (long)state->xdnd_action_copy : 0); } internal void x11_xdnd_drop(X11State *state, XClientMessageEvent *message) { if(!state->xdnd_accept) { x11_send_client_message(state, state->xdnd_source, state->xdnd_finished, (long)state->window, 0, 0, 0, 0); return; } Time time = (state->xdnd_version >= 1) ? (Time)message->data.l[2] : CurrentTime; XConvertSelection(state->display, state->xdnd_selection, state->uri_list, state->xdnd_selection, state->window, time); } internal void x11_xdnd_selection(X11State *state, XSelectionEvent *selection) { if(selection->property != state->xdnd_selection) return; Atom type; int format; unsigned long count = 0, remaining = 0; unsigned char *data = NULL; if(XGetWindowProperty(state->display, state->window, state->xdnd_selection, 0, PLATFORM_MAX_DROPS * PLATFORM_DROP_PATH_SIZE / 4, True, AnyPropertyType, &type, &format, &count, &remaining, &data) == Success && data) { linux_parse_uri_list(&state->input, (const char *)data, (u64)count); state->input.drop_x = state->xdnd_x; state->input.drop_y = state->xdnd_y; state->input.changed = true; XFree(data); } if(state->xdnd_version >= 2 && state->xdnd_source) { x11_send_client_message(state, state->xdnd_source, state->xdnd_finished, (long)state->window, 1, (long)state->xdnd_action_copy, 0, 0); } state->xdnd_source = 0; } //- events internal b32 x11_key_is_down(X11State *state, u32 keycode) { return (state->keys_down[(keycode >> 3) & 31] >> (keycode & 7)) & 1; } internal void x11_set_key_down(X11State *state, u32 keycode, b32 down) { u8 bit = (u8)(1u << (keycode & 7)); if(down) state->keys_down[(keycode >> 3) & 31] |= bit; else state->keys_down[(keycode >> 3) & 31] &= (u8)~bit; } internal void x11_handle_key(X11State *state, XKeyEvent *key) { PlatformInput *input = &state->input; b32 repeat = x11_key_is_down(state, key->keycode); x11_set_key_down(state, key->keycode, true); char text[32]; KeySym keysym = NoSymbol; int length = 0; if(state->ic) { Status status = 0; length = Xutf8LookupString(state->ic, key, text, sizeof(text) - 1, &keysym, &status); if(status != XLookupChars && status != XLookupBoth) length = 0; if(status != XLookupKeySym && status != XLookupBoth) { // NOTE: XLookupChars alone (composed text): still want a keysym for the key event if(status == XLookupChars) keysym = NoSymbol; } } else { length = XLookupString(key, text, sizeof(text) - 1, &keysym, NULL); } if(keysym == NoSymbol) { // NOTE: shift aware like xkb_state_key_get_one_sym: index 1 is the shifted level keysym = XLookupKeysym(key, (key->state & ShiftMask) ? 1 : 0); } u32 mods = x11_mods(key->state); input->mods = mods; // NOTE: XK_ISO_Left_Tab is what xkb gives for shift+tab too, XLookupKeysym may give plain tab if(keysym == XK_Tab && (mods & PLATFORM_MOD_SHIFT)) keysym = XK_ISO_Left_Tab; if(keysym != NoSymbol && input->key_count < PLATFORM_MAX_KEY_EVENTS) { PlatformKeyEvent *event = &input->keys[input->key_count++]; event->keysym = (u32)keysym; event->mods = mods; event->repeat = repeat; } if(length > 0 && !(mods & (PLATFORM_MOD_CTRL | PLATFORM_MOD_ALT)) && (u8)text[0] >= 0x20 && text[0] != 0x7f && input->text_size + (u32)length <= sizeof(input->text)) { MemCpy(input->text + input->text_size, text, (u64)length); input->text_size += (u32)length; } } internal void x11_handle_event(X11State *state, XEvent *event) { PlatformInput *input = &state->input; if(XFilterEvent(event, None)) return; switch(event->type) { case KeyPress: { input->changed = true; state->last_time = event->xkey.time; x11_handle_key(state, &event->xkey); } break; case KeyRelease: { input->changed = true; x11_set_key_down(state, event->xkey.keycode, false); input->mods = x11_mods(event->xkey.state); } break; case ButtonPress: case ButtonRelease: { input->changed = true; b32 down = (event->type == ButtonPress); u32 button = event->xbutton.button; input->mouse_x = (f32)event->xbutton.x / (f32)state->scale; input->mouse_y = (f32)event->xbutton.y / (f32)state->scale; input->mods = x11_mods(event->xbutton.state); if(button == Button1) x11_button_update(&input->mouse[0], down); else if(button == Button3) x11_button_update(&input->mouse[1], down); else if(button == Button2) x11_button_update(&input->mouse[2], down); else if(down && button == Button4) input->scroll_y -= 1.0f; else if(down && button == Button5) input->scroll_y += 1.0f; else if(down && button == 6) input->scroll_x -= 1.0f; else if(down && button == 7) input->scroll_x += 1.0f; } break; case MotionNotify: { input->changed = true; input->mouse_in_window = true; input->mouse_x = (f32)event->xmotion.x / (f32)state->scale; input->mouse_y = (f32)event->xmotion.y / (f32)state->scale; } break; case EnterNotify: { input->changed = true; input->mouse_in_window = true; input->mouse_x = (f32)event->xcrossing.x / (f32)state->scale; input->mouse_y = (f32)event->xcrossing.y / (f32)state->scale; } break; case LeaveNotify: { input->changed = true; input->mouse_in_window = false; // NOTE: x11 keeps an implicit grab while a button is held, a leave then isn't a release if(!(event->xcrossing.state & (Button1Mask | Button2Mask | Button3Mask))) { for(u32 index = 0; index < ArrayCount(input->mouse); ++index) x11_button_update(&input->mouse[index], false); } } break; case FocusIn: { input->changed = true; input->focused = true; if(state->ic) XSetICFocus(state->ic); } break; case FocusOut: { input->changed = true; input->focused = false; input->mods = 0; MemSetZero(state->keys_down, sizeof(state->keys_down)); if(state->ic) XUnsetICFocus(state->ic); } break; case ConfigureNotify: { input->changed = true; x11_update_size(state, (u32)event->xconfigure.width, (u32)event->xconfigure.height); } break; case MapNotify: state->suspended = false; input->changed = true; break; case UnmapNotify: state->suspended = true; input->changed = true; break; case Expose: input->changed = true; break; case PropertyNotify: { if(event->xproperty.atom == state->net_wm_state) { Atom type; int format; unsigned long count = 0, remaining = 0; unsigned char *data = NULL; state->maximized = false; if(XGetWindowProperty(state->display, state->window, state->net_wm_state, 0, 64, False, XA_ATOM, &type, &format, &count, &remaining, &data) == Success && data) { Atom *atoms = (Atom *)data; for(unsigned long index = 0; index < count; ++index) { if(atoms[index] == state->net_wm_state_maximized_vert) state->maximized = true; } XFree(data); } } } break; case ClientMessage: { XClientMessageEvent *message = &event->xclient; if(message->message_type == state->wm_protocols && (Atom)message->data.l[0] == state->wm_delete_window) { state->should_close = true; input->changed = true; } else if(message->message_type == state->xdnd_enter) { x11_xdnd_enter(state, message); } else if(message->message_type == state->xdnd_position) { x11_xdnd_position(state, message); } else if(message->message_type == state->xdnd_leave) { state->xdnd_source = 0; } else if(message->message_type == state->xdnd_drop) { x11_xdnd_drop(state, message); } } break; case SelectionNotify: { if(event->xselection.selection != state->clipboard) x11_xdnd_selection(state, &event->xselection); } break; case SelectionRequest: { // NOTE: someone pastes what we copied XSelectionRequestEvent *req = &event->xselectionrequest; XSelectionEvent reply = {}; reply.type = SelectionNotify; reply.display = req->display; reply.requestor = req->requestor; reply.selection = req->selection; reply.target = req->target; reply.time = req->time; reply.property = None; Atom property = req->property ? req->property : req->target; if(req->selection == state->clipboard && state->clip_owned && state->clip_data) { if(req->target == state->targets) { Atom offered[] = {state->targets, state->utf8_string, state->text_plain_utf8, XA_STRING}; XChangeProperty(state->display, req->requestor, property, XA_ATOM, 32, PropModeReplace, (unsigned char *)offered, ArrayCount(offered)); reply.property = property; } else if(req->target == state->utf8_string || req->target == XA_STRING || req->target == state->text_plain_utf8) { XChangeProperty(state->display, req->requestor, property, req->target, 8, PropModeReplace, (unsigned char *)state->clip_data, (int)state->clip_size); reply.property = property; } } XSendEvent(state->display, req->requestor, False, 0, (XEvent *)&reply); } break; case SelectionClear: { if(event->xselectionclear.selection == state->clipboard) state->clip_owned = false; } break; } } //- public api internal b32 x11_window_open(PlatformWindow *window, WindowParams params) { X11State *state = &window->x11; XInitThreads(); state->display = XOpenDisplay(NULL); if(!state->display) { Log("no x11 display"); return false; } Display *display = state->display; state->screen = DefaultScreen(display); state->root = RootWindow(display, state->screen); state->dpi = x11_read_dpi(display); state->scale = x11_scale_for_dpi(state->dpi); state->width = params.width ? params.width : 960; state->height = params.height ? params.height : 600; state->is_layer = params.layer; #define X11_ATOM(field, name) state->field = XInternAtom(display, name, False); X11_ATOM(wm_protocols, "WM_PROTOCOLS") X11_ATOM(wm_delete_window, "WM_DELETE_WINDOW") X11_ATOM(net_wm_name, "_NET_WM_NAME") X11_ATOM(net_wm_state, "_NET_WM_STATE") X11_ATOM(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT") X11_ATOM(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ") X11_ATOM(net_wm_state_above, "_NET_WM_STATE_ABOVE") X11_ATOM(net_wm_moveresize, "_NET_WM_MOVERESIZE") X11_ATOM(net_wm_window_type, "_NET_WM_WINDOW_TYPE") X11_ATOM(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY") X11_ATOM(motif_wm_hints, "_MOTIF_WM_HINTS") X11_ATOM(utf8_string, "UTF8_STRING") X11_ATOM(net_wm_pid, "_NET_WM_PID") X11_ATOM(xdnd_aware, "XdndAware") X11_ATOM(xdnd_enter, "XdndEnter") X11_ATOM(xdnd_position, "XdndPosition") X11_ATOM(xdnd_status, "XdndStatus") X11_ATOM(xdnd_leave, "XdndLeave") X11_ATOM(xdnd_drop, "XdndDrop") X11_ATOM(xdnd_finished, "XdndFinished") X11_ATOM(xdnd_selection, "XdndSelection") X11_ATOM(xdnd_type_list, "XdndTypeList") X11_ATOM(xdnd_action_copy, "XdndActionCopy") X11_ATOM(uri_list, "text/uri-list") X11_ATOM(clipboard, "CLIPBOARD") X11_ATOM(targets, "TARGETS") X11_ATOM(clip_property, "DESIGN_IT_CLIP") X11_ATOM(text_plain_utf8, "text/plain;charset=utf-8") X11_ATOM(incr, "INCR") #undef X11_ATOM u32 physical_width = state->width * state->scale; u32 physical_height = state->height * state->scale; int x = 0, y = 0; if(state->is_layer) { int screen_width = DisplayWidth(display, state->screen); int screen_height = DisplayHeight(display, state->screen); int margin = params.margin * (int)state->scale; b32 left = (params.anchor & PLATFORM_ANCHOR_LEFT) != 0, right = (params.anchor & PLATFORM_ANCHOR_RIGHT) != 0; b32 top = (params.anchor & PLATFORM_ANCHOR_TOP) != 0, bottom = (params.anchor & PLATFORM_ANCHOR_BOTTOM) != 0; if(left && right) { x = margin; physical_width = (u32)(screen_width - 2 * margin); } else if(left) { x = margin; } else if(right) { x = screen_width - margin - (int)physical_width; } else { x = (screen_width - (int)physical_width) / 2; } if(top && bottom) { y = margin; physical_height = (u32)(screen_height - 2 * margin); } else if(top) { y = margin; } else if(bottom) { y = screen_height - margin - (int)physical_height; } else { y = (screen_height - (int)physical_height) / 2; } } XSetWindowAttributes attributes = {}; attributes.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | LeaveWindowMask | FocusChangeMask | StructureNotifyMask | ExposureMask | PropertyChangeMask; attributes.background_pixel = BlackPixel(display, state->screen); state->window = XCreateWindow(display, state->root, x, y, physical_width, physical_height, 0, CopyFromParent, InputOutput, CopyFromParent, CWEventMask | CWBackPixel, &attributes); if(!state->window) { Log("XCreateWindow failed"); return false; } //- identity: title, class (matches the .desktop file), pid const char *title = params.title ? params.title : params.name; XStoreName(display, state->window, title); XChangeProperty(display, state->window, state->net_wm_name, state->utf8_string, 8, PropModeReplace, (const unsigned char *)title, (int)strlen(title)); XClassHint *class_hint = XAllocClassHint(); class_hint->res_name = (char *)params.name; class_hint->res_class = (char *)params.name; XSetClassHint(display, state->window, class_hint); XFree(class_hint); long pid = (long)getpid(); XChangeProperty(display, state->window, state->net_wm_pid, XA_CARDINAL, 32, PropModeReplace, (const unsigned char *)&pid, 1); XSetWMProtocols(display, state->window, &state->wm_delete_window, 1); if(params.min_width && params.min_height) { XSizeHints *hints = XAllocSizeHints(); hints->flags = PMinSize | (state->is_layer ? PPosition : 0); hints->min_width = (int)(params.min_width * state->scale); hints->min_height = (int)(params.min_height * state->scale); hints->x = x; hints->y = y; XSetWMNormalHints(display, state->window, hints); XFree(hints); } if(state->is_layer) { // NOTE: overlay: borderless (motif hints), utility type, always on top long motif[5] = {2 /* MWM_HINTS_DECORATIONS */, 0, 0, 0, 0}; XChangeProperty(display, state->window, state->motif_wm_hints, state->motif_wm_hints, 32, PropModeReplace, (const unsigned char *)motif, 5); XChangeProperty(display, state->window, state->net_wm_window_type, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&state->net_wm_window_type_utility, 1); XChangeProperty(display, state->window, state->net_wm_state, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&state->net_wm_state_above, 1); } //- drag and drop long xdnd_version = 5; XChangeProperty(display, state->window, state->xdnd_aware, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&xdnd_version, 1); //- input method, for real text input (dead keys, compose) XSetLocaleModifiers(""); state->im = XOpenIM(display, NULL, NULL, NULL); if(state->im) { state->ic = XCreateIC(state->im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, state->window, XNFocusWindow, state->window, (char *)NULL); } // NOTE: no synthetic KeyRelease between autorepeated presses Bool supported = False; XkbSetDetectableAutoRepeat(display, True, &supported); //- cursors, core font names (no libXcursor needed) const unsigned int shapes[8] = {68 /* left_ptr */, 60 /* hand2 */, 52 /* fleur */, 108 /* sb_h_double_arrow */, 116 /* sb_v_double_arrow */, 34 /* crosshair */, 52 /* fleur */, 14 /* bottom_right_corner */}; for(u32 index = 0; index < 8; ++index) state->cursors[index] = XCreateFontCursor(display, shapes[index]); state->cursor = PLATFORM_CURSOR_DEFAULT; XDefineCursor(display, state->window, state->cursors[PLATFORM_CURSOR_DEFAULT]); XMapWindow(display, state->window); if(state->is_layer) XMoveWindow(display, state->window, x, y); XFlush(display); // NOTE: wait until mapped so the swapchain gets a real size for(;;) { XEvent event; XNextEvent(display, &event); x11_handle_event(state, &event); if(event.type == MapNotify && event.xmap.window == state->window) break; } XWindowAttributes current; XGetWindowAttributes(display, state->window, ¤t); x11_update_size(state, (u32)current.width, (u32)current.height); state->resized = false; state->input.changed = true; return true; } internal b32 x11_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask) { if(ready_mask) *ready_mask = 0; X11State *state = &window->x11; if(state->clip_pending) { state->clip_pending = false; XSetSelectionOwner(state->display, state->clipboard, state->window, state->last_time ? state->last_time : CurrentTime); state->clip_owned = XGetSelectionOwner(state->display, state->clipboard) == state->window; } XFlush(state->display); if(XPending(state->display) == 0) { struct pollfd fds[8] = {{ConnectionNumber(state->display), POLLIN, 0}}; u32 which[8] = {}; u32 count = 1; for(u32 index = 0; index < extra_count && count < ArrayCount(fds); ++index) { if(extra_fds[index] >= 0) { which[count] = index; fds[count++] = {extra_fds[index], POLLIN, 0}; } } struct timespec timeout = {}; struct timespec *timeout_ptr = NULL; if(timeout_ns >= 0) { timeout.tv_sec = timeout_ns / 1000000000ll; timeout.tv_nsec = timeout_ns % 1000000000ll; timeout_ptr = &timeout; } int ready = ppoll(fds, count, timeout_ptr, NULL); if(ready > 0 && ready_mask) { for(u32 slot = 1; slot < count; ++slot) if(fds[slot].revents) *ready_mask |= 1u << which[slot]; } } while(XPending(state->display)) { XEvent event; XNextEvent(state->display, &event); x11_handle_event(state, &event); } *input = state->input; for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) state->input.mouse[index].half_transition_count = 0; state->input.key_count = 0; state->input.text_size = 0; state->input.scroll_x = 0; state->input.scroll_y = 0; state->input.drop_count = 0; state->input.changed = false; return !state->should_close; } internal void x11_window_begin_move(PlatformWindow *window) { X11State *state = &window->x11; if(state->is_layer) return; Window root, child; int root_x = 0, root_y = 0, x = 0, y = 0; unsigned int mask = 0; XQueryPointer(state->display, state->window, &root, &child, &root_x, &root_y, &x, &y, &mask); XUngrabPointer(state->display, CurrentTime); // NOTE: _NET_WM_MOVERESIZE_MOVE = 8, button 1, source indication 1 (normal application) x11_send_wm_message(state, state->net_wm_moveresize, root_x, root_y, 8, Button1, 1); XFlush(state->display); x11_button_update(&state->input.mouse[0], false); } internal void x11_window_toggle_maximized(PlatformWindow *window) { X11State *state = &window->x11; if(state->is_layer) return; // NOTE: _NET_WM_STATE_TOGGLE = 2 x11_send_wm_message(state, state->net_wm_state, 2, (long)state->net_wm_state_maximized_vert, (long)state->net_wm_state_maximized_horz, 1, 0); XFlush(state->display); } internal void x11_window_set_cursor(PlatformWindow *window, PlatformCursor cursor) { X11State *state = &window->x11; if(state->cursor == (u32)cursor || (u32)cursor >= ArrayCount(state->cursors)) return; state->cursor = (u32)cursor; XDefineCursor(state->display, state->window, state->cursors[cursor]); XFlush(state->display); } internal void x11_window_close(PlatformWindow *window) { X11State *state = &window->x11; if(!state->display) return; if(state->ic) XDestroyIC(state->ic); if(state->im) XCloseIM(state->im); for(u32 index = 0; index < ArrayCount(state->cursors); ++index) if(state->cursors[index]) XFreeCursor(state->display, state->cursors[index]); if(state->window) XDestroyWindow(state->display, state->window); XCloseDisplay(state->display); state->display = NULL; } //- clipboard api internal void x11_clipboard_set(PlatformWindow *window, const char *data, u64 size) { X11State *state = &window->x11; free(state->clip_data); state->clip_data = (char *)malloc(size + 1); if(!state->clip_data) { state->clip_size = 0; return; } MemCpy(state->clip_data, data, size); state->clip_data[size] = 0; state->clip_size = size; state->clip_pending = true; } internal String8 x11_clipboard_get(PlatformWindow *window, Arena *arena) { X11State *state = &window->x11; if((state->clip_owned || state->clip_pending) && state->clip_data) { u8 *copy = PushArray(arena, u8, state->clip_size + 1); MemCpy(copy, state->clip_data, state->clip_size); copy[state->clip_size] = 0; return ToString8Size(copy, state->clip_size); } if(XGetSelectionOwner(state->display, state->clipboard) == None) return str8_zero(); XDeleteProperty(state->display, state->window, state->clip_property); XConvertSelection(state->display, state->clipboard, state->utf8_string, state->clip_property, state->window, state->last_time ? state->last_time : CurrentTime); XFlush(state->display); // NOTE: wait for our SelectionNotify only, every other event stays queued for the pump XEvent event; b32 got = false; u64 deadline = time_now_ns() + 500000000ull; while(time_now_ns() < deadline) { if(XCheckTypedWindowEvent(state->display, state->window, SelectionNotify, &event)) { if(event.xselection.selection == state->clipboard) { got = true; break; } x11_xdnd_selection(state, &event.xselection); continue; } struct pollfd pfd = {ConnectionNumber(state->display), POLLIN, 0}; poll(&pfd, 1, 10); } if(!got || event.xselection.property == None) return str8_zero(); Atom type; int format; unsigned long count, after; unsigned char *data = NULL; String8 result = str8_zero(); if(XGetWindowProperty(state->display, state->window, state->clip_property, 0, MiB(4) / 4, True, AnyPropertyType, &type, &format, &count, &after, &data) == Success && data) { // NOTE: INCR (very large selections) isn't handled, that's megabytes of diagram text if(type != state->incr && format == 8) { u8 *out = PushArray(arena, u8, count + 1); MemCpy(out, data, count); out[count] = 0; result = ToString8Size(out, count); } XFree(data); } return result; }