#!/bin/bash

# The display manager may leave LC_ALL/LC_* set to its own locale.  Those
# variables take precedence over LANG, so load the locale selected by the user
# before starting the compositor and DDE session.
locale_config="${XDG_CONFIG_HOME:-$HOME/.config}/locale.conf"
if [ -r "$locale_config" ]; then
    unset LANG LANGUAGE LC_ALL LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE \
        LC_MONETARY LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE \
        LC_MEASUREMENT LC_IDENTIFICATION

    set -a
    # shellcheck source=/dev/null
    . "$locale_config"
    set +a

    # D-Bus/systemd activated services do not necessarily inherit the
    # compositor process environment.  Replace their stale locale as well.
    if command -v dbus-update-activation-environment >/dev/null 2>&1; then
        locale_updates=(
            "LANG=${LANG:-C.UTF-8}"
            "LANGUAGE=${LANGUAGE:-}"
            "LC_ALL=${LC_ALL:-}"
        )
        for locale_category in CTYPE NUMERIC TIME COLLATE MONETARY MESSAGES \
            PAPER NAME ADDRESS TELEPHONE MEASUREMENT IDENTIFICATION; do
            locale_variable="LC_$locale_category"
            locale_updates+=("$locale_variable=${!locale_variable:-${LANG:-C.UTF-8}}")
        done
        dbus-update-activation-environment --systemd "${locale_updates[@]}" \
            >/dev/null 2>&1 || true
        unset locale_updates locale_category locale_variable
    fi
fi
unset locale_config

export PATH=$PATH:/sbin
export XDG_SESSION_TYPE=wayland
export XDG_SESSION_DESKTOP=Deepin
export DDE_CURRENT_COMPOSITOR=GXWM
export KYWC_USE_LAYOUT_MANAGER=1  # 设置环境变量这个才会使用配置文件里的分辨率
unset QT_QPA_PLATFORM
unset DTK2_XWAYLAND

# If all DTK5/6 applications keeps crashing, uncomment the line below this
# comment block.
# Note that DTK5/6 graphic effect (blur, border, shadows) will also be gone.
# -----------------------------------------------------------------------------
# 若所有DTK5/6程序不停崩溃，拒绝启动，取消本注释块下方代码的注释
# 请注意，DTK5/6的图形特效（模糊、边框、阴影）也会随之消失。

# export GXWM_DONOT_BROADCAST_TLPM=ON

# The logic of libseat is that seatd is the 1st priority backend, and then
# logind. We are currently using LightDM with logind (The current developing
# GXDE Display manager is based on SDDM, which is also using logind), and hence
# we are forcing the backend to logind.
#
# Unfortunately, we're unable to auto detect which one to use automatically.
#
# Don't be overwhelmed if you are a seatd user. If you are really using seatd,
# You may simply comment out the following if block.
# -----------------------------------------------------------------------------
# libseat的逻辑如下：在有Seatd的情况下优先使用seatd，而GXDE（无论是当前GXDE正在使用的
# LightDM还是正在开发的基于SDDM的GXDE Display Manager）都使用logind。故而我们强制使用
# logind作为后端。
#
# 不幸的是，我们无法自动检测应该使用哪个后端。
#
# 如果您是seatd用户，您也无须担心，只需要注释掉下方这个if块即可。
if [ -z "$LIBSEAT_BACKEND" ] && [ -n "$XDG_SESSION_ID" ] && [ -d /run/systemd/seats ]; then
    export LIBSEAT_BACKEND=logind
fi

# export GDK_BACKEND=wayland
# export WLR_DRM_NO_ATOMIC=1

# === kirin 9000c 芯片走 hisi 硬件桌面 ===
# 检测到麒麟 9000c 内核且安装了 gxde-hisi-9000c-gpu-compat
# （提供 /usr/bin/startgxde_wlcom_9000c，hisi Maleoon 910 硬件加速启动器）
# 时，跳转到硬件加速启动；未安装则继续默认软件启动流程。
if uname -r | grep -q 9000c && [ -x /usr/bin/startgxde_wlcom_9000c ]; then
    # hisi 会话所需的最小环境（详细配置由 startgxde_wlcom_9000c 负责）
    export WLR_BACKENDS=drm,libinput
    exec /usr/bin/startgxde_wlcom_9000c "$@"
fi

exec /usr/bin/gxde-wlcom -s /usr/bin/startdde "$@"
