3

我在 Ubuntu 14.04 平台上,我正在尝试为我的截屏项目制作橡皮筋。我找到了使用 xlib 的橡皮筋示例,但在拖动鼠标时出现闪烁和部分缺失的矩形。我想知道它是否特定于我的系统或已弃用的 xlib 库?有解决方法吗?

我还注意到 imagemagic 的屏幕位置抓取命令import window.miff以同样的方式闪烁。

闪烁的矩形

这是我尝试过的代码

尺子

xrectsel

4

3 回答 3

4

看起来后台的终端正在重新粉刷自己。你可能总是自私的,做一个XGrabServer()在移动/调整窗口大小时使用矩形的 WM。在您释放抓取之前,屏幕上的任何其他内容(时钟、负载监视器)都不会更新。

应避免抢夺服务器。您可能想要添加一个--grab/--nograb选项,让用户决定他们是更喜欢避免视觉伪影还是让其他应用程序(电影播放器​​、负载监视器、时钟......)在橡皮筋期间更新屏幕。

另一种选择是使用半透明窗口而不是轮廓矩形,类似于现代窗口管理器倾向于使用真实窗口而不是橡皮筋来移动/调整窗口大小(即使这可能意味着在操作之前重新绘制窗口很多次)完成,对于使用橡皮筋进行远程 X 移动/调整大小肯定会更好)。

XGrabServer()讨厌的例子:

于 2014-08-08T10:21:41.920 回答
1

如果您在 GC 中使用 xor,您可以通过再次绘制完全相同的东西来选择性地擦除线条。您不需要重新粉刷整个窗口。

XSetFunction(dpy,gc,GXxor); // sets xor mode

只需设置一些缓冲区并保留您绘制的副本,以便您可以再次执行此操作(包括颜色)。

我去年写了这个:https ://www.raspberrypi.org/forums/viewtopic.php?p=1317849 我将最后 50 行存储在循环缓冲区中,然后重绘它们以擦除。我可以运行几个小时而没有不完整的擦除伪影。并且CPU使用率非常低。这几乎是 xscreensaver 质量。 在此处输入图像描述

于 2018-08-21T23:59:13.843 回答
0

定义: 橡皮筋是在屏幕上拖动鼠标时在屏幕上绘制的屏幕选择矩形。当鼠标按钮释放时,它会打印屏幕的几何图形。X Window 系统在桌面缺乏透明度,所以它通过合并图层来使用伪透明。包含桌面壁纸的屏幕称为“根窗口”。在根窗口上绘制是一个问题,因为其他窗口(例如时钟)需要重新绘制自身,在这种情况下,您的橡皮筋矩形会闪烁。为避免此问题,您需要使用覆盖整个屏幕的半透明或透明窗口。

解决方案:正如@ninjalj 在他的回答中概述的那样,您需要使用覆盖整个屏幕的透明/半透明窗口。

源代码:

// gcc xrubberband.c -o xrubberband -lX11
// use case ; maim -w root -g $(./xrubberband)  `date +%H-%M-%S`.png

#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/extensions/XTest.h>
#include <X11/extensions/XInput.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include <stdarg.h>
#include <X11/Xatom.h>

#define MAKE_RECT(etype)    \
    x = event.etype.x_root; \
    y = event.etype.y_root; \
    rw = x - rootx;     \
    if (rw < 0) rw = -rw;   \
    rh = y - rooty;     \
    if (rh < 0) rh = -rh;   \
    rx = x < rootx ? x : rootx; \
    ry = y < rooty ? y : rooty

/* MAKE_CURSOR assigns a correct cursor */
#define MAKE_CURSOR(etype)                      \
    pointer = (event.etype.x_root > rootx ?             \
           (event.etype.y_root > rooty ? pointer2 : pointer4) : \
           (event.etype.y_root > rooty ? pointer3 : pointer1));
             
XRectangle Select_Rect(Display *dpy, int screen_num, Window root, Window parent, unsigned int display_width, unsigned int display_height)

