Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************************************
- *
- * HOW TO REGULATE SCREEN BRIGHTNESS USING THE KEYBOARD IN AN OVERLY COMPLICATED WAY
- * You can compile this thing!!!
- *
- * This following tutorial is a command line utility in disguise. It changes screen brightness and
- * other similar parameters, i.e. those which are regulated by a text file which consists of *only*
- * one integer. For instance, on my laptop, the screen brightness is regulated by the file
- *
- * /sys/class/backlight/apple_backlight/brightness
- *
- * which is a simple text file containing the current screen brightness value.
- * A similar parameter is keyboard brightness, which, on my laptop, is regulated by the file
- *
- * /sys/class/leds/smc::kbd_backlight/brightness
- *
- * Obviously enough, by modifying those values I can modify screen and keyboard brightness.
- * This utility does just that: it reads the current value in those kind of files and changes it by
- * the amount specified by the user.
- * Yes, you can do this is one thousand different and probably better ways. You can do this using a
- * bash script, you can use xbacklight, whatever. Here is another, more intricate way.
- *
- * So, let's say you want to use this program to modify your screen brightness.
- * First, modify the path to the text file which regulates your screen brightness. You can do this
- * by changing the SCREEN_PATH definition below. The first part of the path, /sys/class/backlight ,
- * is likely to be the same for you too.
- * Often the screen brightness value must fall into a certain range. You can define this range by
- * changing the MIN_SCREEN_BRIGHTNESS and MAX_SCREEN_BRIGHTNESS definitions below.
- * Finally, compile the program by running:
- *
- * # gcc skb.c -o skb
- *
- * You can now adjust your screen brightness by running:
- *
- * # sudo ./skb -s n
- *
- * where n is the amount (positive or negative) by which you wish to modify the brightness.
- *
- * NOTE: since the file which regulates screen brightness is often write-protected, you are probably
- * required to run this program as root, i.e. using sudo.
- *
- *
- * If you also want to use this program to modify your keyboard brightness, uncomment the keyboard
- * definitions and the keyboard entry in the "case" construct, edit the path and the range and
- * compile the program. You can now adjust your keyboard brightness by running:
- *
- * # sudo ./skb -k n
- *
- * where n is the amount (positive or negative) by which you wish to modify the brightness.
- *
- * If you want this program to modify parameter X which is regulated by a text file which consists
- * of *only* one integer, which integer must fall into a certain range, you can modify the generic
- * deifinitions and the generic entry in the "case" construct to suit your needs.
- *
- *
- * You can now move the compiled program skb to /usr/bin so that it can be found by your shell:
- *
- * # sudo mv ./skb /usr/bin/
- *
- * For convenience, you can allow your regular user to execute this program by adding the following
- * line to your sudoers file (edit using the "visudo" command):
- *
- * username ALL=NOPASSWD: /usr/bin/skb
- *
- * Lastly, and here comes the whole purpose of this thing, you can bind a keypress to its execution.
- * This way, when you press that key your screen brightness will change! Isn't that magical?
- * ... anyways, you can do this is many many ways. If you use i3wm (and if you don't you're missing
- * out) you can add the following line(s) to your config file:
- *
- * bindsym a_key exec sudo skb -s +1
- * bindsym another_key exec sudo skb -s -1
- * ...
- *
- * DONE
- *
- * FINAL NOTE: use this program at your own risk. It works well enough for me. I am just learning to
- * write in C. If you find in this code some serious flaw that will cause a computer to
- * explode or worse, please report it to me. You can find me in the archlinux forums
- * under the name of stdi. If you just want to yell at me because this is most unuseful,
- * or because my english is so bad, again, archlinux forums.
- *
- **************************************************************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #define ARG_NUM 3
- /* Screen brightness configuration */
- #define SCREEN_PATH "/sys/class/backlight/apple_backlight/brightness"
- #define MIN_SCREEN_BRIGHTNESS 0
- #define MAX_SCREEN_BRIGHTNESS 15
- /* Keyboard brightness configuration */
- /*
- #define KEYBOARD_PATH "/sys/class/leds/smc::kbd_backlight/brightness"
- #define MIN_KEYBOARD_BRIGHTNESS 0
- #define MAX_KEYBOARD_BRIGHTNESS 255
- */
- /* Generic parameter configuration */
- /*
- #define GENERIC_PATH "/path/to/a/file"
- #define MIN_VALUE 0
- #define MAX_VALUE 100
- */
- /* Functions prototypes definition */
- void change_file(char *file_path, int min, int max, int delta);
- /*************************************************************************************************/
- int main(int argc, char **argv){
- /* Definitions of required variables */
- int delta;
- /* Checks if the user called the program correctly */
- if(argc!=ARG_NUM){
- printf("\nRead the source code\n\n");
- return 0;
- }
- /* "Parses" the user input */
- sscanf(argv[2],"%d", &delta);
- /* Checks the user choice and execute the subsequent action */
- switch(argv[1][1]){
- /* To change screen brightness */
- case 's':
- change_file(SCREEN_PATH, MIN_SCREEN_BRIGHTNESS, MAX_SCREEN_BRIGHTNESS, delta);
- break;
- /* To change keyboard brightness */
- /*
- case 'k':
- change_file(KEYBOARD_PATH, MIN_KEYBOARD_BRIGHTNESS, MAX_KEYBOARD_BRIGHTNESS, delta);
- break;
- */
- /* To change parameter X */
- /*
- case 'g':
- change_file(GENERIC_PATH, MIN_VALUE, MAX_VALUE, delta)
- break;
- */
- default:
- printf("\nRead the source code\n\n");
- break;
- }
- return 0;
- }
- /*************************************************************************************************/
- /*
- *
- * This function was written to edit those configuration files which consists of *only* one integer.
- * One of those files is, for instance, the one that regulates the screen brightness.
- * The function takes as input the path to the file (as a string of characters), the range where the
- * integer in the file must fall (as max and min), and the amount by which changing the current
- * value.
- *
- * WARNING: the content of the file is completely erased and replaced with the new value!
- *
- */
- void change_file(char *file_path, int min, int max, int delta){
- FILE *file_pointer;
- int init_value, final_value;
- /* Opens the file and checks it opened correctly */
- file_pointer=fopen(file_path, "r+");
- if(file_pointer==NULL){
- printf("Unable to open the file.\n");
- exit(1);
- }
- /* Reads the content of the file */
- fscanf(file_pointer, "%d", &init_value);
- /* Assigns the final value and makes sure it stays in the required range */
- final_value = init_value + delta;
- if(final_value>max)
- final_value=max;
- if(final_value<min)
- final_value=min;
- /* Goes to the beginning of the file and prints the final_value */
- rewind(file_pointer);
- fprintf(file_pointer, "%d", final_value);
- fclose(file_pointer);
- }
Advertisement
Add Comment
Please, Sign In to add comment