Python-xlib
About Xlib, online related are less, can only get information from the library file Http://python-xlib.sourceforge.net/?page=home
The basic concept is that the program and X server establish a connection, then communicate, event and so on through this link. As for Pixmap, Graphic context and the like, but also need further study.
Sample:
#!/usr/bin/env Pyhon # message.py--example Program illustrating #import dialog #pygtk. PYGTK (' require ') I Mport GTK from collections import namedtuple from Xlib import X, display, Xutil, Xcursorfont (ScreenWidth, ScreenHeight) = Gtk.gdk.get_default_root_window (). Get_size () disp = display. Display () Rootwindow = Disp.screen (). Root wm_hints = Disp.intern_atom ("wm_hints", True) wm_state = Disp.intern_atom ("WM_ State ", true" Wm_desktop = Disp.intern_atom ("_net_wm_desktop", True) def Enumxlibwindow (): ' Enumerate child window of Rootwindow ' return Rootwindow.query_tree (). Children def findwindowbyproperty (Xlibwindow, atom=wm_state): ' ' Find Window ' result = Xlibwindow.query_tree (). Children if not Result:return None els
E:for Children in result:status = Children.get_property (Atom, wm_hints, 0, 0) if status: Child = children Else:child = FindwindoWbyproperty (children, Atom) return child def convertcoord (x, y, Width, height): "Cut out overlop the scree n ' ' xwidth = x + width yheight = y + height if x < 0 and y > 0 and y < Yheight < screenh Eight:return (0, y, width+x, height) if x < 0 and yheight > Screenheight:return (0, Y, WI Dth+x, Height-(yheight-screenheight)) if Xwidth > ScreenWidth and yheight > Screenheight:retur N (x, Y, Width-(xwidth-screenwidth), height-(yheight-screenheight)) if x > 0 and X < Xwidth < s Creenwidth and Yheight > Screenheight:return (x, y, Width, height-(yheight-screenheight)) if Xwid th > ScreenWidth and y > 0 and y < yheight < Screenheight:return (x, y, Width-xwidth-screenwidth , height) if x < 0 and y < 0:return (0, 0, xwidth, yheight) if x > 0 and X < Xwidt H < screenwidth and y < 0:return (x, 0, Width, yheight) if x > 0 and Xwidth > ScreenWidth and y < 0:retu RN (x, 0, Width-(xwidth-screenwidth), yheight) return (x, y, width, height) def getclientwindow (target): ' Enumerate Clientwindow ' status = Target.get_property (Wm_state, wm_hints, 0, 0) if Status:return targe
T client = Findwindowbyproperty (target) If Client:return client return target def Filterwindow (): "Without other window" windowlist = [] for Xlibwindow in Enumxlibwindow (): If Xlibwindow.get_proper Ty (wm_desktop, wm_hints, 0, 0): Windowlist.append (Xlibwindow) else:if Findwindowbyproper Ty (Xlibwindow, wm_desktop): Windowlist.append (Xlibwindow) return windowlist def getwindowcoord (Xlib Window): ' Covert Xlibwindow ' s coord ' Clientwindow = Getclientwindow (Xlibwindow) if Xlibwindow!= Clientwi Ndow:x = XLIBWINdow.get_geometry (). x + clientwindow.get_geometry (). x y = Xlibwindow.get_geometry (). Y + clientwindow.get_geometry ( ). y-26 width = clientwindow.get_geometry (). Width height = clientwindow.get_geometry (). Height + E lse:x = Xlibwindow.get_geometry (). x y = Xlibwindow.get_geometry (). Y width = Xlibwindow.get_geomet Ry (). Width height = xlibwindow.get_geometry (). Height return (x, y, width, height) def getscreenshotwindowinfo (): ' Return (x, y, width, height) ' Coordinfo = namedtuple (' coord ', ' x y width height ') screenshotwindowi
NFO = [] Screenshotwindowinfo.append (coordinfo (0, 0, screenwidth, screenheight)) for Eachwindow in Filterwindow (): (x, y, width, height) = Getwindowcoord (Eachwindow) screenshotwindowinfo.append (Coordinfo *convertcoord (x, Y, width, height)) return screenshotwindowinfo if __name__ = = "__main__": #message = gtk. Messagedialog (TYPE=GTK. Message_error, buttons=Gtk. BUTTONS_OK) #message. Set_markup ("An example Error popup") #message. Run () print getscreenshotwindowinfo ()
A small sample of Xlib:
#/usr/bin/env python import sys, OS import Xlib from Xlib import X, display, Xutil #change path so we find Xlib #sys. Pat
H.insert (1, Os.path.join (sys.path[0], ' ... ')) #Application window class Window:def __init__ (self, display): SELF.D = display #Find which s
Creen to open the window on self.screen = Self.d.screen () #background Pattern bgsize = 20
BGPM = Self.screen.root.create_pixmap (Bgsize, Bgsize, self.screen.root_depth ) BGGC = self.screen.root.create_gc (foreground = Self.screen.black_pixel, ba Ckground = Self.screen.black_pixel) bggc.change (foreground=self.screen.white_pixel) bgpm
. Arc (BGGC,-BGSIZE/2, 0, Bgsize, bgsize, 0, 360*64) bgpm.arc (BGGC, BGSIZE/2, 0, Bgsize, bgsize, 0, 360*64) Bgpm.arc (BGGC, 0,-BGSIZE/2, bgsize, bgsize, 0, 360*64) bgpm.arc (BGGC, 0, BGSIZE/2, Bgsize, Bgsize, 0, 360*64) #Actual window Self.window = Self.screen.root.create_window (100,
0, Self.screen.root_depth, X.inputoutput, X.copyfromparent,
#special attribute Values Background_pixmap = bgpm, Event_mask = (
X.structurenotifymask |
X.buttonreleasemask), ColorMap = x.copyfromparent) #Set some WM info Self. Wm_delete_window = Self.d.intern_atom (' Wm_delete_window ') self.
Wm_protocols = Self.d.intern_atom (' Wm_protocols ') self.window.set_wm_name (' Xlib example:childwin.py ')
Self.window.set_wm_icon_name (' childwin.py ') self.window.set_wm_class (' Childwin ', ' xlibexample ') Self.window.set_wm_protocols ([Self.
Wm_delete_window]) self.window.set_wm_hints (flags = Xutil.statehint, Initial_state = xutil.normalstate) self.window.set_wm_normal_hints ( Flags = (Xutil.pposition | Xutil.psize | xutil.pminsize), min_width = x, min_height = #Map the window, makin G It visible self.window.map () #Child window (self.childwidth, self.childheight) = (20, 2
0) Self.childwindow = Self.window.create_window (self.childwidth, self.childheight, 0, Self.screen.root_depth, X.copyfromparent, x.copyfromparent, #special attribute va
Lues Background_pixel = self.screen.white_pixel, ColorMap = X.copyfromparent,) Self.childWindow.map () def main_loop (self): current = None while 1:e = sel F.d.next_event () #Window has been destroyed, quit if E.type = = X.destroynotify:sys.exit (0) elif E.type = = X.buttonrelease:
if E.detail = = 1:print "Moving child window." Self.childWindow.configure (X=E.EVENT_X-SELF.CHILDWIDTH/2, Y=e.eve NT_Y-SELF.CHILDHEIGHT/2) Self.d.flush () elif E.type = = X.clientmessage:if E.client_type = = self. WM_PROTOCOLS:FMT, data = E.data if FMT = + data[0] = = self. WM_DELETE_WINDOW:sys.exit (0) If __name__ = = "__main__": Window (display. Display ()). Main_loop ()
Python module gtk.keysyms http://www.pygtk.org/docs/pygtk/class-gdkkeymap.html
>a Keymap is a mapping the from a Keymap key to key values. You are canthink of a keymap key as a representation of a symbol printed on a Physicalkeyboard key. That is, it >contains three pieces of information. Itcontains the hardware keycode; This is the identifying number for a physicalkey. Second, it contains the level of the key. The >level indicates whichsymbol on the key is used, in a vertical direction. So in a standard Uskeyboard, the key with the number ' 1 ' on it also has the exclamation >point ("!") character on it. The level indicates whether "1" or "!" Symbol. The letter keys are considered to have a lowercase letter at level0, and a uppercase >letter at level 1, though only T He uppercase letter isprinted. Third, the Keymap key contains a group; Groups are not used Onstandard US keyboards, but are used in many >other. On a keyboardwith groups, there can is 3 or 4 symbols printed on a single key. The groupindicates movement in aHorizontal direction. Usually groups are >used fortwo different. In group 0, a key might have two englishcharacters, and in group 1 it might have two Hebrew. The hebrewcharacters is >printed on the key, next to the 中文版 characters.
Define callback functions to be ctrl+s, Ctrl+z, M+s, M+z, Escape, return the results of these key combinations.
def registerkeybinding (self, Keyeventname, callback):
' Register a keybinding '
self.keybindings[ Keyeventname] = callback