#!/bin/sh

# Copyright (c) 2021, Basealt Ltd
# All rights reserved.
# Author: Sergey V Turchin <zerg@altlinux.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 2 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 <http://www.gnu.org/licenses/>.

# notes:
# Search and Launch menu
# evtest --grab

set_keyboard() {
    TURN="$1"
    local KEYBOARD_SETUP_FILE="$HOME/.config/plasma-workspace/env/kde5-tablet-mode-switch.sh"
    local DESKTOP_FILE="com.github.maliit.keyboard.desktop"
    if [ "$TURN" == "ON" ] ; then
	# setup input method
	mkdir -p `dirname "$KEYBOARD_SETUP_FILE"`
	touch "$KEYBOARD_SETUP_FILE"
	cat > "$KEYBOARD_SETUP_FILE" <<__EOF__
#!/bin/sh
export QT_IM_MODULE=Maliit \
	GTK_IM_MODULE=Maliit \
	QT_QUICK_CONTROLS_MOBILE=1 \
	KDE_KIRIGAMI_TABLET_MODE=1 \
	#
__EOF__
	chmod a+x "$KEYBOARD_SETUP_FILE"
	# autostart virtual keyboard
	mkdir -p ~/.config/autostart/
	cp -ar  /usr/share/applications/"$DESKTOP_FILE" ~/.config/autostart/"$DESKTOP_FILE"
	kwriteconfig5 --file kwinrc --group Wayland --key InputMethod '/usr/share/applications/com.github.maliit.keyboard.desktop'
    else
	# disable input method
	[ -e "$KEYBOARD_SETUP_FILE" ] && rm -f "$KEYBOARD_SETUP_FILE" ||:
	# disable virtual keyboard
	[ -e ~/.config/autostart/"$DESKTOP_FILE" ] && rm -f ~/.config/autostart/"$DESKTOP_FILE" ||:
	kwriteconfig5 --file kwinrc --group Wayland --key InputMethod --delete
    fi

    # setup virtual keyboard
    USER_LANG="POSIX"
    USER_LOCALE_STRING=$LC_ALL
    [ -n "$USER_LOCALE_STRING" ] || USER_LOCALE_STRING=$LC_MESSAGES
    [ -n "$USER_LOCALE_STRING" ] || USER_LOCALE_STRING=$LANG
    if [ -n "$USER_LOCALE_STRING" ] ; then
	USER_LANG=`echo "$USER_LOCALE_STRING" | sed 's|_.*$||'`
    fi

    if [ "$USER_LANG" == "C" -o "$USER_LANG" == "POSIX" -o "$USER_LANG" == "en" ] ; then
	gsettings set org.maliit.keyboard.maliit enabled-languages "['en']"
    else
	gsettings set org.maliit.keyboard.maliit enabled-languages "['en', '$USER_LANG']"
    fi
    gsettings set org.maliit.keyboard.maliit theme "BreezeDark"
}

set_force_font_dpi() {
    FONT_DPI="$1"
    if [ -n "$FONT_DPI" ] ; then
	kwriteconfig5 --file kcmfonts --group General --key forceFontDPI "$FONT_DPI"
	kwriteconfig5 --file kcmfonts --group General --key forceFontDPIWayland "$FONT_DPI"
    else
	kwriteconfig5 --file kcmfonts --group General --key forceFontDPI --delete
	kwriteconfig5 --file kcmfonts --group General --key forceFontDPIWayland --delete
    fi
}

set_scale_factor() {
    SCALE_FACTOR="$1"
    if [ -n "$SCALE_FACTOR" ] ; then
	kwriteconfig5 --file kdeglobals --group KScreen --key ScaleFactor "$SCALE_FACTOR"
    else
	kwriteconfig5 --file kdeglobals --group KScreen --key ScaleFactor --delete
    fi
    kwriteconfig5 --file kdeglobals --group KScreen --key ScreenScaleFactors --delete
}

SWITCH_TO_TABLET=
AUTOMATIC=`kreadconfig5 --file kde5-tablet-mode-switch --group 'Notification Messages' --key automatic`

if [ "$AUTOMATIC" == "true" -o "$AUTOMATIC" == "false" ] ; then
    if [ -n "$QT_IM_MODULE" ]; then
	SWITCH_TO_TABLET="0"
    else
	SWITCH_TO_TABLET="1"
    fi
else
    kdialog \
	--dontagain ~/.config/kde5-tablet-mode-switch:automatic \
	--title "Режим рабочего стола" \
	--warningyesnocancel "Вы будете принудительно выведены из системы.<br/>Некоторые настройки будут форсированы.<br/>Какой режим включить?" \
	--yes-label "Компьютер" \
	--no-label "Планшет"
    SWITCH_TO_TABLET=$?
fi

if [ "$SWITCH_TO_TABLET" == "0" ] ; then
    # desktop
    set_force_font_dpi ""
    set_scale_factor "1"
    set_keyboard OFF
elif [ "$SWITCH_TO_TABLET" == "1" ] ; then
    # tablet
    set_force_font_dpi "192"
    set_scale_factor "2"
    set_keyboard ON
else
    exit 0
fi

# update apps for icons change
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 0
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 1
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 2
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 3
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 4
#qdbus-qt5 org.kde.KIconLoader /KIconLoader iconChanged 5

# logout
qdbus-qt5 org.kde.ksmserver /KSMServer logout 0 3 3
