c++ - 除非调整大小,否则 XLib 应用程序不会重绘

标签 c++ xlib window-managers

因此,我有一个使用 XLib 的 C++ 应用程序。在其中,我使用 ctime 库访问日期和时间,并在 Expose 事件中,从中创建一个字符串并将其放在居中的窗口中。我的问题是,时间仅在调整大小时更新,例如,除非我不断调整窗口大小时,秒数不会改变。这是代码:

#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <ctime>

int main (int argc, char *argv[])
{
  Display                 *display;
  Visual                  *visual;
  int                     depth;
  int                     text_x;
  int                     text_y;
  XSetWindowAttributes    frame_attributes;
  Window                  frame_window;
  XFontStruct             *fontinfo;
  XGCValues               gr_values;
  GC                      graphical_context;
  XKeyEvent               event;
  char                    contents[80] = "                                                                               ";
  time_t                  rawtime;
  struct tm               *timeinfo;
  int                     contents_length = strlen(contents);

  display = XOpenDisplay(NULL);
  visual = DefaultVisual(display, 0);
  depth  = DefaultDepth(display, 0);

  frame_attributes.background_pixel = XWhitePixel(display, 0);
  /* create the application window */
  frame_window = XCreateWindow(display, XRootWindow(display, 0),
      0, 0, 500, 50, 5, depth,
      InputOutput, visual, CWBackPixel,
      &frame_attributes);
  XStoreName(display, frame_window, "Fly Taskbar");
  XSelectInput(display, frame_window, ExposureMask | StructureNotifyMask);

  fontinfo = XLoadQueryFont(display, "10x20");
  gr_values.font = fontinfo->fid;
  gr_values.foreground = XBlackPixel(display, 0);
  graphical_context = XCreateGC(display, frame_window,
      GCFont+GCForeground, &gr_values);
  XMapWindow(display, frame_window);

  while ( 1 ) {
    XNextEvent(display, (XEvent *)&event);
    switch ( event.type ) {
      case Expose:
        {
          XClearWindow(display, frame_window);
          // Upkeep of interface usefulness.
          Window returned_root, returned_parent;
          Window* top_level_windows;
          unsigned int nwin = 0;
          XQueryTree(
              display,
              XDefaultRootWindow(display),
              &returned_root,
              &returned_parent,
              &top_level_windows,
              &nwin);

          time(&rawtime);
          timeinfo = localtime(&rawtime);
          strftime(contents, 80, "%a %d %b, %T %p", timeinfo);

          sprintf(contents, "%s [%d]", contents, nwin);


          XWindowAttributes window_attributes;
          int font_direction, font_ascent, font_descent;
          XCharStruct text_structure;
          XTextExtents(fontinfo, contents, contents_length,
              &font_direction, &font_ascent, &font_descent,
              &text_structure);
          XGetWindowAttributes(display, frame_window, &window_attributes);
          text_x = (window_attributes.width - text_structure.width) + 5;
          text_y = (window_attributes.height -
              (text_structure.ascent+text_structure.descent))/2;
          XDrawString(display, frame_window, graphical_context,
              text_x, text_y, contents, contents_length);
          break;
        }
      default:
        break;
    }
  }
  return 0;
}

如果你们中的任何人想知道这是为了什么目的,我正在编写一个窗口管理器(实际上我已经走得很远了,就像您可以打开应用程序、移动应用程序并调整它们的大小一样),这是一个我需要用于调试目的的工具。 repo here

最佳答案

你需要一些事件循环,例如打电话poll(2)并处理延误。您还应该调用 XFlushXSync(因为 Xlib 正在缓冲请求)。

您可能会考虑给自己发送一些 Expose 事件....

你应该研究一下xclock的源代码

关于c++ - 除非调整大小,否则 XLib 应用程序不会重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30824122/

相关文章:

c++ - 使用 xlib 将图像加载到窗口中

swift - 如何在 Swift 中从 CGWindowID 获取窗口引用(CGWindow、NSWindow 或 WindowRef)?

c++ - Xlib的Colormap在哪里定义的?

c++ - 编译器错误 c++ msys-1.0.dll windows

c++ - C++中类字段对齐与对象实例对齐的关系?

c++ - 将快速委托(delegate)转换为标准函数

c++ - 如何使用 xlib 识别顶级 X11 窗口?

window-managers - i3 - 将容器移动到下一个/上一个 ws(如果不存在的话)

android - WindowManager 中 View 的 onConfigurationChanged 工作不可靠

c++ - 在 lambda 表达式和模板 typedef 习语中提取对成员