Ejemplo n.º 1
0
def get_parent_window_infos():
    """Determines the window id of each
    terminal which displays the program using
    this layer.

    Returns:
        list of TerminalWindowInfo
    """
    window_infos = []
    clients_pid_tty = {}

    if tmux_util.is_used():
        clients_pid_tty = tmux_util.get_client_ttys_by_pid()
    else:
        clients_pid_tty = {psutil.Process().pid: None}

    if clients_pid_tty:
        pid_window_id_map = get_pid_window_id_map()

        for pid, pty in clients_pid_tty.items():
            ppids = get_parent_pids(pid)
            wid = get_first_window_id(pid_window_id_map, ppids)

            if pty is None and not os.isatty(sys.stdout.fileno()):
                # note: this method won't return the desired pseudo tty
                #       if ueberzug runs in tmux
                #       (pty shouldn't be None in this case anyways)
                pty = get_first_pty(ppids)

            if wid:
                window_infos.append(TerminalWindowInfo(wid, pty))

    return window_infos
Ejemplo n.º 2
0
def get_parent_window_infos(display: X.Display):
    """Determines the window id of each
    terminal which displays the program using
    this layer.

    Returns:
        list of TerminalWindowInfo
    """
    window_infos = []
    client_pids = {}

    if tmux_util.is_used():
        client_pids = tmux_util.get_client_pids()
    else:
        client_pids = {process.get_own_pid()}

    if client_pids:
        pid_window_id_map = get_pid_window_id_map(display)

        for pid in client_pids:
            ppids = get_parent_pids(pid)
            ppid_window_id_map = key_intersection(pid_window_id_map, ppids)
            try:
                window_pid, window_id = next(
                    iter(sort_by_key_list(ppid_window_id_map, ppids)))
                window_children_pids = ppids[:ppids.index(window_pid)][::-1]
                pty = get_first_pty(window_children_pids)
                window_infos.append(TerminalWindowInfo(window_id, pty))
            except StopIteration:
                # Window needs to be mapped,
                # otherwise it's not listed in _NET_CLIENT_LIST
                pass

    return window_infos
Ejemplo n.º 3
0
def get_parent_window_infos():
    """Determines the window id of each
    terminal which displays the program using
    this layer.

    Returns:
        list of TerminalWindowInfo
    """
    window_infos = []
    clients_pid_tty = {}
    environ_window_id = os.environ.get('WINDOWID')

    if tmux_util.is_used():
        clients_pid_tty = tmux_util.get_client_ttys_by_pid()
    elif environ_window_id is not None:
        window_infos.append(TerminalWindowInfo(int(environ_window_id)))
    else:
        clients_pid_tty = {psutil.Process().pid: None}

    if clients_pid_tty:
        pid_window_id_map = get_pid_window_id_map()

        for pid, pty in clients_pid_tty.items():
            wid = get_first_window_id(pid_window_id_map, get_parent_pids(pid))
            if wid:
                window_infos.append(TerminalWindowInfo(wid, pty))

    return window_infos
Ejemplo n.º 4
0
def main(options):
    if options['--silent']:
        try:
            outfile = os.open(os.devnull, os.O_WRONLY)
            os.close(sys.stderr.fileno())
            os.dup2(outfile, sys.stderr.fileno())
        finally:
            os.close(outfile)

    display = xutil.get_display()
    screen = display.screen()
    window_infos = xutil.get_parent_window_infos()
    loop = asyncio.get_event_loop()
    executor = thread.DaemonThreadPoolExecutor(max_workers=2)
    parser_object = (parser.ParserOption(options['--parser'])
                     .parser_class())
    image_loader = (loading.ImageLoaderOption(options['--loader'])
                    .loader_class())
    error_handler = error_processor_factory(parser_object)
    view = View()
    tools = Tools(image_loader, parser_object, error_handler)
    window_factory = ui.OverlayWindow.Factory(display, view)
    windows = batch.BatchList(window_factory.create(*window_infos))
    image_loader.register_error_handler(error_handler)
    view.screen_width = screen.width_in_pixels
    view.screen_height = screen.height_in_pixels

    if tmux_util.is_used():
        atexit.register(setup_tmux_hooks())
        view.offset = tmux_util.get_offset()

    with windows, image_loader:
        loop.set_default_executor(executor)

        for sig in (signal.SIGINT, signal.SIGTERM):
            loop.add_signal_handler(
                sig, shutdown_factory(loop))

        loop.add_signal_handler(
            signal.SIGUSR1,
            lambda: asyncio.ensure_future(query_windows(
                window_factory, windows, view)))

        loop.add_signal_handler(
            signal.SIGWINCH,
            lambda: asyncio.ensure_future(
                reset_terminal_info(windows)))

        asyncio.ensure_future(process_xevents(loop, display, windows))
        asyncio.ensure_future(process_commands(
            loop, shutdown_factory(loop),
            windows, view, tools))

        try:
            loop.run_forever()
        finally:
            loop.close()
            executor.shutdown(wait=False)
