之前写的博文里已经实现了屏幕亮度的调节(http://jianshusoft.blog.51cto.com/2380869/810780),但是毕竟没有和Fn快捷键绑定,略有遗憾。所以写个小程序让键盘来控制亮度。原理很简单,监控acpi_video0目录下的brightness文件,如果有改动,则修改到intel_backlight下的brightness。而且把密码隐藏在了程序里,不用担心shell脚本会漏露密码了。
- #include <stdlib.h>
- #include <unistd.h>
- int main()
- {
- int fd = inotify_init();
- if(fd == -1){
- printf("inotify_init error!\n");
- exit(-1);
- }
- system("echo \"这里输入密码\" | sudo -S chmod 777 /sys/class/backlight/intel_backlight/brightness");
- inotify_add_watch(fd, "/sys/class/backlight/acpi_video0/brightness", IN_MODIFY);
- inotify_event buf[1];
- int size = sizeof(inotify_event) * 1;
- int last = 0;
- char brightness[10];
- while(read(fd, buf, size)){
- FILE *source = fopen("/sys/class/backlight/acpi_video0/brightness", "r");
- if(fgets(brightness, 10, source) != NULL){
- fclose(source);
- int temp = (atoi(brightness) + 1) * 4882 / 10;
- if(last != temp ){
- last = temp;
- sprintf(brightness, "%d", temp);
- FILE *file = fopen("/sys/class/backlight/intel_backlight/brightness", "w");
- fputs(brightness, file);
- fclose(file);
- printf("changed %s \n", brightness);
- }
- //每次按键会修改三次,所以忽略后两次
- //read(fd, buf, size);
- //read(fd, buf, size);
- }
- }
- int result = close(fd);
- printf("%d", result);
- }
本文转自 dogegg250 51CTO博客,原文链接:http://blog.51cto.com/jianshusoft/817443,如需转载请自行联系原作者