我编写了一个脚本,可以让我切换笔记本电脑屏幕的亮度(我从受限制的热键运行xbindkeys
),
但它涉及写入/sys/class/backlight/acpi_video0/brightness
。
目前,我只是通过执行该操作使其能够写入该文件,
sudo chown $USER: /sys/class/backlight/acpi_video0/brightness
但每次重新启动笔记本电脑后它都会重置,
因此我需要重新运行它才能使用我的热键。
正确的、持久的方法是什么?
EDIT_3(即添加后以下 2 处编辑)(技术上称为“答案”):
我已经厌倦了尝试让 udev 工作,
并且(在经历了很多混乱之后(其中未知数量的错误是由于我打字失误,漏掉了“brightness”中的“t”))
最终让它像这样工作:
(明确地说,我echo $USER
的是o1
。)
所以
- (1)
我创建了一个文件:
/home/o1/.icanhasbrightness.sh
包含:
#!/usr/bin/bash
chown o1: /sys/class/backlight/acpi_video0/brightness
- (2)
我做到了:
>sudo chown root:root /home/o1/.icanhasbrightness.sh
和
> sudo chmod 0711 /home/o1/.icanhasbrightness.sh
(
例如
> stat /home/o1/.icanhasbrightness.sh|head -n 4|tail -n 1
#=>
Access: (0711/-rwx--x--x) Uid: ( 0/ root) Gid: ( 0/ root)
)
- (3)
我这样做了:
>sudo visudo
并添加了以下行:
o1 ALL=(ALL) NOPASSWD: /home/o1/.icanhasbrightness.sh
- (4)
我修改了我的 fishshell 函数来检查我是否不是亮度文件的所有者,如果不是,则运行该脚本。
(
function brightness_set_current
#todo this is prolly the "wrong" way of handling this but whatever it works
if test $USER != (stat -c %U /sys/class/backlight/acpi_video0/brightness)
sudo /home/o1/.icanhasbrightness.sh
end
echo $argv > /sys/class/backlight/acpi_video0/brightness
end
)
...所以我想这从技术上回答了我自己的问题,但是...
- (1)我觉得这可能是“错误”的做法(?)
- (2)我现在已经厌倦了处理这个问题,无论如何,我现在也不想把它发布为“答案”。
(((
并且为了我过去的自我的任何假设的未来等价物
(
也就是说,这与我试图在这里得到答案的实际问题无关,
但我现在先把它放在这里,然后找个更好的地方放,这样才有可能被那些觉得它有用的人看到
),
只想要一个热键来切换亮度和屏幕色温,
那么假设你已经在使用 fishshell(并且知道将它们保存为自动加载函数等等),代码如下:
function brightness_get_current
cat /sys/class/backlight/acpi_video0/brightness
end
function brightness_set_current
#td FIX(?)
if test $USER != (stat -c %U /sys/class/backlight/acpi_video0/brightness)
sudo /home/o1/.icanhasbrightness.sh
end
echo $argv > /sys/class/backlight/acpi_video0/brightness
end
function brightness_get_max --description 'max brightness, mb'
cat /sys/class/backlight/acpi_video0/max_brightness
end
function brightness_set_max --description 'max brightness, mb'
brightness_set_current (brightness_get_max)
end
function brightness_toggle
#
#
set -l verbose $argv
set -l current_brightness (brightness_get_current)
set -l current_ct (xsct|g --nocolor -o '\d+$')
#bk
# echo current_ct $current_ct
#
set -l brightness
if test $current_brightness -eq 0
set brightness 'min'
else if test $current_brightness -eq (brightness_get_max)
set brightness 'max'
else
set brightness 'mid'
end
set -l redness
if test $current_ct -eq 1000
set redness 'red'
else if test $current_ct -lt 1000
set redness 'red_super'
else if test $current_ct -lt 700
set redness 'red_impossible'
else if test $current_ct -lt 6500
set redness 'mid'
else if test $current_ct -eq 6500
set redness 'white'
else
set redness 'blue'
end
#
function brightness_set_to_max_white
brightness_set_max
D0 xsct 0 #ie 6500
end
function brightness_set_to_min_red
brightness_set_current 0
D0 xsct 1000
end
function brightness_set_to_min_white
brightness_set_current 0
D0 xsct 0 #ie 6500
end
#
#bk
if test "$verbose"
echo \t'current state:'
echo \t\t'brightness:' \t$brightness
echo \t\t'redness:' \t$redness
end
#
if test "$brightness" = 'max' -a "$redness" = 'white'
#bk
test "$verbose"
and echo "1 - setting to min_red ( from max_white )"
brightness_set_to_min_red
else if test "$brightness" = 'min' -a "$redness" = 'red'
#bk
test "$verbose"
and echo "2 - setting to min_white ( from min_red )"
brightness_set_to_min_white
else if test "$brightness" = 'min' -a "$redness" = 'white'
#bk
test "$verbose"
and echo "3 - setting to max_white ( from min_white )"
brightness_set_to_max_white
else
#bk
test "$verbose"
and echo "4 - setting to max_white ( from other )"
brightness_set_to_max_white
end
end
为了将它绑定到一个键,
我只需输入~/.xbindkeysrc
以下内容:
"fish -c 'brightness_toggle'"
Mod4 + less
(Mod4 + less
只是告诉xbindkeys -mk
我当我按下它时它会调用那个组合键)
... 哦,是的,除了安装,xsct
你还必须执行
> git clone 'https://github.com/faf0/sct.git'
(在你想要sct
目录最终所在的目录中)
然后
> sudo apt install libxrandr-dev
(你需要编译它的依赖项)
然后(在sct
由git clone
-ing 创建的目录中)
>sudo make install
(
此外,我设置了热键来翻转屏幕的哪一边是“向上”
(这也依赖于该brightness_set_current
功能,因为至少对我来说,xrandr -o
由于某种原因使用亮度会混乱)
与 fishshell:
function screen_orientation --description 'screen/monitor orientation - possible arguments are normal/left/right/inverted'
set current_brightness (cat /sys/class/backlight/acpi_video-0/max_brightness)
xrandr -o $argv
and brightness_set_current $current_brightness
# guess i don't need this really?
# if thnpadd
# nitrogen --restore
# # and D0 nitrogen --restore
# end
end
和~/.xbindkeysrc
:
"fish -c 'screen_orientation left'"
Mod4 + Left
"fish -c 'screen_orientation normal'"
Mod4 + Up
"fish -c 'screen_orientation right'"
Mod4 + Right
"fish -c 'screen_orientation inverted'"
Mod4 + Down
)
)))
EDIT_1(“udev 可能吗?”):
我发现大维基:
默认情况下,只能
root
通过此方法更改亮度。要允许 [例如] 组中的用户video
更改亮度,可以使用如下 udev 规则:
[写入此文件:]
/etc/udev/rules.d/backlight.rules
[此内容:]
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="acpi_video0", GROUP="video", MODE="0664"
于是我这样做了,然后做了:
sudo groupadd video
并且:(
sudo usermod -aG video o1
好吧
,假设我正确地记得我做了什么,按照什么顺序,
但无论如何,groups $USER
做确认我是在video
群里。
)
但重新启动后,仍然尝试像这样写入它仍然会得到:
> echo 5 > /sys/class/backlight/acpi_video0/brightness
#=>
warning: An error occurred while redirecting file '/sys/class/backlight/acpi_video0/brightness'
open: Permission denied
还有这个:
> stat /sys/class/backlight/acpi_video0/brightness
#=>
File: /sys/class/backlight/acpi_video0/brightness
Size: 4096 Blocks: 0 IO Block: 4096 regular file
Device: 16h/22d Inode: 22741 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-09-24 10:51:53.894540644 -0700
Modify: 2021-09-24 10:51:53.888124116 -0700
Change: 2021-09-24 10:51:53.888124116 -0700
Birth: -
这意味着仍然只有root
该文件具有写权限,对吗?
该 udev 规则曾是应该...将文件的所有权更改为video
组,对吗?
(
假设“此文件归该组所有”这个概念和/或术语正确吗……?
无论如何,它应该授予该video
组对该文件的写权限,对吗?
)
那么为什么它不起作用呢?
——我的意思是,由于我无法跳上 Arch 并尝试一下,
我只能认为我所做的本来会在 arch(?) 上工作,
那么 ubuntu 有何不同?
EDIT_2(“udev应该工作??”):
据我了解本 udev 教程,
它应该工作?
(
或者至少,它做说:
Reading rules file: /etc/udev/rules.d/backlight.rules
——但是它没有说任何有关“ GROUP
“或“ MODE
“的事情。
)
(我还尝试将=
's改为:=
's(“确保它不会被其他规则覆盖”),以防万一。)
> udevadm info -a /sys/class/backlight/acpi_video0/
#=>
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/pci0000:00/0000:00:01.0/backlight/acpi_video0':
KERNEL=="acpi_video0"
SUBSYSTEM=="backlight"
DRIVER==""
ATTR{max_brightness}=="15"
ATTR{actual_brightness}=="15"
ATTR{bl_power}=="0"
ATTR{type}=="firmware"
ATTR{brightness}=="15"
ATTR{scale}=="unknown"
looking at parent device '/devices/pci0000:00/0000:00:01.0':
KERNELS=="0000:00:01.0"
SUBSYSTEMS=="pci"
DRIVERS=="radeon"
ATTRS{local_cpulist}=="0-1"
ATTRS{msi_bus}=="1"
ATTRS{max_link_width}=="255"
ATTRS{consistent_dma_mask_bits}=="40"
ATTRS{driver_override}=="(null)"
ATTRS{power_dpm_state}=="balanced"
ATTRS{revision}=="0x00"
ATTRS{device}=="0x9648"
ATTRS{local_cpus}=="3"
ATTRS{subsystem_device}=="0x21ea"
ATTRS{d3cold_allowed}=="1"
ATTRS{power_method}=="dpm"
ATTRS{numa_node}=="-1"
ATTRS{broken_parity_status}=="0"
ATTRS{power_state}=="D0"
ATTRS{boot_vga}=="1"
ATTRS{irq}=="31"
ATTRS{current_link_width}=="0"
ATTRS{current_link_speed}=="Unknown"
ATTRS{class}=="0x030000"
ATTRS{power_profile}=="default"
ATTRS{dma_mask_bits}=="40"
ATTRS{vendor}=="0x1002"
ATTRS{subsystem_vendor}=="0x17aa"
ATTRS{enable}=="1"
ATTRS{power_dpm_force_performance_level}=="auto"
ATTRS{ari_enabled}=="0"
ATTRS{max_link_speed}=="Unknown"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
和:
> udevadm test --action="add" '/devices/pci0000:00/0000:00:01.0/backlight/acpi_video0'
#=>
Load module index
Parsed configuration file /usr/lib/systemd/network/99-default.link
Parsed configuration file /usr/lib/systemd/network/73-usb-net-by-mac.link
Created link configuration context.
Reading rules file: /usr/lib/udev/rules.d/39-usbmuxd.rules
Reading rules file: /usr/lib/udev/rules.d/40-usb-media-players.rules
Reading rules file: /usr/lib/udev/rules.d/40-usb_modeswitch.rules
Reading rules file: /usr/lib/udev/rules.d/40-vm-hotadd.rules
Reading rules file: /usr/lib/udev/rules.d/50-apport.rules
Reading rules file: /usr/lib/udev/rules.d/50-firmware.rules
Reading rules file: /usr/lib/udev/rules.d/50-udev-default.rules
Reading rules file: /usr/lib/udev/rules.d/55-dm.rules
Reading rules file: /usr/lib/udev/rules.d/55-ippusbxd.rules
Reading rules file: /usr/lib/udev/rules.d/56-hpmud.rules
Reading rules file: /usr/lib/udev/rules.d/56-lvm.rules
Reading rules file: /usr/lib/udev/rules.d/60-autosuspend-chromiumos.rules
Reading rules file: /usr/lib/udev/rules.d/60-block.rules
Reading rules file: /usr/lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /usr/lib/udev/rules.d/60-crda.rules
Reading rules file: /usr/lib/udev/rules.d/60-drm.rules
Reading rules file: /usr/lib/udev/rules.d/60-evdev.rules
Reading rules file: /usr/lib/udev/rules.d/60-fido-id.rules
Reading rules file: /usr/lib/udev/rules.d/60-input-id.rules
Reading rules file: /usr/lib/udev/rules.d/60-inputattach.rules
Reading rules file: /usr/lib/udev/rules.d/60-libgphoto2-6.rules
Reading rules file: /usr/lib/udev/rules.d/60-libsane.rules
Reading rules file: /usr/lib/udev/rules.d/60-pcmcia.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-dm.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /usr/lib/udev/rules.d/60-sensor.rules
Reading rules file: /usr/lib/udev/rules.d/60-serial.rules
Reading rules file: /usr/lib/udev/rules.d/60-tpm-udev.rules
Reading rules file: /usr/lib/udev/rules.d/61-autosuspend-manual.rules
Reading rules file: /usr/lib/udev/rules.d/61-kde-bluetooth-rfkill.rules
Reading rules file: /usr/lib/udev/rules.d/61-persistent-storage-android.rules
Reading rules file: /usr/lib/udev/rules.d/64-btrfs.rules
Reading rules file: /usr/lib/udev/rules.d/64-xorg-xkb.rules
Reading rules file: /usr/lib/udev/rules.d/65-libwacom.rules
Reading rules file: /usr/lib/udev/rules.d/66-snapd-autoimport.rules
Reading rules file: /usr/lib/udev/rules.d/69-cd-sensors.rules
Reading rules file: /usr/lib/udev/rules.d/69-libmtp.rules
Reading rules file: /usr/lib/udev/rules.d/69-lvm-metad.rules
Reading rules file: /usr/lib/udev/rules.d/69-wacom.rules
Reading rules file: /usr/lib/udev/rules.d/70-joystick.rules
Reading rules file: /usr/lib/udev/rules.d/70-mouse.rules
Reading rules file: /usr/lib/udev/rules.d/70-pcspkr-beep.rules
Reading rules file: /usr/lib/udev/rules.d/70-power-switch.rules
Reading rules file: /usr/lib/udev/rules.d/70-printers.rules
Reading rules file: /usr/lib/udev/rules.d/70-spice-vdagentd.rules
Reading rules file: /usr/lib/udev/rules.d/70-touchpad.rules
Reading rules file: /usr/lib/udev/rules.d/70-u2f.rules
Reading rules file: /usr/lib/udev/rules.d/70-uaccess.rules
Reading rules file: /usr/lib/udev/rules.d/71-power-switch-proliant.rules
Reading rules file: /usr/lib/udev/rules.d/71-seat.rules
Reading rules file: /usr/lib/udev/rules.d/71-u-d-c-gpu-detection.rules
Reading rules file: /usr/lib/udev/rules.d/73-seat-late.rules
Reading rules file: /usr/lib/udev/rules.d/73-special-net-names.rules
Reading rules file: /usr/lib/udev/rules.d/75-net-description.rules
Reading rules file: /usr/lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-broadmobi-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-cinterion-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-dell-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-dlink-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-ericsson-mbm.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-fibocom-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-foxconn-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-gosuncn-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-haier-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-huawei-net-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-longcheer-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-mtk-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-nokia-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-pcmcia-device-blacklist.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-qdl-device-blacklist.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-quectel-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-sierra.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-simtech-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-telit-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-tplink-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-ublox-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-usb-device-blacklist.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-usb-serial-adapters-greylist.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-x22x-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-zte-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/78-graphics-card.rules
Reading rules file: /usr/lib/udev/rules.d/78-sound-card.rules
Reading rules file: /usr/lib/udev/rules.d/80-debian-compat.rules
Reading rules file: /usr/lib/udev/rules.d/80-drivers.rules
Reading rules file: /usr/lib/udev/rules.d/80-iio-sensor-proxy.rules
Reading rules file: /usr/lib/udev/rules.d/80-libinput-device-groups.rules
Reading rules file: /usr/lib/udev/rules.d/80-mm-candidate.rules
Reading rules file: /usr/lib/udev/rules.d/80-net-setup-link.rules
Reading rules file: /usr/lib/udev/rules.d/80-udisks2.rules
Reading rules file: /usr/lib/udev/rules.d/81-net-dhcp.rules
Reading rules file: /usr/lib/udev/rules.d/84-nm-drivers.rules
Reading rules file: /usr/lib/udev/rules.d/85-hdparm.rules
Reading rules file: /usr/lib/udev/rules.d/85-hplj10xx.rules
Reading rules file: /usr/lib/udev/rules.d/85-nm-unmanaged.rules
Reading rules file: /usr/lib/udev/rules.d/85-regulatory.rules
Reading rules file: /usr/lib/udev/rules.d/85-tlp-rdw.rules
Reading rules file: /usr/lib/udev/rules.d/85-tlp.rules
Reading rules file: /usr/lib/udev/rules.d/90-alsa-restore.rules
Reading rules file: /usr/lib/udev/rules.d/90-bolt.rules
Reading rules file: /usr/lib/udev/rules.d/90-console-setup.rules
Reading rules file: /usr/lib/udev/rules.d/90-fwupd-devices.rules
Reading rules file: /usr/lib/udev/rules.d/90-libinput-fuzz-override.rules
Reading rules file: /usr/lib/udev/rules.d/90-nm-thunderbolt.rules
Reading rules file: /usr/lib/udev/rules.d/90-pulseaudio.rules
Reading rules file: /usr/lib/udev/rules.d/95-cd-devices.rules
Reading rules file: /usr/lib/udev/rules.d/95-dm-notify.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-csr.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-hid.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-hidpp.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-wup.rules
Reading rules file: /usr/lib/udev/rules.d/96-e2scrub.rules
Reading rules file: /usr/lib/udev/rules.d/97-hid2hci.rules
Reading rules file: /usr/lib/udev/rules.d/99-systemd.rules
Reading rules file: /etc/udev/rules.d/backlight.rules
Unload module index
Unloaded link configuration context.
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.
DEVPATH=/devices/pci0000:00/0000:00:01.0/backlight/acpi_video0
ACTION=add
SUBSYSTEM=backlight
TAGS=:seat:systemd:
ID_PATH=pci-0000:00:01.0
ID_PATH_TAG=pci-0000_00_01_0
ID_FOR_SEAT=backlight-pci-0000_00_01_0
SYSTEMD_WANTS=systemd-backlight@backlight:acpi_video0.service
USEC_INITIALIZED=2680045
答案1
这个罐头答案可能帮助。
许多设备访问问题可以通过组成员身份变更来解决。
sudo journalctl --follow
您可以在连接设备时通过观察来找到设备名称。或者ls -1 /dev >dev.before
,连接设备,等待 10 秒,ls -1 /dev >dev.after;diff dev.{before,after}
。
具体来说,如果ls -l
显示组权限(第二个“ rwx
”三元组)为“ rw
”(例如“ -rw-rw----
”),那么将自己添加到拥有该设备的组将授予rw
访问权限。
就是这样:
device="/dev/whatever"
sudo adduser $USER $(stat -c "%G" $device)
rw
这允许您成为可以使用该设备的群组的成员,但还有一步。
要使所有进程成为新组的成员,请注销并登录。组成员资格是按时设置的login
。
要在新组中创建单个进程(用于测试,在注销/登录之前):
newgrp $(stat -c "%G" $device)
或者,只需键入组名。请参阅man newgrp
。