linux如何设置显示器亮度调节软件,为 Linux 启用色温和亮度调节工具 | 您所在的位置:网站首页 › 设置屏幕饱和度的软件 › linux如何设置显示器亮度调节软件,为 Linux 启用色温和亮度调节工具 |
## 前言 大家都知道,长时间注视电子屏幕对眼睛伤害很大。特别是在夜晚的时候,亮度过高的屏幕更是让人的眼睛异常酸痛。虽然我们可以通过使用显示器的配置菜单,更改屏幕亮度和色温,但每次都要手动调,未免太过麻烦。实际上,在 Linux 下,有好几款自动调节屏幕色温和亮度的工具。 ## 使用 RedShift RedShift 是 redshift 命令的图形化工具,集成为 gnome 插件。RedShift 界面简洁,非常容易使用,能改根据时间自动地调整屏幕色温: ![](https://leanote.com/api/file/getImage?fileId=58fbf3f1ab644129eb004ed5) 对于 Fedora 用户,可以使用下面命令进行安装: sudo dnf copr enable mystro256/gnome-redshift sudo dnf install gnome-shell-extension-redshift 注意,由于 Wayland 默认不支持 RedShift,因此需要一些额外的工作来使它工作。具体见附录。 **2017-04-23 更新** 如果遇到下图所示错误: ![](https://leanote.com/api/file/getImage?fileId=58fbf1b4ab644129eb004e9a) 请加上 --allowerasing 安装,如果还是不行,请尝试将桌面服务器切换为 xorg 后再次尝试。 ## 使用 ddccontrol ddccontrol 工具可以配置显示器的参数,因此灵活性较高,但配置比较复杂。对于大部分用户而言,可能只需要配置显示器的色温和亮度。为了方便配置,可以使用国外友人编写好的工具:[danielng01/iris-floss-wayland](https://github.com/danielng01/iris-floss-wayland)。这个工具把所有设备检测和操作都封装起来了,使得我们只需要给出目标色温和亮度的值即可。**注意,运行这个工具需要 root 权限**。 ### 安装 ddccontrol sudo dnf install ddccontrol ### 安装 iris-floss-wayland 由于我的系统是 Fedora 25,而默认图形服务器是 Wayland,因此安装的简化工具是 iris-floss-wayland。如果读者的图形服务器是 X11,可以到[Iris Floss仓库](https://github.com/danielng01/iris-floss)下载工具。 下载完成后,执行 make 进行编译。然后会生成可执行文件 iris-floss-wayland。以`sudo`方式执行该程序,同时给出两个参数——第一个是色温,第二个是亮度。如下面的代码将色温设置为 5000K,亮度设置为 40%: sudo ./iris-floss-wayland 5000 40 如果执行后不起作用,可以加上`-v`标志,输出调试信息。 ### 使用 crontab 使用 ddccontrol 工具的一个缺点是需要自己调用它,而使用 RedShift 则不需要,因为 RedShift 因此把这一切都包装好了。为了能改按照时间自动地进行屏幕色温和亮度的切换,可以使用`crontab`命令。如果不熟悉 crontab,可以阅读本文附录给出的相关教程。 #### 创建 crontab 文件 由于需要以 root 运行 iris-floss-wayland(实际上 ddccontrol ),因此 crontab 的添加用户应该是 root。可以在 root 家目录下创建一个 crontab 文件(假设名为 .cron/screen_light_mode.cron),然后为它添加以下内容: # Siwtching screen to day mode, starts at 6 am. 0 6-17 * * * switch-to-day-light-mode # Siwtching screen to night mode, starts at 6 pm. 0 18-23 * * * switch-to-night-light-mode 其中以'#'开头的是注释,crontab 会忽略它。文件中的一行代表一条 crontab 记录,一个包含六列,各列的定义如下: 分 时 日 月 星期 要运行的命令 对于`0 6-17 * * * switch-to-day-light-mode`来说,意思是每天6点到17点,每整时调用命令 switch-to-day-light-mode。 #### 分离配置与调用 在这里,命令 switch-to-day-light-mode 只是对 iris-floss-wayland 调用的一个封装。为什么要做这样一个封装呢?结果很明显:将调用和配置分开。如果将与参数有关的命令直接写在 crontab 中,则每一次修改参数都要运行相关的 crontab 命令进行更新。 这个封装我保存在文件 /etc/profile.d/screen_light_mode.sh,该文件的内容如下: ```bash # Command of siwtching screen light mode # Change these params to fit your eyes. day_temperature=5000 day_lightness=40 night_temperature=3300 night_lightness=35 # The following content should not be modified. day_light_mode_cmd="sudo iris-floss-wayland $day_temperature $day_lightness" night_light_mode_cmd="sudo iris-floss-wayland $night_temperature $night_lightness" export current_screen_light_mode current_screen_light_mode=${current_screen_light_mode:-"day"} switch-to-day-light-mode() { if [ "$current_screen_light_mode" = "night" ]; then current_screen_light_mode="day" $day_light_mode_cmd $* fi } switch-to-night-light-mode() { if [ "$current_screen_light_mode" = "day" ]; then current_screen_light_mode="night" $night_light_mode_cmd $* fi } ``` 通过修改 day_temperature、day_lightness、night_temperature 以及 night_lightness 分别为白天模式和晚上模式指定色温和亮度。 ### 添加 crontab 任务 完成了 crontab 文件的创建后,执行下列命令就完成了定期任务的添加: crontab .cron/screen_light_mode.cron 再罗嗦一句,记得是为特权用户添加该任务,否则配置工具将因缺少权限而执行失败。 ### 添加开机服务 添加 crontab 解决了日光和夜光模式的自动切换,但它无法解决一个问题:在检查间隔期间启动机器的话,便无法自动根据当前时间切换。虽然我们可以将 crontab 的执行间隔缩小,但那样未免杀鸡用牛刀了。实际上,简单地添加一个开机任务,根据启动系统时的时间,更改屏幕模式即可 。 对于使用 systemd 的 Linux 发行版来说,以往存放在 /etc/rc.d/ 目录下的开机启动脚本都会被映射为一个 systemd service unit(服务单元)。服务单元相比以往的启动脚本更容易管理,因为我们可以使用 `systemctl` 命令对其进行管理。sytemd 提供了向前兼容性,使得我们可以像往常那样在 /etc/rc.d/rc.local 中添加启动脚本。但在添加启动脚本之前,要需要进行一些配置工作,使 rc.local 机制工作。 首先编辑 /etc/rc.d/rc.local 文件,在文件中添加以下内容: . /root/scripts/auto-switch-screen-light-mode.sh 注意为 /etc/rc.d/rc.local 添加可执行权限: sudo chmod +x /etc/rc.d/rc.local 接着,编辑 /usr/lib/systemd/system/rc-local.service,添加以下内容: [Install] WantedBy=multi-user.target 然后使能 rc-local 服务: sudo systemctl enable rc-local.service 到此 rc.local 就配置好了,下次启动系统时,便会自动地执行 rc.local 文件中的内容。 最后,完成我们的启动脚本的编写。/root/scripts/auto-switch-screen-light-mode.sh 的内容如下: ```bash #!/usr/bin/bash # Automatically switch screen light mode according to current date's hour. # Load configurations. . /etc/profile.d/screen_light_mode.sh # Get current hour. date_hour=`date +"%H"` # Determine screen light mode. if [ "$date_hour" -lt "18" ]; then $day_light_mode_cmd $* current_screen_light_mode="day" else $night_light_mode_cmd $* current_screen_light_mode="night" fi ``` ## 总结 能改使用 RedShift 工具就好办了……可惜的是在我的系统上安装 RedShift 提示缺少依赖 gnome-settings-daemon-redshift,,而我在网上又找不到,最后只能转而使用 ddccontrol。 ddccontrol 不仅配置麻烦,要自己做一堆附加工作,而且还时不时不能工作。看着身边小伙伴用着和我一样的系统(Fedora 25),却没有这些问题,一个 dnf 就达成了需求。看来还是得找时间好好解决 RedShift 的依赖问题。 **2017-04-22 更新** 坑爹啊……今天再一次尝试安装 RedShift,但又遇到了同样的错误: ![](https://leanote.com/api/file/getImage?fileId=58fbf1b4ab644129eb004e9a) 这一次抱着试试的心态加上了选项 --allowerasing,结果安装成功了!之前好像也有试过,但没有成功……在这期间也就把桌面服务器从 Wayland 换成了xorg,该不会是因为这个才……如果你也遇到了这样的问题,你可以试一下。 ## References - [Redshift functionality on Fedora 25 (GNOME + Wayland).](http://www.thelinuxrain.com/articles/redshift-functionality-on-fedora-25-gnome-wayland-yes-its-possible) - [crontab 定时任务](http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html) - [Fedora 23 实现开机自启脚本](https://semsevens.com/2016/03/08/make-startup-script-in-fedora/) - [Improve your sleep by using Redshift on Fedora](https://fedoramagazine.org/safe-eyes-redshift/) |
CopyRight 2018-2019 实验室设备网 版权所有 |