【问题标题】:how to hide cursor in XCB?如何在 XCB 中隐藏光标?
【发布时间】:2019-09-08 12:03:13
【问题描述】:

我想在 Xorg 中隐藏系统光标

我使用 xcb 为 Xorg 编写 X11-app,它会在某些情况下隐藏光标(如“xbanish”或“unclutter”)。我试过使用 Xfixes:它适用于 xlib,但不适用于 xcb。

我的 xlib 代码,隐藏光标:

#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>

Display *conn = XOpenDisplay(NULL);
XFixesHideCursor(conn, DefaultRootWindow(conn));
Xflush(conn);

我的 xcb 代码,什么都不做:

#include <xcb/xcb.h>
#include <xcb/xfixes.h>

xcb_connection_t *conn = xcb_connect(NULL, NULL);
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
xcb_xfixes_hide_cursor(conn, screen->root);
xcb_flush(conn);

我想了解为什么 xcb 的代码什么都不做,或者只是在 xcb 中隐藏光标。

UPD

xtrace 什么也没给我,它没有看到错误。 但我敢肯定,xcb_xfixes_hide_cursor 中存在错误,因为这段代码给了我非 NULL generic_error

xcb_void_cookie_t cookie = xcb_xfixes_hide_cursor_checked(conn, screen->root);
xcb_generic_error_t *generic_error = xcb_request_check(conn, cookie);

实际上,它给了我这个错误:

{
  "error_code": 1,
  "major_code": 138,
  "minor_code": 29,
  "sequence:": 2,
  "full_sequence": 2
}

我使用来自 xcb-util-errors 的 xcb_errors_get_name_for_minor_codexcb_errors_get_name_for_major_code 来了解有关错误的任何信息。它出现在xcb_xfixes_hide_cursor_checked 内部。

【问题讨论】:

  • x11trace 在 xcb 版本中显示什么?

标签: c++ c x11 xlib xcb


【解决方案1】:

实际上,它给了我这个错误:

{ “错误代码”:1, “主要代码”:138, “次要代码”:29, “序列:”:2, “全序列”:2 }

错误 1 ​​是 BadRequest / XCB_REQUEST。您收到 BadRequest 错误,因为您没有初始化 XFIXES 扩展(= 通知 X11 服务器您支持的版本)。

服务器中检查请求是否对客户端提供的版本有效的相关代码: https://codesearch.debian.net/show?file=xorg-server_2%3A1.20.4-1%2Fxfixes%2Fxfixes.c&line=150#L150

来自协议规范 (https://codesearch.debian.net/show?file=xorg-server_2%3A1.20.4-1%2Fxfixes%2Fxfixes.c&line=150#L150):

4. Extension initialization

The client must negotiate the version of the extension before executing
extension requests.  Behavior of the server is undefined otherwise.

QueryVersion
[...]

因此,回答您的问题:您需要先执行xcb_xfixes_query_version(c, 4, 0),然后才能执行HideCursor 请求。

回答您的第一个后续问题:4.0 版是引入HideCursor 的版本。这可以在协议规范中看到,因为HideCursor 记录在“XFIXES VERSION 4 OR BETTER”下。

回答您的第二个后续问题:XFixesHideCursor 自动为您查询版本:https://codesearch.debian.net/show?file=libxfixes_1%3A5.0.3-1%2Fsrc%2FCursor.c&line=255#L250

此代码最终调用XFixesHideCursor -> XFixesFindDisplay -> XFixesExtAddDisplay 并且此函数查询版本:https://codesearch.debian.net/show?file=libxfixes_1%3A5.0.3-1%2Fsrc%2FXfixes.c&line=79#L79

【讨论】:

    【解决方案2】:

    一般来说,隐藏光标最简单的方法就是将其改为空光标。

    xcb_void_cookie_t
    xcb_create_glyph_cursor (xcb_connection_t *connection,
                             xcb_cursor_t      cursorId,
                             xcb_font_t        source_font, /* font for the source glyph */
                             xcb_font_t        mask_font,   /* font for the mask glyph or XCB_NONE */
                             uint16_t          source_char, /* character glyph for the source */
                             uint16_t          mask_char,   /* character glyph for the mask */
                             uint16_t          fore_red,    /* red value for the foreground of the source */
                             uint16_t          fore_green,  /* green value for the foreground of the source */
                             uint16_t          fore_blue,   /* blue value for the foreground of the source */
                             uint16_t          back_red,    /* red value for the background of the source */
                             uint16_t          back_green,  /* green value for the background of the source */
                             uint16_t          back_blue ); /* blue value for the background of the source */
    

    我们应该能够为 source_char 和 mask_char 传递' '。但是,我们需要传递一个普通字体而不是光标字体才能工作。

    我发现显示和隐藏光标的旧想法有些问题。据我所知,API 对现代窗口管理器有些敏感。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多