gpt4 book ai didi

python - XMotionEvent : Segmentation faults 的 ctypes 和 XPeekIfEvent

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:31 25 4
gpt4 key购买 nike

<分区>

我在从 libX11 执行 XPeekIfEvent 时遇到问题,第二次调用时出现段错误。当我将 send_event 添加到结构时会发生这种情况。

如果你在代码中看到一个@NOTE,这是我给你的一个注释。

完整代码可在此处获得:https://github.com/Centril/sublime-autohide-sidebar/blob/9f3e505c1b78a474de569299fddb704e3dc77d8f/x.py

相关文档:

Xlib.hX.h 中的相关 C/C++ 定义:

// X.h
typedef unsigned long XID;
typedef XID Window;
typedef CARD32 XID;
typedef CARD32 Time;
typedef XID Window;

// Xlib.h
#define Bool int

typedef struct _XDisplay Display;
struct _XDisplay; /* Forward declare before use for C++ */

typedef struct {
int type; /* of event */
unsigned long serial; /* # of last request processed by server */
Bool send_event; /* true if this came from a SendEvent request */
Display *display; /* Display the event was read from */
Window window; /* "event" window reported relative to */
Window root; /* root window that the event occurred on */
Window subwindow; /* child window */
Time time; /* milliseconds */
int x, y; /* pointer x, y coordinates in event window */
int x_root, y_root; /* coordinates relative to root */
unsigned int state; /* key or button mask */
char is_hint; /* detail */
Bool same_screen; /* same screen flag */
} XMotionEvent;
typedef XMotionEvent XPointerMovedEvent;

typedef struct {
int type;
unsigned long serial; /* # of last request processed by server */
Bool send_event; /* true if this came from a SendEvent request */
Display *display;/* Display the event was read from */
Window window; /* window on which event was requested in event mask */
} XAnyEvent;

/*
* this union is defined so Xlib can always use the same sized
* event structure internally, to avoid memory fragmentation.
*/
typedef union _XEvent {
int type; /* must not be changed; first element */
XAnyEvent xany;
XKeyEvent xkey;
XButtonEvent xbutton;
XMotionEvent xmotion;
XCrossingEvent xcrossing;
XFocusChangeEvent xfocus;
XExposeEvent xexpose;
XGraphicsExposeEvent xgraphicsexpose;
XNoExposeEvent xnoexpose;
XVisibilityEvent xvisibility;
XCreateWindowEvent xcreatewindow;
XDestroyWindowEvent xdestroywindow;
XUnmapEvent xunmap;
XMapEvent xmap;
XMapRequestEvent xmaprequest;
XReparentEvent xreparent;
XConfigureEvent xconfigure;
XGravityEvent xgravity;
XResizeRequestEvent xresizerequest;
XConfigureRequestEvent xconfigurerequest;
XCirculateEvent xcirculate;
XCirculateRequestEvent xcirculaterequest;
XPropertyEvent xproperty;
XSelectionClearEvent xselectionclear;
XSelectionRequestEvent xselectionrequest;
XSelectionEvent xselection;
XColormapEvent xcolormap;
XClientMessageEvent xclient;
XMappingEvent xmapping;
XErrorEvent xerror;
XKeymapEvent xkeymap;
XGenericEvent xgeneric;
XGenericEventCookie xcookie;
long pad[24];
} XEvent;

我的代码如下(删除了一些不相关的部分......):

# License: GPL2+

# Typedefs:
class Display( Structure ):
_fields_ = []

Bool = c_int
Time = c_ulong
Atom = c_ulong
#Display = c_int
DisplayPtr = POINTER( Display )
Window = c_ulong
WindowPtr = POINTER( Window )
XPointer = c_char_p

# Typedefs: Structs & Unions & FunPtr:s:
class XAnyEvent( Structure ):
_fields_ = [
('type', c_int),
('serial', c_ulong),
('send_event', Bool),
('display', DisplayPtr ),
('window', Window)
]

class XMotionEvent( Structure ):
_fields_ = [
('type', c_int),
('serial', c_ulong),
('send_event', Bool),
('display', DisplayPtr ),
('window', Window),
('root', Window),
('subwindow', Window),
('time', Time),
('x', c_int), ('y', c_int),
('x_root', c_int), ('y_root', c_int),
('state', c_uint),
('is_hint', c_char),
('same_screen', Bool)
]

class XEvent( Union ):
_fields_ = [
('type', c_int),
('xany', XAnyEvent),
('xmotion', XMotionEvent)
]

EventPredicate = CFUNCTYPE( Bool, DisplayPtr, POINTER( XEvent ), XPointer )

# Constants:
MotionNotify = 6
PointerMotionMask = (1 << 6)

# @NOTE: irrelevant parts removed.
# @NOTE: Assume sublimes is a list of X11 Window:s but wrapped.

for w in sublimes: w.select_input( PointerMotionMask )

# NOTE: All of the below really happens in another thread:

def motion_predicate( d, e, a ):
return e.contents.type == MotionNotify
pred = EventPredicate( motion_predicate )

# Event pump:
atexit.register( self.stop )
event = XEvent()
while self.alive:
print( "a" )
print( "#6")
# NOTE: Here we fail!
x.XPeekIfEvent( disp, byref( event ), pred, None )
print( event )
print( "type", event.type )
print( "b" )

如果我在 XAnyEventXMotionEvent 中删除除 type 之外的所有字段它有效,否则它会因段错误而失败:

window 81788931 pid 2990 title ~/programming/sublime-autohide-sidebar/x.py (sublime-autohide-sidebar) - Sublime Text
window 82632928 pid 2990 title untitled - Sublime Text
#1
#1
<__main__.XEvent object at 0x7f9a873b4bf8>
#2 type 6
#3
#1
Segmentation fault (core dumped)

或者:

window 81788931 pid 2990 title ~/programming/sublime-autohide-sidebar/x.py (sublime-autohide-sidebar) - Sublime Text
window 82632928 pid 2990 title untitled - Sublime Text
#1
*** Error in `python3': malloc(): memory corruption (fast): 0x00007fb904001060 ***
Aborted (core dumped)

当我实际将鼠标移入其中一个窗口时会发生这种情况。

我错过了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com