Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file sbacklight.c
- * @author Mikhail Klementyev jollheef<AT>riseup.net
- * @license GNU GPLv3
- * @date March, 2014
- * @brief Manage backlight
- *
- * Управление яркостью подсветки дисплея.
- */
- #define IN
- #define OUT
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <signal.h>
- #include <unistd.h>
- #include <errno.h>
- #include <getopt.h>
- //#define DEBUG
- #define ERROR (-1)
- #define TEMP_FILE_PATH "/tmp/.sbacklight"
- #ifdef DEBUG
- #define error(message) \
- perror(message); \
- if (errno) \
- { \
- return -1; \
- }
- #else
- #define error(message) \
- if (errno) \
- { \
- perror(message); \
- return -1; \
- }
- #endif
- #define BRIGHTNESS_PATH "/sys/class/backlight/acpi_video0/brightness"
- /**
- * Получает яркость.
- *
- * @param filePath [in] -- путь к файлу.
- * @return уровень яркости подсветки, в процентах или -1 в случае ошибки.
- */
- int
- getBacklight(IN char* filePath)
- {
- int value;
- FILE* file = fopen(filePath, "r");
- error("Open file");
- fscanf(file, "%d", &value);
- error("Read file");
- fclose(file);
- return value;
- }
- /**
- * Устанавливает яркость плавно.
- *
- * @param filePath [in] -- путь к файлу.
- * @param value [in] -- уровень яркости подсветки, в процентах.
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- setBacklightSmooth(IN char* filePath, IN int value)
- {
- /* Нормализуем значение */
- if (value > 100)
- {
- value = 100;
- }
- if (value <= 0)
- {
- value = 1;
- }
- int oldValue = getBacklight(BRIGHTNESS_PATH);
- int step = oldValue > value ? -1 : 1;
- for (; oldValue != (value + step); oldValue += step)
- {
- FILE* file = fopen(filePath, "w");
- error("Open file");
- fprintf(file, "%d\n", oldValue);
- error("Write file");
- fclose(file);
- }
- return 0;
- }
- /**
- * Устанавливает яркость.
- *
- * @param filePath [in] -- путь к файлу.
- * @param value [in] -- уровень яркости подсветки, в процентах.
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- setBacklight(IN char* filePath, IN int value)
- {
- /* Нормализуем значение */
- if (value > 100)
- {
- value = 100;
- }
- if (value < 0)
- {
- value = 0;
- }
- FILE* file = fopen(filePath, "w");
- error("Open file");
- fprintf(file, "%d\n", value);
- error("Write file");
- fclose(file);
- return 0;
- }
- /**
- * Увеличивает яркость.
- *
- * @param value [in] -- величина увеличения яркости.
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- incBacklight(IN int value)
- {
- int backlight = getBacklight(BRIGHTNESS_PATH);
- if (ERROR != backlight)
- {
- return setBacklightSmooth(BRIGHTNESS_PATH,
- getBacklight(BRIGHTNESS_PATH) + value);
- }
- return -1;
- }
- /**
- * Уменьшает яркость.
- *
- * @param value [in] -- величина уменьшения яркости.
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- decBacklight(IN int value)
- {
- int backlight = getBacklight(BRIGHTNESS_PATH);
- if (ERROR != backlight)
- {
- return setBacklightSmooth(BRIGHTNESS_PATH,
- getBacklight(BRIGHTNESS_PATH) - value);
- }
- return -1;
- }
- /**
- * Переключает яркость.
- *
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- toggleBacklight()
- {
- int brightness = getBacklight(BRIGHTNESS_PATH);
- if (0 == brightness)
- {
- brightness = getBacklight(TEMP_FILE_PATH);
- setBacklight(BRIGHTNESS_PATH, brightness);
- }
- else
- {
- setBacklight(TEMP_FILE_PATH, brightness);
- setBacklight(BRIGHTNESS_PATH, 0);
- }
- return 0;
- }
- /**
- * Инициализирует временный файл, содержащий значение яркости.
- *
- * @param filePath [in] -- путь к файлу.
- * @return 0 в случае успеха, -1 в случае ошибки.
- */
- int
- initTempFile(IN char* filePath)
- {
- FILE* file = fopen(filePath, "w");
- error("Open file");
- fprintf(file, "%d\n", 80);
- error("Write file");
- fclose(file);
- return 0;
- }
- /**
- * Точка входа в программу.
- *
- * @param argc [in] -- количество аргументов.
- * @param argv [in] -- указатель на массив аргументов.
- * @return статус завершения.
- */
- int
- main(IN int argc, IN char* argv[])
- {
- int opt, option_index;
- struct option longOpts[] = {
- {"inc", required_argument, NULL, 'i'},
- {"dec", required_argument, NULL, 'd'},
- {"set", required_argument, NULL, 's'},
- {"toggle", no_argument, NULL, 't'},
- {"get", no_argument, NULL, 'g'},
- {NULL, no_argument, NULL, 0}
- };
- //initTempFile(TEMP_FILE_PATH);
- if ((opt = getopt_long(argc, argv, "i:d:s:tg", longOpts, &option_index)) != -1)
- {
- int value;
- switch (opt) {
- case 'i':
- value = atoi(strdup(optarg));
- incBacklight(value);
- return 0;
- case 'd':
- value = atoi(strdup(optarg));
- decBacklight(value);
- return 0;
- case 's':
- value = atoi(strdup(optarg));
- setBacklightSmooth(BRIGHTNESS_PATH, value);
- return 0;
- case 't':
- toggleBacklight();
- return 0;
- case 'g':
- printf("%d\n", getBacklight(BRIGHTNESS_PATH));
- return 0;
- }
- }
- fprintf(stderr, "Usage: %s -[i|d|s|t] value.\n", argv[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement