#!/usr/bin/env python3
import os
import subprocess
import time

from shuffler_tools import get_window, app_path

"""
WindowShuffler
Author: Jacob Vlijm
Copyright © 2017-2018 Ubuntu Budgie Developers
Website=https://ubuntubudgie.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 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/>.
"""

if not get_window("WindowMatrix"):
    subprocess.Popen(os.path.join(app_path, "window_shuffler"))
else:
    subprocess.Popen(["wmctrl", "-c", "WindowMatrix"])

time.sleep(2)

fail = 0

while True:
    time.sleep(0.5)
    if not get_window("WindowMatrix"):
        fail = fail + 1
        if fail == 2:
            break
    else:
        fail = 0
        subprocess.Popen(["wmctrl", "-a", "WindowMatrix"])
