Example #1
0
File: nilwm.c Project: nqv/nilwm
/** Get window text property
 */
int get_text_prop(xcb_window_t win, xcb_atom_t atom, char *s, unsigned int len) {
    xcb_get_property_cookie_t cookie;
    xcb_icccm_get_text_property_reply_t reply;

    if (len == 0) {
        return 0;
    }
    cookie = xcb_icccm_get_text_property(nil_.con, win, atom);
    if (!xcb_icccm_get_text_property_reply(nil_.con, cookie, &reply, 0)) {
        NIL_LOG("no reply get_text_property %d", atom);
        return -1;
    }
    if (reply.encoding != XCB_ATOM_STRING) {
        xcb_icccm_get_text_property_reply_wipe(&reply);
        return -1;
    }
    --len;  /* null terminated */
    if (reply.name_len < len) {
        len = reply.name_len;
    }
    strncpy(s, reply.name, len);
    s[len] = '\0';
    xcb_icccm_get_text_property_reply_wipe(&reply);
    return (int)len;
}
Example #2
0
/** Get the current X selection buffer.
 * \param L The Lua VM state.
 * \return The number of elements pushed on stack.
 * \luastack
 * \lreturn A string with the current X selection buffer.
 */
static int
luaA_selection_get(lua_State *L)
{
    luaA_deprecate(L, "selection.getter(\"PRIMARY\", \"UTF8_STRING\")");
    if(selection_window == XCB_NONE)
    {
        xcb_screen_t *screen = globalconf.screen;
        uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
        uint32_t values[] = { screen->black_pixel, 1, XCB_EVENT_MASK_PROPERTY_CHANGE };

        selection_window = xcb_generate_id(globalconf.connection);

        xcb_create_window(globalconf.connection, screen->root_depth, selection_window, screen->root,
                          0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT, screen->root_visual,
                          mask, values);
        xwindow_set_class_instance(selection_window);
        xwindow_set_name_static(selection_window, "Awesome selection window");
    }

    xcb_convert_selection(globalconf.connection, selection_window,
                          XCB_ATOM_PRIMARY, UTF8_STRING, XSEL_DATA, globalconf.timestamp);
    xcb_flush(globalconf.connection);

    xcb_generic_event_t *event;

    while(true)
    {
        event = xcb_wait_for_event(globalconf.connection);

        if(!event)
            return 0;

        if(XCB_EVENT_RESPONSE_TYPE(event) != XCB_SELECTION_NOTIFY)
        {
            /* \todo Eventually, this may be rewritten with adding a static
             * buffer, then a event handler for XCB_SELECTION_NOTIFY, then call
             * xcb_event_poll_for_event_loop() and awesome_refresh(),
             * then check if some static buffer has been filled with data.
             * If yes, that'd be the xsel data, otherwise, re-loop.
             * Anyway that's still brakes the socket or D-Bus, so maybe using
             * ev_loop() would be even better.
             */
            event_handle(event);
            p_delete(&event);
            awesome_refresh();
            continue;
        }

        xcb_selection_notify_event_t *event_notify =
            (xcb_selection_notify_event_t *) event;

        if(event_notify->selection == XCB_ATOM_PRIMARY
           && event_notify->property != XCB_NONE)
        {
            xcb_icccm_get_text_property_reply_t prop;
            xcb_get_property_cookie_t cookie =
                xcb_icccm_get_text_property(globalconf.connection,
					    event_notify->requestor,
					    event_notify->property);

            if(xcb_icccm_get_text_property_reply(globalconf.connection,
						 cookie, &prop, NULL))
	      {
                lua_pushlstring(L, prop.name, prop.name_len);

                xcb_icccm_get_text_property_reply_wipe(&prop);

                xcb_delete_property(globalconf.connection,
                                    event_notify->requestor,
                                    event_notify->property);

                p_delete(&event);

                return 1;
            }
            else
                break;
        }
    }

    p_delete(&event);
    return 0;
}