def _activity_monitor(self): """Sample user activity """ display = Xlib.display.Display() km_mon = self._keyboard_mouse_activity_monitor() while True: gevent.sleep(UPDATE_INTERVAL) #gevent.Timeout(UPDATE_INTERVAL).start() #try: # gevent.sleep(UPDATE_INTERVAL * 2) #except gevent.Timeout, t: # pass if self._day_has_changed(): self.save_log() self._create_empty_log() window = display.get_input_focus().focus application_name, title = self._get_application_name(window) self._last_application = (application_name, title) activity_ms = next(km_mon) if activity_ms and activity_ms < 20: # Do not count if the user has been idle for > 20 seconds self._app_usage.update([application_name,]) self._app_usage_detail[application_name].update([title,]) self._totcnt += 1 self._update_km_activity(activity_ms)
def active_window_title() : window = display.get_input_focus().focus wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() return wmname
def active_window_title(): window = display.get_input_focus().focus wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() return wmname
def get_foreground_window_title(self) -> Optional[str]: display = Xlib.display.Display() window = display.get_input_focus().focus wm_class = window.get_wm_class() if wm_class is None: window = window.query_tree().parent wm_class = window.get_wm_class() winclass = wm_class[1] return winclass
def get_active_window_wm_class(display=Xlib.display.Display()): """Get active window's WM_CLASS""" current_window = display.get_input_focus().focus pair = get_class_name(current_window) if pair: # (process name, class name) return str(pair[1]) else: return ""
def mouse_click(window, mouse_button): old_focus = display.get_input_focus() #display.set_input_focus(window, Xlib.X.RevertToPointerRoot, Xlib.X.CurrentTime) #display.sync() fake_input(display, Xlib.X.ButtonPress, mouse_button) display.sync() fake_input(display, Xlib.X.ButtonRelease, mouse_button) display.sync()
def get_active_window_wm_name(quiet, display=Xlib.display.Display()): """Get active window's WM_CLASS""" atom = display.intern_atom('_NET_WM_NAME') current_window = display.get_input_focus().focus name = get_window_name(current_window, atom) if not quiet: print(name) if name: # (process name, class name) return str(name.decode('utf-8')) else: return ""
def current_app(x): """ Metric function for MIDAS that returns the app currently in focus """ display = Xlib.display.Display() window = display.get_input_focus().focus wmclass = window.get_wm_class() if wmclass is None: window = window.query_tree().parent wmclass = window.get_wm_class() return(wmclass[1])
def send_key(emulated_key): window = display.get_input_focus()._data["focus"]; # Generate the correct keycode keysym = Xlib.XK.string_to_keysym(emulated_key) keycode = display.keysym_to_keycode(keysym) # Send a fake keypress via xtest Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) display.sync() time.sleep(0.5) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) display.sync()
def get_focused_window(): #BUG: Certain applications returning None for wm_name #connects to default display display = Xlib.display.Display() #gets current window in foucs w = display.get_input_focus().focus w_name, w_class = w.get_wm_name(), w.get_wm_class() if not w_name or not w_class: w = w.query_tree().parent if not w_name: w_name = w.get_wm_name() if not w_class: w_class = w.get_wm_class() return (w_name, w_class)
def getActiveWindow(self): import Xlib.display display = Xlib.display.Display() window = display.get_input_focus().focus root = window.query_tree().root.id wmname = "ok" while window.query_tree().parent.id != root: if not wmname: #wmname = window.get_wm_name() wmname="ok" window = window.query_tree().parent geo = window.get_geometry() return dict(name=wmname.decode('utf8','ignore'),x=geo.x,y=geo.y,w=geo.width,h=geo.height)
def get_name_window(window): #print window.name global last_window if window.name == None or window.name == '': display = Xlib.display.Display() window = display.get_input_focus().focus wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() last_window = wmname else: last_window = window.name return last_window
def send_key(keycode, shift_down, ctrl_down, alt_down, meta_down): print "send_key", keycode, shift_down, ctrl_down, alt_down, meta_down if UseXTest: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) else: mod_mask = 0 if shift_down: mod_mask |= Xlib.X.ShiftMask if ctrl_down: mod_mask |= Xlib.X.ControlMask # I don't know which masks alt and meta/windows correspond to; # these are just guesses. if alt_down: mod_mask |= Xlib.X.Mod1Mask if meta_down: mod_mask |= Xlib.X.Mod2Mask window = display.get_input_focus()._data["focus"] event = Xlib.protocol.event.KeyPress(time=int(time.time()), root=display.screen().root, window=window, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=mod_mask, detail=keycode) window.send_event(event, propagate=True) event = Xlib.protocol.event.KeyRelease(time=int(time.time()), root=display.screen().root, window=window, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=mod_mask, detail=keycode) window.send_event(event, propagate=True)
def get_active_window(display=Xlib.display.Display()): """Get active window's WM_CLASS""" window = display.get_input_focus().focus try: while True: wmname = window.get_wm_name() wmclass = window.get_wm_class() # workaround for Java app # https://github.com/JetBrains/jdk8u_jdk/blob/master/src/solaris/classes/sun/awt/X11/XFocusProxyWindow.java#L35 if (wmclass is None and wmname is None) or "FocusProxy" in wmclass: window = window.query_tree().parent if window: continue break return Window(wm_class=str(wmclass[1]), id=window.id) except: pass return Window(wm_class="", id=None)
def send_key(keycode, shift_down, ctrl_down, alt_down, meta_down): print "send_key", keycode, shift_down, ctrl_down, alt_down, meta_down if UseXTest: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) else: mod_mask = 0 if shift_down: mod_mask |= Xlib.X.ShiftMask if ctrl_down: mod_mask |= Xlib.X.ControlMask # I don't know which masks alt and meta/windows correspond to; # these are just guesses. if alt_down: mod_mask |= Xlib.X.Mod1Mask if meta_down: mod_mask |= Xlib.X.Mod2Mask window = display.get_input_focus()._data["focus"] event = Xlib.protocol.event.KeyPress( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = mod_mask, detail = keycode ) window.send_event(event, propagate = True) event = Xlib.protocol.event.KeyRelease( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = mod_mask, detail = keycode ) window.send_event(event, propagate = True)
def current_app(x): """ Metric function for MIDAS that returns the app currently in focus """ try: display = Xlib.display.Display() window = display.get_input_focus().focus wmclass = window.get_wm_class() if wmclass is None: window = window.query_tree().parent wmclass = window.get_wm_class() display.close() del display if wmclass: return(wmclass[1]) else: return('UNKNOWN') except: return('ERROR!')
def current_app(x): """ Metric function for MIDAS that returns the app currently in focus """ try: display = Xlib.display.Display() window = display.get_input_focus().focus wmclass = window.get_wm_class() if wmclass is None: window = window.query_tree().parent wmclass = window.get_wm_class() display.close() del display if wmclass: return (wmclass[1]) else: return ('UNKNOWN') except: return ('ERROR!')
#!/usr/bin/python import Xlib.display import Xlib.X def handle_event(event): print event display = Xlib.display.Display() root = display.screen().root focused = display.get_input_focus().focus print focused wmname = focused.get_wm_name() wmclass = focused.get_wm_class() if wmclass is None and wmname is None: focused = focused.query_tree().parent wmname = focused.get_wm_name() print "WM Name: %s" % (wmname, ) # Tell the X server we want to catch KeyRelease events. root.change_attributes(Xlib.X.PropertyChangeMask) while True: event = root.display.next_event() print event
import Xlib.X import Xlib.XK import Xlib.protocol.event UseXTest = True try: import Xlib.ext.xtest except ImportError: UseXTest = False print "no XTest extension; using XSendEvent" import sys, time display = Xlib.display.Display() window = display.get_input_focus()._data["focus"] if UseXTest and not display.query_extension("XTEST"): UseXTest = False special_X_keysyms = { '\b': "BackSpace", ' ': "space", '\t': "Tab", '\n': "Return", # for some reason this needs to be cr, not lf '\r': "Return", '\e': "Escape", '!': "exclam", '#': "numbersign", '%': "percent", '$': "dollar",
import Xlib.display display = Xlib.display.Display() window = display.get_input_focus().focus wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() print("WM Name: %s" % (wmname, ))
import Xlib.X import Xlib.XK import Xlib.protocol.event UseXTest = True try : import Xlib.ext.xtest except ImportError: UseXTest = False print("no XTest extension; using XSendEvent") import sys, time display = Xlib.display.Display() window = display.get_input_focus()._data["focus"]; if UseXTest and not display.query_extension("XTEST") : UseXTest = False special_X_keysyms = { '\b' : "BackSpace", ' ' : "space", '\t' : "Tab", '\n' : "Return", # for some reason this needs to be cr, not lf '\r' : "Return", '\e' : "Escape", '!' : "exclam", '#' : "numbersign", '%' : "percent", '$' : "dollar",
ev = m_type(time=0, child=0, state=m_state, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=key_code) window.send_event(ev) display.sync() #}}} if __name__ == "__main__": # Open display try: display = Xlib.display.Display() except: print >> sys.stderr, "Failed to open the display" sys.exit(1) # Get the active window (widget) focus_widget = display.get_input_focus().focus # Get wm title match_title = title = window_name(focus_widget) uninteresting_strings = (" - Mozilla Firefox", " - Chromium", " - Opera") for string in uninteresting_strings: match_title = match_title.replace(string, "") print "Password match against: ", match_title # Load the passfile pass_text = "" pw_data = os.popen("gpg -d --use-agent --batch -q --no-tty <~/.myshadow").readlines() if len(pw_data) == 0: error_message("Failed to decrypt the shadow file.") sys.exit(1)
import configparser import Xlib.display from subprocess import call display = Xlib.display.Display() window = display.get_input_focus().focus wmclass = window.get_wm_class() # Options theme = "default" i3path = '/home/robin/.config/i3/' i3themerpath = '/home/robin/.config/i3/themer/' config = configparser.ConfigParser() config.read(i3themerpath+'themes/'+theme+'.ini') def renderi3(window): template = open(i3themerpath+'theme.template').read() for key in config[window]: template = template.replace(key, config[window][key]) outputConf = open(i3path+'config', 'w') outputConf.truncate() outputConf.write(template) def renderi3blocks(window): template = open(i3themerpath+'i3blocks.template').read() for key in config[window]: template = template.replace(key, config[window][key])
def fetch_window_title(self) -> str: display = Xlib.display.Display() window_in_focus = display.get_input_focus().focus return window_in_focus.get_wm_name()
def getActiveWindow_Linux(): try: import wnck except ImportError: if DEBUG is not None: print("wnck is not installed") wnck = None if wnck is not None: #TRY WITH WNCK screen = wnck.screen_get_default() # Recommended per wnck documentation screen.force_update() window = screen.get_active_window() if window is not None: return psutil.Process(window.get_pid()).name() else: try: import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Wnck G = "Installed" except ImportError: if DEBUG is not None: print("gi.repository not installed") G = None if G is not None: #TRY WITH GTK WNCK # Necessary if not using a Gtk.main() loop Gtk.init([]) screen = Wnck.Screen.get_default() # Recommended per Wnck documentation screen.force_update() active_window = screen.get_active_window() pid = active_window.get_pid() return psutil.Process(pid).name() else: try: from ewmh import EWMH ewmh = EWMH() except ImportError: if DEBUG is not None: print("EWMH not installed") ewmh = None if ewmh is not None: #TRY WITH EXTENDED XLib win = ewmh.getActiveWindow() return psutil.Process(ewmh.getWmPid(win)).name() else: try: import Xlib.display X = "Installed" except ImportError: X = None if X is not None: #TRY WITH Xlib (different result) display = Xlib.display.Display() window = display.get_input_focus().focus pid = window.get_wm_pid wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() return wmname #If nothing happened return None
def main(): offHeight = 0; offWidth = 0; offX = 0; offY = 0; dir = Direction.NONE try: opts, args = getopt.getopt(sys.argv[1:],"hx:y:d:a:b",["offHeight=","offWidth=", "offX=", "offY=","direction=", "help"]) except getopt.GetoptError as err: # print help information and exit: print str(err) usage() sys.exit(2) for opt, arg in opts: if opt in ('-h', "--help"): help() sys.exit() elif opt in ('-a', "--offHeight"): offHeight = int(arg) elif opt in ('-b', "--offWidth"): offWidth = int(arg) elif opt in ('-x', "--offX"): offX = int(arg) elif opt in ('-y', "--offY"): offY = int(arg) elif opt in ('-d', "--direction"): dir = int(arg) window = gtk.Window() # the screen contains all monitors screen = window.get_screen() # collect data about each monitor curMon = screen.get_monitor_at_window(screen.get_active_window()) currMonX, currMonY, currMonWidth, currMonHeight = screen.get_monitor_geometry(curMon) display = Xlib.display.Display() fcsWin = display.get_input_focus().focus fcsWinNude = None fcsWinNudeGeo = None fcsWinHolder = fcsWin # find the window with decoration and without while fcsWinNude is None: fcsWinHolderGeo = fcsWinHolder.get_geometry() if fcsWinHolderGeo.width <= 1 or fcsWinHolderGeo.height <= 1: fcsWinHolder = fcsWinHolder.query_tree().parent continue fcsWinNude = fcsWinHolder fcsWinNudeGeo = fcsWinHolderGeo fcsWinClthd = fcsWinNude.query_tree().parent fcsWinClthdGeo = fcsWinClthd.get_geometry() # print "screen.get_monitor_geometry(curMon): %s" % screen.get_monitor_geometry(curMon) # print "fcsWinGeo: %s" % fcsWin.get_geometry() # print "fcsWinNudeGeo: %s" % fcsWinNudeGeo # print "fcsWinClthdGeo: %s" % fcsWinClthdGeo clthHeight = fcsWinClthdGeo.height - fcsWinNudeGeo.height clthWidth = fcsWinClthdGeo.width - fcsWinNudeGeo.width # print "--------------------------------" # print "clthWidth: %s" % clthWidth # print "clthHeight: %s" % clthHeight if dir == Direction.NONE: print "no direction given" sys.exit(2) if dir > 8 or dir < 0: print "not acceptable direction given" sys.exit(2) # halfs if dir == Direction.UP: newX = currMonX newY = currMonY newWidth = currMonWidth + offWidth newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.LEFT: newX = currMonX newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight - clthHeight + offHeight elif dir == Direction.RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight - clthHeight + offHeight elif dir == Direction.DOWN: newX = currMonX newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth + offWidth newHeight = currMonHeight/2 - clthHeight + offHeight/2 # quarters elif dir == Direction.UP_RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.DOWN_RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.DOWN_LEFT: newX = currMonX newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.UP_LEFT: newX = currMonX newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 # max size elif dir == Direction.ALL: newX = currMonX newY = currMonY newWidth = currMonWidth + offWidth newHeight = currMonHeight - clthHeight + offHeight # default else: # should not be possible sys.exit(2) newX += offX newY += offY # print "--------------------------------" # print "newWidth: %s" % newWidth # print "newHeight: %s" % newHeight # print "newX: %s" % newX # print "newY: %s" % newY fcsWinNude.configure(width = newWidth, height = newHeight, x = newX, y = newY) display.sync()
import Xlib.display display = Xlib.display.Display() focus = display.get_input_focus() print "WM Class: %s" % ( focus.focus.get_wm_class(), ) print "WM Name: %s" % ( focus.focus.get_wm_name(), ) X11 Idle Time using the XScreenSaver extension import ctypes import os http://thp.io/2007/09/x11-idle-time-and-focused-window-in.html http://www.freedesktop.org/software/ConsoleKit/doc/ConsoleKit.html#Session:idle-hint class XScreenSaverInfo( ctypes.Structure): """ typedef struct { ... } XScreenSaverInfo; """ _fields_ = [('window', ctypes.c_ulong), # screen saver window ('state', ctypes.c_int), # off,on,disabled ('kind', ctypes.c_int), # blanked,internal,external ('since', ctypes.c_ulong), # milliseconds ('idle', ctypes.c_ulong), # milliseconds ('event_mask', ctypes.c_ulong)] # events xlib = ctypes.cdll.LoadLibrary( 'libX11.so') dpy = xlib.XOpenDisplay( os.environ['DISPLAY']) root = xlib.XDefaultRootWindow( dpy) xss = ctypes.cdll.LoadLibrary( 'libXss.so') xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo) xss_info = xss.XScreenSaverAllocInfo() xss.XScreenSaverQueryInfo( dpy, root, xss_info) print "Idle time in milliseconds: %d" % ( xss_info.contents.idle, ) If
window.send_event(ev) display.sync() #}}} if __name__ == "__main__": # Open display try: display = Xlib.display.Display() except: print("Failed to open the display", file=sys.stderr) sys.exit(1) # Get the active window (widget) focus_widget = display.get_input_focus().focus # Get wm title match_title = title = window_name(focus_widget) uninteresting_strings = (" - Mozilla Firefox", " - Chromium", " - Opera") for string in uninteresting_strings: match_title = match_title.replace(string, "") print("Password match against: ", match_title) # Load the passfile pass_text = "" pw_data = os.popen( "gpg -d --use-agent --batch -q --no-tty <~/.myshadow").readlines() if len(pw_data) == 0: error_message("Failed to decrypt the shadow file.")
#!/usr/bin/python import Xlib.display import Xlib.X def handle_event(event) : print event display = Xlib.display.Display() root = display.screen().root focused = display.get_input_focus().focus print focused wmname = focused.get_wm_name() wmclass = focused.get_wm_class() if wmclass is None and wmname is None: focused = focused.query_tree().parent wmname = focused.get_wm_name() print "WM Name: %s" % ( wmname, ) # Tell the X server we want to catch KeyRelease events. root.change_attributes(Xlib.X.PropertyChangeMask) while True : event = root.display.next_event() print event