123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const char *BRIGHTFILE = "/sys/class/backlight/intel_backlight/brightness";
- const char *MAX_BRIGHTFILE = "/sys/class/backlight/intel_backlight/max_brightness";
- void
- helper(char *progname) {
- printf("%s - utility to set the laptop's display brightness (backlight) from the command-line\n", progname);
- printf("Usage: %s BRIGHTNESS [+BRIGHTNESS] [-BRIGHTNESS]\n", progname);
- printf("Where BRIGHTNESS is an absolute value between 1 and 100, or a 'step' value preceeded by a + (increase) or a - (decrease) sign.\n");
- printf("Examples:\n");
- printf("%s 50 # sets the display brightness to 50%%\n", progname);
- printf("%s +10 # sets the brightness 10%% brighter\n", progname);
- printf("%s -15 # sets the brightness 15%% dimmer\n", progname);
- }
- struct
- argtuple {
- char sign;
- int value;
- };
- struct argtuple
- parseArg(char token[])
- {
- struct argtuple t;
- sscanf(token, "%1c%d", &t.sign, &t.value);
- if (!(t.sign == '+' || t.sign == '-')) {
-
- t.sign = '!';
- t.value = 0;
- }
- return t;
- }
- int
- main(int argc, char *argv[])
- {
- int status = 0;
- int maxbrightness = 0;
- int brightness = 0;
- int is_absolute = 0;
- int multiplier = 1;
- struct argtuple at;
- FILE *fp;
-
- if (argc == 2) {
- if (strncmp(argv[1], "-h", 2) == 0 || strncmp(argv[1], "--help", 6) == 0) {
- helper(argv[0]);
- return 0;
- }
- }
- fp = fopen(BRIGHTFILE, "r");
- if (fp == NULL) {
- printf("Error: brightness file does not exist.\n");
- printf("Make sure some sort of brightness file exists under '/sys', edit it in the source and try again.\n");
- return 1;
- }
- else {
- fscanf(fp, "%d", &brightness);
- status = fclose(fp);
- }
-
-
- fp = fopen(MAX_BRIGHTFILE, "r");
- if (fp == NULL) {
- printf("Error: maximum brightness file does not exist.\n");
- printf("Make sure this file exists under '/sys', edit it in the source and try again.\n");
- return 1;
- }
- else {
-
- fscanf(fp, "%d", &maxbrightness);
- status = fclose(fp);
- }
-
- if (argc == 1) {
- printf("The current brightness is %d, or %.1f%%\n", brightness, 100.0*brightness / maxbrightness);
- printf("Pass a value from 1 to 100 to set the new brightness.\n");
- return 0;
- }
-
-
- at = parseArg(argv[1]);
- if (at.sign == '!') {
- printf("[DEBUG] Setting *absolute* brightness\n");
- is_absolute = 1;
- }
- else {
- printf("[DEBUG] Setting *relative* brightness\n");
- is_absolute = 0;
- }
-
-
-
-
-
- if (is_absolute == 1) {
- multiplier = atoi(argv[1]);
-
-
- if (multiplier > 100 || multiplier < 1) {
- printf("Error: pass a number from 1 to 100 to set new brightness.\n");
- return 1;
- }
-
- brightness = (int)((0.01 * multiplier) * maxbrightness);
- }
- else {
-
- multiplier = (int)((100.0 * brightness) / maxbrightness);
- if (at.sign == '-')
- multiplier -= at.value;
- else
- multiplier += at.value;
- if (multiplier <= 0) {
- printf("Cannot set brightness lower than 0. Already at minimum.\n");
- multiplier = 2;
- }
- else if (multiplier >= 100) {
- printf("Cannot set brightness higher than 100. Already at max.\n");
- multiplier = 100;
- }
- }
-
- brightness = (int)((0.01 * multiplier) * maxbrightness);
- fp = fopen(BRIGHTFILE, "w");
- if (fp == NULL) {
- printf("Error: could not open the brightness file for writing.\n");
- printf("Try it as root or with sudo as member of the 'video' group.\n");
- return 1;
- }
- else {
- fprintf(fp, "%d\n", brightness);
- status = fclose(fp);
- printf("The new brightness is %d, or %d%%\n", brightness, multiplier);
- }
- return 0;
- }
|