light.h 5.2KB

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