def getWins():
    '''
    getWins() -> retval.
    get all active window id and title.
    '''
    ewmh = EWMH()
    wins, winHDs = [], ewmh.getClientListStacking()

    for winHD in winHDs:
        try:
            title = ewmh.getWmName(winHD)
            pid = ewmh.getWmPid(winHD)
        except:
            continue
        if title is not None:
            wins.append((winHD, title, pid))

    return wins
Example #2
0
class Display:
    def __init__(self):
        self.display = display.Display()
        self.ewmh = EWMH(self.display)

        mod_map = self.display.get_modifier_mapping()
        syms = {}

        for key, index in KEY_TO_MOD_INDEX.items():
            mod = mod_map[index]
            sym = {key}
            syms[mod[0]] = {key}
            syms[mod[1]] = {key}

        self.mod_syms = syms

    def query_keymap_raw(self):
        return self.display.query_keymap()

    def query_keyboard(self):
        keymap = self.query_keymap_raw()
        chord = set()
        for index, num in enumerate(keymap):
            for bit in range(8):
                if num & (1 << bit):
                    keycode = 8 * index + bit
                    # print(keycode)
                    if keycode in self.mod_syms:
                        chord |= self.mod_syms[keycode]
                    for ind in range(4):
                        keysym = self.display.keycode_to_keysym(keycode, ind)
                        if not keysym:
                            continue
                        s = self.display.lookup_string(keysym)
                        if s:
                            chord |= {s}
                            break
        return chord

    def query_mouse(self):
        root = self.display.screen()['root']
        pointer = root.query_pointer()
        mask = pointer._data['mask']
        # print(mask)
        buttons = set()
        for button, motion in BUTTON_TO_MOTION_MASK.items():
            if motion & mask:
                buttons |= {button}
        return buttons

    def get_active_window(self):
        focus = self.display.get_input_focus()
        return focus._data['focus']

    def get_clipboard_owner_window(self, source):
        window = self.display.get_selection_owner(
                self.display.get_atom(source))
        while not window.get_wm_class():
            window = window.query_tree()._data['parent']
            if window == 0:
                return None
        return window

    def get_window_name(self, window):
        title = window.get_wm_name()
        if not title:
            title = window.get_full_text_property(Xatom.WM_NAME)
        if isinstance(title, bytes):
            return title.decode(errors='ignore')
        return title

    def get_window_pid(self, window):
        return self.ewmh.getWmPid(window)

    def focus_window(self, window):
        self.display.set_input_focus(window, X.RevertToParent, X.CurrentTime)
Example #3
0
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