light.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. struct _light_context_t
  55. {
  56. struct
  57. {
  58. LFUNCCOMMAND command; // What command was issued
  59. // Only one of value and raw_value is populated; which one depends on the command
  60. uint64_t value; // The input value, in raw mode
  61. float float_value; // The input value as a float
  62. bool raw_mode; // Whether or not we use raw or percentage mode
  63. light_device_target_t *device_target; // The device target to act on
  64. } run_params;
  65. struct
  66. {
  67. char conf_dir[NAME_MAX]; // The path to the application cache directory
  68. } sys_params;
  69. light_device_enumerator_t **enumerators;
  70. uint64_t num_enumerators;
  71. };
  72. // The different available commands
  73. bool light_cmd_print_help(light_context_t *ctx); // H,h
  74. bool light_cmd_print_version(light_context_t *ctx); // V
  75. bool light_cmd_list_devices(light_context_t *ctx); // L
  76. bool light_cmd_set_brightness(light_context_t *ctx); // S
  77. bool light_cmd_get_brightness(light_context_t *ctx); // G
  78. bool light_cmd_get_max_brightness(light_context_t *ctx); // M
  79. bool light_cmd_set_min_brightness(light_context_t *ctx); // N
  80. bool light_cmd_get_min_brightness(light_context_t *ctx); // P
  81. bool light_cmd_add_brightness(light_context_t *ctx); // A
  82. bool light_cmd_sub_brightness(light_context_t *ctx); // U
  83. bool light_cmd_mul_brightness(light_context_t *ctx); // T
  84. bool light_cmd_save_brightness(light_context_t *ctx); // O
  85. bool light_cmd_restore_brightness(light_context_t *ctx); // I
  86. /* Initializes the application, given the command-line. Returns a context. */
  87. light_context_t* light_initialize(int argc, char **argv);
  88. /* Executes the given context. Returns true on success, false on failure. */
  89. bool light_execute(light_context_t*);
  90. /* Frees the given context */
  91. void light_free(light_context_t*);
  92. /* Create a device enumerator in the given context */
  93. light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT, LFUNCENUMFREE);
  94. /* Initializes all the device enumerators (and its devices, targets) */
  95. bool light_init_enumerators(light_context_t *ctx);
  96. /* Frees all the device enumerators (and its devices, targets) */
  97. bool light_free_enumerators(light_context_t *ctx);
  98. /* Use this to create a device. Will automatically be added to enumerator. */
  99. light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data);
  100. /* Use this to delete a device. */
  101. void light_delete_device(light_device_t *device);
  102. /* Use this to create a device target. Will automatically be added to device. */
  103. light_device_target_t *light_create_device_target(light_device_t *device, char const *name, LFUNCVALSET setfunc, LFUNCVALGET getfunc, LFUNCMAXVALGET getmaxfunc, LFUNCCUSTOMCMD cmdfunc, void *target_data);
  104. /* Use this to delete a device target. */
  105. void light_delete_device_target(light_device_target_t *device_target);
  106. typedef struct _light_target_path_t light_target_path_t;
  107. struct _light_target_path_t
  108. {
  109. char enumerator[NAME_MAX];
  110. char device[NAME_MAX];
  111. char target[NAME_MAX];
  112. };
  113. bool light_split_target_path(char const * in_path, light_target_path_t *out_path);
  114. /* Returns the found device target, or null. Name should be enumerator/device/target */
  115. light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name);