Last active
September 8, 2017 12:34
-
-
Save dieggsy/f2d3fbcde760435fd1ca98c83c335d4d to your computer and use it in GitHub Desktop.
Python, Emacs Lisp, Common Lisp, and Chicken Scheme implementations of https://wiki.archlinux.org/index.php/Keyboard_backlight for practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun kb-light (&optional up-down) | |
(interactive) | |
(require 'dbus) | |
(cl-flet ((kb-light-call (method &rest args) | |
(apply #'dbus-call-method | |
:system | |
"org.freedesktop.UPower" | |
"/org/freedesktop/UPower/KbdBacklight" | |
"org.freedesktop.UPower.KbdBacklight" | |
method | |
args))) | |
(let* ((maximum (kb-light-call "GetMaxBrightness")) | |
(curr (kb-light-call "GetBrightness")) | |
(delta (cond ((string= up-down "+") 17) | |
((string= up-down "-") -17) | |
(t 0))) | |
(new (max 0 (+ curr delta)))) | |
(when (<= 0 new maximum) | |
(kb-light-call "SetBrightness" :int32 new)) | |
(message "Keyboard Brightness: %s" (round (* 100 (/ (float curr) maximum))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; (ql:quickload :dbus) | |
(defun kb-light (&optional up-down) | |
(dbus:with-open-bus (bus (dbus:system-server-addresses)) | |
(dbus:with-introspected-object | |
(kb-light bus | |
"/org/freedesktop/UPower/KbdBacklight" | |
"org.freedesktop.UPower") | |
(let* ((delta (cond ((string= up-down "+") 2) | |
((string= up-down "-") -2) | |
(t 0))) | |
(current (kb-light "org.freedesktop.UPower.KbdBacklight" | |
"GetBrightness")) | |
(maximum (kb-light "org.freedesktop.UPower.KbdBacklight" | |
"GetMaxBrightness")) | |
(new (max 0 (+ current delta) ))) | |
(when (<= 0 new maximum) | |
(setq current new) | |
(kb-light "org.freedesktop.UPower.KbdBacklight" | |
"SetBrightness" | |
current)) | |
(format t "~a~%" (round (* 100 (float (/ current maximum))))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# coding: utf-8 | |
# Original source: https://wiki.archlinux.org/index.php/Keyboard_backlight | |
from sys import argv | |
import dbus | |
def kb_light_set(delta): | |
bus = dbus.SystemBus() | |
kbd_backlight_proxy = bus.get_object('org.freedesktop.UPower', '/org/freedesktop/UPower/KbdBacklight') | |
kbd_backlight = dbus.Interface(kbd_backlight_proxy, 'org.freedesktop.UPower.KbdBacklight') | |
current = kbd_backlight.GetBrightness() | |
maximum = kbd_backlight.GetMaxBrightness() | |
new = max(0, current + delta) | |
if 0 <= new <= maximum: | |
current = new | |
kbd_backlight.SetBrightness(current) | |
# Return current backlight level percentage | |
return 100 * current / maximum | |
if __name__ == '__main__': | |
if len(argv[1:]) == 1: | |
if argv[1] == "--up" or argv[1] == "+": | |
# ./kb-light.py (+|--up) to increment | |
print(kb_light_set(2)) | |
elif argv[1] == "--down" or argv[1] == "-": | |
# ./kb-light.py (-|--down) to decrement | |
print(kb_light_set(-2)) | |
else: | |
print("Unknown argument:", argv[1]) | |
elif len(argv[1:]) == 0: | |
print(kb_light_set(0)) | |
else: | |
print("Script takes exactly one argument.", len(argv[1:]), "arguments provided.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/csi -s | |
(use dbus) | |
(: main (-> string)) | |
(define (main) | |
(let* ((kb-light (make-context | |
#:bus system-bus | |
#:service 'org.freedesktop.UPower | |
#:interface 'org.freedesktop.UPower.KbdBacklight | |
#:path '/org/freedesktop/UPower/KbdBacklight)) | |
(argv (command-line-arguments)) | |
(up-down (when (not (null? argv)) (car argv))) | |
(delta (cond ((equal? up-down "+") 17) | |
((equal? up-down "-") -17) | |
(else 0))) | |
(current (car (call kb-light "GetBrightness"))) | |
(maximum (car (call kb-light "GetMaxBrightness"))) | |
(new (max 0 (+ current delta)))) | |
(when (<= 0 new maximum) | |
(call kb-light "SetBrightness" new)) | |
(format #t "~s~%" (inexact->exact | |
(round (* 100 (/ new maximum))))))) | |
(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment