avoid drift in clocks
This adds a sleep program that wakes up when the time (in seconds) is divisible by a specific number, so for example passing 60 to it will make it exit at the beginning of each minute. This program is also able to exit when the system exits suspend (when the kernel syncs the clock), which means we don't need to signal the clocks anymore. Signed-off-by: Stacy Harper <contact@stacyharper.net>
This commit is contained in:
11
Makefile
11
Makefile
@@ -10,6 +10,7 @@ OPENRC:=1
|
||||
|
||||
CC ?= $(CROSS_COMPILE)gcc
|
||||
PROGRAMS = \
|
||||
programs/sxmo_aligned_sleep \
|
||||
programs/sxmo_vibrate \
|
||||
programs/sxmo_splitchar
|
||||
|
||||
@@ -20,14 +21,11 @@ test: shellcheck
|
||||
shellcheck:
|
||||
find . -type f -name '*.sh' -print0 | xargs -0 shellcheck -x --shell=sh
|
||||
|
||||
programs/sxmo_vibrate: programs/sxmo_vibrate.c
|
||||
$(CC) -o programs/sxmo_vibrate programs/sxmo_vibrate.c
|
||||
|
||||
programs/sxmo_splitchar: programs/sxmo_splitchar.c
|
||||
$(CC) -o programs/sxmo_splitchar programs/sxmo_splitchar.c
|
||||
programs/%: programs/%.c
|
||||
gcc -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f programs/sxmo_vibrate programs/sxmo_splitchar
|
||||
rm -f programs/sxmo_aligned_sleep programs/sxmo_vibrate programs/sxmo_splitchar
|
||||
|
||||
install: $(PROGRAMS)
|
||||
cd configs && find . -type f -not -name sxmo-setpermissions -exec install -D -m 0644 "{}" "$(DESTDIR)$(PREFIX)/share/sxmo/{}" \; && cd ..
|
||||
@@ -68,6 +66,7 @@ install: $(PROGRAMS)
|
||||
# Bin
|
||||
install -D -t $(DESTDIR)$(PREFIX)/bin scripts/*/*.sh
|
||||
|
||||
install -D programs/sxmo_aligned_sleep $(DESTDIR)$(PREFIX)/bin/
|
||||
install -D programs/sxmo_vibrate $(DESTDIR)$(PREFIX)/bin/
|
||||
install -D programs/sxmo_splitchar $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
|
@@ -14,7 +14,6 @@
|
||||
|
||||
light -S "$(cat "$XDG_RUNTIME_DIR"/sxmo.brightness.presuspend.state)"
|
||||
|
||||
sxmo_hook_statusbar.sh time
|
||||
sxmo_hook_unlock.sh
|
||||
|
||||
# Add here whatever you want to do
|
||||
|
@@ -27,5 +27,3 @@ if ! [ -e "$XDG_CACHE_HOME/sxmo/sxmo.nosuspend" ]; then
|
||||
timeout "$((${SXMO_UNLOCK_IDLE_TIME:-120}+10))" 'sxmo_daemons.sh start periodic_state_mutex_check sxmo_run_periodically.sh 10 sxmo_hook_check_state_mutexes.sh' \
|
||||
resume 'sxmo_daemons.sh stop periodic_state_mutex_check'
|
||||
fi
|
||||
|
||||
sxmo_superd_signal.sh sxmo_desktop_widget -USR2
|
||||
|
@@ -19,40 +19,23 @@ pangodraw() {
|
||||
}
|
||||
|
||||
if [ -n "$WAYLAND_DISPLAY" ] && command -v wayout > /dev/null; then
|
||||
trap 'kill -- $WAYOUT' TERM INT EXIT
|
||||
|
||||
# For wayland we use wayout:
|
||||
tmp="$(mktemp)"
|
||||
(
|
||||
pangodraw
|
||||
pangodraw
|
||||
sleep 1 # wayout misses output sent to it as it's starting
|
||||
while : ; do
|
||||
sleep 60
|
||||
pangodraw
|
||||
sxmo_aligned_sleep 60
|
||||
done
|
||||
) >> "$tmp" &
|
||||
PIDS="$!"
|
||||
|
||||
forceredraw() {
|
||||
pangodraw >> "$tmp"
|
||||
for PID in $PIDS; do
|
||||
wait "$PID"
|
||||
done
|
||||
}
|
||||
trap 'forceredraw' USR2
|
||||
|
||||
tail -f "$tmp" | wayout --font "FiraMono Nerd Font" --foreground-color "#ffffff" --fontsize "80" --height 200 --textalign center --feed-par &
|
||||
PIDS="$! $PIDS"
|
||||
|
||||
finish() {
|
||||
for PID in $PIDS; do
|
||||
kill "$PID"
|
||||
done
|
||||
rm "$tmp"
|
||||
}
|
||||
trap 'finish' TERM INT EXIT
|
||||
|
||||
for PID in $PIDS; do
|
||||
wait "$PID"
|
||||
done
|
||||
) | wayout --font "FiraMono Nerd Font" \
|
||||
--foreground-color "#ffffff" \
|
||||
--fontsize "80" \
|
||||
--height 200 \
|
||||
--textalign center \
|
||||
--feed-par &
|
||||
WAYOUT="$!"
|
||||
wait
|
||||
elif [ -n "$DISPLAY" ] && command -v conky > /dev/null; then
|
||||
# For X we use conky (if not already running):
|
||||
exec conky -c "$(xdg_data_path sxmo/appcfg/conky24h.conf)" #24 hour clock
|
||||
|
@@ -60,7 +60,7 @@ superctl start sxmo_desktop_widget
|
||||
|
||||
# Periodically update some status bar components
|
||||
sxmo_hook_statusbar.sh all
|
||||
sxmo_daemons.sh start statusbar_periodics sxmo_run_periodically.sh 55 \
|
||||
sxmo_daemons.sh start statusbar_periodics sxmo_run_aligned.sh 60 \
|
||||
sxmo_hook_statusbar.sh periodics
|
||||
|
||||
# Monitor the battery
|
||||
|
@@ -29,5 +29,3 @@ if ! [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ]; then
|
||||
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
|
||||
timeout 8 "sxmo_hook_screenoff.sh"
|
||||
fi
|
||||
|
||||
sxmo_superd_signal.sh sxmo_desktop_widget -USR2
|
||||
|
@@ -19,8 +19,6 @@ if [ "$UNSUSPENDREASON" != "modem" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
sxmo_hook_statusbar.sh time
|
||||
|
||||
if [ "$UNSUSPENDREASON" = "rtc" ] || [ "$UNSUSPENDREASON" = "usb power" ]; then
|
||||
if grep -q screenoff "$XDG_RUNTIME_DIR/sxmo.state"; then
|
||||
# We stopped it in presuspend
|
||||
|
@@ -44,5 +44,3 @@ else
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
sxmo_superd_signal.sh sxmo_desktop_widget -USR2
|
||||
|
48
programs/sxmo_aligned_sleep.c
Normal file
48
programs/sxmo_aligned_sleep.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <errno.h> // error handling
|
||||
#include <stdint.h> // uint64_t type
|
||||
#include <stdio.h> // dprintf
|
||||
#include <stdlib.h> // strtol, exit
|
||||
#include <string.h> // strerror, strcmp
|
||||
#include <sys/timerfd.h> // timers
|
||||
#include <unistd.h> // read
|
||||
|
||||
static void error(char *message, int err) {
|
||||
dprintf(2, "%s: %s\n", message, strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
uint64_t buf;
|
||||
long duration = -1;
|
||||
int time_fd;
|
||||
|
||||
struct timespec now;
|
||||
struct itimerspec time = {
|
||||
.it_value = { .tv_sec = 0, .tv_nsec = 0 },
|
||||
.it_interval = { .tv_sec = 0, .tv_nsec = 0 }
|
||||
};
|
||||
|
||||
// Process arguments
|
||||
if (argc >= 2) duration = strtol(argv[1], NULL, 0);
|
||||
if (duration <= 0) {
|
||||
dprintf(2, "Usage: %s <seconds>\n", argv[0]);
|
||||
dprintf(2, "ERROR: duration must be a positive integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup timer
|
||||
if ((time_fd = timerfd_create(CLOCK_REALTIME, 0)) == -1)
|
||||
error("Failed to create timerfd", errno);
|
||||
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
|
||||
error("Failed to get current time", errno);
|
||||
|
||||
// find the next time that's divisable by the provided number of seconds
|
||||
time.it_value.tv_sec = now.tv_sec + duration - (now.tv_sec % duration);
|
||||
|
||||
if (timerfd_settime(time_fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &time, NULL) == -1)
|
||||
error("Failed to set timer",errno);
|
||||
|
||||
// Wait for timer
|
||||
read(time_fd, &buf, sizeof(uint64_t));
|
||||
}
|
||||
|
24
scripts/core/sxmo_run_aligned.sh
Normal file
24
scripts/core/sxmo_run_aligned.sh
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Copyright 2022 Sxmo Contributors
|
||||
|
||||
timeout="$1"
|
||||
shift
|
||||
|
||||
finish() {
|
||||
kill "$CMDPID"
|
||||
kill "$SLEEPPID"
|
||||
exit 0
|
||||
}
|
||||
|
||||
trap 'finish' TERM INT
|
||||
|
||||
while : ; do
|
||||
"$@" &
|
||||
CMDPID="$!"
|
||||
wait "$CMDPID"
|
||||
|
||||
sxmo_aligned_sleep "$timeout" &
|
||||
SLEEPPID="$!"
|
||||
wait "$SLEEPPID"
|
||||
done
|
Reference in New Issue
Block a user