{

    int status;
    XRectangle xrect;
    XEvent event;
    unsigned int x = 0, y = 0, rootx = 0, rooty = 0;
    Cursor pointer1, pointer2, pointer3, pointer4, pointer;
    int boxDrawn = False, selectionDone = False;
    int rx = 0, ry = 0, rw = 0, rh = 0;


    GC gc;

    /* get some cursors for rectangle formation */
    pointer1 = XCreateFontCursor(dpy, XC_ul_angle);
    pointer2 = XCreateFontCursor(dpy, XC_lr_angle);
    pointer3 = XCreateFontCursor(dpy, XC_ll_angle);
    pointer4 = XCreateFontCursor(dpy, XC_ur_angle);

    /* grab the pointer */
    status = XGrabPointer(dpy, root, False, ButtonPressMask,
              GrabModeAsync, GrabModeAsync, root,
              pointer1, CurrentTime);
 //    if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");
//if (status != GrabSuccess) fprintf( stderr, "Can't grab the mouse.\n");

    /* create a graphics context to draw with */
    gc = XCreateGC(dpy, parent, 0, NULL);
 //   if (!gc) Fatal_Error("Could not get drawing resources.");
if (!gc) fprintf( stderr, "Could not get drawing resources.\n");
    XSetSubwindowMode(dpy, gc, IncludeInferiors);
    XSetForeground(dpy, gc, 255);
    XSetFunction(dpy, gc, GXinvert);

    /* get a button-press and pull out the root location */
    XMaskEvent(dpy, ButtonPressMask, &event);
    rootx = rx = event.xbutton.x_root;
    rooty = ry = event.xbutton.y_root;

    /* get pointer motion events */
    XChangeActivePointerGrab(dpy, ButtonMotionMask | ButtonReleaseMask, pointer2, CurrentTime);

    /* loop to let the user drag a rectangle */
    while (!selectionDone) {
    XNextEvent(dpy, &event);
    switch(event.type) {
    
    case ButtonRelease:
        if (boxDrawn) {
        XDrawRectangle(dpy, parent, gc, rx, ry, rw, rh);
        boxDrawn = False;
        }
        XFlush(dpy);
        /* record the final location */
        MAKE_RECT(xbutton);
        selectionDone = True;
        break;

    case MotionNotify:
        if (boxDrawn) {
        XDrawRectangle(dpy, parent, gc, rx, ry, rw, rh);
        boxDrawn = False;
        }
    
//      while (XCheckTypedEvent(dpy, MotionNotify, &event)) { }
        MAKE_RECT(xmotion);

        XDrawRectangle(dpy, parent, gc, rx, ry, rw, rh);
        boxDrawn = True;
        MAKE_CURSOR(xmotion);
        XChangeActivePointerGrab(dpy,
                     ButtonMotionMask | ButtonReleaseMask,
                     pointer, CurrentTime);
        break;
    }
    }

    xrect.x      = rx;
    xrect.y      = ry;
    xrect.width  = rw;
    xrect.height = rh;

    /* release resources */
    XFreeGC(dpy, gc);
    XFreeCursor(dpy, pointer1);
    XFreeCursor(dpy, pointer2);
    XFreeCursor(dpy, pointer3);
    XFreeCursor(dpy, pointer4);

    XUngrabPointer(dpy, CurrentTime);
    int x1 = rx + rw, y1 = ry + rh;
    if ( display_width < x1 ) x1 = rx - rw;
    if ( display_height < y1 ) y1 = ry - rh;
printf("\n%dx%d+%d+%d", rw, rh, rx, ry);
    return xrect;
}             
int main(int argc, char* argv[])
{
  Display* dpy;     /* pointer to X Display structure.           */
  int screen_num;       /* number of screen to place the window on.  */
  Window win;           /* pointer to the newly created window.      */
  unsigned int display_width,
               display_height;  /* height and width of the X display.        */
  unsigned int width, height;   /* height and width for the new window.      */
  unsigned int win_x, win_y;    /* location of the window's top-left corner. */
  unsigned int win_border_width; /* width of window's border.                */
  char *display_name = getenv("DISPLAY");  /* address of the X display.      */
  Atom skip, state;
  dpy = XOpenDisplay(display_name);
  if (dpy == NULL) {
    fprintf(stderr, "%s: cannot connect to X server '%s'\n",
            argv[0], display_name);
    exit(1);
  }

  /* get the geometry of the default screen for our display. */
  screen_num = DefaultScreen(dpy);
  display_width = DisplayWidth(dpy, screen_num);
  display_height = DisplayHeight(dpy, screen_num);
// display_width =1280;
// display_height =800;
 char *window_name = (char*)"Drawing window";
 int whiteColor = WhitePixel(dpy, screen_num);

  Window root = RootWindow(dpy,screen_num);
    XVisualInfo vinfo;
    XMatchVisualInfo(dpy, DefaultScreen(dpy), 32, TrueColor, &vinfo);

    XSetWindowAttributes attr;
    attr.colormap = XCreateColormap(dpy, root, vinfo.visual, AllocNone);
    attr.border_pixel = 0;
    attr.background_pixel = 0;
    attr.override_redirect = 1;
    attr.border_pixel = 0;
    Window parent = XCreateWindow(dpy, root, 0, 0, display_width, display_height, 0, vinfo.depth, InputOutput, vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr);

  state = XInternAtom(dpy, "_NET_WM_STATE", True);
  skip = XInternAtom(dpy, "_NET_WM_STATE_SKIP_TASKBAR", True);

    Atom wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
    XSetWMProtocols(dpy, parent, &wm_delete_window, 1);
    XChangeProperty(dpy, parent, state, XA_ATOM, 32,
          PropModeReplace, (unsigned char*)&skip, 1);

 XStoreName(dpy, parent, window_name);
 XMapWindow(dpy, parent);
 XSelectInput(dpy, parent, StructureNotifyMask| ButtonPressMask| PointerMotionMask| LeaveWindowMask| EnterWindowMask| ButtonReleaseMask );
 Drawable d = parent;
 XGCValues values;
 values.line_width = 4;
 values.line_style = LineSolid;
 GC gc = XCreateGC(dpy, d, GCLineWidth, &values);

 Select_Rect(dpy, screen_num, root, parent, display_width, display_height);
 

  return 0;
}

#define MAKE_RECT(etype)http://web.mit.edu/graphics/src/xgrabsc/xgrabsc.c使用

于 2021-07-28T13:30:49.970 回答