#! /usr/bin/python3 -sP
# -*- coding: UTF-8 -*-

# Copyright © 2007 Martin Böhme <martin.bohm@kubuntu.org>
# Copyright © 2009 Chris Jones <tortoise@tortuga>
# Copyright © 2012 marmuta <marmvta@gmail.com>
#
# This file is part of Onboard.
#
# Onboard 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.
#
# Onboard 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 <http://www.gnu.org/licenses/>.

from __future__ import division, print_function, unicode_literals

__copyright__ = "Copyright © 2009 Chris Jones"
__author__    = "Chris Jones <chrisejones@gmail.com>"

import sys
import os
import gettext


def get_translator():
    translation = gettext.translation("onboard", fallback=True)
    return translation.gettext


_ = get_translator()


def is_non_gui_cli_request():
    return any(arg in ("-h", "--help", "--version") for arg in sys.argv[1:])


def is_wayland_warning_disabled():
    return (
        os.getenv("ONBOARD_ALLOW_WAYLAND", "0").lower()
        in ("true", "t", "yes", "y", "1")
    )


def get_gdk_display_name():
    try:
        import gi

        gi.require_version("Gdk", "3.0")
        from gi.repository import Gdk

        display = Gdk.Display.get_default()
        if display:
            return display.get_name().lower()
    except Exception:
        pass

    return ""


def is_wayland_environment():
    backends = [
        item.strip().lower()
        for item in os.getenv("GDK_BACKEND", "").split(",")
        if item.strip()
    ]
    if "wayland" in backends:
        return True

    session_type = os.getenv("XDG_SESSION_TYPE", "").lower()
    if session_type == "wayland":
        return True

    return bool(os.getenv("WAYLAND_DISPLAY"))


def is_wayland_session():
    display_name = get_gdk_display_name()
    if display_name:
        return "wayland" in display_name.lower()

    return is_wayland_environment()


def show_wayland_warning_and_exit():
    warning_text = _(
        "Onboard does not support Wayland.\n"
        "Please start Onboard on X11 or use a Wayland-compatible keyboard."
    )

    print(warning_text, file=sys.stderr)

    try:
        import gi

        gi.require_version("Gtk", "3.0")
        from gi.repository import Gtk

        dialog = Gtk.MessageDialog(
            message_type=Gtk.MessageType.WARNING,
            text=warning_text,
            title="Onboard",
            buttons=Gtk.ButtonsType.OK,
        )
        dialog.run()
        dialog.destroy()
    except Exception:
        pass

    sys.exit(1)


if (
    not is_non_gui_cli_request() and
    not is_wayland_warning_disabled() and
    is_wayland_session()
):
    show_wayland_warning_and_exit()

# Replace the default exception handler with one which handles chained
# exceptions.
from Onboard.Exceptions import chain_handler
sys.excepthook = chain_handler
    
from Onboard.OnboardGtk import OnboardGtk as Onboard
ob = Onboard()
