From 0c680db1ae58b5d629ce773dda94b43ae006dcdf Mon Sep 17 00:00:00 2001 From: Jas-SinghFSU Date: Mon, 1 Jul 2024 23:30:52 -0700 Subject: [PATCH] Added brightness service that was missed in the last commit. --- services/Brightness.js | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 services/Brightness.js diff --git a/services/Brightness.js b/services/Brightness.js new file mode 100644 index 0000000..86d8788 --- /dev/null +++ b/services/Brightness.js @@ -0,0 +1,84 @@ +class BrightnessService extends Service { + // every subclass of GObject.Object has to register itself + static { + // takes three arguments + // the class itself + // an object defining the signals + // an object defining its properties + Service.register( + this, + { + // 'name-of-signal': [type as a string from GObject.TYPE_], + 'screen-changed': ['float'], + }, + { + // 'kebab-cased-name': [type as a string from GObject.TYPE_, 'r' | 'w' | 'rw'] + // 'r' means readable + // 'w' means writable + // guess what 'rw' means + 'screen-value': ['float', 'rw'], + }, + ); + } + + // this Service assumes only one device with backlight + #interface = Utils.exec("sh -c 'ls -w1 /sys/class/backlight | head -1'"); + + // # prefix means private in JS + #screenValue = 0; + #max = Number(Utils.exec('brightnessctl max')); + + // the getter has to be in snake_case + get screen_value() { + return this.#screenValue; + } + + // the setter has to be in snake_case too + set screen_value(percent) { + if (percent < 0) + percent = 0; + + if (percent > 1) + percent = 1; + + Utils.execAsync(`brightnessctl set ${percent * 100}% -q`); + // the file monitor will handle the rest + } + + constructor() { + super(); + + // setup monitor + const brightness = `/sys/class/backlight/${this.#interface}/brightness`; + Utils.monitorFile(brightness, () => this.#onChange()); + + // initialize + this.#onChange(); + } + + #onChange() { + this.#screenValue = Number(Utils.exec('brightnessctl get')) / this.#max; + + // signals have to be explicitly emitted + this.emit('changed'); // emits "changed" + this.notify('screen-value'); // emits "notify::screen-value" + + // or use Service.changed(propName: string) which does the above two + // this.changed('screen-value'); + + // emit screen-changed with the percent as a parameter + this.emit('screen-changed', this.#screenValue); + } + + // overwriting the connect method, let's you + // change the default event that widgets connect to + connect(event = 'screen-changed', callback) { + return super.connect(event, callback); + } +} + +// the singleton instance +const service = new BrightnessService; + +// export to use in other modules +export default service;