Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 45 additions & 30 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"sync"

"github.com/jetkvm/kvm/internal/confparser"
"github.com/jetkvm/kvm/internal/logging"
"github.com/jetkvm/kvm/internal/network"
"github.com/jetkvm/kvm/internal/usbgadget"
Expand Down Expand Up @@ -129,41 +130,56 @@ func (c *Config) SetDisplayRotation(rotation string) error {

const configPath = "/userdata/kvm_config.json"

var defaultConfig = &Config{
CloudURL: "https://api.jetkvm.com",
CloudAppURL: "https://app.jetkvm.com",
AutoUpdateEnabled: true, // Set a default value
ActiveExtension: "",
KeyboardMacros: []KeyboardMacro{},
DisplayRotation: "270",
KeyboardLayout: "en-US",
DisplayMaxBrightness: 64,
DisplayDimAfterSec: 120, // 2 minutes
DisplayOffAfterSec: 1800, // 30 minutes
JigglerEnabled: false,
// This is the "Standard" jiggler option in the UI
JigglerConfig: &JigglerConfig{
// it's a temporary solution to avoid sharing the same pointer
// we should migrate to a proper config solution in the future
var (
defaultJigglerConfig = JigglerConfig{
InactivityLimitSeconds: 60,
JitterPercentage: 25,
ScheduleCronTab: "0 * * * * *",
Timezone: "UTC",
},
TLSMode: "",
UsbConfig: &usbgadget.Config{
}
defaultUsbConfig = usbgadget.Config{
VendorId: "0x1d6b", //The Linux Foundation
ProductId: "0x0104", //Multifunction Composite Gadget
SerialNumber: "",
Manufacturer: "JetKVM",
Product: "USB Emulation Device",
},
UsbDevices: &usbgadget.Devices{
}
defaultUsbDevices = usbgadget.Devices{
AbsoluteMouse: true,
RelativeMouse: true,
Keyboard: true,
MassStorage: true,
},
NetworkConfig: &network.NetworkConfig{},
DefaultLogLevel: "INFO",
}
)

func getDefaultConfig() Config {
return Config{
CloudURL: "https://api.jetkvm.com",
CloudAppURL: "https://app.jetkvm.com",
AutoUpdateEnabled: true, // Set a default value
ActiveExtension: "",
KeyboardMacros: []KeyboardMacro{},
DisplayRotation: "270",
KeyboardLayout: "en-US",
DisplayMaxBrightness: 64,
DisplayDimAfterSec: 120, // 2 minutes
DisplayOffAfterSec: 1800, // 30 minutes
JigglerEnabled: false,
// This is the "Standard" jiggler option in the UI
JigglerConfig: func() *JigglerConfig { c := defaultJigglerConfig; return &c }(),
TLSMode: "",
UsbConfig: func() *usbgadget.Config { c := defaultUsbConfig; return &c }(),
UsbDevices: func() *usbgadget.Devices { c := defaultUsbDevices; return &c }(),
NetworkConfig: func() *network.NetworkConfig {
c := &network.NetworkConfig{}
_ = confparser.SetDefaultsAndValidate(c)
return c
}(),
DefaultLogLevel: "INFO",
VideoQualityFactor: 1.0,
}
}

var (
Expand Down Expand Up @@ -196,7 +212,8 @@ func LoadConfig() {
}

// load the default config
config = defaultConfig
defaultConfig := getDefaultConfig()
config = &defaultConfig

file, err := os.Open(configPath)
if err != nil {
Expand All @@ -208,7 +225,7 @@ func LoadConfig() {
defer file.Close()

// load and merge the default config with the user config
loadedConfig := *defaultConfig
loadedConfig := getDefaultConfig()
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
logger.Warn().Err(err).Msg("config file JSON parsing failed")
configSuccess.Set(0.0)
Expand All @@ -217,28 +234,26 @@ func LoadConfig() {

// merge the user config with the default config
if loadedConfig.UsbConfig == nil {
loadedConfig.UsbConfig = defaultConfig.UsbConfig
loadedConfig.UsbConfig = getDefaultConfig().UsbConfig
}

if loadedConfig.UsbDevices == nil {
loadedConfig.UsbDevices = defaultConfig.UsbDevices
loadedConfig.UsbDevices = getDefaultConfig().UsbDevices
}

if loadedConfig.NetworkConfig == nil {
loadedConfig.NetworkConfig = defaultConfig.NetworkConfig
loadedConfig.NetworkConfig = getDefaultConfig().NetworkConfig
}

if loadedConfig.JigglerConfig == nil {
loadedConfig.JigglerConfig = defaultConfig.JigglerConfig
loadedConfig.JigglerConfig = getDefaultConfig().JigglerConfig
}

// fixup old keyboard layout value
if loadedConfig.KeyboardLayout == "en_US" {
loadedConfig.KeyboardLayout = "en-US"
}

config = &loadedConfig

logging.GetRootLogger().UpdateLogLevel(config.DefaultLogLevel)

configSuccess.Set(1.0)
Expand Down
6 changes: 3 additions & 3 deletions internal/native/cgo/ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ int jetkvm_ui_add_flag(const char *obj_name, const char *flag_name) {
if (obj == NULL) {
return -1;
}

lv_obj_flag_t flag_val = str_to_lv_obj_flag(flag_name);
if (flag_val == 0)
{
Expand Down Expand Up @@ -368,7 +368,7 @@ void jetkvm_video_stop() {
}

int jetkvm_video_set_quality_factor(float quality_factor) {
if (quality_factor < 0 || quality_factor > 1) {
if (quality_factor <= 0 || quality_factor > 1) {
return -1;
}
video_set_quality_factor(quality_factor);
Expand Down Expand Up @@ -417,4 +417,4 @@ void jetkvm_crash() {
// let's call a function that will crash the program
int* p = 0;
*p = 0;
}
}
2 changes: 1 addition & 1 deletion internal/native/cgo/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ int video_init(float factor)
{
detect_sleep_mode();

if (factor < 0 || factor > 1) {
if (factor <= 0 || factor > 1) {
factor = 1.0f;
}
quality_factor = factor;
Expand Down
2 changes: 1 addition & 1 deletion internal/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewNative(opts NativeOptions) *Native {
sleepModeSupported := isSleepModeSupported()

defaultQualityFactor := opts.DefaultQualityFactor
if defaultQualityFactor < 0 || defaultQualityFactor > 1 {
if defaultQualityFactor <= 0 || defaultQualityFactor > 1 {
defaultQualityFactor = 1.0
}

Expand Down
12 changes: 7 additions & 5 deletions jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@ func rpcReboot(force bool) error {
return nil
}

var streamFactor = 1.0

func rpcGetStreamQualityFactor() (float64, error) {
return streamFactor, nil
return config.VideoQualityFactor, nil
}

func rpcSetStreamQualityFactor(factor float64) error {
Expand All @@ -212,7 +210,10 @@ func rpcSetStreamQualityFactor(factor float64) error {
return err
}

streamFactor = factor
config.VideoQualityFactor = factor
if err := SaveConfig(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
return nil
}

Expand Down Expand Up @@ -719,7 +720,8 @@ func rpcSetWakeOnLanDevices(params SetWakeOnLanDevicesParams) error {
}

func rpcResetConfig() error {
config = defaultConfig
defaultConfig := getDefaultConfig()
config = &defaultConfig
if err := SaveConfig(); err != nil {
return fmt.Errorf("failed to reset config: %w", err)
}
Expand Down