cuesdk_stub.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // A stand-in for Corsair's CUESDK.x64_2017.dll (iCUE SDK 3.x C API) that logs what Wallpaper Engine's LED plugin
  2. // (plugins/led/ledextensions64.dll) does, so real WE can be observed under Wine with no hardware.
  3. //
  4. // Devices (CUE_STUB_DIR picks where the logs go, default Z:\tmp\lwe-cuestub):
  5. // 0: a "probe" keyboard with 80 x 10 LEDs of 1 x 1 mm on a grid. The plugin samples its 80x10 frame at every LED's
  6. // centre, so every flush gives that frame pixel for pixel (written as probe_<n>.ppm)
  7. // 1: a K70 like keyboard, 22 x 6 keys of 18 mm on a 19 mm pitch
  8. // 2: a mouse mat, 15 LEDs in a row
  9. // Every call is written to calls.log with a time stamp.
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <windows.h>
  14. #define EXPORT __declspec (dllexport)
  15. typedef struct {
  16. const char* sdkVersion;
  17. const char* serverVersion;
  18. int sdkProtocolVersion;
  19. int serverProtocolVersion;
  20. unsigned char breakingChanges;
  21. } ProtocolDetails;
  22. typedef struct {
  23. int type;
  24. const char* model;
  25. int physicalLayout;
  26. int logicalLayout;
  27. int capsMask;
  28. int ledsCount;
  29. struct {
  30. int channelsCount;
  31. void* channels;
  32. } channels;
  33. char deviceId[128];
  34. } DeviceInfo;
  35. typedef struct {
  36. int ledId;
  37. double top;
  38. double left;
  39. double height;
  40. double width;
  41. } LedPosition;
  42. typedef struct {
  43. int numberOfLed;
  44. LedPosition* pLedPosition;
  45. } LedPositions;
  46. typedef struct {
  47. int ledId;
  48. int r;
  49. int g;
  50. int b;
  51. } LedColor;
  52. #define DEVICES 3
  53. static DeviceInfo g_info[DEVICES];
  54. static LedPositions g_positions[DEVICES];
  55. static LedColor* g_buffer[DEVICES];
  56. static int g_bufferCount[DEVICES];
  57. static char g_dir[MAX_PATH];
  58. static FILE* g_log;
  59. static int g_frame;
  60. static int g_flushes;
  61. static ULONGLONG g_start;
  62. static int g_ready;
  63. static void logf_ (const char* format, ...) {
  64. if (!g_log) {
  65. return;
  66. }
  67. va_list args;
  68. va_start (args, format);
  69. fprintf (g_log, "%8llu ", GetTickCount64 () - g_start);
  70. vfprintf (g_log, format, args);
  71. fputc ('\n', g_log);
  72. fflush (g_log);
  73. va_end (args);
  74. }
  75. static void setupDevice (int index, int type, const char* model, int cols, int rows, double pitch, double size) {
  76. const int count = cols * rows;
  77. LedPosition* positions = calloc (count, sizeof (LedPosition));
  78. for (int i = 0; i < count; i++) {
  79. positions[i].ledId = i + 1;
  80. positions[i].left = (i % cols) * pitch;
  81. positions[i].top = (i / cols) * pitch;
  82. positions[i].width = size;
  83. positions[i].height = size;
  84. }
  85. g_info[index].type = type;
  86. g_info[index].model = model;
  87. g_info[index].ledsCount = count;
  88. snprintf (g_info[index].deviceId, sizeof (g_info[index].deviceId), "stub-%d", index);
  89. g_positions[index].numberOfLed = count;
  90. g_positions[index].pLedPosition = positions;
  91. g_buffer[index] = calloc (count, sizeof (LedColor));
  92. }
  93. static void ensureReady (void) {
  94. if (g_ready) {
  95. return;
  96. }
  97. g_ready = 1;
  98. g_start = GetTickCount64 ();
  99. const char* dir = getenv ("CUE_STUB_DIR");
  100. snprintf (g_dir, sizeof (g_dir), "%s", dir && *dir ? dir : "Z:\\tmp\\lwe-cuestub");
  101. CreateDirectoryA (g_dir, NULL);
  102. char path[MAX_PATH + 32];
  103. snprintf (path, sizeof (path), "%s\\calls.log", g_dir);
  104. g_log = fopen (path, "a");
  105. setupDevice (0, 2, "Probe 80x10", 80, 10, 1.0, 1.0);
  106. setupDevice (1, 2, "K70 RGB", 22, 6, 19.0, 18.0);
  107. setupDevice (2, 4, "MM800", 15, 1, 20.0, 18.0);
  108. logf_ ("loaded");
  109. }
  110. static void writeProbe (void) {
  111. if (g_frame >= 400) {
  112. return;
  113. }
  114. char path[MAX_PATH + 40];
  115. snprintf (path, sizeof (path), "%s\\probe_%05d.ppm", g_dir, g_frame++);
  116. FILE* file = fopen (path, "wb");
  117. if (!file) {
  118. return;
  119. }
  120. fprintf (file, "P6\n80 10\n255\n");
  121. for (int i = 0; i < 800; i++) {
  122. const LedColor* color = &g_buffer[0][i];
  123. fputc (color->r, file);
  124. fputc (color->g, file);
  125. fputc (color->b, file);
  126. }
  127. fclose (file);
  128. }
  129. EXPORT ProtocolDetails CorsairPerformProtocolHandshake (void) {
  130. ensureReady ();
  131. ProtocolDetails details = { "3.0.0", "4.0.97", 7, 7, 0 };
  132. logf_ ("CorsairPerformProtocolHandshake");
  133. return details;
  134. }
  135. EXPORT unsigned char CorsairSubscribeForEvents (void* handler, void* context) {
  136. ensureReady ();
  137. logf_ ("CorsairSubscribeForEvents");
  138. return 1;
  139. }
  140. EXPORT unsigned char CorsairUnsubscribeFromEvents (void) {
  141. logf_ ("CorsairUnsubscribeFromEvents");
  142. return 1;
  143. }
  144. EXPORT unsigned char CorsairRequestControl (int mode) {
  145. ensureReady ();
  146. logf_ ("CorsairRequestControl %d", mode);
  147. return 1;
  148. }
  149. EXPORT unsigned char CorsairReleaseControl (int mode) {
  150. logf_ ("CorsairReleaseControl %d", mode);
  151. return 1;
  152. }
  153. EXPORT int CorsairGetDeviceCount (void) {
  154. ensureReady ();
  155. logf_ ("CorsairGetDeviceCount -> %d", DEVICES);
  156. return DEVICES;
  157. }
  158. EXPORT DeviceInfo* CorsairGetDeviceInfo (int index) {
  159. ensureReady ();
  160. logf_ ("CorsairGetDeviceInfo %d", index);
  161. return index >= 0 && index < DEVICES ? &g_info[index] : NULL;
  162. }
  163. EXPORT LedPositions* CorsairGetLedPositionsByDeviceIndex (int index) {
  164. ensureReady ();
  165. logf_ ("CorsairGetLedPositionsByDeviceIndex %d", index);
  166. return index >= 0 && index < DEVICES ? &g_positions[index] : NULL;
  167. }
  168. EXPORT unsigned char CorsairSetLedsColorsBufferByDeviceIndex (int index, int size, LedColor* colors) {
  169. if (index < 0 || index >= DEVICES) {
  170. return 0;
  171. }
  172. if (size > g_info[index].ledsCount) {
  173. size = g_info[index].ledsCount;
  174. }
  175. memcpy (g_buffer[index], colors, size * sizeof (LedColor));
  176. g_bufferCount[index] = size;
  177. return 1;
  178. }
  179. EXPORT unsigned char CorsairSetLedsColorsFlushBuffer (void) {
  180. g_flushes++;
  181. if (g_bufferCount[0] == 800) {
  182. writeProbe ();
  183. }
  184. if (g_flushes <= 5 || g_flushes % 100 == 0) {
  185. logf_ ("CorsairSetLedsColorsFlushBuffer #%d (devices %d/%d/%d LEDs set)", g_flushes, g_bufferCount[0], g_bufferCount[1], g_bufferCount[2]);
  186. }
  187. for (int i = 0; i < DEVICES; i++) {
  188. if (g_bufferCount[i] > 0 && (g_flushes <= 3 || g_flushes % 200 == 0)) {
  189. char line[2048];
  190. int length = 0;
  191. for (int led = 0; led < g_bufferCount[i] && led < 40; led++) {
  192. length += snprintf (line + length, sizeof (line) - length, " %d:%d,%d,%d", g_buffer[i][led].ledId,
  193. g_buffer[i][led].r, g_buffer[i][led].g, g_buffer[i][led].b);
  194. }
  195. logf_ (" device %d (%s):%s", i, g_info[i].model, line);
  196. }
  197. }
  198. return 1;
  199. }
  200. EXPORT unsigned char CorsairSetLedsColorsAsync (int size, LedColor* colors, void* callback, void* context) {
  201. logf_ ("CorsairSetLedsColorsAsync %d", size);
  202. return 1;
  203. }