#!/usr/bin/python3

import sys
import os
import gettext
import signal
import subprocess
import time
import gi
from gi.repository import Gio

# i18n
gettext.install("mintdesktop", "/usr/share/linuxmint/locale")

settings = Gio.Settings("com.linuxmint.desktop")

# Detect which DE is running
if "XDG_CURRENT_DESKTOP" not in os.environ:
    print ("window-manager-launcher: XDG_CURRENT_DESKTOP is not set! Exiting..")
    sys.exit(0)

current_desktop = os.environ["XDG_CURRENT_DESKTOP"]

if current_desktop not in ["MATE", "XFCE"]:
    print ("Current desktop %s is not supported." % current_desktop)
    sys.exit(0)

if current_desktop == "MATE":
    wm = settings.get_string("mate-window-manager")
else:
    wm = settings.get_string("xfce-window-manager")

# Kill all compositors/managers first
p = subprocess.Popen(['ps', '-u', str(os.getuid())], stdout=subprocess.PIPE)
out, err = p.communicate()
processes_found = False
for process in ['compton', "marco", "xfwm4", "compiz", "mutter", "metacity", "openbox", "awesome" ]:
    for line in out.splitlines():
        pname = line.decode('utf-8').split()[-1]
        if process in pname:
            pid = int(line.split(None, 1)[0])
            print ("Killing pid %d (%s)" % (pid, pname))
            try:
                os.kill(pid, signal.SIGKILL)
            except Exception as e:
                print ("Failed to kill process %d (%s): %s" % (pid, pname, e))
            processes_found = True

if (processes_found):
    # avoid race conditions before launching new WMs
    print ("Waiting 0.2 seconds...")
    time.sleep(0.2)

if wm == "marco":
    settings = Gio.Settings("org.mate.Marco.general")
    settings.set_boolean("compositing-manager", False)
    subprocess.Popen(["marco", "--no-composite", "--replace"])
elif wm == "marco-composite":
    settings = Gio.Settings("org.mate.Marco.general")
    settings.set_boolean("compositing-manager", True)
    subprocess.Popen(["marco", "--composite", "--replace"])
elif wm == "marco-compton":
    settings = Gio.Settings("org.mate.Marco.general")
    settings.set_boolean("compositing-manager", False)
    subprocess.Popen(["marco", "--no-composite", "--replace"])
    time.sleep(2)
    subprocess.Popen(["compton", "--backend", "glx", "--vsync", "opengl-swc"])
elif wm == "xfwm4":
    subprocess.Popen(["xfconf-query", "-c", "xfwm4", "-p", "/general/use_compositing", "--set", "false"])
    subprocess.Popen(["xfwm4", "--compositor=off", "--replace"])
elif wm == "xfwm4-composite":
    subprocess.Popen(["xfconf-query", "-c", "xfwm4", "-p", "/general/use_compositing", "--set", "true"])
    subprocess.Popen(["xfwm4", "--compositor=on", "--replace"])
elif wm == "xfwm4-compton":
    subprocess.Popen(["xfconf-query", "-c", "xfwm4", "-p", "/general/use_compositing", "--set", "false"])
    subprocess.Popen(["xfwm4", "--compositor=off", "--replace"])
    time.sleep(2)
    subprocess.Popen(["compton", "--backend", "glx", "--vsync", "opengl-swc"])
elif wm == "compiz":
    subprocess.Popen(["compiz", "--replace"])
elif wm == "mutter":
    subprocess.Popen(["mutter", "--replace"])
elif wm == "metacity":
    settings = Gio.Settings("org.gnome.metacity")
    settings.set_boolean("compositing-manager", False)
    subprocess.Popen(["metacity", "--replace"])
elif wm == "metacity-composite":
    settings = Gio.Settings("org.gnome.metacity")
    settings.set_boolean("compositing-manager", True)
    subprocess.Popen(["metacity", "--replace"])
elif wm == "metacity-compton":
    os.environ["META_COMPOSITOR"] = "external"
    subprocess.Popen(["metacity", "--replace"])
    time.sleep(2)
    subprocess.Popen(["compton", "--backend", "glx", "--vsync", "opengl-swc"])
elif wm == "openbox":
    subprocess.Popen(["openbox", "--replace"])
elif wm == "openbox-compton":
    subprocess.Popen(["openbox", "--replace"])
    time.sleep(2)
    subprocess.Popen(["compton", "--backend", "glx", "--vsync", "opengl-swc"])
elif wm == "awesome":
    # subprocess.call(["killall", "marco", "xfwm4", "compiz", "mutter", "metacity", "openbox", "awesome"]) # Kill all other window managers that might possibly still be running
    # time.sleep(0.1) # Wait some time until really all other window managers are killed otherwise awesome won't start up
    subprocess.Popen(["awesome"])
    if current_desktop == "MATE":  # The mate panel seems to move up a bit when starting awesome
        subprocess.Popen(["mate-panel", "--replace"])  # this seems to fix this issue
