1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#!/bin/bash
#
# wechat-universal.sh - wrapper for WeChat Universal
# Copyright (C) 2024 Guoxin "7Ji" Pu <pugokushin@gmail.com>
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
dbus_call() { # $1: dest, $2: object path, $3: method
local args=(dbus-send --session --dest="$1" --print-reply "${@:2}")
echo "D-Bus calling with: ${args[@]}" >&2
"${args[@]}"
}
# Based on: https://github.com/web1n/wechat-universal-flatpak/blob/91f5d0d246881077812881e800a22f5a02cea438/wechat.sh#L3
notifier_get() {
local dbus_item
for dbus_item in $(
dbus_call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames |
sed -n 's/ string "\(org\.kde\.StatusNotifierItem.\+\)"/\1/p'
)
do
if dbus_call "${dbus_item}" \
/StatusNotifierItem \
org.freedesktop.DBus.Properties.Get \
string:org.kde.StatusNotifierItem \
string:Id |
grep -q 'string "wechat"'
then
echo "${dbus_item}"
return
fi
done
}
try_move_foreground() {
# Try to find existing WeChat window first and display it, before actually opening a new one
notifier_item=$(notifier_get)
if [[ "${notifier_item}" ]]; then
echo 'WeChat is already running, moving it to foreground'
# Based on: https://github.com/web1n/wechat-universal-flatpak/blob/91f5d0d246881077812881e800a22f5a02cea438/wechat.sh#L25
dbus_call "${notifier_item}" \
/StatusNotifierItem \
org.kde.StatusNotifierItem.Activate \
int32:0 \
int32:0
return $?
fi
return 1
}
try_start() {
# Data folder setup
# If user has declared a custom data dir, no need to query xdg for documents dir, but always resolve that to absolute path
if [[ "${WECHAT_DATA_DIR}" ]]; then
WECHAT_DATA_DIR=$(readlink -f -- "${WECHAT_DATA_DIR}")
else
XDG_DOCUMENTS_DIR="${XDG_DOCUMENTS_DIR:-$(xdg-user-dir DOCUMENTS)}"
if [[ -z "${XDG_DOCUMENTS_DIR}" ]]; then
echo 'Error: Failed to get XDG_DOCUMENTS_DIR, refuse to continue'
exit 1
fi
WECHAT_DATA_DIR="${XDG_DOCUMENTS_DIR}/WeChat_Data"
fi
echo "Using '${WECHAT_DATA_DIR}' as Wechat Data folder"
WECHAT_FILES_DIR="${WECHAT_DATA_DIR}/xwechat_files"
WECHAT_HOME_DIR="${WECHAT_DATA_DIR}/home"
# Runtime folder setup
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-$(xdg-user-dir RUNTIME)}"
if [[ -z "${XDG_RUNTIME_DIR}" ]]; then
echo 'Error: Failed to get XDG_RUNTIME_DIR, refuse to continue'
exit 1
fi
if [[ -z "${XAUTHORITY}" ]]; then
echo 'Warning: No XAUTHORITY set, runnning in no-X environment? Generating it'
export XAUTHORITY=$(mktemp "${XDG_RUNTIME_DIR}"/xauth_XXXXXX)
echo "Info: Generated XAUTHORITY at '${XAUTHORITY}'"
fi
# wechat-universal only supports xcb
export QT_QPA_PLATFORM=xcb \
QT_AUTO_SCREEN_SCALE_FACTOR=1 \
PATH="/sandbox:${PATH}"
if [[ -z "${WECHAT_IME_WORKAROUND}" || "${WECHAT_IME_WORKAROUND}" == 'auto' ]]; then
case "${XMODIFIERS}" in
*@im=fcitx*)
WECHAT_IME_WORKAROUND='fcitx'
;;
*@im=ibus*)
WECHAT_IME_WORKAROUND='ibus'
;;
esac
fi
case "${WECHAT_IME_WORKAROUND}" in
fcitx)
echo "IME workaround for fcitx applied"
export QT_IM_MODULE=fcitx \
GTK_IM_MODULE=fcitx
;;
ibus)
echo "IME workaround for ibus applied"
export QT_IM_MODULE=ibus \
GTK_IM_MODULE=ibus \
IBUS_USE_PORTAL=1
;;
esac
BWRAP_DEV_BINDS=()
for DEV_NODE in /dev/{nvidia{-uvm,ctl,*[0-9]},video*[0-9]}; do
[[ -e "${DEV_NODE}" ]] && BWRAP_DEV_BINDS+=(--dev-bind "${DEV_NODE}"{,})
done
# Custom exposed folders
# echo "Hint: Custom binds could either be declared in '~/.config/wechat-universal/binds.list', each line a path, absolute or relative to your HOME; or as argument \`--bind [custom bind]\`"
BWRAP_CUSTOM_BINDS=()
if [[ -f "${WECHAT_CUSTOM_BINDS_CONFIG}" ]]; then
mapfile -t WECHAT_CUSTOM_BINDS_PRESISTENT < "${WECHAT_CUSTOM_BINDS_CONFIG}"
fi
cd ~ # 7Ji: two chdir.3 should be cheaper than a lot of per-dir calculation
for WECHAT_CUSTOM_BIND in "${WECHAT_CUSTOM_BINDS_RUNTIME[@]}" "${WECHAT_CUSTOM_BINDS_PRESISTENT[@]}"; do
WECHAT_CUSTOM_BIND=$(readlink -f -- "${WECHAT_CUSTOM_BIND}")
echo "Custom bind: '${WECHAT_CUSTOM_BIND}'"
BWRAP_CUSTOM_BINDS+=(--bind "${WECHAT_CUSTOM_BIND}"{,})
done
cd - > /dev/null
DBUS_SESSION_BUS_PATH=''
if [[ "${DBUS_SESSION_BUS_ADDRESS}" ]]; then
DBUS_SESSION_BUS_PATH=$(echo "${DBUS_SESSION_BUS_ADDRESS}" | sed 's/^unix:\(.\+=.\+\)\?path=\(.\+\)\(,.\+=.\+\|$\)/\2/')
fi
if [[ -z "${DBUS_SESSION_BUS_PATH}" ]]; then
DBUS_SESSION_BUS_PATH="${XDG_RUNTIME_DIR}/bus"
echo "Failed to extract \$DBUS_SESSION_BUS_ADDRESS from \$DBUS_SESSION_BUS_ADDRESS (${DBUS_SESSION_BUS_ADDRESS}), using fallback ${DBUS_SESSION_BUS_PATH}"
fi
mkdir -p "${WECHAT_FILES_DIR}" "${WECHAT_HOME_DIR}"
ln -snf "${WECHAT_FILES_DIR}" "${WECHAT_HOME_DIR}/xwechat_files"
BWRAP_ARGS=(
# Drop privileges
--unshare-all
--share-net
--cap-drop ALL
--die-with-parent
# /usr
--ro-bind /usr{,}
--symlink usr/lib /lib
--symlink usr/lib /lib64
--symlink usr/bin /bin
--symlink usr/bin /sbin
--bind /usr/bin/{true,lsblk}
# /sandbox
--ro-bind /{usr/lib/flatpak-xdg-utils,sandbox}/xdg-open
--ro-bind /usr/lib/wechat-universal/common.sh /sandbox/dde-file-manager
# /dev
--dev /dev
--dev-bind /dev/dri{,}
--tmpfs /dev/shm
# /proc
--proc /proc
# /etc
--ro-bind /etc/machine-id{,}
--ro-bind /etc/passwd{,}
--ro-bind /etc/nsswitch.conf{,}
--ro-bind /etc/resolv.conf{,}
--ro-bind /etc/localtime{,}
--ro-bind-try /etc/fonts{,}
# /sys
--dir /sys/dev # hack for Intel / AMD graphics, mesa calling virtual nodes needs /sys/dev being 0755
--ro-bind /sys/dev/char{,}
--ro-bind /sys/devices{,}
# /tmp
--tmpfs /tmp
# /opt
--ro-bind /opt/wechat-universal{,}
--ro-bind-try /opt/lol{,} # Loong New World (WeChat) on Loong Old World (LoongArchLinux)
# license fixups in various places
--ro-bind {/usr/lib/wechat-universal,}/usr/lib/license
--ro-bind {/usr/lib/wechat-universal,}/etc/lsb-release
# /home
--bind "${WECHAT_HOME_DIR}" "${HOME}"
--bind "${WECHAT_FILES_DIR}"{,}
--bind-try "${HOME}/.pki"{,}
--ro-bind-try "${HOME}/.fontconfig"{,}
--ro-bind-try "${HOME}/.fonts"{,}
--ro-bind-try "${HOME}/.config/fontconfig"{,}
--ro-bind-try "${HOME}/.local/share/fonts"{,}
--ro-bind-try "${HOME}/.icons"{,}
--ro-bind-try "${HOME}/.local/share/icons"{,}
# /run
--dev-bind /run/dbus{,}
--ro-bind-try /run/systemd/userdb{,}
--ro-bind-try "${XAUTHORITY}"{,}
--ro-bind "${DBUS_SESSION_BUS_PATH}"{,}
--ro-bind "${XDG_RUNTIME_DIR}/pulse"{,}
)
exec bwrap "${BWRAP_ARGS[@]}" "${BWRAP_CUSTOM_BINDS[@]}" "${BWRAP_DEV_BINDS[@]}" /opt/wechat-universal/wechat "$@"
echo "Error: Failed to exec bwrap, rerun this script with 'bash -x $0' to show the full command history"
return 1
}
applet_start() {
IFS=':' read -r -a WECHAT_CUSTOM_BINDS_RUNTIME <<< "${WECHAT_CUSTOM_BINDS}"
if [[ -z "${WECHAT_CUSTOM_BINDS_CONFIG}" && ! -v WECHAT_CUSTOM_BINDS_CONFIG ]]; then
WECHAT_CUSTOM_BINDS_CONFIG=~/.config/wechat-universal/binds.list
fi
# Parsing arguments, for any option, argument > environment
while (( $# )); do
case "$1" in
'--data')
WECHAT_DATA_DIR="$2"
shift
;;
'--bind')
WECHAT_CUSTOM_BINDS_RUNTIME+=("$2")
shift
;;
'--binds-config')
WECHAT_CUSTOM_BINDS_CONFIG="$2"
shift
;;
'--ime')
WECHAT_IME_WORKAROUND="$2"
shift
;;
'--help')
if [[ "${LANG}" == zh_CN* ]]; then
echo "$0 (--data [微信数据文件夹]) (--bind [自定义绑定挂载] (--bind ...))) (--ime [输入法]) (--help)"
echo
printf ' --%s\t%s\n' \
'data [微信数据文件夹]' '微信数据文件夹的路径,绝对路径,或相对于用户HOME的相对路径。 默认:~/文档/Wechat_Data;环境变量: WECHAT_DATA_DIR' \
'bind [自定义绑定挂载]' '自定义的绑定挂载,可被声明多次,绝对路径,或相对于用户HOME的相对路径。环境变量: WECHAT_CUSTOM_BINDS, (用冒号:分隔,与PATH相似)' \
'binds-config [文件]' '以每行一个的方式列明应被绑定挂载的路径的纯文本配置文件,每行定义与--bind一致。默认:~/.config/wechat-universal/binds.list;环境变量:WECHAT_CUSTOM_BINDS_CONFIG' \
'ime [输入法名称或特殊值]' '应用输入法对应环境变量修改,可支持:fcitx (不论是否为5), ibus,特殊值:none不应用,auto自动判断。默认: auto;环境变量: WECHAT_IME_WORKAROUND'\
'help' ''
echo
echo "命令行参数比环境变量优先级更高,如果命令行参数与环境变量皆为空,则使用默认值"
else
echo "$0 (--data [wechat data]) (--bind [custom bind] (--bind ...))) (--ime [ime]) (--help)"
echo
printf ' --%s\t%s\n' \
'data [wechat data]' 'Path to Wechat_Data folder, absolute or relative to user home, default: ~/Documents/Wechat_Data, as environment: WECHAT_DATA_DIR' \
'bind [custom bind]' 'Custom bindings, could be specified multiple times, absolute or relative to user home, as environment: WECHAT_CUSTOM_BINDS (colon ":" seperated like PATH)' \
'binds-config [file]' 'Path to text file that contains one --bind value per line, default: ~/.config/wechat-universal/binds.list, as environment: WECHAT_CUSTOM_BINDS_CONFIG'\
'ime [input method]' 'Apply IME-specific workaround, support: fcitx (also for 5), ibus, default: auto, as environment: WECHAT_IME_WORKAROUND'\
'help' ''
echo
echo "Arguments take priority over environment, if both argument and environment are empty, the default value would be used"
fi
return 0
;;
*)
echo "Unknown option: $1, pass --help to read help message"
;;
esac
shift
done
if try_move_foreground; then
return 0
else
try_start "$@"
return $?
fi
}
applet_stop() {
notifier_item=$(notifier_get)
[[ -z "${notifier_item}" ]] && return
echo 'Stopping running WeChat instance'
# Based on: https://github.com/web1n/wechat-universal-flatpak/blob/91f5d0d246881077812881e800a22f5a02cea438/wechat.sh#L35
dbus_call "${notifier_item}" \
/MenuBar \
com.canonical.dbusmenu.Event \
int32:1 \
string:clicked \
variant:string:'Quit WeChat' \
uint32:0
}
applet_dde() {
local item=
while (( $# )); do
case "$1" in
'--show-item')
item="$2"
shift
;;
esac
shift
done
if [[ "${item}" ]]; then
local path object target
path=$(readlink -f -- "${item}") # Resolve this to absolute path that's same across host / guest
echo "Fake deepin file manager: dbus-send to open '${path}' in file manager"
if [[ -d "${path}" ]]; then
# WeChat pass both files and folders in the same way, if we use ShowItems for folders,
# it would open that folder's parent folder, which is not right.
object=ShowFolders
target=folders
else
object=ShowItems
target=items
fi
exec dbus-send --print-reply --dest=org.freedesktop.FileManager1 \
/org/freedesktop/FileManager1 \
org.freedesktop.FileManager1."${object}" \
array:string:"file://${path}" \
string:fake-dde-file-manager-show-"${target}"
# We should not fall to here, but add a fallback anyway
echo "Fake deepin file manager: fallback: xdg-open to show '${path}' in file manager"
exec xdg-open "${path}"
else
echo "Fake deepin file manager: xdg-open with args $@"
exec xdg-open "$@"
fi
# At this stage, it's either: dbus-send not found, or xdg-open not found, this should not happen
# In whatever case, bail out
echo "Fake deepin file manager: could not open any thing, original args: $@"
return 1
}
applet="${0##*/}"
case "${applet}" in
'stop.sh'|'stop')
applet_stop "$@"
;;
'start.sh'|'start'|'wechat-universal.sh'|'wechat-universal')
applet_start "$@"
;;
'dde-file-manager')
applet_dde "$@"
;;
*)
echo "Unknown applet '${applet}', allowed: start.sh (alias start, wechat-universal, wechat-universal.sh), stop.sh (alias stop)"
false
;;
esac
exit $?
|