From 616b625a5c16fa71419e52135beaf7d212046ce3 Mon Sep 17 00:00:00 2001 From: Siyuan Miao Date: Sun, 24 Aug 2025 19:23:26 +0200 Subject: [PATCH] chore: use UIObjSetState instead of Add+Remove --- .vscode/settings.json | 3 ++- Makefile | 7 +++++-- cloud.go | 1 + display.go | 41 ++++++++++++++++++++++++----------------- jsonrpc.go | 2 +- native.go | 4 ++-- network.go | 2 +- usb.go | 4 +--- webrtc.go | 2 +- 9 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index eeecce00..6186222e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,6 @@ "tailwindCSS.classFunctions": [ "cva", "cx" - ] + ], + "cmake.sourceDirectory": "/Users/aveline/Projects/JetKVM/ymjk/internal/native/cgo" } \ No newline at end of file diff --git a/Makefile b/Makefile index e4957ab7..7ffe695c 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ GO_ARGS := GOOS=linux GOARCH=arm GOARM=7 ARCHFLAGS="-arch arm" ifneq ($(wildcard $(BUILDKIT_PATH)),) GO_ARGS := $(GO_ARGS) \ CGO_CFLAGS="-I$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/include -I$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/sysroot/usr/include" \ - CGO_LDFLAGS="-L$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/lib -L$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/sysroot/usr/lib -lrockit -lrockchip_mpp -lrga -lpthread -lm" \ + CGO_LDFLAGS="-L$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/lib -L$(BUILDKIT_PATH)/$(BUILDKIT_FLAVOR)/sysroot/usr/lib -lrockit -lrockchip_mpp -lrga -lpthread -lm -lgpiod" \ CC="$(BUILDKIT_PATH)/bin/$(BUILDKIT_FLAVOR)-gcc" \ LD="$(BUILDKIT_PATH)/bin/$(BUILDKIT_FLAVOR)-ld" \ CGO_ENABLED=1 @@ -41,7 +41,10 @@ TEST_DIRS := $(shell find . -name "*_test.go" -type f -exec dirname {} \; | sort build_native: @echo "Building native..." - cd internal/native/cgo && ./ui_index.gen.sh && ./build.sh + cd internal/native/cgo && ./ui_index.gen.sh && \ + CC="$(BUILDKIT_PATH)/bin/$(BUILDKIT_FLAVOR)-gcc" \ + LD="$(BUILDKIT_PATH)/bin/$(BUILDKIT_FLAVOR)-ld" \ + ./build.sh build_dev: build_native @echo "Building..." diff --git a/cloud.go b/cloud.go index fb138508..a851d51f 100644 --- a/cloud.go +++ b/cloud.go @@ -170,6 +170,7 @@ func setCloudConnectionState(state CloudConnectionState) { go waitCtrlAndRequestDisplayUpdate( previousState != state, + "set_cloud_connection_state", ) } diff --git a/display.go b/display.go index 926ae505..4d41e3ee 100644 --- a/display.go +++ b/display.go @@ -51,17 +51,21 @@ func updateDisplay() { if usbState == "configured" { nativeInstance.UpdateLabelIfChanged("usb_status_label", "Connected") - _, _ = nativeInstance.UIObjSetState("usb_status", "LV_STATE_DEFAULT") + _, _ = nativeInstance.UIObjAddState("usb_status", "LV_STATE_DEFAULT") + _, _ = nativeInstance.UIObjRemoveState("usb_status", "LV_STATE_DISABLED") } else { nativeInstance.UpdateLabelIfChanged("usb_status_label", "Disconnected") - _, _ = nativeInstance.UIObjSetState("usb_status", "LV_STATE_DISABLED") + _, _ = nativeInstance.UIObjAddState("usb_status", "LV_STATE_DISABLED") + _, _ = nativeInstance.UIObjRemoveState("usb_status", "LV_STATE_DEFAULT") } if lastVideoState.Ready { nativeInstance.UpdateLabelIfChanged("hdmi_status_label", "Connected") - _, _ = nativeInstance.UIObjSetState("hdmi_status", "LV_STATE_DEFAULT") + _, _ = nativeInstance.UIObjAddState("hdmi_status", "LV_STATE_DEFAULT") + _, _ = nativeInstance.UIObjRemoveState("hdmi_status", "LV_STATE_DISABLED") } else { nativeInstance.UpdateLabelIfChanged("hdmi_status_label", "Disconnected") - _, _ = nativeInstance.UIObjSetState("hdmi_status", "LV_STATE_DISABLED") + _, _ = nativeInstance.UIObjAddState("hdmi_status", "LV_STATE_DISABLED") + _, _ = nativeInstance.UIObjRemoveState("hdmi_status", "LV_STATE_DEFAULT") } nativeInstance.UpdateLabelIfChanged("cloud_status_label", fmt.Sprintf("%d active", actionSessions)) @@ -166,7 +170,7 @@ var ( waitDisplayUpdate = sync.Mutex{} ) -func requestDisplayUpdate(shouldWakeDisplay bool) { +func requestDisplayUpdate(shouldWakeDisplay bool, reason string) { displayUpdateLock.Lock() defer displayUpdateLock.Unlock() @@ -176,7 +180,7 @@ func requestDisplayUpdate(shouldWakeDisplay bool) { } go func() { if shouldWakeDisplay { - wakeDisplay(false) + wakeDisplay(false, reason) } displayLogger.Debug().Msg("display updating") //TODO: only run once regardless how many pending updates @@ -184,12 +188,12 @@ func requestDisplayUpdate(shouldWakeDisplay bool) { }() } -func waitCtrlAndRequestDisplayUpdate(shouldWakeDisplay bool) { +func waitCtrlAndRequestDisplayUpdate(shouldWakeDisplay bool, reason string) { waitDisplayUpdate.Lock() defer waitDisplayUpdate.Unlock() // nativeInstance.WaitCtrlClientConnected() - requestDisplayUpdate(shouldWakeDisplay) + requestDisplayUpdate(shouldWakeDisplay, reason) } func updateStaticContents() { @@ -224,7 +228,7 @@ func updateStaticContents() { // setDisplayBrightness sets /sys/class/backlight/backlight/brightness to alter // the backlight brightness of the JetKVM hardware's display. -func setDisplayBrightness(brightness int) error { +func setDisplayBrightness(brightness int, reason string) error { // NOTE: The actual maximum value for this is 255, but out-of-the-box, the value is set to 64. // The maximum set here is set to 100 to reduce the risk of drawing too much power (and besides, 255 is very bright!). if brightness > 100 || brightness < 0 { @@ -243,14 +247,14 @@ func setDisplayBrightness(brightness int) error { return err } - displayLogger.Info().Int("brightness", brightness).Msg("set brightness") + displayLogger.Info().Int("brightness", brightness).Str("reason", reason).Msg("set brightness") return nil } // tick_displayDim() is called when when dim ticker expires, it simply reduces the brightness // of the display by half of the max brightness. func tick_displayDim() { - err := setDisplayBrightness(config.DisplayMaxBrightness / 2) + err := setDisplayBrightness(config.DisplayMaxBrightness/2, "tick_display_dim") if err != nil { displayLogger.Warn().Err(err).Msg("failed to dim display") } @@ -263,7 +267,7 @@ func tick_displayDim() { // tick_displayOff() is called when the off ticker expires, it turns off the display // by setting the brightness to zero. func tick_displayOff() { - err := setDisplayBrightness(0) + err := setDisplayBrightness(0, "tick_display_off") if err != nil { displayLogger.Warn().Err(err).Msg("failed to turn off display") } @@ -276,7 +280,7 @@ func tick_displayOff() { // wakeDisplay sets the display brightness back to config.DisplayMaxBrightness and stores the time the display // last woke, ready for displayTimeoutTick to put the display back in the dim/off states. // Set force to true to skip the backlight state check, this should be done if altering the tickers. -func wakeDisplay(force bool) { +func wakeDisplay(force bool, reason string) { if backlightState == 0 && !force { return } @@ -286,7 +290,11 @@ func wakeDisplay(force bool) { return } - err := setDisplayBrightness(config.DisplayMaxBrightness) + if reason == "" { + reason = "wake_display" + } + + err := setDisplayBrightness(config.DisplayMaxBrightness, reason) if err != nil { displayLogger.Warn().Err(err).Msg("failed to wake display") } @@ -308,7 +316,7 @@ func startBacklightTickers() { // Don't start the tickers if the display is switched off. // Set the display to off if that's the case. if config.DisplayMaxBrightness == 0 { - _ = setDisplayBrightness(0) + _ = setDisplayBrightness(0, "display_disabled") return } @@ -358,7 +366,6 @@ func initDisplay() { displayInited = true displayLogger.Info().Msg("display inited") startBacklightTickers() - wakeDisplay(true) - requestDisplayUpdate(true) + requestDisplayUpdate(true, "init_display") }() } diff --git a/jsonrpc.go b/jsonrpc.go index 563b8173..e94adb65 100644 --- a/jsonrpc.go +++ b/jsonrpc.go @@ -363,7 +363,7 @@ func rpcSetBacklightSettings(params BacklightSettings) error { // are reset to the new settings, and will bring the display up to maxBrightness. // Calling with force set to true, to ignore the current state of the display, and force // it to reset the tickers. - wakeDisplay(true) + wakeDisplay(true, "backlight_settings_changed") return nil } diff --git a/native.go b/native.go index 805b76b6..0d034b70 100644 --- a/native.go +++ b/native.go @@ -19,11 +19,11 @@ func initNative(systemVersion *semver.Version, appVersion *semver.Version) { OnVideoStateChange: func(state native.VideoState) { lastVideoState = state triggerVideoStateUpdate() - requestDisplayUpdate(true) + requestDisplayUpdate(true, "video_state_changed") }, OnIndevEvent: func(event string) { nativeLogger.Trace().Str("event", event).Msg("indev event received") - wakeDisplay(false) + wakeDisplay(false, "indev_event") }, OnVideoFrameReceived: func(frame []byte, duration time.Duration) { if currentSession != nil { diff --git a/network.go b/network.go index a1397ef3..a6c3e0cc 100644 --- a/network.go +++ b/network.go @@ -18,7 +18,7 @@ var ( func networkStateChanged(isOnline bool) { // do not block the main thread - go waitCtrlAndRequestDisplayUpdate(true) + go waitCtrlAndRequestDisplayUpdate(true, "network_state_changed") if timeSync != nil { if networkState != nil { diff --git a/usb.go b/usb.go index 99287a30..b30c4824 100644 --- a/usb.go +++ b/usb.go @@ -99,8 +99,6 @@ func checkUSBState() { return } usbLogger.Info().Str("from", usbState).Str("to", newState).Msg("USB state changed") - usbState = newState - - requestDisplayUpdate(true) + requestDisplayUpdate(true, "usb_state_changed") triggerUSBStateUpdate() } diff --git a/webrtc.go b/webrtc.go index 7fd13929..dba01b79 100644 --- a/webrtc.go +++ b/webrtc.go @@ -367,7 +367,7 @@ func newSession(config SessionConfig) (*Session, error) { var actionSessions = 0 func onActiveSessionsChanged() { - requestDisplayUpdate(true) + requestDisplayUpdate(true, "active_sessions_changed") } func onFirstSessionConnected() {