#!/usr/bin/python3

# yaws - Switch windows under Sway using Rofi
# Copyright (C) 2025 Damian Ludwig <ludwigd@fedoraproject.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import sys, os
from i3ipc import Connection as Sway

ROFI_ENTRY="{0}\0info\x1f{1}\x1fmeta\x1f{2}"

def print_window_list():
    windows = Sway().get_tree().leaves()
    for win in windows:
        if win.app_id:
            print(ROFI_ENTRY.format(win.name, win.id, win.app_id))
        else:
            print(ROFI_ENTRY.format(win.name, win.id, win.window_class))

def focus_window(id):
    Sway().command("[con_id={0}] focus".format(id))

if __name__=="__main__":
    if len(sys.argv)==1:
        print_window_list()
    else:
        focus_window(os.getenv("ROFI_INFO"))
