buttonmapping.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Physical->logical button mapping
  3. *
  4. * Copyright (C) 2010-2011 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "buttonmapping.h"
  17. #include <string.h>
  18. #include <errno.h>
  19. int razer_create_buttonmap(void *buffer, size_t bufsize,
  20. struct razer_buttonmapping *mappings, size_t nr_mappings,
  21. unsigned int struct_spacing)
  22. {
  23. uint8_t *buf = buffer;
  24. struct razer_buttonmapping *mapping;
  25. size_t i, bufptr = 0;
  26. memset(buffer, 0, bufsize);
  27. for (i = 0; i < nr_mappings; i++) {
  28. mapping = &mappings[i];
  29. if (bufptr + 2 >= bufsize) {
  30. razer_error("razer_create_buttonmap: Buffer overflow\n");
  31. return -ENOSPC;
  32. }
  33. buf[bufptr + 0] = mapping->physical;
  34. buf[bufptr + 1] = mapping->logical;
  35. bufptr += 2;
  36. bufptr += struct_spacing;
  37. }
  38. return 0;
  39. }
  40. int razer_parse_buttonmap(void *rawdata, size_t rawsize,
  41. struct razer_buttonmapping *mappings, size_t nr_mappings,
  42. unsigned int struct_spacing)
  43. {
  44. uint8_t *raw = rawdata;
  45. size_t rawptr = 0, count;
  46. struct razer_buttonmapping mapping, *target;
  47. memset(mappings, 0, nr_mappings * sizeof(*mappings));
  48. target = mappings;
  49. for (count = 0; count < nr_mappings; count++) {
  50. if (rawptr + 2 >= rawsize) {
  51. razer_error("razer_parse_buttonmap: Raw data does not "
  52. "contain all mappings\n");
  53. return -EINVAL;
  54. }
  55. mapping.physical = raw[rawptr + 0];
  56. mapping.logical = raw[rawptr + 1];
  57. /*FIXME
  58. if (mapping.physical == 0) {
  59. razer_error("razer_parse_buttonmap: Physical mapping for %u "
  60. "is invalid\n", (unsigned int)count);
  61. return -EINVAL;
  62. }
  63. */
  64. /*FIXME triggers for lachesis5k6
  65. if (mapping.logical == 0) {
  66. razer_error("razer_parse_buttonmap: Logical mapping for 0x%02X "
  67. "is invalid\n", mapping.physical);
  68. return -EINVAL;
  69. }
  70. */
  71. target->physical = mapping.physical;
  72. target->logical = mapping.logical;
  73. rawptr += 2;
  74. if (!razer_buffer_is_all_zero(&raw[rawptr],
  75. min(struct_spacing, rawsize - rawptr))) {
  76. razer_debug("razer_parse_buttonmap: Buttonmap spacing contains "
  77. "nonzero data\n");
  78. }
  79. rawptr += struct_spacing;
  80. target++;
  81. }
  82. return 0;
  83. }
  84. struct razer_button_function * razer_get_buttonfunction_by_id(
  85. struct razer_button_function *functions, size_t nr_functions,
  86. uint8_t logical_id)
  87. {
  88. struct razer_button_function *func = NULL;
  89. size_t i;
  90. for (i = 0; i < nr_functions; i++) {
  91. if (functions[i].id == logical_id) {
  92. func = &functions[i];
  93. break;
  94. }
  95. }
  96. return func;
  97. }
  98. struct razer_button_function * razer_get_buttonfunction_by_button(
  99. struct razer_buttonmapping *mappings, size_t nr_mappings,
  100. struct razer_button_function *functions, size_t nr_functions,
  101. const struct razer_button *button)
  102. {
  103. struct razer_buttonmapping *mapping;
  104. mapping = razer_get_buttonmapping_by_physid(mappings, nr_mappings,
  105. button->id);
  106. if (!mapping)
  107. return NULL;
  108. return razer_get_buttonfunction_by_id(functions, nr_functions,
  109. mapping->logical);
  110. }
  111. struct razer_buttonmapping * razer_get_buttonmapping_by_physid(
  112. struct razer_buttonmapping *mappings, size_t nr_mappings,
  113. uint8_t physical_id)
  114. {
  115. struct razer_buttonmapping *mapping = NULL;
  116. size_t i;
  117. for (i = 0; i < nr_mappings; i++) {
  118. if (mappings[i].physical == physical_id) {
  119. mapping = &mappings[i];
  120. break;
  121. }
  122. }
  123. return mapping;
  124. }