razer.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "impl/razer.h"
  2. #include "light.h"
  3. #include "helpers.h"
  4. #include <stdio.h> //snprintf
  5. #include <stdlib.h> // malloc, free
  6. #include <dirent.h> // opendir, readdir
  7. static void _impl_razer_add_target(light_device_t *device, char const *name, char const *filename, uint64_t max_brightness)
  8. {
  9. impl_razer_data_t *target_data = malloc(sizeof(impl_razer_data_t));
  10. snprintf(target_data->brightness, sizeof(target_data->brightness), "/sys/bus/hid/drivers/razerkbd/%s/%s", device->name, filename);
  11. target_data->max_brightness = max_brightness;
  12. // Only add targets that actually exist, as we aren't fully sure exactly what targets exist for a given device
  13. if(light_file_exists(target_data->brightness))
  14. {
  15. light_create_device_target(device, name, impl_razer_set, impl_razer_get, impl_razer_getmax, impl_razer_command, target_data);
  16. }
  17. else
  18. {
  19. LIGHT_WARN("razer: couldn't add target %s to device %s, the file %s doesn't exist", name, device->name, filename);
  20. // target_data will not be freed automatically if we dont add a device target with it as userdata, so free it here
  21. free(target_data);
  22. }
  23. }
  24. static void _impl_razer_add_device(light_device_enumerator_t *enumerator, char const *device_id)
  25. {
  26. // Create a new razer device
  27. light_device_t *new_device = light_create_device(enumerator, device_id, NULL);
  28. // Setup a target to backlight
  29. _impl_razer_add_target(new_device, "backlight", "matrix_brightness", 255);
  30. // Setup targets to different possible leds
  31. _impl_razer_add_target(new_device, "game_led", "game_led_state", 1);
  32. _impl_razer_add_target(new_device, "macro_led", "macro_led_state", 1);
  33. _impl_razer_add_target(new_device, "logo_led", "logo_led_state", 1);
  34. _impl_razer_add_target(new_device, "profile_led_r", "profile_led_red", 1);
  35. _impl_razer_add_target(new_device, "profile_led_g", "profile_led_green", 1);
  36. _impl_razer_add_target(new_device, "profile_led_b", "profile_led_blue", 1);
  37. }
  38. bool impl_razer_init(light_device_enumerator_t *enumerator)
  39. {
  40. // Iterate through the led controllers and create a device_target for each controller
  41. DIR *razer_dir;
  42. struct dirent *curr_entry;
  43. if((razer_dir = opendir("/sys/bus/hid/drivers/razerkbd/")) == NULL)
  44. {
  45. // Razer driver isnt properly installed, so we cant add devices in this enumerator
  46. return true;
  47. }
  48. while((curr_entry = readdir(razer_dir)) != NULL)
  49. {
  50. // Skip dot entries
  51. if(curr_entry->d_name[0] == '.')
  52. {
  53. continue;
  54. }
  55. _impl_razer_add_device(enumerator, curr_entry->d_name);
  56. }
  57. closedir(razer_dir);
  58. return true;
  59. }
  60. bool impl_razer_free(light_device_enumerator_t *enumerator)
  61. {
  62. return true;
  63. }
  64. bool impl_razer_set(light_device_target_t *target, uint64_t in_value)
  65. {
  66. impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data;
  67. if(!light_file_write_uint64(data->brightness, in_value))
  68. {
  69. LIGHT_ERR("failed to write to razer device");
  70. return false;
  71. }
  72. return true;
  73. }
  74. bool impl_razer_get(light_device_target_t *target, uint64_t *out_value)
  75. {
  76. impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data;
  77. if(!light_file_read_uint64(data->brightness, out_value))
  78. {
  79. LIGHT_ERR("failed to read from razer device");
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool impl_razer_getmax(light_device_target_t *target, uint64_t *out_value)
  85. {
  86. impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data;
  87. *out_value = data->max_brightness;
  88. return true;
  89. }
  90. bool impl_razer_command(light_device_target_t *target, char const *command_string)
  91. {
  92. // No current need for custom commands in sysfs enumerator
  93. // To implement support, simply parse the command string to your liking, and return false on invalid input or results!
  94. return true;
  95. }