Ejemplo n.º 5
0
def main_layer(options):
    display = xutil.get_display()
    window_infos = xutil.get_parent_window_infos()
    loop = asyncio.get_event_loop()
    executor = futures.ThreadPoolExecutor(max_workers=2)
    shutdown_routine = shutdown(loop)  #pylint: disable=E1111
    parser_class = parser.ParserOption(options['--parser']).parser_class
    media = {}
    window_factory = ui.OverlayWindow.Factory(display, media)
    windows = batch.BatchList(window_factory.create(*window_infos))

    if tmux_util.is_used():
        atexit.register(setup_tmux_hooks())

    with windows:
        # this could lead to unexpected behavior,
        # but hey otherwise it breaks exiting the script..
        # as readline for example won't return till a line was read
        # and there's no (already integrated) way to
        # disable it only for a specific threadpoolexecutor
        # see: https://github.com/python/cpython/blob/master/Lib/concurrent/futures/thread.py#L33
        # -> TODO: reimplement ThreadPoolExecutor
        atexit.unregister(futures.thread._python_exit)  #pylint: disable=W0212
        loop.set_default_executor(executor)

        for sig in (signal.SIGINT, signal.SIGTERM):
            loop.add_signal_handler(
                sig, functools.partial(asyncio.ensure_future,
                                       shutdown_routine))

        loop.add_signal_handler(
            signal.SIGUSR1, lambda: asyncio.ensure_future(
                query_windows(window_factory, windows)))

        asyncio.ensure_future(main_xevents(loop, display, windows))
        asyncio.ensure_future(
            main_commands(loop, shutdown_routine, parser_class(), windows,
                          media))

        try:
            loop.run_forever()
        finally:
            loop.close()
            executor.shutdown(wait=False)
Ejemplo n.º 6
0
def get_parent_window_infos(window_id_override=None):
    """Determines the window id of each
    terminal which displays the program using
    this layer.

    Returns:
        list of TerminalWindowInfo
    """
    window_infos = []
    client_pids = {}

    if tmux_util.is_used():
        client_pids = tmux_util.get_client_pids()
    else:
        client_pids = {process.get_own_pid()}

    if client_pids:
        pid_window_id_map = get_pid_window_id_map()

        for pid in client_pids:
            ppids = get_parent_pids(pid)
            ppid_window_id_map = key_intersection(pid_window_id_map, ppids)
            try:
                window_pid, window_ids = next(
                    iter(sort_by_key_list(ppid_window_id_map, ppids)))
                if window_id_override:
                    if window_id_override not in window_ids:
                        raise ValueError(
                            "Given window id '%s' does not belongs to current pid '%s'."
                            % (window_id_override, window_pid))
                    window_id = window_id_override
                else:
                    window_id = window_ids[0]
                window_children_pids = ppids[:ppids.index(window_pid)][::-1]
                pty = get_first_pty(window_children_pids)
                window_infos.append(TerminalWindowInfo(window_id, pty))
            except StopIteration:
                # Window needs to be mapped,
                # otherwise it's not listed in _NET_CLIENT_LIST
                pass

    return window_infos
Ejemplo n.º 7
0
def main_layer(options):
    display = xutil.get_display()
    window_infos = xutil.get_parent_window_infos()
    loop = asyncio.get_event_loop()
    executor = thread.DaemonThreadPoolExecutor(max_workers=2)
    parser_class = parser.ParserOption(options['--parser']).parser_class
    view = ui.View()
    window_factory = ui.OverlayWindow.Factory(display, view)
    windows = batch.BatchList(window_factory.create(*window_infos))

    if tmux_util.is_used():
        atexit.register(setup_tmux_hooks())
        view.offset = tmux_util.get_offset()

    if options['--silent']:
        sys.stderr = open('/dev/null', 'w')

    with windows:
        loop.set_default_executor(executor)

        for sig in (signal.SIGINT, signal.SIGTERM):
            loop.add_signal_handler(
                sig, shutdown_factory(loop))

        loop.add_signal_handler(
            signal.SIGUSR1,
            lambda: asyncio.ensure_future(query_windows(
                window_factory, windows, view)))

        asyncio.ensure_future(main_xevents(loop, display, windows))
        asyncio.ensure_future(main_commands(
            loop, shutdown_factory(loop), parser_class(),
            windows, view))

        try:
            loop.run_forever()
        finally:
            loop.close()
            executor.shutdown(wait=False)