我想在屏幕上的特定像素处绘制简单的图元(类似于 question )。为了做到这一点,我使用窗口管理器的覆盖窗口在所有窗口之上绘制。我可以看到我正在绘制的形状和鼠标事件通过但我没有看到例如覆盖窗口下方的窗口移动(除非我终止我的应用程序)。我是 Xlib 编程的新手,很抱歉问了一个可能很简单的问题。
#include <assert.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/shape.h>
#include <cairo.h>
#include <cairo-xlib.h>
Display *d;
Window overlay;
Window root;
int width, height;
void
allow_input_passthrough (Window w)
{
XserverRegion region = XFixesCreateRegion (d, NULL, 0);
XFixesSetWindowShapeRegion (d, w, ShapeBounding, 0, 0, 0);
XFixesSetWindowShapeRegion (d, w, ShapeInput, 0, 0, region);
XFixesDestroyRegion (d, region);
}
void
prep_overlay (void)
{
overlay = XCompositeGetOverlayWindow (d, root);
allow_input_passthrough (overlay);
}
void draw(cairo_t *cr) {
int quarter_w = width / 4;
int quarter_h = height / 4;
cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
cairo_fill(cr);
}
int main() {
d = XOpenDisplay(NULL);
int s = DefaultScreen(d);
root = RootWindow(d, s);
XCompositeRedirectSubwindows (d, root, CompositeRedirectAutomatic);
XSelectInput (d, root, SubstructureNotifyMask);
width = DisplayWidth(d, s);
height = DisplayHeight(d, s);
prep_overlay();
cairo_surface_t *surf = cairo_xlib_surface_create(d, overlay,
DefaultVisual(d, s),
width, height);
cairo_t *cr = cairo_create(surf);
XSelectInput(d, overlay, ExposureMask);
draw(cr);
XEvent ev;
while (1) {
XNextEvent(d, &ev);
if (ev.type == Expose) {
draw(cr);
}
}
cairo_destroy(cr);
cairo_surface_destroy(surf);
XCloseDisplay(d);
return 0;
}
如何在覆盖窗口上绘制像素并仍然看到下面的窗口?
最佳答案
我可以建议一个更简单、纯 X11 的解决方案,它没有我遇到并提到的闪烁问题 here .它使用 override_redirect Xlib 中的功能:
#include <assert.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <chrono>
#include <thread>
void draw(cairo_t *cr) {
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.5);
cairo_rectangle(cr, 0, 0, 200, 200);
cairo_fill(cr);
}
int main() {
Display *d = XOpenDisplay(NULL);
Window root = DefaultRootWindow(d);
int default_screen = XDefaultScreen(d);
// these two lines are really all you need
XSetWindowAttributes attrs;
attrs.override_redirect = true;
XVisualInfo vinfo;
if (!XMatchVisualInfo(d, DefaultScreen(d), 32, TrueColor, &vinfo)) {
printf("No visual found supporting 32 bit color, terminating\n");
exit(EXIT_FAILURE);
}
// these next three lines add 32 bit depth, remove if you dont need and change the flags below
attrs.colormap = XCreateColormap(d, root, vinfo.visual, AllocNone);
attrs.background_pixel = 0;
attrs.border_pixel = 0;
// Window XCreateWindow(
// Display *display, Window parent,
// int x, int y, unsigned int width, unsigned int height, unsigned int border_width,
// int depth, unsigned int class,
// Visual *visual,
// unsigned long valuemask, XSetWindowAttributes *attributes
// );
Window overlay = XCreateWindow(
d, root,
0, 0, 200, 200, 0,
vinfo.depth, InputOutput,
vinfo.visual,
CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel, &attrs
);
XMapWindow(d, overlay);
cairo_surface_t* surf = cairo_xlib_surface_create(d, overlay,
vinfo.visual,
200, 200);
cairo_t* cr = cairo_create(surf);
draw(cr);
XFlush(d);
// show the window for 10 seconds
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
cairo_destroy(cr);
cairo_surface_destroy(surf);
XUnmapWindow(d, overlay);
XCloseDisplay(d);
return 0;
}
我继续添加了 32 位深度,但你明白了。如果需要,您可以将其删除。
请注意,就原始问题而言(在 覆盖窗口上绘图),这是行不通的。这会绘制一个 窗口,该窗口的外观和行为与问题中的其他要求非常相似。但是,它不保证在作为 X11 一部分的复合覆盖窗口上进行任何直接绘制。
它所做的只是告诉窗口管理器“不要以任何方式弄乱或装饰这个窗口”,这本身就是一个建议,窗口管理器没有义务遵守这条规则(尽管几乎所有人都这样做) .您可能不想直接在叠加窗口上绘制,因为那样会干扰合成器并且几乎肯定无法正确绘制任何内容。我什至不确定如果合成器已经请求访问复合覆盖,你会怎么做,一次只有一个进程可以访问它。
此外,叠加窗口与窗口管理器无关,它是合成器 (Xcomposite) 的一部分。
关于c - X11 - 在叠加窗口上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780789/