stdi

skb

May 17th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.48 KB | None | 0 0
  1. /**************************************************************************************************
  2. *
  3. *       HOW TO REGULATE SCREEN BRIGHTNESS USING THE KEYBOARD IN AN OVERLY COMPLICATED WAY
  4. *                                  You can compile this thing!!!                                  
  5. *
  6. * This following tutorial is a command line utility in disguise.  It changes screen brightness  and
  7. * other similar parameters,  i.e. those which are regulated by a text file which consists of *only*
  8. * one integer. For instance, on my laptop, the screen brightness is regulated by the file
  9. *
  10. *            /sys/class/backlight/apple_backlight/brightness
  11. *
  12. * which is a simple text file containing the current screen brightness value.
  13. * A similar parameter is keyboard brightness, which, on my laptop, is regulated by the file
  14. *
  15. *            /sys/class/leds/smc::kbd_backlight/brightness
  16. *
  17. * Obviously enough, by modifying those values I can modify screen and keyboard brightness.
  18. * This utility does just that:  it reads the current value in those kind of files and changes it by
  19. * the amount specified by the user.
  20. * Yes, you can do this is one thousand different and probably better ways.  You can do this using a
  21. * bash script, you can use xbacklight, whatever. Here is another, more intricate way.
  22. *
  23. * So, let's say you want to use this program to modify your screen brightness.
  24. * First,  modify the path to the text file which regulates your screen brightness.  You can do this
  25. * by changing the SCREEN_PATH definition below.  The first part of the path, /sys/class/backlight ,
  26. * is likely to be the same for you too.
  27. * Often  the screen brightness value must fall into a certain range.  You can define this range  by
  28. * changing the MIN_SCREEN_BRIGHTNESS and MAX_SCREEN_BRIGHTNESS definitions below.
  29. * Finally, compile the program by running:
  30. *
  31. *            # gcc skb.c -o skb
  32. *
  33. * You can now adjust your screen brightness by running:
  34. *
  35. *            # sudo ./skb -s n
  36. *
  37. * where n is the amount (positive or negative) by which you wish to modify the brightness.
  38. *
  39. * NOTE: since the file which regulates screen brightness is often write-protected, you are probably
  40. *       required to run this program as root, i.e. using sudo.
  41. *
  42. *
  43. * If  you also want to use this program to modify your keyboard brightness,  uncomment the keyboard
  44. * definitions and the keyboard entry in the "case" construct, edit the path and the range and
  45. * compile the program. You can now adjust your keyboard brightness by running:
  46. *
  47. *            # sudo ./skb -k n
  48. *
  49. * where n is the amount (positive or negative) by which you wish to modify the brightness.
  50. *
  51. * If  you want this program to modify parameter X which is regulated by a text file which  consists
  52. * of  *only*  one integer, which integer must fall into a certain range, you can modify the generic
  53. * deifinitions and the generic entry in the "case" construct to suit your needs.
  54. *
  55. *
  56. * You can now move the compiled program skb to /usr/bin so that it can be found by your shell:
  57. *
  58. *            # sudo mv ./skb /usr/bin/
  59. *
  60. * For convenience,  you can allow your regular user to execute this program by adding the following
  61. * line to your sudoers file (edit using the "visudo" command):
  62. *
  63. *            username ALL=NOPASSWD: /usr/bin/skb
  64. *
  65. * Lastly, and here comes the whole purpose of this thing, you can bind a keypress to its execution.
  66. * This  way,  when  you  press that key your screen brightness will  change!  Isn't  that  magical?
  67. * ... anyways, you can do this is many many ways.  If you use i3wm (and if you don't you're missing
  68. * out) you can add the following line(s) to your config file:
  69. *
  70. *            bindsym a_key  exec sudo skb -s +1
  71. *            bindsym another_key exec sudo skb -s -1
  72. *            ...
  73. *
  74. * DONE
  75. *
  76. * FINAL NOTE: use this program at your own risk. It works well enough for me. I am just learning to
  77. *             write in C.  If you find in this code some serious flaw that will cause a computer to
  78. *             explode or worse,  please  report it to me.  You  can find me in the archlinux forums
  79. *             under the name of stdi. If you just want to yell at me because this is most unuseful,
  80. *             or because my english is so bad, again, archlinux forums.
  81. *
  82. **************************************************************************************************/
  83.  
  84. #include<stdio.h>
  85. #include<stdlib.h>
  86. #define ARG_NUM 3
  87.  
  88.  
  89. /* Screen brightness configuration */
  90. #define SCREEN_PATH "/sys/class/backlight/apple_backlight/brightness"
  91. #define MIN_SCREEN_BRIGHTNESS 0
  92. #define MAX_SCREEN_BRIGHTNESS 15
  93.  
  94.  
  95. /* Keyboard brightness configuration */
  96. /*
  97. #define KEYBOARD_PATH "/sys/class/leds/smc::kbd_backlight/brightness"
  98. #define MIN_KEYBOARD_BRIGHTNESS 0
  99. #define MAX_KEYBOARD_BRIGHTNESS 255
  100. */
  101.  
  102.  
  103. /* Generic parameter configuration */
  104. /*
  105. #define GENERIC_PATH "/path/to/a/file"
  106. #define MIN_VALUE 0
  107. #define MAX_VALUE 100
  108. */
  109.  
  110.  
  111. /* Functions prototypes definition */
  112. void change_file(char *file_path, int min, int max, int delta);
  113.  
  114. /*************************************************************************************************/
  115.  
  116. int main(int argc, char **argv){
  117.  
  118.    /* Definitions of required variables */
  119.    int delta;
  120.  
  121.    /* Checks if the user called the program correctly */
  122.    if(argc!=ARG_NUM){
  123.       printf("\nRead the source code\n\n");
  124.       return 0;
  125.    }
  126.  
  127.    /* "Parses" the user input */
  128.    sscanf(argv[2],"%d", &delta);
  129.  
  130.    /* Checks the user choice and execute the subsequent action */
  131.    switch(argv[1][1]){
  132.  
  133.  
  134.       /* To change screen brightness */
  135.       case 's':
  136.          change_file(SCREEN_PATH, MIN_SCREEN_BRIGHTNESS, MAX_SCREEN_BRIGHTNESS, delta);
  137.          break;
  138.  
  139.  
  140.       /* To change keyboard brightness */
  141.       /*
  142.       case 'k':
  143.          change_file(KEYBOARD_PATH, MIN_KEYBOARD_BRIGHTNESS, MAX_KEYBOARD_BRIGHTNESS, delta);
  144.          break;
  145.       */
  146.  
  147.  
  148.       /* To change parameter X */
  149.       /*
  150.       case 'g':
  151.          change_file(GENERIC_PATH, MIN_VALUE, MAX_VALUE, delta)
  152.          break;
  153.       */
  154.  
  155.  
  156.       default:
  157.          printf("\nRead the source code\n\n");
  158.          break;
  159.    }
  160.  
  161.    return 0;
  162.  
  163. }
  164.  
  165.  
  166. /*************************************************************************************************/
  167.  
  168.  
  169. /*
  170. *
  171. * This function was written to edit those configuration files which consists of *only* one integer.
  172. * One of those files is, for instance, the one that regulates the screen brightness.
  173. * The function takes as input the path to the file (as a string of characters), the range where the
  174. * integer in the file must fall (as max and min), and the amount by which changing the current
  175. * value.
  176. *
  177. * WARNING: the content of the file is completely erased and replaced with the new value!
  178. *
  179. */
  180.  
  181.  
  182. void change_file(char *file_path, int min, int max, int delta){
  183.  
  184.    FILE *file_pointer;
  185.    int init_value, final_value;
  186.  
  187.    /* Opens the file and checks it opened correctly */
  188.    file_pointer=fopen(file_path, "r+");
  189.    if(file_pointer==NULL){
  190.       printf("Unable to open the file.\n");
  191.       exit(1);
  192.    }
  193.  
  194.    /* Reads the content of the file */
  195.    fscanf(file_pointer, "%d", &init_value);
  196.  
  197.    /* Assigns the final value and makes sure it stays in the required range */
  198.    final_value = init_value + delta;
  199.    if(final_value>max)
  200.       final_value=max;
  201.    if(final_value<min)
  202.       final_value=min;
  203.  
  204.    /* Goes to the beginning of the file and prints the final_value */
  205.    rewind(file_pointer);
  206.    fprintf(file_pointer, "%d", final_value);
  207.    fclose(file_pointer);  
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment