Skip to content

Commit 70db172

Browse files
committed
update state
1 parent 76c4144 commit 70db172

File tree

10 files changed

+178
-53
lines changed

10 files changed

+178
-53
lines changed

display.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ func updateDisplay() {
3838

3939
if usbState == "configured" {
4040
nativeInstance.UpdateLabelIfChanged("usb_status_label", "Connected")
41-
_, _ = nativeInstance.UIObjSetState("usb_status", "LV_STATE_DEFAULT")
41+
_, _ = nativeInstance.UIObjAddState("usb_status_label", "LV_STATE_CHECKED")
4242
} else {
4343
nativeInstance.UpdateLabelIfChanged("usb_status_label", "Disconnected")
44-
_, _ = nativeInstance.UIObjSetState("usb_status", "LV_STATE_DISABLED")
44+
_, _ = nativeInstance.UIObjClearState("usb_status_label", "LV_STATE_CHECKED")
4545
}
4646
if lastVideoState.Ready {
4747
nativeInstance.UpdateLabelIfChanged("hdmi_status_label", "Connected")
48-
_, _ = nativeInstance.UIObjSetState("hdmi_status", "LV_STATE_DEFAULT")
48+
_, _ = nativeInstance.UIObjAddState("hdmi_status_label", "LV_STATE_CHECKED")
4949
} else {
5050
nativeInstance.UpdateLabelIfChanged("hdmi_status_label", "Disconnected")
51-
_, _ = nativeInstance.UIObjSetState("hdmi_status", "LV_STATE_DISABLED")
51+
_, _ = nativeInstance.UIObjClearState("hdmi_status_label", "LV_STATE_CHECKED")
5252
}
5353
nativeInstance.UpdateLabelIfChanged("cloud_status_label", fmt.Sprintf("%d active", actionSessions))
5454

internal/native/cgo/ctrl.c

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -247,36 +247,55 @@ void jetkvm_ui_set_image(const char *obj_name, const char *image_name) {
247247
lv_img_set_src(obj, image_name);
248248
}
249249

250-
void jetkvm_ui_set_state(const char *obj_name, const char *state_name) {
251-
lv_obj_t *obj = ui_get_obj(obj_name);
252-
if (obj == NULL) {
253-
return;
250+
lv_state_t str_to_lv_state(const char *state_name) {
251+
if (strcmp(state_name, "LV_STATE_USER_1") == 0) {
252+
return LV_STATE_USER_1;
254253
}
255-
lv_obj_add_state(obj, LV_STATE_USER_1);
256-
lv_state_t state_val = LV_STATE_DEFAULT;
257-
if (strcmp(state_name, "LV_STATE_USER_1") == 0)
258-
{
259-
state_val = LV_STATE_USER_1;
254+
else if (strcmp(state_name, "LV_STATE_USER_2") == 0) {
255+
return LV_STATE_USER_2;
260256
}
261-
else if (strcmp(state_name, "LV_STATE_USER_2") == 0)
262-
{
263-
state_val = LV_STATE_USER_2;
257+
else if (strcmp(state_name, "LV_STATE_USER_3") == 0) {
258+
return LV_STATE_USER_3;
264259
}
265-
else if (strcmp(state_name, "LV_STATE_USER_3") == 0)
266-
{
267-
state_val = LV_STATE_USER_3;
260+
else if (strcmp(state_name, "LV_STATE_USER_4") == 0) {
261+
return LV_STATE_USER_4;
268262
}
269-
else if (strcmp(state_name, "LV_STATE_USER_4") == 0)
270-
{
271-
state_val = LV_STATE_USER_4;
263+
else if (strcmp(state_name, "LV_STATE_DISABLED") == 0) {
264+
return LV_STATE_DISABLED;
272265
}
273-
else if (strcmp(state_name, "LV_STATE_DISABLED") == 0)
274-
{
275-
state_val = LV_STATE_DISABLED;
266+
else if (strcmp(state_name, "LV_STATE_DEFAULT") == 0) {
267+
return LV_STATE_DEFAULT;
268+
}
269+
else if (strcmp(state_name, "LV_STATE_CHECKED") == 0) {
270+
return LV_STATE_CHECKED;
271+
}
272+
else if (strcmp(state_name, "LV_STATE_FOCUSED") == 0) {
273+
return LV_STATE_FOCUSED;
274+
}
275+
return LV_STATE_DEFAULT;
276+
}
277+
278+
void jetkvm_ui_add_state(const char *obj_name, const char *state_name) {
279+
lv_obj_t *obj = ui_get_obj(obj_name);
280+
if (obj == NULL) {
281+
return;
276282
}
277-
// TODO: use LV_STATE_USER_* once eez supports it
278-
lv_obj_clear_state(obj, LV_STATE_USER_1 | LV_STATE_USER_2 | LV_STATE_USER_3 | LV_STATE_USER_4 | LV_STATE_DISABLED);
283+
lv_state_t state_val = str_to_lv_state(state_name);
279284
lv_obj_add_state(obj, state_val);
285+
286+
lv_obj_refresh_style(obj, LV_PART_ANY, LV_STYLE_PROP_ANY);
287+
}
288+
289+
void jetkvm_ui_clear_state(const char *obj_name, const char *state_name) {
290+
lv_obj_t *obj = ui_get_obj(obj_name);
291+
if (obj == NULL) {
292+
return;
293+
}
294+
lv_state_t state_val = str_to_lv_state(state_name);
295+
lv_obj_clear_state(obj, state_val);
296+
297+
log_info("cleared state %s from object %s: %d", state_name, obj_name, state_val);
298+
lv_obj_refresh_style(obj, LV_PART_ANY, LV_STYLE_PROP_ANY);
280299
}
281300

282301
int jetkvm_ui_add_flag(const char *obj_name, const char *flag_name) {

internal/native/cgo/ctrl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const char *jetkvm_ui_get_current_screen();
3939
void jetkvm_ui_load_screen(const char *obj_name);
4040
int jetkvm_ui_set_text(const char *obj_name, const char *text);
4141
void jetkvm_ui_set_image(const char *obj_name, const char *image_name);
42-
void jetkvm_ui_set_state(const char *obj_name, const char *state_name);
42+
void jetkvm_ui_add_state(const char *obj_name, const char *state_name);
43+
void jetkvm_ui_clear_state(const char *obj_name, const char *state_name);
4344
void jetkvm_ui_fade_in(const char *obj_name, u_int32_t duration);
4445
void jetkvm_ui_fade_out(const char *obj_name, u_int32_t duration);
4546
void jetkvm_ui_set_opacity(const char *obj_name, u_int8_t opacity);

internal/native/cgo_linux.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package native
44

55
import (
66
"fmt"
7+
"sync"
78
"unsafe"
89

910
"github.com/rs/zerolog"
@@ -45,6 +46,8 @@ static inline void jetkvm_cgo_setup_rpc_handler() {
4546
*/
4647
import "C"
4748

49+
var cgoLock sync.Mutex
50+
4851
//export jetkvm_go_video_state_handler
4952
func jetkvm_go_video_state_handler(state *C.jetkvm_video_state_t) {
5053
videoState := VideoState{
@@ -100,6 +103,9 @@ func uiEventCodeToName(code int) string {
100103
}
101104

102105
func setUpNativeHandlers() {
106+
cgoLock.Lock()
107+
defer cgoLock.Unlock()
108+
103109
C.jetkvm_cgo_setup_log_handler()
104110
C.jetkvm_cgo_setup_video_state_handler()
105111
C.jetkvm_cgo_setup_video_handler()
@@ -108,17 +114,26 @@ func setUpNativeHandlers() {
108114
}
109115

110116
func uiInit(rotation uint16) {
117+
cgoLock.Lock()
118+
defer cgoLock.Unlock()
119+
111120
cRotation := C.u_int16_t(rotation)
112121
defer C.free(unsafe.Pointer(&cRotation))
113122

114123
C.jetkvm_ui_init(cRotation)
115124
}
116125

117126
func uiTick() {
127+
cgoLock.Lock()
128+
defer cgoLock.Unlock()
129+
118130
C.jetkvm_ui_tick()
119131
}
120132

121133
func videoInit() error {
134+
cgoLock.Lock()
135+
defer cgoLock.Unlock()
136+
122137
ret := C.jetkvm_video_init()
123138
if ret != 0 {
124139
return fmt.Errorf("failed to initialize video: %d", ret)
@@ -127,18 +142,30 @@ func videoInit() error {
127142
}
128143

129144
func videoShutdown() {
145+
cgoLock.Lock()
146+
defer cgoLock.Unlock()
147+
130148
C.jetkvm_video_shutdown()
131149
}
132150

133151
func videoStart() {
152+
cgoLock.Lock()
153+
defer cgoLock.Unlock()
154+
134155
C.jetkvm_video_start()
135156
}
136157

137158
func videoStop() {
159+
cgoLock.Lock()
160+
defer cgoLock.Unlock()
161+
138162
C.jetkvm_video_stop()
139163
}
140164

141165
func uiSetVar(name string, value string) {
166+
cgoLock.Lock()
167+
defer cgoLock.Unlock()
168+
142169
nameCStr := C.CString(name)
143170
defer C.free(unsafe.Pointer(nameCStr))
144171

@@ -149,38 +176,68 @@ func uiSetVar(name string, value string) {
149176
}
150177

151178
func uiGetVar(name string) string {
179+
cgoLock.Lock()
180+
defer cgoLock.Unlock()
181+
152182
nameCStr := C.CString(name)
153183
defer C.free(unsafe.Pointer(nameCStr))
154184

155185
return C.GoString(C.jetkvm_ui_get_var(nameCStr))
156186
}
157187

158188
func uiSwitchToScreen(screen string) {
189+
cgoLock.Lock()
190+
defer cgoLock.Unlock()
191+
159192
screenCStr := C.CString(screen)
160193
defer C.free(unsafe.Pointer(screenCStr))
161194
C.jetkvm_ui_load_screen(screenCStr)
162195
}
163196

164197
func uiGetCurrentScreen() string {
198+
cgoLock.Lock()
199+
defer cgoLock.Unlock()
200+
165201
screenCStr := C.jetkvm_ui_get_current_screen()
166202
return C.GoString(screenCStr)
167203
}
168204

169-
func uiObjSetState(objName string, state string) (bool, error) {
205+
func uiObjAddState(objName string, state string) (bool, error) {
206+
cgoLock.Lock()
207+
defer cgoLock.Unlock()
208+
209+
objNameCStr := C.CString(objName)
210+
defer C.free(unsafe.Pointer(objNameCStr))
211+
stateCStr := C.CString(state)
212+
defer C.free(unsafe.Pointer(stateCStr))
213+
C.jetkvm_ui_add_state(objNameCStr, stateCStr)
214+
return true, nil
215+
}
216+
217+
func uiObjClearState(objName string, state string) (bool, error) {
218+
cgoLock.Lock()
219+
defer cgoLock.Unlock()
220+
170221
objNameCStr := C.CString(objName)
171222
defer C.free(unsafe.Pointer(objNameCStr))
172223
stateCStr := C.CString(state)
173224
defer C.free(unsafe.Pointer(stateCStr))
174-
C.jetkvm_ui_set_state(objNameCStr, stateCStr)
225+
C.jetkvm_ui_clear_state(objNameCStr, stateCStr)
175226
return true, nil
176227
}
177228

178229
func uiGetLVGLVersion() string {
230+
cgoLock.Lock()
231+
defer cgoLock.Unlock()
232+
179233
return C.GoString(C.jetkvm_ui_get_lvgl_version())
180234
}
181235

182236
// TODO: use Enum instead of string but it's not a hot path and performance is not a concern now
183237
func uiObjAddFlag(objName string, flag string) (bool, error) {
238+
cgoLock.Lock()
239+
defer cgoLock.Unlock()
240+
184241
objNameCStr := C.CString(objName)
185242
defer C.free(unsafe.Pointer(objNameCStr))
186243
flagCStr := C.CString(flag)
@@ -190,6 +247,9 @@ func uiObjAddFlag(objName string, flag string) (bool, error) {
190247
}
191248

192249
func uiObjClearFlag(objName string, flag string) (bool, error) {
250+
cgoLock.Lock()
251+
defer cgoLock.Unlock()
252+
193253
objNameCStr := C.CString(objName)
194254
defer C.free(unsafe.Pointer(objNameCStr))
195255
flagCStr := C.CString(flag)
@@ -207,6 +267,9 @@ func uiObjShow(objName string) (bool, error) {
207267
}
208268

209269
func uiObjSetOpacity(objName string, opacity int) (bool, error) {
270+
cgoLock.Lock()
271+
defer cgoLock.Unlock()
272+
210273
objNameCStr := C.CString(objName)
211274
defer C.free(unsafe.Pointer(objNameCStr))
212275

@@ -215,6 +278,9 @@ func uiObjSetOpacity(objName string, opacity int) (bool, error) {
215278
}
216279

217280
func uiObjFadeIn(objName string, duration uint32) (bool, error) {
281+
cgoLock.Lock()
282+
defer cgoLock.Unlock()
283+
218284
objNameCStr := C.CString(objName)
219285
defer C.free(unsafe.Pointer(objNameCStr))
220286

@@ -224,6 +290,9 @@ func uiObjFadeIn(objName string, duration uint32) (bool, error) {
224290
}
225291

226292
func uiObjFadeOut(objName string, duration uint32) (bool, error) {
293+
cgoLock.Lock()
294+
defer cgoLock.Unlock()
295+
227296
objNameCStr := C.CString(objName)
228297
defer C.free(unsafe.Pointer(objNameCStr))
229298

@@ -233,6 +302,9 @@ func uiObjFadeOut(objName string, duration uint32) (bool, error) {
233302
}
234303

235304
func uiLabelSetText(objName string, text string) (bool, error) {
305+
cgoLock.Lock()
306+
defer cgoLock.Unlock()
307+
236308
objNameCStr := C.CString(objName)
237309
defer C.free(unsafe.Pointer(objNameCStr))
238310

@@ -247,6 +319,9 @@ func uiLabelSetText(objName string, text string) (bool, error) {
247319
}
248320

249321
func uiImgSetSrc(objName string, src string) (bool, error) {
322+
cgoLock.Lock()
323+
defer cgoLock.Unlock()
324+
250325
objNameCStr := C.CString(objName)
251326
defer C.free(unsafe.Pointer(objNameCStr))
252327

@@ -259,6 +334,9 @@ func uiImgSetSrc(objName string, src string) (bool, error) {
259334
}
260335

261336
func uiDispSetRotation(rotation uint16) (bool, error) {
337+
cgoLock.Lock()
338+
defer cgoLock.Unlock()
339+
262340
nativeLogger.Info().Uint16("rotation", rotation).Msg("setting rotation")
263341

264342
cRotation := C.u_int16_t(rotation)
@@ -269,21 +347,33 @@ func uiDispSetRotation(rotation uint16) (bool, error) {
269347
}
270348

271349
func videoGetStreamQualityFactor() (float64, error) {
350+
cgoLock.Lock()
351+
defer cgoLock.Unlock()
352+
272353
factor := C.jetkvm_video_get_quality_factor()
273354
return float64(factor), nil
274355
}
275356

276357
func videoSetStreamQualityFactor(factor float64) error {
358+
cgoLock.Lock()
359+
defer cgoLock.Unlock()
360+
277361
C.jetkvm_video_set_quality_factor(C.float(factor))
278362
return nil
279363
}
280364

281365
func videoGetEDID() (string, error) {
366+
cgoLock.Lock()
367+
defer cgoLock.Unlock()
368+
282369
edidCStr := C.jetkvm_video_get_edid_hex()
283370
return C.GoString(edidCStr), nil
284371
}
285372

286373
func videoSetEDID(edid string) error {
374+
cgoLock.Lock()
375+
defer cgoLock.Unlock()
376+
287377
edidCStr := C.CString(edid)
288378
defer C.free(unsafe.Pointer(edidCStr))
289379
C.jetkvm_video_set_edid(edidCStr)

internal/native/cgo_notlinux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ func uiGetCurrentScreen() string {
2828
return ""
2929
}
3030

31-
func uiObjSetState(objName string, state string) (bool, error) {
31+
func uiObjAddState(objName string, state string) (bool, error) {
32+
panicPlatformNotSupported()
33+
return false, nil
34+
}
35+
36+
func uiObjClearState(objName string, state string) (bool, error) {
3237
panicPlatformNotSupported()
3338
return false, nil
3439
}

0 commit comments

Comments
 (0)