使用X时减少输入延迟

vin*_*nux 24 c++ xlib

我最近一直在阅读一些关于输入延迟的文章:

https://danluu.com/term-latency/

https://pavelfatin.com/typing-with-pleasure/

我一直在努力改善我的小文本编辑器的用户体验.我正在使用SDL来汇集输入并创建窗口,但决定删除它并执行我自己的X实现.应用程序启动已经改进,但输入延迟并没有那么多.是否有任何特定的技术可以改善我的应用程序从X中获取鼠标和键盘数据的方式?或者我应该放弃并强迫Wayland?

我已经考虑运行我XNextEvent例行()在一个单独的线程循环,但真正这样做的唯一的解决办法?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <GL/glx.h>
#include <GL/glext.h>
#include <GL/glu.h>

Display *dpy;
Window root, win;
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
XWindowAttributes wa;
XEvent xev;
Mask mask;

float TimeCounter, LastFrameTimeCounter, DT, prevTime = 0.0, FPS;
struct timeval tv, tv0;
int Frame = 1, FramesPerFPS;

void CreateWindow() {
  if ((dpy = XOpenDisplay(NULL)) == NULL) {
    printf("\n\tcannot connect to x server\n\n");
    exit(0);
  }

  root = DefaultRootWindow(dpy);

  if ((vi = glXChooseVisual(dpy, 0, att)) == NULL) {
    printf("\n\tno matching visual\n\n");
    exit(0);
  }

  if ((cmap = XCreateColormap(dpy, root, vi->visual, AllocNone)) == 0) {
    printf("\n\tcannot create colormap\n\n");
    exit(0);
  }

  swa.event_mask = KeyPressMask;
  swa.colormap = cmap;
  win = XCreateWindow(dpy, root, 0, 0, 1024, 768, 0, vi->depth, InputOutput,
                      vi->visual, CWColormap | CWEventMask, &swa);
  XStoreName(dpy, win, "ed");
  XMapWindow(dpy, win);
}

void Close() {
  XDestroyWindow(dpy, win);
  XCloseDisplay(dpy);
  exit(0);
}

int main(int argc, char *argv[]) {
  CreateWindow();

  while (true) {
    mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
    while (XCheckWindowEvent(dpy, win, mask, &xev) ||
           XCheckTypedWindowEvent(dpy, win, ClientMessage, &xev)) {
      char *key_string =
          XKeysymToString(XkbKeycodeToKeysym(dpy, xev.xkey.keycode, 0, 0));

      if (strncmp(key_string, "Escape", 5) == 0) {
        Close();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

您也可以直接监听 evdev 输入驱动程序,但随后您必须通过自己的实现将事件传递到窗口。

XNextEvent()依次转换所有数学转换(从原始事件到基于窗口),计算焦点窗口和许多其他内容。

XNextEvent()如果您追求简单性和易于实施,我认为这是唯一的选择。