keyboard-mouse-share
Version:
Share keyboard and mouse between Mac and Windows
86 lines (69 loc) • 1.98 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os');
const CONFIG_DIR = path.join(os.homedir(), '.kms');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
const DEFAULT_CONFIG = {
mode: null, // "server" or "client"
port: 5555,
host: null, // For client mode
hotkey: 'LEFT CTRL + ENTER', // Ctrl + Enter (fallback)
screenEdge: 'right', // 'left', 'right', 'top', 'bottom', 'none'
edgeThreshold: 5, // Pixels from edge to trigger
autoSwitch: true, // Enable automatic edge switching
logLevel: 'INFO'
};
class Config {
constructor() {
this.config = this.load();
}
load() {
if (!fs.existsSync(CONFIG_FILE)) {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
this.save(DEFAULT_CONFIG);
return { ...DEFAULT_CONFIG };
}
try {
const data = fs.readFileSync(CONFIG_FILE, 'utf8');
const config = JSON.parse(data);
// Merge with defaults for any missing keys
return { ...DEFAULT_CONFIG, ...config };
} catch (error) {
console.error(`Error loading config: ${error.message}`);
return { ...DEFAULT_CONFIG };
}
}
save(config = null) {
const configToSave = config || this.config;
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
fs.writeFileSync(CONFIG_FILE, JSON.stringify(configToSave, null, 2));
}
get(key, defaultValue = null) {
return this.config[key] !== undefined ? this.config[key] : defaultValue;
}
set(key, value) {
this.config[key] = value;
this.save();
}
update(updates) {
this.config = { ...this.config, ...updates };
this.save();
}
isConfigured() {
return this.config.mode !== null;
}
isServer() {
return this.config.mode === 'server';
}
isClient() {
return this.config.mode === 'client';
}
getAll() {
return { ...this.config };
}
}
module.exports = Config;