-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathwatchface
More file actions
executable file
·906 lines (790 loc) · 31.1 KB
/
watchface
File metadata and controls
executable file
·906 lines (790 loc) · 31.1 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
#! /bin/bash
#
# watchface - Utility script for managing AsteroidOS watchfaces
#
# This script provides a comprehensive set of tools for deploying, testing,
# and managing watchfaces for AsteroidOS smartwatches. It supports both
# SSH (Developer Mode) and ADB (ADB Mode) communication methods.
#
# Key features:
# - Deploy watchfaces to an AsteroidOS watch
# - Test watchfaces locally using qmlscene
# - Clone existing watchfaces to create new ones
# - Interactive menu systems (text-based and GUI)
# - Automatic font configuration handling for custom fonts
# - Preview image generation and conversion
#
# Usage: ./watchface [options] [command] [arguments]
# See showHelp() function for detailed usage information.
#
# Version number of this script
VERSION="1.2"
# Display the current version of the script
function showVersion {
echo "watchface v${VERSION}"
}
# Display comprehensive help information about the script
# This function prints the version and complete usage documentation
# including all available options and commands.
function showHelp {
showVersion
cat << EOF
watchface [option] [command...]
Utility functions for AsteroidOS watchfaces. By default, uses "SSH Mode"
over ssh, but can also use "ADB Mode" using ADB.
Available options:
-h or --help print this help screen and quit
-a or --adb use ADB command to communicate with watch
-b or --boot reboot watch after deploying multiple watchfaces
-c or --circlewall WP set the wallpaper for circular watchface thumbnail to the named file (WP)
-e or --every select every watchface (deploy only)
-g or --gui use the GTK+ gui
-p or --port specify an IP port to use for ssh and scp commands
-q or --qemu communicate with QEMU emulated watch (same as -r localhost -p 2222 )
-r or --remote specify the remote (watch) name or address for ssh and scp commands
-t or --transport when used win ADB mode, specifies the transport id
-w or --wall WP set the wallpaper for deploy or test to the named file (WP)
-v or --verbose print verbose messages (useful for debugging)
Available commands:
update use git to update your local copy of the unoffical-watchfaces repository
version display the version of this program and exit
deploy WF push the named watchface to the watch and activate it
deployall deploy all watchfaces
clone WF NEWWF clone the named watchface WF to new watchface NEWWF
test WF test the named watchface on the computer using qmlscene
raw QMLFILE test a raw QML file without the standard directory structure
EOF
}
# ============================================================================
# Environment Configuration Variables
# ============================================================================
# These variables are set based on parsed command-line switches and control
# how the script communicates with the watch and which features are enabled.
# SSH connection settings (default for SSH mode)
# These are the defaults for SSH access to a watch on the local network
WATCHPORT=22 # SSH port number
WATCHADDR=192.168.2.15 # Default IP address of the watch
# QEMU emulator connection settings
# These are the defaults for local QEMU target
QEMUPORT=2222 # SSH port for QEMU
QEMUADDR=localhost # QEMU runs on localhost
# Communication method flags
ADB=false # Use ADB mode instead of SSH (default: false)
GUI=false # Use GUI menu with zenity (default: false)
# Interactive mode control
# Only show interactive prompt if the user hasn't
# already specified a command on the command line
SKIP_INTERACTIVE_PROMPT=false
# Wallpaper configuration
# Default wallpaper files unless user specifies others
WALLPAPER="background.jpg" # Square display wallpaper
WALLPAPER_ROUND="background-round.jpg" # Circular display wallpaper
# Watchface deployment settings
DEFAULTOPTION=OFF # Default to all watchfaces unselected for deploy
DEPLOY_ALL=false # Default to not deploying all watchfaces
# Debug and logging
VERBOSE_MESSAGES=false # Print verbose messages to stderr (default: false)
# ADB transport configuration
# Transport id is not needed unless multiple watches are connected
TRANSPORT_ID=
# Command execution state
COMMAND="" # Command to execute (deploy, test, clone, etc.)
declare -a COMMAND_ARGS=() # Arguments for the command
# ============================================================================
# Core Communication Functions
# ============================================================================
# Execute a command on the watch as a specific user
# This function abstracts the difference between ADB and SSH communication
# Args:
# $1 - user: Either "root" or "ceres" - the user to run the command as
# $2 - cmd: The shell command to execute on the watch
# Returns: Exit status of the remote command
function runWatchCommand {
local user="$1"
local cmd=$2
case ${user} in
root)
if [ "$ADB" = "true" ] ; then
adb ${TRANSPORT_ID} shell "${cmd}"
else
ssh -p "${WATCHPORT}" -t root@"${WATCHADDR}" ${cmd}
fi
;;
ceres)
if [ "$ADB" = "true" ] ; then
printf -v cmd %q "${cmd}"
adb ${TRANSPORT_ID} shell "su -l -c ${cmd} ceres"
else
ssh -p "${WATCHPORT}" -t ceres@"${WATCHADDR}" ${cmd}
fi
;;
*)
echo "Error: unknown watch user ${user}"
;;
esac
}
# Print a message only if verbose mode is enabled
# Args:
# $1 - message: The message to print
function printVerbose {
local message="$1"
if [ "${VERBOSE_MESSAGES}" = "true" ] ; then
echo "$message"
fi
}
# Set a dconf configuration setting on the watch
# Args:
# $1 - dconfsetting: The dconf key path (e.g., "/desktop/asteroid/watchface")
# $2 - filename: The file path to set as the value
function setDconf {
local dconfsetting="$1"
local filename="$2"
runWatchCommand "ceres" "dconf write ${dconfsetting} '\"file://${filename}\"'"
}
# Push files or directories to the watch
# This function abstracts the difference between ADB push and SCP
# Args:
# $1 - user: The user account (for SSH mode, ignored for ADB)
# $2 - sourcedir: Local source file or directory path
# $3 - destdir: Remote destination path on the watch
function pushFiles {
local user="$1"
local sourcedir="$2"
local destdir="$3"
if [ "$ADB" = "true" ] ; then
adb ${TRANSPORT_ID} push ${sourcedir} "${destdir}"
else
scp -P"${WATCHPORT}" -r ${sourcedir} "${user}@${WATCHADDR}:${destdir}"
fi
}
# Push a watchface directory structure to the watch
# Args:
# $1 - Watchface directory path containing usr/share/* structure
function pushWatchface {
pushFiles "root" "${1}"'/usr/share/*' "/usr/share/"
}
# Push a wallpaper image to the watch and set it as the active background
# Args:
# $1 - source: Local path to the wallpaper image file
function pushWallpaper {
local source="$1"
local wallpaper
wallpaper="$(basename "$1")"
local destination="/usr/share/asteroid-launcher/wallpapers/full/${wallpaper}"
pushFiles "root" "${source}" "${destination}"
setDconf "/desktop/asteroid/background-filename" "${destination}"
}
# Restart the ceres user session on the watch
# This is necessary after deploying new watchfaces or fonts
# to make them available to the launcher
function restartCeres {
runWatchCommand "root" "systemctl restart user@1000"
}
# Reboot the entire watch
# Sometimes needed when ceres restart doesn't properly reset everything
function rebootWatch {
runWatchCommand "ceres" "reboot"
}
# Activate a specific watchface on the watch
# Args:
# $1 - Watchface name (without .qml extension)
function activateWatchface {
setDconf "/desktop/asteroid/watchface" "/usr/share/asteroid-launcher/watchfaces/${1}.qml"
}
# ============================================================================
# Watchface Management Functions
# ============================================================================
# Clone a watchface to create a new one with a different name
# This copies all files and replaces occurrences of the source name
# with the destination name in paths and QML files
# Args:
# $1 - source_path: Source watchface path (can be absolute)
# $2 - dest_name: Destination watchface name (relative to CWD)
function watchfaceClone {
local source_path="$1"
local dest_name="$2"
local files
if [ ! -d "${source_path}" ] ; then
echo "Error: source watchface path \"${source_path}\" does not exist" >&2
exit 1
fi
local source_name
source_name=$(basename "${source_path}")
mapfile -t files <<<"$(cd "${source_path}" && find . -type f | sed 's|^\./||')"
for file in "${files[@]}" ; do
if [[ -z "${file}" ]] ; then
continue
fi
local dest_file
local destdir
dest_file="${file//${source_name}/${dest_name}}"
destdir="${dest_name}/$(dirname "${dest_file}")"
mkdir -p "${destdir}"
cp "${source_path}/${file}" "${dest_name}/${dest_file}"
local extension="${dest_file##*.}"
if [ "${extension}" = "qml" ] ; then
sed -i "s/${source_name}/${dest_name}/g" "${dest_name}/${dest_file}"
fi
done
}
# Find a watchface by name, searching CWD first, then script directory
# This allows users to work on watchfaces in any location
# Args:
# $1 - watchface_name: Watchface name (without trailing '/')
# Returns:
# Outputs the full path to the watchface directory
# Returns 0 if found, 1 if not found
function findWatchface {
local watchface_name="$1"
local cwd_path="${PWD}/${watchface_name}"
local script_path="${SCRIPT_DIRPATH}/${watchface_name}"
if [[ -d "${cwd_path}/usr/share/asteroid-launcher/watchfaces/" ]] ; then
echo "${cwd_path}"
return 0
fi
if [[ -d "${script_path}/usr/share/asteroid-launcher/watchfaces/" ]] ; then
echo "${script_path}"
return 0
fi
echo ""
return 1
}
# Deploy a watchface to the watch
# Args:
# $1 - sourcewatchfacename: Name of the watchface to deploy
# $2 - activate: "true" to activate the watchface and push wallpaper, "false" otherwise
function deployface {
local sourcewatchfacename="${1%/*}"
local activate="$2"
if ! sourcewatchfacedir=$(findWatchface "${sourcewatchfacename}") ; then
exit 1
fi
echo "Deploying ${sourcewatchfacename}"
pushWatchface "${sourcewatchfacedir}"
if [ "${activate}" = "true" ] ; then
activateWatchface "${sourcewatchfacename}"
if [ -f "${WALLPAPER}" ] ; then
pushWallpaper "${WALLPAPER}"
fi
fi
}
# Delete a temporary fontconfig configuration file
# This is called as a cleanup function via trap
# Args:
# $1 - temp_font_config: Path to the temporary fontconfig file
function deleteTempFontconfig {
local temp_font_config="$1"
printVerbose "Deleting temporary fontconfig config file \"${temp_font_config}\""
rm -f "${temp_font_config}"
}
# Test a watchface locally using qmlscene
# This function sets up the environment to test a watchface on the computer
# without deploying it to the watch. It handles custom fonts by creating
# temporary fontconfig configuration.
# Args:
# $1 - sourcewatchfacename: Name of the watchface to test
function testface {
local sourcewatchfacename="${1%/*}"
if ! sourcewatchfacedir=$(findWatchface "${sourcewatchfacename}") ; then
exit 1
fi
echo "Testing ${sourcewatchfacename}"
WATCHFACE_FONTS_PATH="${sourcewatchfacedir}/usr/share/fonts/"
if [ -d "${WATCHFACE_FONTS_PATH}" ]; then
printVerbose "Watchface has custom fonts in path \"${WATCHFACE_FONTS_PATH}\""
# Create a temporary fontconfig configuration file that points to the
# fonts subdirectory of the watchface. This allows Qt to load the font
# from that local directory - very useful when testing a watch, because
# then, installing that font is not necessary. The temporary config is
# generated in the local fontconfig config directory to let fontconfig
# find it.
# This is better than setting the FONTCONFIG_FILE environment variable
# to point to the autogenerated file, because the latter approach will
# use the autogenerated config file as a full replacement for the system
# config. This causes the Qt UI itself to not find fonts for its widgets,
# and can also cause the watchface to not show certain glyphs if they
# are only available in fallback fonts.
# Check for the presence of the base fontconfig path. If it does not
# exist, then the autogenerated config below probably would not work
# because fontconfig is not present or configured in an unknown fashion.
# Exit with an error in that case.
LOCAL_FONTCONFIG_BASE_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/fontconfig"
if [ ! -d "${LOCAL_FONTCONFIG_BASE_PATH}" ]; then
echo "Cannot generate temporary fontconfig config file; " \
"fontconfig path ${LOCAL_FONTCONFIG_BASE_PATH} does not exist" >&2
exit 1
fi
LOCAL_FONTCONFIG_PATH="${LOCAL_FONTCONFIG_BASE_PATH}/conf.d"
mkdir -p "$LOCAL_FONTCONFIG_PATH"
local temp_font_config
# Use 9999- prefix to make sure this autogenerated configuration is applied
# last, since fontconfig will list the config files in lexicographic order.
# Also, the .conf suffix is used, since fontconfig requires that suffix;
# it ignores files that do not end with ".conf".
temp_font_config=$(mktemp -p "$CWD/$LOCAL_FONTCONFIG_PATH" --suffix=".conf" -t 9999-temp_font_config.XXXXXX) || { echo "Failed to generate temporary fontconfig config file" >&2; exit 1; }
printVerbose "Generating temporary fontconfig config file \"${temp_font_config}\""
trap "deleteTempFontconfig \"${temp_font_config}\"" EXIT
cat <<EOF > "${temp_font_config}"
<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>${WATCHFACE_FONTS_PATH}/</dir>
</fontconfig>
EOF
else
printVerbose "Watchface has no custom fonts"
fi
if [ ! -f "${PWD}/${WALLPAPER}" ] ; then
WALLPAPER="${SCRIPT_DIRPATH}/${WALLPAPER}"
fi
if [ ! -f "${PWD}/${WALLPAPER_ROUND}" ] ; then
WALLPAPER_ROUND="${SCRIPT_DIRPATH}/${WALLPAPER_ROUND}"
fi
qmlscene "${sourcewatchfacename}" "${PWD}/${WALLPAPER}" "${PWD}/${WALLPAPER_ROUND}" "${sourcewatchfacedir}/usr/share/asteroid-launcher/watchfaces/" "${SCRIPT_DIRPATH}/loader.qml" -I "${SCRIPT_DIRPATH}/fake-components"
convertPreviews "${sourcewatchfacename}" "${sourcewatchfacedir}"
}
# Test a raw QML file without the standard watchface directory structure
# This is useful for quick prototyping or testing standalone QML files
# Args:
# $1 - qmlfile: Path to the QML file to test
function testraw {
local qmlfile="$1"
if [ ! -f "${qmlfile}" ] ; then
echo "Error: QML file \"${qmlfile}\" does not exist" >&2
exit 1
fi
local abs_qmlfile=$(readlink -f "${qmlfile}")
local qml_dir=$(dirname "${abs_qmlfile}")
local qml_basename=$(basename "${abs_qmlfile}")
local qml_name="${qml_basename%.qml}"
echo "Testing raw QML file: ${abs_qmlfile}"
printVerbose "QML directory: ${qml_dir}"
if [ -d "${qml_dir}/fonts" ]; then
WATCHFACE_FONTS_PATH="${qml_dir}/fonts/"
printVerbose "Found fonts directory: ${WATCHFACE_FONTS_PATH}"
LOCAL_FONTCONFIG_BASE_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/fontconfig"
if [ ! -d "${LOCAL_FONTCONFIG_BASE_PATH}" ]; then
echo "Warning: Cannot generate temporary fontconfig config file; " \
"fontconfig path ${LOCAL_FONTCONFIG_BASE_PATH} does not exist" >&2
else
LOCAL_FONTCONFIG_PATH="${LOCAL_FONTCONFIG_BASE_PATH}/conf.d"
mkdir -p "$LOCAL_FONTCONFIG_PATH"
local temp_font_config
temp_font_config=$(mktemp -p "$LOCAL_FONTCONFIG_PATH" --suffix=".conf" -t 9999-temp_font_config.XXXXXX) || { echo "Failed to generate temporary fontconfig config file" >&2; exit 1; }
printVerbose "Generating temporary fontconfig config file \"${temp_font_config}\""
trap "deleteTempFontconfig \"${temp_font_config}\"" EXIT
cat <<EOF > "${temp_font_config}"
<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>${WATCHFACE_FONTS_PATH}/</dir>
</fontconfig>
EOF
fi
else
printVerbose "No fonts directory found in ${qml_dir}"
fi
if [ ! -f "${PWD}/${WALLPAPER}" ] ; then
WALLPAPER="${SCRIPT_DIRPATH}/${WALLPAPER}"
fi
if [ ! -f "${PWD}/${WALLPAPER_ROUND}" ] ; then
WALLPAPER_ROUND="${SCRIPT_DIRPATH}/${WALLPAPER_ROUND}"
fi
qmlscene "${qml_name}" "${PWD}/${WALLPAPER}" "${PWD}/${WALLPAPER_ROUND}" "${qml_dir}/" "${SCRIPT_DIRPATH}/loader.qml" -I "${SCRIPT_DIRPATH}/fake-components"
}
# Convert preview images for a watchface
# This function processes PNG preview images and converts them to various
# resolutions for both the watch and web display
# Args:
# $1 - watchfacename: Name of the watchface
# $2 - watchfacedir: Directory path of the watchface
function convertPreviews {
local watchfacename="$1"
local watchfacedir="$2"
local transparent="${watchfacename}-trans.png"
local webResolution=320
local magickcmd=""
if [ -f "${transparent}" ] ; then
echo "Converting ${transparent}"
magickcmd="$(command -v magick || command -v convert)"
if [ -z "${magickcmd}" ] ; then
echo "Error: cannot find either magick or convert (part of ImageMagick) on path"
exit 1
fi
# Generate the transparent thumbnail images for the watch
# Various resolutions are needed for different watch models
for res in 112 128 144 160 182 ; do
respath="${watchfacedir}/usr/share/asteroid-launcher/watchfaces-preview/${res}"
mkdir -p "${respath}"
"${magickcmd}" "${transparent}" \
-resize "${res}x${res}" -adaptive-sharpen 0x.8 -quality 70 -strip \
"${respath}/${watchfacename}.png"
done
# Generate the thumbnail images for the web (README display)
[ -d "${PWD}/.thumbnails" ] || mkdir "${PWD}/.thumbnails"
for thumb in "${watchfacename}" "${watchfacename}-round" ; do
"${magickcmd}" "${thumb}.png" \
-resize "${webResolution}x${webResolution}" -adaptive-sharpen 0x.4 -quality 85 \
"${PWD}/.thumbnails/${thumb}.webp"
done
rm "${watchfacename}.png" "${watchfacename}-round.png" "${transparent}"
echo "|  | | [${watchfacename}](${watchfacename}/usr/share/asteroid-launcher/watchfaces/${watchfacename}.qml) | [yourname](https://github.com/yourname) |" > "${watchfacename}.md"
echo "Add the contents of ${watchfacename}.md to README.md, adding font licenses and your name"
else
echo "Did not find ${transparent}"
fi
}
# Clone a watchface (wrapper function for interactive use)
# Args:
# $1 - sourcewatchfacename: Name of the source watchface
# $2 - destwatchface: Name of the destination watchface
function cloneface {
local sourcewatchfacename="${1%/*}"
local destwatchface="$2"
if ! sourcewatchfacedir=$(findWatchface "${sourcewatchfacename}") ; then
exit 1
fi
echo "Cloning ${sourcewatchfacename} into ${destwatchface}"
if [ -e "${destwatchface}" ] ; then
echo "Error: ${destwatchface} already exists, exiting program"
exit
fi
watchfaceClone "${sourcewatchfacedir}" "${destwatchface}"
}
# ============================================================================
# Interactive Menu Functions
# ============================================================================
# Display a GUI menu using zenity for selecting and executing actions
# This provides a graphical interface for users who prefer GUI over terminal
function guiMenu {
local watchface
local action
local default
if ! action=$(zenity --title="Select action" --list --radiolist \
--column="selected" --column="action" \
TRUE deploy \
FALSE clone \
FALSE test); then
exit
fi
if [ "${DEFAULTOPTION}" == "ON" ] ; then
default=TRUE
else
default=FALSE
fi
case $action in
deploy)
declare -a args=('--title="Choose watchfaces"' "--list" "--checklist" '--column="Selected"' '--column="Name"')
for wf in "${WATCHFACES[@]}" ; do
args+=("${default}" "${wf}")
done
if ! watchface=$(zenity "${args[@]}"); then
exit
fi
;;
clone|test)
if ! watchface=$(zenity --title="Choose a watchface" --list --column="Name" "${WATCHFACES[@]}"); then
exit
fi
;;
esac
watchface=${watchface//|/ }
if zenity --question --title="Confirming" --text="${action} ${watchface}" ; then
echo "OK!"
if [ "${action}" = "clone" ] ; then
if newface=$(zenity --entry --title="Cloning ${watchface}" \
--text="Enter name of new watchface:" \
--entry-text "newface")
then cloneface "${watchface}" "${newface}"
else echo "No name entered"
fi
elif [ "${action}" = "deploy" ] ; then
local first=true
for wf in ${watchface}; do
deployface "${wf}" "${first}"
first=false
done
restartCeres
if [ "${REBOOT}" = "true" ] ; then
rebootWatch
fi
else
testface "${watchface}"
fi
else
echo "Canceled"
fi
}
# Display a text-based menu using dialog or whiptail
# This provides a terminal-based interactive menu for selecting actions
function textMenu {
local args
local watchface
local newface
if ! action=$(${MENUPROGRAM} --title "Select action" --clear --radiolist \
"Select action" 0 0 3 \
"deploy" "Deploy watchface to watch" ON \
"clone" "Clone watchface to new name" OFF \
"test" "Test watchface on computer" OFF \
3>&1 1>&2 2>&3); then
exit
fi
case $action in
deploy)
declare -a args=("--title" "Watchfaces" "--clear" "--checklist" "Choose watchfaces:" 25 78 15 "--" )
for wf in "${WATCHFACES[@]}" ; do
args+=("${wf}" "" "${DEFAULTOPTION}")
done
if ! watchface=$(${MENUPROGRAM} "${args[@]}" 3>&1 1>&2 2>&3); then
exit
fi
;;
clone|test)
declare -a args=("--title" "Watchfaces" "--clear" "--menu" "Choose a watchface:" 25 78 15 "--" )
for wf in "${WATCHFACES[@]}" ; do
args+=("${wf}" "")
done
if ! watchface=$(${MENUPROGRAM} "${args[@]}" 3>&1 1>&2 2>&3); then
exit
fi
;;
esac
if (${MENUPROGRAM} --clear --title "Confirming" --yesno "${action} ${watchface}" 0 0); then
echo "OK!"
if [ "${action}" = "clone" ] ; then
if newface=$(${MENUPROGRAM} \
--title "Cloning ${watchface}" --clear \
--inputbox "Enter name of new watchface:" \
8 50 "newface" \
3>&1 1>&2 2>&3)
then
cloneface "${watchface}" "${newface}"
else
echo "No name entered"
fi
elif [ "${action}" = "deploy" ] ; then
local first=true
for wf in ${watchface}; do
deployface "${wf}" "${first}"
first=false
done
if [ "${REBOOT}" = "true" ] ; then
rebootWatch
fi
else
testface "${watchface}"
fi
else
echo "Canceled"
fi
}
# Deploy all available watchfaces to the watch
# This is useful for setting up a watch with all watchfaces at once
function deployall {
for wf in "${WATCHFACES[@]}" ; do
# Deploy each watchface without activating it
deployface "${wf}" false
done
}
# ============================================================================
# Main Script Initialization
# ============================================================================
# Prevent the script from being sourced
# This script is meant to be executed directly, not sourced into another script
(return 0 2>/dev/null) && script_is_sourced=1 || script_is_sourced=0
if [ "$script_is_sourced" -gt 0 ]; then
echo "You are trying to source this script. It is not meant to be sourced." >&2
return
fi
# Determine which menu program to use (dialog or whiptail)
# Dialog is preferred if available, otherwise fall back to whiptail
if hash dialog 2>/dev/null; then
MENUPROGRAM="dialog"
elif hash whiptail 2>/dev/null; then
MENUPROGRAM="whiptail --separate-output"
fi
# Location of temporary font directory (currently unused)
FONT_DIR="${HOME}/.fonts"
# Temporary files and symlinks to delete (currently unused)
declare -a DELETE_LIST=()
# Get the directory path where this script is located
# This allows the script to find resources like loader.qml and fake-components
# regardless of where it's called from
SCRIPT_DIRPATH="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
# ============================================================================
# Command-Line Argument Parsing
# ============================================================================
# Preprocess command line switches with getopt to rearrange
# them for easier parsing, separating non-option arguments
# and option arguments.
REARRANGED_OPTS=$(getopt -o 'abc:egqp:r:t:hw:v' --long 'adb,boot,circlewall:,every,gui,qemu,port:,remote:,transport:,help,wall:,verbose' -n "$0" -- "$@")
if [[ $? -ne 0 ]]; then
echo "Error parsing arguments" >&2
showHelp
exit 1
fi
# Replace existing arguments with the rearranged ones
eval set -- "$REARRANGED_OPTS"
unset REARRANGED_OPTS
# Process all option flags
while true; do
case "$1" in
'-a'|'--adb')
ADB=true
shift
;;
'-b'|'--boot')
REBOOT=true
shift
;;
'-c'|'--circlewall')
WALLPAPER_ROUND="$2"
shift 2
;;
'-e'|'--every')
DEFAULTOPTION=ON
shift
;;
'-g'|'--gui')
GUI=true
shift
;;
'-q'|'--qemu')
WATCHPORT=${QEMUPORT}
WATCHADDR=${QEMUADDR}
shift
;;
'-p'|'--port')
WATCHPORT="$2"
shift 2
;;
'-r'|'--remote')
WATCHADDR="$2"
shift 2
;;
'-t'|'--transport')
TRANSPORT_ID="-t $2"
shift 2
;;
'-h'|'--help')
showHelp
exit 1
;;
'-w'|'--wall')
WALLPAPER="$2"
shift 2
;;
'-v'|'--verbose')
VERBOSE_MESSAGES=true
shift
;;
'--')
shift
break
;;
*)
echo "Internal error parsing arguments" >&2
exit 1
;;
esac
done
# Process remaining arguments as command and its arguments
if [[ $# -gt 0 ]]; then
COMMAND="$1"
shift
COMMAND_ARGS=("$@")
fi
# ============================================================================
# Command Validation
# ============================================================================
# Validate that the command has the correct number of arguments
case "$COMMAND" in
"clone")
if [[ ${#COMMAND_ARGS[@]} -ne 2 ]]; then
echo "Error: clone command requires exactly 2 arguments (source and destination)" >&2
exit 1
fi
;;
"deploy"|"test"|"raw")
if [[ ${#COMMAND_ARGS[@]} -ne 1 ]]; then
echo "Error: $COMMAND command requires exactly 1 argument" >&2
exit 1
fi
;;
"deployall"|"update"|"version")
if [[ ${#COMMAND_ARGS[@]} -ne 0 ]]; then
echo "Error: $COMMAND command takes no arguments" >&2
exit 1
fi
;;
"")
;;
*)
echo "Error: Unknown command '$COMMAND'" >&2
showHelp
exit 1
;;
esac
printVerbose "Script is located in directory path \"$SCRIPT_DIRPATH\""
# ============================================================================
# Command Execution
# ============================================================================
case "$COMMAND" in
"clone")
cloneface "${COMMAND_ARGS[0]}" "${COMMAND_ARGS[1]}"
SKIP_INTERACTIVE_PROMPT=true
;;
"deployall")
DEPLOY_ALL=true
SKIP_INTERACTIVE_PROMPT=true
;;
"deploy")
deployface "${COMMAND_ARGS[0]}" true
restartCeres
SKIP_INTERACTIVE_PROMPT=true
;;
"test")
testface "${COMMAND_ARGS[0]}"
SKIP_INTERACTIVE_PROMPT=true
;;
"raw")
testraw "${COMMAND_ARGS[0]}"
SKIP_INTERACTIVE_PROMPT=true
;;
"update")
git pull
SKIP_INTERACTIVE_PROMPT=true
;;
"version")
showVersion
SKIP_INTERACTIVE_PROMPT=true
;;
"")
echo "No command specified, entering interactive mode"
;;
esac
# ============================================================================
# Interactive Mode
# ============================================================================
# If no command was specified, start the interactive dialog
# Find all watchfaces in CWD first, then in script directory
# A watchface is identified by having the usr/share directory structure
mapfile -t CWD_WATCHFACES < <(find . -maxdepth 3 -type d -path "*/usr/share" 2>/dev/null | sed 's|/usr/share$||' | sort | awk -F '/' '{print $2}')
mapfile -t SCRIPT_WATCHFACES < <(cd "${SCRIPT_DIRPATH}" && find . -maxdepth 3 -type d -path "*/usr/share" 2>/dev/null | sed 's|/usr/share$||' | sort | awk -F '/' '{print $2}')
mapfile -t WATCHFACES < <(printf '%s\n' "${CWD_WATCHFACES[@]}" "${SCRIPT_WATCHFACES[@]}" | awk '!seen[$0]++')
# Start interactive mode if no command was executed
if [ "${SKIP_INTERACTIVE_PROMPT}" != "true" ] ; then
if [ "${GUI}" = "true" ] ; then
if hash zenity 2>/dev/null; then
guiMenu
else
echo "Error: install 'zenity' to use gui menus."
fi
else
if [ -z "${MENUPROGRAM}" ] ; then
echo "Error: install either 'dialog' or 'whiptail' to use text menus."
else
textMenu
fi
fi
elif [ "${DEPLOY_ALL}" == "true" ] ; then
deployall
fi