我想知道是否可以使用Xlib反转区域的颜色。区域可以是由两个坐标(x1,y1到x2,y2)定义的矩形区域。下面的代码是脚本的最新修改版本,用于反转活动窗口的一部分,根据JvO的建议
import Xlib
from Xlib import X, display, Xutil
d = display.Display()
screen = d.screen()
bgsize = 20
act_win = d.get_input_focus().focus
wmname = act_win.get_wm_name()
wmclass = act_win.get_wm_class()
if wmclass is None and wmname is None:
act_win = act_win.query_tree().parent
wmname = act_win.get_wm_name()
print "Active Window: %s" % ( wmname, )
#
# Creating a pixmap of size 200x2000 from active window
#
pm = act_win.create_pixmap(200, 2000, screen.root_depth)
#
# Creating two graphics contexts, one to be inverted, and one normal
#
gc = pm.create_gc()
#
# Inverting the to be inverted graphics context
# Changes after looking at code at:
# https://github.com/alexer/python-xlib/blob/master/Xlib/protocol/structs.py
#
gc.change(function = X.GXcopyInverted)
#
# Copying the content of act_win to pix map using to_invert_gc
#
# copy_area(gc, src_drawable, src_x, src_y, width, height, dst_x, dst_y)
#
pm.copy_area(gc, act_win, 0, 0, 200, 2000, 0, 0, onerror=None)
#
# Copying back the content of pixmap to act_win using norm_gc
#
# copy_area(gc, src_drawable, src_x, src_y, width, height, dst_x, dst_y)
#
gc.change(function = X.GXcopy)
act_win.copy_area(gc, pm, 0, 0, 200, 2000, 0, 0, onerror=None)
以上代码的C版本如下:
#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
void main(void) {
Display *dpy;
Window root_window, focused;
Pixmap pm;
GC gc;
XGCValues gcv, gcv_ret;
int revert_to;
XTextProperty text_prop;
int screen;
dpy = XOpenDisplay(":0.0");
screen = DefaultScreen(dpy);
root_window = RootWindow (dpy, DefaultScreen(dpy));
XGetInputFocus(dpy, &focused, &revert_to);
XGetWMName(dpy, focused, &text_prop);
printf("Active Window name: %s\n", text_prop.value);
pm = XCreatePixmap(dpy, focused, 200, 200, 1);
gc = XCreateGC(dpy, focused, 0, NULL);
gcv.function = GXcopyInverted;
XChangeGC(dpy, gc, GCFunction, &gcv);
XFlushGC(dpy, gc);
XGetGCValues(dpy, gc, 1, &gcv_ret);
printf("Function while copying from focused window to pixmap: %d\n", gcv_ret.function);
XCopyArea(dpy, focused, pm, gc, 0, 0, 200, 200, 0, 0);
gcv.function = GXcopy;
XChangeGC(dpy, gc, GCFunction, &gcv);
XFlushGC(dpy, gc);
XGetGCValues(dpy, gc, 1, &gcv_ret);
printf("Function while copying from pixmap to focused window: %d\n", gcv_ret.function);
XCopyArea(dpy, pm, focused, gc, 50, 50, 200, 200, 0, 0);
XFlush(dpy);
}
使用gcc inv.c -o inv -lX11编译上述代码。编译成功,但运行代码不会导致聚焦窗口的任何部分反转
以下是输出:
urc@ubuntu-desktop: ~/Temporary Files$ ./inv
Active Window name: urc@ubuntu-desktop: ~/Temporary Files
Function while copying from focused window to pixmap: 12
Function while copying from pixmap to focused window: 3
urc@ubuntu-desktop: ~/Temporary Files$
问题似乎是将一个名为“子窗口模式”参数的参数设置为ClipByChildren(默认情况下)。将其设置为IncludeInferiors使其工作。解决方案在以下链接中找到:
https://groups.google.com/forum/#!topic/comp.windows.x/_TcGJq2uhmI
支持代码示例:
http://www.mit.edu/afs.new/sipb/user/web/src/xpunt/xpunt.c
以下是现在的工作代码:
#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
void main(void) {
Display *dpy;
Window root_window, focused, target, default_root_window;
Pixmap pm;
GC gc;
XGCValues gcv, gcv_ret;
int revert_to;
XTextProperty text_prop;
int screen, depth, whiteColor, blackColor;
XEvent e;
char text[1];
KeySym key;
dpy = XOpenDisplay(":0.0");
screen = DefaultScreen(dpy);
depth = DefaultDepth(dpy, screen);
whiteColor = WhitePixel(dpy, screen);
blackColor = BlackPixel(dpy, screen);
root_window = RootWindow (dpy, screen);
XGetInputFocus(dpy, &focused, &revert_to);
XGetWMName(dpy, focused, &text_prop);
printf("Active Window name: %s\n", text_prop.value);
target = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 1, whiteColor, blackColor);
XSelectInput(dpy, target, ButtonPressMask | StructureNotifyMask | ExposureMask | KeyPressMask | PropertyChangeMask | VisibilityChangeMask);
XMapRaised(dpy, target);
for(;;) {
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
pm = XCreatePixmap(dpy, focused, 200, 200, depth);
gc = XCreateGC(dpy, focused, 0, NULL);
XSetSubwindowMode(dpy, gc, IncludeInferiors);
gcv.function = GXcopyInverted;
XChangeGC(dpy, gc, GCFunction, &gcv);
XFlushGC(dpy, gc);
while(1) {
XNextEvent(dpy, &e);
if(e.type == Expose) {
XSetInputFocus(dpy, target, RevertToNone, CurrentTime);
} else if (e.type == MapNotify) {
printf("Map notify\n");
} else if (e.type == KeyPress) {
XLookupString(&e.xkey, text, 255, &key,0);
if (text[0] == 'c') {
printf("Copying plane\n");
XCopyArea(dpy, focused, target, gc, 0, 0, 200, 200, 0, 0);
XFlush(dpy);
}
if (text[0] == 'q') {
printf("Quitting ...\n");
break;
}
}
}
}
答案 0 :(得分:1)
您可以尝试以下操作:
请注意,这只是对RGB值进行位反转,这对于彩色图像看起来不太好,但对于黑白(灰度)效果很好。
答案 1 :(得分:0)
以下代码现在可以使用:
#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
void main(void) {
Display *dpy;
Window root_window, focused, target, default_root_window;
Pixmap pm;
GC gc;
XGCValues gcv, gcv_ret;
int revert_to;
XTextProperty text_prop;
int screen, depth, whiteColor, blackColor;
XEvent e;
char text[1];
KeySym key;
dpy = XOpenDisplay(":0.0");
screen = DefaultScreen(dpy);
depth = DefaultDepth(dpy, screen);
whiteColor = WhitePixel(dpy, screen);
blackColor = BlackPixel(dpy, screen);
root_window = RootWindow (dpy, screen);
XGetInputFocus(dpy, &focused, &revert_to);
XGetWMName(dpy, focused, &text_prop);
printf("Active Window name: %s\n", text_prop.value);
target = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 1, whiteColor, blackColor);
XSelectInput(dpy, target, ButtonPressMask | StructureNotifyMask | ExposureMask | KeyPressMask | PropertyChangeMask | VisibilityChangeMask);
XMapRaised(dpy, target);
for(;;) {
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
pm = XCreatePixmap(dpy, focused, 200, 200, depth);
gc = XCreateGC(dpy, focused, 0, NULL);
XSetSubwindowMode(dpy, gc, IncludeInferiors);
gcv.function = GXcopyInverted;
XChangeGC(dpy, gc, GCFunction, &gcv);
XFlushGC(dpy, gc);
while(1) {
XNextEvent(dpy, &e);
if(e.type == Expose) {
XSetInputFocus(dpy, target, RevertToNone, CurrentTime);
} else if (e.type == MapNotify) {
printf("Map notify\n");
} else if (e.type == KeyPress) {
XLookupString(&e.xkey, text, 255, &key,0);
if (text[0] == 'c') {
printf("Copying plane\n");
XCopyArea(dpy, focused, target, gc, 0, 0, 200, 200, 0, 0);
XFlush(dpy);
}
if (text[0] == 'q') {
printf("Quitting ...\n");
break;
}
}
}
}