light.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <limits.h> // NAME_MAX
  5. #include <stddef.h> // NULL
  6. #include "config.h"
  7. #define LIGHT_YEAR "2012 - 2018"
  8. #define LIGHT_AUTHOR "Fredrik Haikarainen"
  9. struct _light_device_target_t;
  10. typedef struct _light_device_target_t light_device_target_t;
  11. struct _light_device_t;
  12. typedef struct _light_device_t light_device_t;
  13. struct _light_device_enumerator_t;
  14. typedef struct _light_device_enumerator_t light_device_enumerator_t;
  15. /* Function pointers that implementations have to set for device targets */
  16. typedef bool (*LFUNCVALSET)(light_device_target_t*, uint64_t);
  17. typedef bool (*LFUNCVALGET)(light_device_target_t*, uint64_t*);
  18. typedef bool (*LFUNCMAXVALGET)(light_device_target_t*, uint64_t*);
  19. typedef bool (*LFUNCCUSTOMCMD)(light_device_target_t*, char const *);
  20. /* Describes a target within a device (for example a led on a keyboard, or a controller for a backlight) */
  21. struct _light_device_target_t
  22. {
  23. char name[256];
  24. LFUNCVALSET set_value;
  25. LFUNCVALGET get_value;
  26. LFUNCMAXVALGET get_max_value;
  27. LFUNCCUSTOMCMD custom_command;
  28. void *device_target_data;
  29. light_device_t *device;
  30. };
  31. /* Describes a device (a backlight, a keyboard, a led-strip) */
  32. struct _light_device_t
  33. {
  34. char name[256];
  35. light_device_target_t **targets;
  36. uint64_t num_targets;
  37. void *device_data;
  38. light_device_enumerator_t *enumerator;
  39. };
  40. typedef bool (*LFUNCENUMINIT)(light_device_enumerator_t*);
  41. typedef bool (*LFUNCENUMFREE)(light_device_enumerator_t*);
  42. /* An enumerator that is responsible for creating and freeing devices as well as their targets */
  43. struct _light_device_enumerator_t
  44. {
  45. char name[256];
  46. LFUNCENUMINIT init;
  47. LFUNCENUMFREE free;
  48. light_device_t **devices;
  49. uint64_t num_devices;
  50. };
  51. typedef struct _light_context_t light_context_t;
  52. // A command that can be run (set, get, add, subtract, print help, print version, list devices etc.)
  53. typedef bool (*LFUNCCOMMAND)(light_context_t *);
  54. /*
  55. Options
  56. v Specify verbosity, defaults to o
  57. s Specify target, defaults to sysfs/backlight/auto
  58. r Use raw values instead of percentage
  59. Operations
  60. H,h Print help and exit
  61. V Print version and exit
  62. L List devices
  63. G Get brigthness
  64. M Get max brightness
  65. N Set minimum cap
  66. P Get minimum cap
  67. S Set brigthness
  68. A Increase brightness
  69. U Decrease brightness
  70. O Save brightness
  71. I Restore brightness
  72. */
  73. // The different available commands
  74. bool light_cmd_print_help(light_context_t *ctx); // H,h
  75. bool light_cmd_print_version(light_context_t *ctx); // V
  76. bool light_cmd_list_devices(light_context_t *ctx); // L
  77. bool light_cmd_set_brightness(light_context_t *ctx); // S
  78. bool light_cmd_get_brightness(light_context_t *ctx); // G
  79. bool light_cmd_get_max_brightness(light_context_t *ctx); // M
  80. bool light_cmd_set_min_brightness(light_context_t *ctx); // N
  81. bool light_cmd_get_min_brightness(light_context_t *ctx); // P
  82. bool light_cmd_add_brightness(light_context_t *ctx); // A
  83. bool light_cmd_sub_brightness(light_context_t *ctx); // U
  84. bool light_cmd_save_brightness(light_context_t *ctx); // O
  85. bool light_cmd_restore_brightness(light_context_t *ctx); // I
  86. // CONFDIR/targets/<enumerator>/<device>/<target>/ minimum|saved
  87. struct _light_context_t
  88. {
  89. struct
  90. {
  91. LFUNCCOMMAND command; // What command was issued
  92. uint64_t value; // The input value, in raw mode
  93. bool raw_mode; // Whether or not we use raw or percentage mode
  94. light_device_target_t *device_target; // The device target to act on
  95. } run_params;
  96. struct
  97. {
  98. char conf_dir[NAME_MAX]; // The path to the application cache directory
  99. } sys_params;
  100. light_device_enumerator_t **enumerators;
  101. uint64_t num_enumerators;
  102. };
  103. /* Initializes the application, given the command-line. Returns a context. */
  104. light_context_t* light_initialize(int argc, char **argv);
  105. /* Executes the given context. Returns true on success, false on failure. */
  106. bool light_execute(light_context_t*);
  107. /* Frees the given context */
  108. void light_free(light_context_t*);
  109. /* Create a device enumerator in the given context */
  110. light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT, LFUNCENUMFREE);
  111. /* Initializes all the device enumerators (and its devices, targets) */
  112. bool light_init_enumerators(light_context_t *ctx);
  113. /* Frees all the device enumerators (and its devices, targets) */
  114. bool light_free_enumerators(light_context_t *ctx);
  115. void light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device);
  116. void light_add_device_target(light_device_t *device, light_device_target_t *new_target);
  117. typedef struct _light_target_path_t light_target_path_t;
  118. struct _light_target_path_t
  119. {
  120. char enumerator[NAME_MAX];
  121. char device[NAME_MAX];
  122. char target[NAME_MAX];
  123. };
  124. bool light_split_target_path(char const * in_path, light_target_path_t *out_path);
  125. /* Returns the found device target, or null. Name should be enumerator/device/target */
  126. light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name);