razerd.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. /*
  2. * Razer daemon
  3. * Daemon to keep track of Razer device state.
  4. *
  5. * Copyright (C) 2008-2016 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include "librazer.h"
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <sys/stat.h>
  27. #include <sys/time.h>
  28. #include <sys/socket.h>
  29. #include <sys/un.h>
  30. #include <getopt.h>
  31. #include <syslog.h>
  32. #include <stdarg.h>
  33. #include <stdbool.h>
  34. #ifdef __DragonFly__
  35. #include <sys/endian.h>
  36. #else
  37. #include <byteswap.h>
  38. #endif
  39. #undef min
  40. #undef max
  41. #undef offsetof
  42. #define offsetof(type, member) ((size_t)&((type *)0)->member)
  43. #define min(x, y) ({ __typeof__(x) __x = (x); \
  44. __typeof__(y) __y = (y); \
  45. __x < __y ? __x : __y; })
  46. #define max(x, y) ({ __typeof__(x) __x = (x); \
  47. __typeof__(y) __y = (y); \
  48. __x > __y ? __x : __y; })
  49. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  50. #define _packed __attribute__((__packed__))
  51. enum {
  52. LOGLEVEL_ERROR = 0,
  53. LOGLEVEL_WARNING,
  54. LOGLEVEL_INFO,
  55. LOGLEVEL_DEBUG,
  56. };
  57. struct commandline_args {
  58. bool background;
  59. const char *configfile;
  60. const char *pidfile;
  61. int loglevel;
  62. bool force;
  63. bool no_profile_emu;
  64. } cmdargs = {
  65. #ifdef DEBUG
  66. .loglevel = LOGLEVEL_DEBUG,
  67. #else
  68. .loglevel = LOGLEVEL_INFO,
  69. #endif
  70. };
  71. #define VAR_RUN "/var/run"
  72. #define VAR_RUN_RAZERD VAR_RUN "/razerd"
  73. #define SOCKPATH VAR_RUN_RAZERD "/socket"
  74. #define PRIV_SOCKPATH VAR_RUN_RAZERD "/socket.privileged"
  75. #define INTERFACE_REVISION 6
  76. #define COMMAND_MAX_SIZE 512
  77. #define COMMAND_HDR_SIZE sizeof(struct command_hdr)
  78. #define BULK_CHUNK_SIZE 128
  79. #define MAX_FIRMWARE_SIZE 0x400000
  80. enum {
  81. COMMAND_ID_GETREV = 0, /* Get the revision number of the socket interface. */
  82. COMMAND_ID_RESCANMICE, /* Rescan mice. */
  83. COMMAND_ID_GETMICE, /* Get a list of detected mice. */
  84. COMMAND_ID_GETFWVER, /* Get the firmware rev of a mouse. */
  85. COMMAND_ID_SUPPFREQS, /* Get a list of supported frequencies. */
  86. COMMAND_ID_SUPPRESOL, /* Get a list of supported resolutions. */
  87. COMMAND_ID_SUPPDPIMAPPINGS, /* Get a list of supported DPI mappings. */
  88. COMMAND_ID_CHANGEDPIMAPPING, /* Modify a DPI mapping. */
  89. COMMAND_ID_GETDPIMAPPING, /* Get the active DPI mapping for a profile. */
  90. COMMAND_ID_SETDPIMAPPING, /* Set the active DPI mapping for a profile. */
  91. COMMAND_ID_GETLEDS, /* Get a list of LEDs on the device. */
  92. COMMAND_ID_SETLED, /* Set the state of a LED. */
  93. COMMAND_ID_GETFREQ, /* Get the current frequency. */
  94. COMMAND_ID_SETFREQ, /* Set the frequency. */
  95. COMMAND_ID_GETPROFILES, /* Get a list of supported profiles. */
  96. COMMAND_ID_GETACTIVEPROF, /* Get the active profile. */
  97. COMMAND_ID_SETACTIVEPROF, /* Set the active profile. */
  98. COMMAND_ID_SUPPBUTTONS, /* Get a list of physical buttons. */
  99. COMMAND_ID_SUPPBUTFUNCS, /* Get a list of supported button functions. */
  100. COMMAND_ID_GETBUTFUNC, /* Get the current function of a button. */
  101. COMMAND_ID_SETBUTFUNC, /* Set the current function of a button. */
  102. COMMAND_ID_SUPPAXES, /* Get a list of supported axes. */
  103. COMMAND_ID_RECONFIGMICE, /* Reconfigure all mice. */
  104. COMMAND_ID_GETMOUSEINFO, /* Get detailed information about a mouse */
  105. COMMAND_ID_GETPROFNAME, /* Get a profile name. */
  106. COMMAND_ID_SETPROFNAME, /* Set a profile name. */
  107. /* Privileged commands */
  108. COMMAND_PRIV_FLASHFW = 128, /* Upload and flash a firmware image */
  109. COMMAND_PRIV_CLAIM, /* Claim the device. */
  110. COMMAND_PRIV_RELEASE, /* Release the device. */
  111. };
  112. enum {
  113. ERR_NONE = 0, /* No error */
  114. ERR_CMDSIZE,
  115. ERR_NOMEM,
  116. ERR_NOMOUSE,
  117. ERR_NOLED,
  118. ERR_CLAIM,
  119. ERR_FAIL,
  120. ERR_PAYLOAD,
  121. ERR_NOTSUPP,
  122. };
  123. enum mouseinfo_flags {
  124. MOUSEINFOFLG_RESULTOK = (1 << 0), /* Other flags are ok, if this is set. */
  125. MOUSEINFOFLG_GLOBAL_LEDS = (1 << 1), /* The device has global LEDs. */
  126. MOUSEINFOFLG_PROFILE_LEDS = (1 << 2), /* The device has per-profile LEDs. */
  127. MOUSEINFOFLG_GLOBAL_FREQ = (1 << 3), /* The device has global frequency settings. */
  128. MOUSEINFOFLG_PROFILE_FREQ = (1 << 4), /* The device has per-profile frequency settings. */
  129. MOUSEINFOFLG_PROFNAMEMUTABLE = (1 << 5), /* Profile names can be changed. */
  130. MOUSEINFOFLG_SUGGESTFWUP = (1 << 6), /* A firmware update is suggested. */
  131. };
  132. enum led_flags {
  133. LED_FLAG_HAVECOLOR = (1 << 0),
  134. LED_FLAG_CHANGECOLOR = (1 << 1),
  135. };
  136. enum profile_special_values {
  137. PROFILE_INVALID = 0xFFFFFFFF,
  138. };
  139. struct command_hdr {
  140. uint8_t id;
  141. } _packed;
  142. struct command {
  143. struct command_hdr hdr;
  144. char idstr[RAZER_IDSTR_MAX_SIZE];
  145. union {
  146. struct {
  147. } _packed getfwver;
  148. struct {
  149. } _packed suppaxes;
  150. struct {
  151. } _packed suppfreqs;
  152. struct {
  153. } _packed suppresol;
  154. struct {
  155. } _packed suppdpimappings;
  156. struct {
  157. uint32_t id;
  158. uint32_t dimension;
  159. uint32_t new_resolution;
  160. } _packed changedpimapping;
  161. struct {
  162. uint32_t profile_id;
  163. uint32_t axis_id;
  164. } _packed getdpimapping;
  165. struct {
  166. uint32_t profile_id;
  167. uint32_t axis_id;
  168. uint32_t mapping_id;
  169. } _packed setdpimapping;
  170. struct {
  171. uint32_t profile_id;
  172. } _packed getleds;
  173. struct {
  174. uint32_t profile_id;
  175. char led_name[RAZER_LEDNAME_MAX_SIZE];
  176. uint8_t new_state;
  177. uint8_t new_mode;
  178. uint32_t color;
  179. } _packed setled;
  180. struct {
  181. uint32_t profile_id;
  182. uint32_t new_frequency;
  183. } _packed setfreq;
  184. struct {
  185. } _packed getprofiles;
  186. struct {
  187. } _packed getactiveprof;
  188. struct {
  189. uint32_t id;
  190. } _packed setactiveprof;
  191. struct {
  192. uint32_t profile_id;
  193. } _packed getfreq;
  194. struct {
  195. } _packed suppbuttons;
  196. struct {
  197. } _packed suppbutfuncs;
  198. struct {
  199. uint32_t profile_id;
  200. uint32_t button_id;
  201. } _packed getbutfunc;
  202. struct {
  203. uint32_t profile_id;
  204. uint32_t button_id;
  205. uint32_t function_id;
  206. } _packed setbutfunc;
  207. struct {
  208. } _packed getmouseinfo;
  209. struct {
  210. uint32_t profile_id;
  211. } _packed getprofname;
  212. struct {
  213. uint32_t profile_id;
  214. uint8_t utf16be_name[64 * 2];
  215. } _packed setprofname;
  216. struct {
  217. uint32_t imagesize;
  218. } _packed flashfw;
  219. struct {
  220. } _packed claim;
  221. struct {
  222. } _packed release;
  223. } _packed;
  224. } _packed;
  225. #define CMD_SIZE(name) (offsetof(struct command, name) + \
  226. sizeof(((struct command *)0)->name))
  227. enum {
  228. REPLY_ID_U32 = 0, /* An unsigned 32bit integer. */
  229. REPLY_ID_STR, /* A string */
  230. /* Asynchonous notifications. */
  231. NOTIFY_ID_NEWMOUSE = 128, /* New mouse was connected. */
  232. NOTIFY_ID_DELMOUSE, /* A mouse was removed. */
  233. };
  234. enum string_encoding {
  235. STRING_ENC_ASCII,
  236. STRING_ENC_UTF8,
  237. STRING_ENC_UTF16BE,
  238. };
  239. struct reply_hdr {
  240. uint8_t id;
  241. } _packed;
  242. struct reply {
  243. struct reply_hdr hdr;
  244. union {
  245. struct {
  246. uint32_t val;
  247. } _packed u32;
  248. struct {
  249. uint8_t encoding;
  250. uint16_t len; /* in characters */
  251. uint8_t str[0]; /* Payload buffer */
  252. } _packed string;
  253. struct {
  254. } _packed notify_newmouse;
  255. struct {
  256. } _packed notify_delmouse;
  257. } _packed;
  258. } _packed;
  259. #define REPLY_SIZE(name) (offsetof(struct reply, name) + \
  260. sizeof(((struct reply *)0)->name))
  261. struct client {
  262. struct client *next;
  263. struct sockaddr_un sockaddr;
  264. socklen_t socklen;
  265. int fd;
  266. };
  267. /* Control socket FDs. */
  268. static int ctlsock = -1;
  269. static int privsock = -1;
  270. /* Linked list of connected clients. */
  271. static struct client *clients;
  272. static struct client *privileged_clients;
  273. /* Linked list of detected mice. */
  274. static struct razer_mouse *mice;
  275. static inline uint32_t cpu_to_be32(uint32_t v)
  276. {
  277. #ifdef BIG_ENDIAN_HOST
  278. return v;
  279. #else
  280. return bswap_32(v);
  281. #endif
  282. }
  283. static inline uint16_t cpu_to_be16(uint16_t v)
  284. {
  285. #ifdef BIG_ENDIAN_HOST
  286. return v;
  287. #else
  288. return bswap_16(v);
  289. #endif
  290. }
  291. static inline uint32_t be32_to_cpu(uint32_t v)
  292. {
  293. return cpu_to_be32(v);
  294. }
  295. static inline uint16_t be16_to_cpu(uint16_t v)
  296. {
  297. return cpu_to_be16(v);
  298. }
  299. static void loginfo(const char *fmt, ...)
  300. {
  301. va_list args;
  302. if (cmdargs.loglevel < LOGLEVEL_INFO)
  303. return;
  304. va_start(args, fmt);
  305. if (cmdargs.background)
  306. vsyslog(LOG_MAKEPRI(LOG_DAEMON, LOG_INFO), fmt, args);
  307. else
  308. vfprintf(stdout, fmt, args);
  309. va_end(args);
  310. }
  311. static void logerr(const char *fmt, ...)
  312. {
  313. va_list args;
  314. if (cmdargs.loglevel < LOGLEVEL_ERROR)
  315. return;
  316. va_start(args, fmt);
  317. if (cmdargs.background)
  318. vsyslog(LOG_MAKEPRI(LOG_DAEMON, LOG_ERR), fmt, args);
  319. else
  320. vfprintf(stderr, fmt, args);
  321. va_end(args);
  322. }
  323. static void logdebug(const char *fmt, ...)
  324. {
  325. va_list args;
  326. if (cmdargs.loglevel < LOGLEVEL_DEBUG)
  327. return;
  328. va_start(args, fmt);
  329. if (cmdargs.background) {
  330. vsyslog(LOG_MAKEPRI(LOG_DAEMON, LOG_DEBUG), fmt, args);
  331. } else {
  332. fprintf(stdout, "[razerd debug]: ");
  333. vfprintf(stdout, fmt, args);
  334. }
  335. va_end(args);
  336. }
  337. static void remove_pidfile(void)
  338. {
  339. if (!cmdargs.pidfile)
  340. return;
  341. if (unlink(cmdargs.pidfile)) {
  342. logerr("Failed to remove PID-file %s: %s\n",
  343. cmdargs.pidfile, strerror(errno));
  344. }
  345. }
  346. static int create_pidfile(void)
  347. {
  348. char buf[32] = { 0, };
  349. pid_t pid = getpid();
  350. int fd;
  351. ssize_t res;
  352. if (!cmdargs.pidfile)
  353. return 0;
  354. if (cmdargs.force)
  355. unlink(cmdargs.pidfile);
  356. fd = open(cmdargs.pidfile, O_RDWR | O_CREAT | O_TRUNC, 0444);
  357. if (fd < 0) {
  358. logerr("Failed to create PID-file %s: %s\n",
  359. cmdargs.pidfile, strerror(errno));
  360. return -1;
  361. }
  362. snprintf(buf, sizeof(buf), "%lu",
  363. (unsigned long)pid);
  364. res = write(fd, buf, strlen(buf));
  365. close(fd);
  366. if (res < 0 || (size_t)res != strlen(buf)) {
  367. logerr("Failed to write PID-file %s: %s\n",
  368. cmdargs.pidfile, strerror(errno));
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. static void cleanup_var_run(void)
  374. {
  375. unlink(SOCKPATH);
  376. close(ctlsock);
  377. ctlsock = -1;
  378. unlink(PRIV_SOCKPATH);
  379. close(privsock);
  380. privsock = -1;
  381. remove_pidfile();
  382. rmdir(VAR_RUN_RAZERD);
  383. }
  384. static int create_socket(const char *path, unsigned int perm,
  385. unsigned int nrlisten)
  386. {
  387. struct sockaddr_un sockaddr;
  388. int err, fd;
  389. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  390. if (fd == -1) {
  391. logerr("Failed to create socket %s: %s\n",
  392. path, strerror(errno));
  393. goto error;
  394. }
  395. err = fcntl(fd, F_SETFL, O_NONBLOCK);
  396. if (err) {
  397. logerr("Failed to set O_NONBLOCK on socket %s: %s\n",
  398. path, strerror(errno));
  399. goto error_close_sock;
  400. }
  401. sockaddr.sun_family = AF_UNIX;
  402. razer_strlcpy(sockaddr.sun_path, path, sizeof(sockaddr.sun_path));
  403. err = bind(fd, (struct sockaddr *)&sockaddr, SUN_LEN(&sockaddr));
  404. if (err) {
  405. logerr("Failed to bind socket to %s: %s\n",
  406. path, strerror(errno));
  407. goto error_close_sock;
  408. }
  409. err = chmod(path, perm);
  410. if (err) {
  411. logerr("Failed to set %s socket permissions: %s\n",
  412. path, strerror(errno));
  413. goto error_unlink_sock;
  414. }
  415. err = listen(fd, nrlisten);
  416. if (err) {
  417. logerr("Failed to listen on socket %s: %s\n",
  418. path, strerror(errno));
  419. goto error_unlink_sock;
  420. }
  421. return fd;
  422. error_unlink_sock:
  423. unlink(path);
  424. error_close_sock:
  425. close(fd);
  426. error:
  427. return -1;
  428. }
  429. static int setup_var_run(void)
  430. {
  431. int err;
  432. /* Create /var/run subdirectory. */
  433. err = mkdir(VAR_RUN_RAZERD, 0755);
  434. if (err && errno != EEXIST) {
  435. logerr("Failed to create directory %s: %s\n",
  436. VAR_RUN_RAZERD, strerror(errno));
  437. return err;
  438. }
  439. create_pidfile();
  440. /* Create the control socket. */
  441. if (cmdargs.force)
  442. unlink(SOCKPATH);
  443. ctlsock = create_socket(SOCKPATH, 0666, 25);
  444. if (ctlsock == -1)
  445. goto err_remove_pidfile;
  446. /* Create the socket for privileged operations. */
  447. if (cmdargs.force)
  448. unlink(PRIV_SOCKPATH);
  449. privsock = create_socket(PRIV_SOCKPATH, 0660, 15);
  450. if (privsock == -1)
  451. goto err_remove_ctlsock;
  452. return 0;
  453. err_remove_ctlsock:
  454. unlink(SOCKPATH);
  455. close(ctlsock);
  456. ctlsock = -1;
  457. err_remove_pidfile:
  458. remove_pidfile();
  459. rmdir(VAR_RUN_RAZERD);
  460. return -1;
  461. }
  462. static int setup_environment(void)
  463. {
  464. int err;
  465. err = razer_init(!cmdargs.no_profile_emu);
  466. if (err) {
  467. logerr("librazer initialization failed. (%d)\n", err);
  468. return err;
  469. }
  470. razer_set_logging(cmdargs.loglevel >= LOGLEVEL_INFO ? loginfo : NULL,
  471. cmdargs.loglevel >= LOGLEVEL_ERROR ? logerr : NULL,
  472. cmdargs.loglevel >= LOGLEVEL_DEBUG ? logdebug : NULL);
  473. err = razer_load_config(cmdargs.configfile);
  474. if (cmdargs.configfile && err) {
  475. logerr("Failed to load config file %s\n",
  476. cmdargs.configfile);
  477. goto err_exit;
  478. }
  479. err = setup_var_run();
  480. if (err)
  481. goto err_exit;
  482. return 0;
  483. err_exit:
  484. razer_exit();
  485. return err;
  486. }
  487. static void cleanup_environment(void)
  488. {
  489. cleanup_var_run();
  490. razer_exit();
  491. }
  492. static void signal_handler(int signum)
  493. {
  494. switch (signum) {
  495. case SIGINT:
  496. case SIGTERM:
  497. loginfo("Terminating razerd.\n");
  498. cleanup_environment();
  499. exit(0);
  500. break;
  501. case SIGPIPE:
  502. /* Ignore */
  503. break;
  504. default:
  505. logerr("Received unknown signal %d\n", signum);
  506. }
  507. }
  508. static void setup_sighandler(void)
  509. {
  510. struct sigaction act;
  511. memset(&act, 0, sizeof(act));
  512. sigemptyset(&act.sa_mask);
  513. act.sa_flags = 0;
  514. act.sa_handler = signal_handler;
  515. sigaction(SIGINT, &act, NULL);
  516. sigaction(SIGTERM, &act, NULL);
  517. sigaction(SIGPIPE, &act, NULL);
  518. }
  519. static void free_client(struct client *client)
  520. {
  521. free(client);
  522. }
  523. static struct client * new_client(const struct sockaddr_un *sockaddr,
  524. socklen_t socklen, int fd)
  525. {
  526. struct client *client;
  527. client = malloc(sizeof(*client));
  528. if (!client)
  529. return NULL;
  530. memset(client, 0, sizeof(*client));
  531. memcpy(&client->sockaddr, sockaddr, sizeof(client->sockaddr));
  532. client->socklen = socklen;
  533. client->fd = fd;
  534. return client;
  535. }
  536. static void client_list_add(struct client **base, struct client *new_entry)
  537. {
  538. struct client *i;
  539. new_entry->next = NULL;
  540. if (!(*base)) {
  541. *base = new_entry;
  542. return;
  543. }
  544. for (i = *base; i->next; i = i->next)
  545. ;
  546. i->next = new_entry;
  547. }
  548. static void client_list_del(struct client **base, struct client *del_entry)
  549. {
  550. struct client *i;
  551. if (del_entry == *base) {
  552. *base = (*base)->next;
  553. return;
  554. }
  555. for (i = *base; i && (i->next != del_entry); i = i->next)
  556. ;
  557. if (i)
  558. i->next = del_entry->next;
  559. }
  560. static void check_control_socket(int socket_fd, struct client **client_list)
  561. {
  562. socklen_t socklen;
  563. struct client *client;
  564. struct sockaddr_un remoteaddr;
  565. int err, fd;
  566. socklen = sizeof(remoteaddr);
  567. fd = accept(socket_fd, (struct sockaddr *)&remoteaddr, &socklen);
  568. if (fd == -1)
  569. return;
  570. /* Connected */
  571. err = fcntl(fd, F_SETFL, O_NONBLOCK);
  572. if (err) {
  573. logerr("Failed to set O_NONBLOCK on client: %s\n",
  574. strerror(errno));
  575. close(fd);
  576. return;
  577. }
  578. client = new_client(&remoteaddr, socklen, fd);
  579. if (!client) {
  580. close(fd);
  581. return;
  582. }
  583. client_list_add(client_list, client);
  584. if (client_list == &privileged_clients)
  585. logdebug("Privileged client connected (fd=%d)\n", fd);
  586. else
  587. logdebug("Client connected (fd=%d)\n", fd);
  588. }
  589. static void disconnect_client(struct client **client_list, struct client *client)
  590. {
  591. client_list_del(client_list, client);
  592. if (client_list == &privileged_clients)
  593. logdebug("Privileged client disconnected (fd=%d)\n", client->fd);
  594. else
  595. logdebug("Client disconnected (fd=%d)\n", client->fd);
  596. free_client(client);
  597. }
  598. static int send_reply(struct client *client, struct reply *r, size_t len)
  599. {
  600. const uint8_t *buf = (const uint8_t *)r;
  601. int ret;
  602. while (len) {
  603. ret = send(client->fd, buf, len, 0);
  604. if (ret < 0) {
  605. if (errno == EAGAIN ||
  606. errno == EINTR) {
  607. razer_msleep(1);
  608. continue;
  609. }
  610. logerr("send() failed: %s", strerror(errno));
  611. return -errno;
  612. }
  613. len -= ret;
  614. buf += ret;
  615. }
  616. return 0;
  617. }
  618. static int send_u32(struct client *client, uint32_t v)
  619. {
  620. struct reply r;
  621. r.hdr.id = REPLY_ID_U32;
  622. r.u32.val = cpu_to_be32(v);
  623. return send_reply(client, &r, REPLY_SIZE(u32));
  624. }
  625. static int send_string(struct client *client, const char *str)
  626. {
  627. struct reply *r;
  628. size_t i, len = strlen(str);
  629. int err;
  630. r = malloc(len + REPLY_SIZE(string));
  631. if (!r) {
  632. logerr("Out of memory\n");
  633. return -ENOMEM;
  634. }
  635. r->hdr.id = REPLY_ID_STR;
  636. r->string.encoding = STRING_ENC_ASCII;
  637. r->string.len = cpu_to_be16(len);
  638. for (i = 0; i < len; i++) {
  639. if ((unsigned char)str[i] <= 0x7Fu)
  640. r->string.str[i] = str[i];
  641. else
  642. r->string.str[i] = '?'; /* Non-ASCII char. */
  643. }
  644. err = send_reply(client, r, REPLY_SIZE(string) + len);
  645. free(r);
  646. return err;
  647. }
  648. static int send_utf16_string(struct client *client, const razer_utf16_t *str)
  649. {
  650. struct reply *r;
  651. size_t i, len = razer_utf16_strlen(str);
  652. int err;
  653. r = malloc(len * 2 + REPLY_SIZE(string));
  654. if (!r) {
  655. logerr("Out of memory\n");
  656. return -ENOMEM;
  657. }
  658. r->hdr.id = REPLY_ID_STR;
  659. r->string.encoding = STRING_ENC_UTF16BE;
  660. r->string.len = cpu_to_be16(len);
  661. for (i = 0; i < len; i++)
  662. ((uint16_t *)r->string.str)[i] = cpu_to_be16(str[i]);
  663. err = send_reply(client, r, REPLY_SIZE(string) + len * 2);
  664. free(r);
  665. return err;
  666. }
  667. static int recv_bulk(struct client *client, char *buf, unsigned int len)
  668. {
  669. unsigned int next_len, i;
  670. int nr;
  671. //FIXME timeout
  672. for (i = 0; i < len; i += BULK_CHUNK_SIZE) {
  673. next_len = BULK_CHUNK_SIZE;
  674. if (i + next_len > len)
  675. next_len = len - i;
  676. while (1) {
  677. nr = recv(client->fd, buf + i, next_len, 0);
  678. if (nr < 0) {
  679. if (errno == EAGAIN ||
  680. errno == EINTR) {
  681. razer_msleep(1);
  682. continue;
  683. }
  684. }
  685. if (nr > 0)
  686. break;
  687. }
  688. if (nr < 0 || (unsigned int)nr != next_len) {
  689. send_u32(client, ERR_PAYLOAD);
  690. return -1;
  691. }
  692. send_u32(client, ERR_NONE);
  693. }
  694. return 0;
  695. }
  696. struct razer_mouse * find_mouse(const char *idstr)
  697. {
  698. struct razer_mouse *m, *next;
  699. razer_for_each_mouse(m, next, mice) {
  700. if (strncmp(m->idstr, idstr, RAZER_IDSTR_MAX_SIZE) == 0)
  701. return m;
  702. }
  703. return NULL;
  704. }
  705. static struct razer_mouse_profile * find_mouse_profile(struct razer_mouse *mouse,
  706. unsigned int profile_id)
  707. {
  708. struct razer_mouse_profile *list;
  709. unsigned int i;
  710. if (!mouse->get_profiles)
  711. return NULL;
  712. list = mouse->get_profiles(mouse);
  713. if (!list)
  714. return NULL;
  715. for (i = 0; i < mouse->nr_profiles; i++) {
  716. if (list[i].nr == profile_id)
  717. return &list[i];
  718. }
  719. return NULL;
  720. }
  721. static struct razer_axis * find_mouse_axis(struct razer_mouse *mouse,
  722. unsigned int axis_id)
  723. {
  724. struct razer_axis *list;
  725. int i, count;
  726. if (!mouse->supported_axes)
  727. return NULL;
  728. count = mouse->supported_axes(mouse, &list);
  729. if (count <= 0)
  730. return NULL;
  731. for (i = 0; i < count; i++) {
  732. if (list[i].id == axis_id)
  733. return &list[i];
  734. }
  735. return NULL;
  736. }
  737. static struct razer_mouse_dpimapping * find_mouse_dpimapping(struct razer_mouse *mouse,
  738. unsigned int mapping_id)
  739. {
  740. struct razer_mouse_dpimapping *list;
  741. int i, count;
  742. if (!mouse->supported_dpimappings)
  743. return NULL;
  744. count = mouse->supported_dpimappings(mouse, &list);
  745. if (count <= 0)
  746. return NULL;
  747. for (i = 0; i < count; i++) {
  748. if (list[i].nr == mapping_id)
  749. return &list[i];
  750. }
  751. return NULL;
  752. }
  753. static struct razer_button * find_mouse_button(struct razer_mouse *mouse,
  754. unsigned int button_id)
  755. {
  756. struct razer_button *list;
  757. int i, count;
  758. if (!mouse->supported_buttons)
  759. return NULL;
  760. count = mouse->supported_buttons(mouse, &list);
  761. if (count <= 0)
  762. return NULL;
  763. for (i = 0; i < count; i++) {
  764. if (list[i].id == button_id)
  765. return &list[i];
  766. }
  767. return NULL;
  768. }
  769. static struct razer_button_function * find_mouse_button_function(struct razer_mouse *mouse,
  770. unsigned int button_id)
  771. {
  772. struct razer_button_function *list;
  773. int i, count;
  774. if (!mouse->supported_button_functions)
  775. return NULL;
  776. count = mouse->supported_button_functions(mouse, &list);
  777. if (count <= 0)
  778. return NULL;
  779. for (i = 0; i < count; i++) {
  780. if (list[i].id == button_id)
  781. return &list[i];
  782. }
  783. return NULL;
  784. }
  785. static void command_getmice(struct client *client, const struct command *cmd, unsigned int len)
  786. {
  787. unsigned int count;
  788. char str[RAZER_IDSTR_MAX_SIZE + 1];
  789. struct razer_mouse *mouse, *next;
  790. count = 0;
  791. razer_for_each_mouse(mouse, next, mice)
  792. count++;
  793. send_u32(client, count);
  794. razer_for_each_mouse(mouse, next, mice) {
  795. snprintf(str, sizeof(str), "%s", mouse->idstr);
  796. send_string(client, str);
  797. }
  798. }
  799. static void command_getfwver(struct client *client, const struct command *cmd, unsigned int len)
  800. {
  801. struct razer_mouse *mouse;
  802. uint32_t fwver = 0xFFFFFFFF;
  803. int err;
  804. if (len < CMD_SIZE(getfwver))
  805. goto out;
  806. mouse = find_mouse(cmd->idstr);
  807. if (!mouse || !mouse->get_fw_version)
  808. goto out;
  809. err = mouse->claim(mouse);
  810. if (err)
  811. goto out;
  812. fwver = mouse->get_fw_version(mouse);
  813. mouse->release(mouse);
  814. out:
  815. send_u32(client, fwver);
  816. }
  817. static void command_getfreq(struct client *client, const struct command *cmd, unsigned int len)
  818. {
  819. struct razer_mouse *mouse;
  820. struct razer_mouse_profile *profile;
  821. enum razer_mouse_freq freq;
  822. unsigned int profile_id;
  823. if (len < CMD_SIZE(getfreq))
  824. goto error;
  825. mouse = find_mouse(cmd->idstr);
  826. if (!mouse)
  827. goto error;
  828. profile_id = be32_to_cpu(cmd->getfreq.profile_id);
  829. if (profile_id == PROFILE_INVALID) {
  830. if (!mouse->global_get_freq)
  831. goto error;
  832. freq = mouse->global_get_freq(mouse);
  833. } else {
  834. profile = find_mouse_profile(mouse, profile_id);
  835. if (!profile || !profile->get_freq)
  836. goto error;
  837. freq = profile->get_freq(profile);
  838. }
  839. send_u32(client, freq);
  840. return;
  841. error:
  842. send_u32(client, RAZER_MOUSE_FREQ_UNKNOWN);
  843. }
  844. static void command_suppfreqs(struct client *client, const struct command *cmd, unsigned int len)
  845. {
  846. struct razer_mouse *mouse;
  847. enum razer_mouse_freq *freq_list;
  848. int i, count;
  849. if (len < CMD_SIZE(suppfreqs))
  850. goto error;
  851. mouse = find_mouse(cmd->idstr);
  852. if (!mouse || !mouse->supported_freqs)
  853. goto error;
  854. count = mouse->supported_freqs(mouse, &freq_list);
  855. if (count <= 0)
  856. goto error;
  857. send_u32(client, count);
  858. for (i = 0; i < count; i++)
  859. send_u32(client, freq_list[i]);
  860. razer_free_freq_list(freq_list, count);
  861. return;
  862. error:
  863. count = 0;
  864. send_u32(client, count);
  865. }
  866. static void command_suppresol(struct client *client, const struct command *cmd, unsigned int len)
  867. {
  868. struct razer_mouse *mouse;
  869. enum razer_mouse_res *res_list;
  870. int i, count;
  871. if (len < CMD_SIZE(suppresol))
  872. goto error;
  873. mouse = find_mouse(cmd->idstr);
  874. if (!mouse || !mouse->supported_resolutions)
  875. goto error;
  876. count = mouse->supported_resolutions(mouse, &res_list);
  877. if (count <= 0)
  878. goto error;
  879. send_u32(client, count);
  880. for (i = 0; i < count; i++)
  881. send_u32(client, res_list[i]);
  882. razer_free_resolution_list(res_list, count);
  883. return;
  884. error:
  885. count = 0;
  886. send_u32(client, count);
  887. }
  888. static void command_suppdpimappings(struct client *client, const struct command *cmd, unsigned int len)
  889. {
  890. struct razer_mouse *mouse;
  891. struct razer_mouse_dpimapping *list;
  892. int i, j, count;
  893. if (len < CMD_SIZE(suppdpimappings))
  894. goto error;
  895. mouse = find_mouse(cmd->idstr);
  896. if (!mouse || !mouse->supported_dpimappings)
  897. goto error;
  898. count = mouse->supported_dpimappings(mouse, &list);
  899. if (count <= 0)
  900. goto error;
  901. send_u32(client, count);
  902. for (i = 0; i < count; i++) {
  903. send_u32(client, list[i].nr);
  904. send_u32(client, list[i].dimension_mask);
  905. for (j = 0; j < RAZER_NR_DIMS; j++)
  906. send_u32(client, list[i].res[j]);
  907. send_u32(client, (list[i].profile_mask >> 32) & 0xFFFFFFFF);
  908. send_u32(client, (list[i].profile_mask >> 0) & 0xFFFFFFFF);
  909. send_u32(client, list[i].change ? 1 : 0);
  910. }
  911. /* No need to free list. It's statically allocated. */
  912. return;
  913. error:
  914. count = 0;
  915. send_u32(client, count);
  916. }
  917. static void command_changedpimapping(struct client *client, const struct command *cmd, unsigned int len)
  918. {
  919. struct razer_mouse *mouse;
  920. struct razer_mouse_dpimapping *mapping;
  921. int err;
  922. uint32_t errorcode = ERR_NONE;
  923. if (len < CMD_SIZE(changedpimapping)) {
  924. errorcode = ERR_CMDSIZE;
  925. goto error;
  926. }
  927. mouse = find_mouse(cmd->idstr);
  928. if (!mouse) {
  929. errorcode = ERR_NOMOUSE;
  930. goto error;
  931. }
  932. mapping = find_mouse_dpimapping(mouse, be32_to_cpu(cmd->changedpimapping.id));
  933. if (!mapping || !mapping->change) {
  934. errorcode = ERR_FAIL;
  935. goto error;
  936. }
  937. err = mouse->claim(mouse);
  938. if (err) {
  939. errorcode = ERR_CLAIM;
  940. goto error;
  941. }
  942. err = mapping->change(mapping,
  943. be32_to_cpu(cmd->changedpimapping.dimension),
  944. be32_to_cpu(cmd->changedpimapping.new_resolution));
  945. if (err)
  946. errorcode = ERR_FAIL;
  947. mouse->release(mouse);
  948. error:
  949. send_u32(client, errorcode);
  950. }
  951. static void command_getdpimapping(struct client *client, const struct command *cmd, unsigned int len)
  952. {
  953. struct razer_mouse *mouse;
  954. struct razer_mouse_profile *profile;
  955. struct razer_axis *axis;
  956. struct razer_mouse_dpimapping *mapping;
  957. if (len < CMD_SIZE(getdpimapping))
  958. goto error;
  959. mouse = find_mouse(cmd->idstr);
  960. if (!mouse)
  961. goto error;
  962. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getdpimapping.profile_id));
  963. if (!profile)
  964. goto error;
  965. axis = find_mouse_axis(mouse, be32_to_cpu(cmd->getdpimapping.axis_id));
  966. mapping = profile->get_dpimapping(profile, axis);
  967. if (!mapping)
  968. goto error;
  969. send_u32(client, mapping->nr);
  970. return;
  971. error:
  972. send_u32(client, 0xFFFFFFFF);
  973. }
  974. static void command_setdpimapping(struct client *client, const struct command *cmd, unsigned int len)
  975. {
  976. struct razer_mouse *mouse;
  977. struct razer_mouse_profile *profile;
  978. struct razer_axis *axis;
  979. struct razer_mouse_dpimapping *mapping;
  980. uint32_t errorcode = ERR_NONE;
  981. int err;
  982. if (len < CMD_SIZE(setdpimapping)) {
  983. errorcode = ERR_CMDSIZE;
  984. goto error;
  985. }
  986. mouse = find_mouse(cmd->idstr);
  987. if (!mouse) {
  988. errorcode = ERR_NOMOUSE;
  989. goto error;
  990. }
  991. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setdpimapping.profile_id));
  992. if (!profile || !profile->set_dpimapping) {
  993. errorcode = ERR_FAIL;
  994. goto error;
  995. }
  996. axis = find_mouse_axis(mouse, be32_to_cpu(cmd->setdpimapping.axis_id));
  997. mapping = find_mouse_dpimapping(mouse, be32_to_cpu(cmd->setdpimapping.mapping_id));
  998. if (!mapping) {
  999. errorcode = ERR_FAIL;
  1000. goto error;
  1001. }
  1002. err = mouse->claim(mouse);
  1003. if (err) {
  1004. errorcode = ERR_CLAIM;
  1005. goto error;
  1006. }
  1007. err = profile->set_dpimapping(profile, axis, mapping);
  1008. mouse->release(mouse);
  1009. if (err) {
  1010. errorcode = ERR_FAIL;
  1011. goto error;
  1012. }
  1013. error:
  1014. send_u32(client, errorcode);
  1015. }
  1016. static void command_rescanmice(struct client *client, const struct command *cmd, unsigned int len)
  1017. {
  1018. mice = razer_rescan_mice();
  1019. }
  1020. static void command_reconfigmice(struct client *client, const struct command *cmd, unsigned int len)
  1021. {
  1022. razer_reconfig_mice();
  1023. }
  1024. static void command_getmouseinfo(struct client *client, const struct command *cmd, unsigned int len)
  1025. {
  1026. struct razer_mouse *mouse;
  1027. struct razer_mouse_profile *profiles;
  1028. unsigned int flags;
  1029. if (len < CMD_SIZE(getmouseinfo))
  1030. goto error;
  1031. mouse = find_mouse(cmd->idstr);
  1032. if (!mouse)
  1033. goto error;
  1034. flags = MOUSEINFOFLG_RESULTOK;
  1035. if (mouse->global_get_leds)
  1036. flags |= MOUSEINFOFLG_GLOBAL_LEDS;
  1037. if (mouse->global_get_freq)
  1038. flags |= MOUSEINFOFLG_GLOBAL_FREQ;
  1039. if (mouse->nr_profiles && mouse->get_profiles) {
  1040. profiles = mouse->get_profiles(mouse);
  1041. if (profiles) {
  1042. if (profiles[0].get_leds)
  1043. flags |= MOUSEINFOFLG_PROFILE_LEDS;
  1044. if (profiles[0].get_freq)
  1045. flags |= MOUSEINFOFLG_PROFILE_FREQ;
  1046. if (profiles[0].set_name)
  1047. flags |= MOUSEINFOFLG_PROFNAMEMUTABLE;
  1048. }
  1049. }
  1050. if (mouse->flags & RAZER_MOUSEFLG_SUGGESTFWUP)
  1051. flags |= MOUSEINFOFLG_SUGGESTFWUP;
  1052. send_u32(client, flags);
  1053. return;
  1054. error:
  1055. send_u32(client, 0);
  1056. }
  1057. static void command_getleds(struct client *client, const struct command *cmd, unsigned int len)
  1058. {
  1059. struct razer_mouse *mouse;
  1060. struct razer_mouse_profile *profile;
  1061. struct razer_led *leds_list, *led;
  1062. int count;
  1063. unsigned int flags, profile_id;
  1064. if (len < CMD_SIZE(getleds))
  1065. goto error;
  1066. mouse = find_mouse(cmd->idstr);
  1067. if (!mouse)
  1068. goto error;
  1069. profile_id = be32_to_cpu(cmd->getleds.profile_id);
  1070. if (profile_id == PROFILE_INVALID) {
  1071. if (!mouse->global_get_leds)
  1072. goto error;
  1073. count = mouse->global_get_leds(mouse, &leds_list);
  1074. } else {
  1075. profile = find_mouse_profile(mouse, profile_id);
  1076. if (!profile)
  1077. goto error;
  1078. if (!profile->get_leds)
  1079. goto error;
  1080. count = profile->get_leds(profile, &leds_list);
  1081. }
  1082. if (count <= 0)
  1083. goto error;
  1084. send_u32(client, count);
  1085. for (led = leds_list; led; led = led->next) {
  1086. flags = 0;
  1087. if (led->color.valid)
  1088. flags |= LED_FLAG_HAVECOLOR;
  1089. if (led->change_color)
  1090. flags |= LED_FLAG_CHANGECOLOR;
  1091. send_u32(client, flags);
  1092. send_string(client, led->name);
  1093. send_u32(client, led->state);
  1094. send_u32(client, led->mode);
  1095. send_u32(client, led->supported_modes_mask);
  1096. send_u32(client, ((uint32_t)led->color.r << 16) |
  1097. ((uint32_t)led->color.g << 8) |
  1098. ((uint32_t)led->color.b << 0));
  1099. }
  1100. razer_free_leds(leds_list);
  1101. return;
  1102. error:
  1103. count = 0;
  1104. send_u32(client, count);
  1105. }
  1106. static struct razer_led * razer_mouse_find_led(struct razer_led *leds_list,
  1107. const char *led_name)
  1108. {
  1109. struct razer_led *led;
  1110. for (led = leds_list; led; led = led->next) {
  1111. if (strncasecmp(led->name, led_name, RAZER_LEDNAME_MAX_SIZE) == 0)
  1112. return led;
  1113. }
  1114. return NULL;
  1115. }
  1116. static void command_setled(struct client *client, const struct command *cmd, unsigned int len)
  1117. {
  1118. struct razer_mouse *mouse;
  1119. struct razer_mouse_profile *profile;
  1120. struct razer_led *leds_list = NULL, *led;
  1121. enum razer_led_state new_state;
  1122. enum razer_led_mode new_mode;
  1123. struct razer_rgb_color new_color;
  1124. int err, count;
  1125. uint32_t errorcode = ERR_NONE;
  1126. unsigned int profile_id, value;
  1127. if (len < CMD_SIZE(setled)) {
  1128. errorcode = ERR_CMDSIZE;
  1129. goto error;
  1130. }
  1131. mouse = find_mouse(cmd->idstr);
  1132. if (!mouse) {
  1133. errorcode = ERR_NOMOUSE;
  1134. goto error;
  1135. }
  1136. profile_id = be32_to_cpu(cmd->setled.profile_id);
  1137. if (profile_id == PROFILE_INVALID) {
  1138. if (!mouse->global_get_leds) {
  1139. errorcode = ERR_NOLED;
  1140. goto error;
  1141. }
  1142. count = mouse->global_get_leds(mouse, &leds_list);
  1143. } else {
  1144. profile = find_mouse_profile(mouse, profile_id);
  1145. if (!profile) {
  1146. errorcode = ERR_NOLED;
  1147. goto error;
  1148. }
  1149. if (!profile->get_leds) {
  1150. errorcode = ERR_NOLED;
  1151. goto error;
  1152. }
  1153. count = profile->get_leds(profile, &leds_list);
  1154. }
  1155. if (count <= 0) {
  1156. errorcode = ERR_NOMEM;
  1157. goto error;
  1158. }
  1159. led = razer_mouse_find_led(leds_list, cmd->setled.led_name);
  1160. if (!led) {
  1161. errorcode = ERR_NOLED;
  1162. goto error;
  1163. }
  1164. err = mouse->claim(mouse);
  1165. if (err) {
  1166. errorcode = ERR_CLAIM;
  1167. goto error;
  1168. }
  1169. new_state = cmd->setled.new_state ? RAZER_LED_ON : RAZER_LED_OFF;
  1170. if (new_state != led->state) {
  1171. err = led->toggle_state(led, new_state);
  1172. if (err) {
  1173. mouse->release(mouse);
  1174. errorcode = ERR_FAIL;
  1175. goto error;
  1176. }
  1177. }
  1178. new_mode = cmd->setled.new_mode;
  1179. if (new_mode != led->mode) {
  1180. if (led->set_mode) {
  1181. err = led->set_mode(led, new_mode);
  1182. if (err) {
  1183. mouse->release(mouse);
  1184. errorcode = ERR_FAIL;
  1185. goto error;
  1186. }
  1187. }
  1188. }
  1189. if (led->change_color) {
  1190. memset(&new_color, 0, sizeof(new_color));
  1191. value = be32_to_cpu(cmd->setled.color);
  1192. new_color.valid = 1;
  1193. new_color.r = (value >> 16) & 0xFF;
  1194. new_color.g = (value >> 8) & 0xFF;
  1195. new_color.b = (value >> 0) & 0xFF;
  1196. if (!!new_color.valid != !!led->color.valid ||
  1197. new_color.r != led->color.r ||
  1198. new_color.g != led->color.g ||
  1199. new_color.b != led->color.b) {
  1200. err = led->change_color(led, &new_color);
  1201. if (err) {
  1202. mouse->release(mouse);
  1203. errorcode = ERR_FAIL;
  1204. goto error;
  1205. }
  1206. }
  1207. }
  1208. mouse->release(mouse);
  1209. error:
  1210. razer_free_leds(leds_list);
  1211. send_u32(client, errorcode);
  1212. }
  1213. static void command_setfreq(struct client *client, const struct command *cmd, unsigned int len)
  1214. {
  1215. struct razer_mouse *mouse;
  1216. struct razer_mouse_profile *profile = NULL;
  1217. int err;
  1218. uint32_t errorcode = ERR_NONE;
  1219. unsigned int profile_id;
  1220. if (len < CMD_SIZE(setfreq)) {
  1221. errorcode = ERR_CMDSIZE;
  1222. goto error;
  1223. }
  1224. mouse = find_mouse(cmd->idstr);
  1225. if (!mouse) {
  1226. errorcode = ERR_NOMOUSE;
  1227. goto error;
  1228. }
  1229. profile_id = be32_to_cpu(cmd->setfreq.profile_id);
  1230. if (profile_id == PROFILE_INVALID) {
  1231. if (!mouse->global_set_freq) {
  1232. errorcode = ERR_NOTSUPP;
  1233. goto error;
  1234. }
  1235. } else {
  1236. profile = find_mouse_profile(mouse, profile_id);
  1237. if (!profile) {
  1238. errorcode = ERR_FAIL;
  1239. goto error;
  1240. }
  1241. if (!profile->set_freq) {
  1242. errorcode = ERR_NOTSUPP;
  1243. goto error;
  1244. }
  1245. }
  1246. err = mouse->claim(mouse);
  1247. if (err) {
  1248. errorcode = ERR_CLAIM;
  1249. goto error;
  1250. }
  1251. if (profile_id == PROFILE_INVALID) {
  1252. err = mouse->global_set_freq(mouse,
  1253. be32_to_cpu(cmd->setfreq.new_frequency));
  1254. } else {
  1255. err = profile->set_freq(profile,
  1256. be32_to_cpu(cmd->setfreq.new_frequency));
  1257. }
  1258. mouse->release(mouse);
  1259. if (err) {
  1260. errorcode = ERR_FAIL;
  1261. goto error;
  1262. }
  1263. error:
  1264. send_u32(client, errorcode);
  1265. }
  1266. static void command_getprofiles(struct client *client, const struct command *cmd, unsigned int len)
  1267. {
  1268. struct razer_mouse *mouse;
  1269. struct razer_mouse_profile *list;
  1270. unsigned int i;
  1271. if (len < CMD_SIZE(getprofiles))
  1272. goto error;
  1273. mouse = find_mouse(cmd->idstr);
  1274. if (!mouse)
  1275. goto error;
  1276. list = mouse->get_profiles(mouse);
  1277. if (!list)
  1278. goto error;
  1279. send_u32(client, mouse->nr_profiles);
  1280. for (i = 0; i < mouse->nr_profiles; i++)
  1281. send_u32(client, list[i].nr);
  1282. return;
  1283. error:
  1284. send_u32(client, 0);
  1285. }
  1286. static void command_getprofname(struct client *client, const struct command *cmd, unsigned int len)
  1287. {
  1288. struct razer_mouse *mouse;
  1289. struct razer_mouse_profile *profile;
  1290. const razer_utf16_t *name;
  1291. razer_utf16_t namebuf[64] = { };
  1292. char asciibuf[64] = { };
  1293. if (len < CMD_SIZE(getprofname))
  1294. goto error;
  1295. mouse = find_mouse(cmd->idstr);
  1296. if (!mouse)
  1297. goto error;
  1298. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getprofname.profile_id));
  1299. if (!profile)
  1300. goto error;
  1301. if (profile->get_name) {
  1302. name = profile->get_name(profile);
  1303. } else {
  1304. snprintf(asciibuf, sizeof(asciibuf),
  1305. "Profile %u", profile->nr + 1);
  1306. razer_ascii_to_utf16(namebuf, ARRAY_SIZE(namebuf), asciibuf);
  1307. name = namebuf;
  1308. }
  1309. if (!name)
  1310. goto error;
  1311. send_utf16_string(client, name);
  1312. return;
  1313. error:
  1314. send_string(client, "");
  1315. }
  1316. static void command_setprofname(struct client *client, const struct command *cmd, unsigned int len)
  1317. {
  1318. struct razer_mouse *mouse;
  1319. struct razer_mouse_profile *profile;
  1320. razer_utf16_t namebuf[sizeof(cmd->setprofname.utf16be_name) / 2 + 1] = { };
  1321. unsigned int i;
  1322. uint32_t errorcode = ERR_NONE;
  1323. if (len < CMD_SIZE(setprofname)) {
  1324. errorcode = ERR_CMDSIZE;
  1325. goto error;
  1326. }
  1327. mouse = find_mouse(cmd->idstr);
  1328. if (!mouse) {
  1329. errorcode = ERR_NOMOUSE;
  1330. goto error;
  1331. }
  1332. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setprofname.profile_id));
  1333. if (!profile) {
  1334. errorcode = ERR_FAIL;
  1335. goto error;
  1336. }
  1337. if (!profile->set_name) {
  1338. errorcode = ERR_NOTSUPP;
  1339. goto error;
  1340. }
  1341. for (i = 0; i < ARRAY_SIZE(namebuf) - 1; i++)
  1342. namebuf[i] = be16_to_cpu(((const uint16_t *)cmd->setprofname.utf16be_name)[i]);
  1343. if (profile->set_name(profile, namebuf)) {
  1344. errorcode = ERR_FAIL;
  1345. goto error;
  1346. }
  1347. error:
  1348. send_u32(client, errorcode);
  1349. }
  1350. static void command_getactiveprof(struct client *client, const struct command *cmd, unsigned int len)
  1351. {
  1352. struct razer_mouse *mouse;
  1353. struct razer_mouse_profile *activeprof;
  1354. if (len < CMD_SIZE(getactiveprof))
  1355. goto error;
  1356. mouse = find_mouse(cmd->idstr);
  1357. if (!mouse)
  1358. goto error;
  1359. activeprof = mouse->get_active_profile(mouse);
  1360. if (!activeprof)
  1361. goto error;
  1362. send_u32(client, activeprof->nr);
  1363. return;
  1364. error:
  1365. send_u32(client, 0xFFFFFFFF);
  1366. }
  1367. static void command_setactiveprof(struct client *client, const struct command *cmd, unsigned int len)
  1368. {
  1369. struct razer_mouse *mouse;
  1370. struct razer_mouse_profile *profile;
  1371. int err;
  1372. uint32_t errorcode = ERR_NONE;
  1373. if (len < CMD_SIZE(setactiveprof)) {
  1374. errorcode = ERR_CMDSIZE;
  1375. goto error;
  1376. }
  1377. mouse = find_mouse(cmd->idstr);
  1378. if (!mouse) {
  1379. errorcode = ERR_NOMOUSE;
  1380. goto error;
  1381. }
  1382. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setactiveprof.id));
  1383. if (!profile) {
  1384. errorcode = ERR_FAIL;
  1385. goto error;
  1386. }
  1387. err = mouse->claim(mouse);
  1388. if (err) {
  1389. errorcode = ERR_CLAIM;
  1390. goto error;
  1391. }
  1392. err = mouse->set_active_profile(mouse, profile);
  1393. if (err)
  1394. errorcode = ERR_FAIL;
  1395. mouse->release(mouse);
  1396. error:
  1397. send_u32(client, errorcode);
  1398. }
  1399. static void command_suppbuttons(struct client *client, const struct command *cmd, unsigned int len)
  1400. {
  1401. struct razer_mouse *mouse;
  1402. struct razer_button *list;
  1403. int count, i;
  1404. if (len < CMD_SIZE(suppbuttons))
  1405. goto error;
  1406. mouse = find_mouse(cmd->idstr);
  1407. if (!mouse || !mouse->supported_buttons)
  1408. goto error;
  1409. count = mouse->supported_buttons(mouse, &list);
  1410. if (count <= 0)
  1411. goto error;
  1412. send_u32(client, count);
  1413. for (i = 0; i < count; i++) {
  1414. send_u32(client, list[i].id);
  1415. send_string(client, list[i].name);
  1416. }
  1417. return;
  1418. error:
  1419. send_u32(client, 0);
  1420. }
  1421. static void command_suppbutfuncs(struct client *client, const struct command *cmd, unsigned int len)
  1422. {
  1423. struct razer_mouse *mouse;
  1424. struct razer_button_function *list;
  1425. int count, i;
  1426. if (len < CMD_SIZE(suppbutfuncs))
  1427. goto error;
  1428. mouse = find_mouse(cmd->idstr);
  1429. if (!mouse || !mouse->supported_button_functions)
  1430. goto error;
  1431. count = mouse->supported_button_functions(mouse, &list);
  1432. if (count <= 0)
  1433. goto error;
  1434. send_u32(client, count);
  1435. for (i = 0; i < count; i++) {
  1436. send_u32(client, list[i].id);
  1437. send_string(client, list[i].name);
  1438. }
  1439. return;
  1440. error:
  1441. send_u32(client, 0);
  1442. }
  1443. static void command_getbutfunc(struct client *client, const struct command *cmd, unsigned int len)
  1444. {
  1445. struct razer_mouse *mouse;
  1446. struct razer_mouse_profile *profile;
  1447. struct razer_button_function *func;
  1448. struct razer_button *button;
  1449. if (len < CMD_SIZE(getbutfunc))
  1450. goto error;
  1451. mouse = find_mouse(cmd->idstr);
  1452. if (!mouse)
  1453. goto error;
  1454. button = find_mouse_button(mouse, be32_to_cpu(cmd->getbutfunc.button_id));
  1455. if (!button)
  1456. goto error;
  1457. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getbutfunc.profile_id));
  1458. if (!profile || !profile->get_button_function)
  1459. goto error;
  1460. func = profile->get_button_function(profile, button);
  1461. if (!func)
  1462. goto error;
  1463. send_u32(client, func->id);
  1464. send_string(client, func->name);
  1465. return;
  1466. error:
  1467. send_u32(client, 0);
  1468. send_string(client, "");
  1469. }
  1470. static void command_setbutfunc(struct client *client, const struct command *cmd, unsigned int len)
  1471. {
  1472. struct razer_mouse *mouse;
  1473. struct razer_mouse_profile *profile;
  1474. struct razer_button_function *func;
  1475. struct razer_button *button;
  1476. uint32_t errorcode = ERR_NONE;
  1477. int err;
  1478. if (len < CMD_SIZE(setbutfunc)) {
  1479. errorcode = ERR_CMDSIZE;
  1480. goto error;
  1481. }
  1482. mouse = find_mouse(cmd->idstr);
  1483. if (!mouse) {
  1484. errorcode = ERR_NOMOUSE;
  1485. goto error;
  1486. }
  1487. button = find_mouse_button(mouse, be32_to_cpu(cmd->setbutfunc.button_id));
  1488. if (!button) {
  1489. errorcode = ERR_FAIL;
  1490. goto error;
  1491. }
  1492. func = find_mouse_button_function(mouse, be32_to_cpu(cmd->setbutfunc.function_id));
  1493. if (!func) {
  1494. errorcode = ERR_FAIL;
  1495. goto error;
  1496. }
  1497. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setbutfunc.profile_id));
  1498. if (!profile || !profile->set_button_function) {
  1499. errorcode = ERR_FAIL;
  1500. goto error;
  1501. }
  1502. err = mouse->claim(mouse);
  1503. if (err) {
  1504. errorcode = ERR_CLAIM;
  1505. goto error;
  1506. }
  1507. err = profile->set_button_function(profile, button, func);
  1508. mouse->release(mouse);
  1509. if (err) {
  1510. errorcode = ERR_FAIL;
  1511. goto error;
  1512. }
  1513. error:
  1514. send_u32(client, errorcode);
  1515. }
  1516. static void command_suppaxes(struct client *client, const struct command *cmd, unsigned int len)
  1517. {
  1518. struct razer_mouse *mouse;
  1519. struct razer_axis *list;
  1520. int count, i;
  1521. if (len < CMD_SIZE(suppaxes))
  1522. goto error;
  1523. mouse = find_mouse(cmd->idstr);
  1524. if (!mouse || !mouse->supported_axes)
  1525. goto error;
  1526. count = mouse->supported_axes(mouse, &list);
  1527. if (count <= 0)
  1528. goto error;
  1529. send_u32(client, count);
  1530. for (i = 0; i < count; i++) {
  1531. send_u32(client, list[i].id);
  1532. send_string(client, list[i].name);
  1533. send_u32(client, list[i].flags);
  1534. }
  1535. return;
  1536. error:
  1537. send_u32(client, 0);
  1538. }
  1539. static void command_flashfw(struct client *client, const struct command *cmd, unsigned int len)
  1540. {
  1541. struct razer_mouse *mouse;
  1542. uint32_t image_size;
  1543. int err;
  1544. uint32_t errorcode = ERR_NONE;
  1545. char *image = NULL;
  1546. if (len < CMD_SIZE(flashfw)) {
  1547. errorcode = ERR_CMDSIZE;
  1548. goto error;
  1549. }
  1550. image_size = be32_to_cpu(cmd->flashfw.imagesize);
  1551. if (image_size > MAX_FIRMWARE_SIZE) {
  1552. errorcode = ERR_CMDSIZE;
  1553. goto error;
  1554. }
  1555. image = malloc(image_size);
  1556. if (!image) {
  1557. errorcode = ERR_NOMEM;
  1558. goto error;
  1559. }
  1560. err = recv_bulk(client, image, image_size);
  1561. if (err) {
  1562. errorcode = ERR_PAYLOAD;
  1563. goto error;
  1564. }
  1565. mouse = find_mouse(cmd->idstr);
  1566. if (!mouse) {
  1567. errorcode = ERR_NOMOUSE;
  1568. goto error;
  1569. }
  1570. if (!mouse->flash_firmware) {
  1571. errorcode = ERR_NOTSUPP;
  1572. goto error;
  1573. }
  1574. err = mouse->claim(mouse);
  1575. if (err) {
  1576. errorcode = ERR_CLAIM;
  1577. goto error;
  1578. }
  1579. err = mouse->flash_firmware(mouse, image, image_size,
  1580. RAZER_FW_FLASH_MAGIC);
  1581. mouse->release(mouse);
  1582. if (err) {
  1583. errorcode = ERR_FAIL;
  1584. goto error;
  1585. }
  1586. error:
  1587. send_u32(client, errorcode);
  1588. free(image);
  1589. }
  1590. static void command_claim(struct client *client, const struct command *cmd, unsigned int len)
  1591. {
  1592. struct razer_mouse *mouse;
  1593. uint32_t errorcode = ERR_NONE;
  1594. int err;
  1595. if (len < CMD_SIZE(claim)) {
  1596. errorcode = ERR_CMDSIZE;
  1597. goto error;
  1598. }
  1599. mouse = find_mouse(cmd->idstr);
  1600. if (!mouse) {
  1601. errorcode = ERR_NOMOUSE;
  1602. goto error;
  1603. }
  1604. err = mouse->claim(mouse);
  1605. if (err) {
  1606. errorcode = ERR_FAIL;
  1607. goto error;
  1608. }
  1609. error:
  1610. send_u32(client, errorcode);
  1611. }
  1612. static void command_release(struct client *client, const struct command *cmd, unsigned int len)
  1613. {
  1614. struct razer_mouse *mouse;
  1615. uint32_t errorcode = ERR_NONE;
  1616. if (len < CMD_SIZE(release)) {
  1617. errorcode = ERR_CMDSIZE;
  1618. goto error;
  1619. }
  1620. mouse = find_mouse(cmd->idstr);
  1621. if (!mouse) {
  1622. errorcode = ERR_NOMOUSE;
  1623. goto error;
  1624. }
  1625. mouse->release(mouse);
  1626. error:
  1627. send_u32(client, errorcode);
  1628. }
  1629. static void handle_received_command(struct client *client, const char *_cmd, unsigned int len)
  1630. {
  1631. const struct command *cmd = (const struct command *)_cmd;
  1632. if (len < COMMAND_HDR_SIZE)
  1633. return;
  1634. switch (cmd->hdr.id) {
  1635. case COMMAND_ID_GETREV:
  1636. send_u32(client, INTERFACE_REVISION);
  1637. break;
  1638. case COMMAND_ID_RESCANMICE:
  1639. command_rescanmice(client, cmd, len);
  1640. break;
  1641. case COMMAND_ID_GETMICE:
  1642. command_getmice(client, cmd, len);
  1643. break;
  1644. case COMMAND_ID_GETFWVER:
  1645. command_getfwver(client, cmd, len);
  1646. break;
  1647. case COMMAND_ID_SUPPFREQS:
  1648. command_suppfreqs(client, cmd, len);
  1649. break;
  1650. case COMMAND_ID_SUPPRESOL:
  1651. command_suppresol(client, cmd, len);
  1652. break;
  1653. case COMMAND_ID_SUPPDPIMAPPINGS:
  1654. command_suppdpimappings(client, cmd, len);
  1655. break;
  1656. case COMMAND_ID_CHANGEDPIMAPPING:
  1657. command_changedpimapping(client, cmd, len);
  1658. break;
  1659. case COMMAND_ID_GETDPIMAPPING:
  1660. command_getdpimapping(client, cmd, len);
  1661. break;
  1662. case COMMAND_ID_SETDPIMAPPING:
  1663. command_setdpimapping(client, cmd, len);
  1664. break;
  1665. case COMMAND_ID_GETLEDS:
  1666. command_getleds(client, cmd, len);
  1667. break;
  1668. case COMMAND_ID_SETLED:
  1669. command_setled(client, cmd, len);
  1670. break;
  1671. case COMMAND_ID_SETFREQ:
  1672. command_setfreq(client, cmd, len);
  1673. break;
  1674. case COMMAND_ID_GETPROFILES:
  1675. command_getprofiles(client, cmd, len);
  1676. break;
  1677. case COMMAND_ID_GETACTIVEPROF:
  1678. command_getactiveprof(client, cmd, len);
  1679. break;
  1680. case COMMAND_ID_SETACTIVEPROF:
  1681. command_setactiveprof(client, cmd, len);
  1682. break;
  1683. case COMMAND_ID_GETFREQ:
  1684. command_getfreq(client, cmd, len);
  1685. break;
  1686. case COMMAND_ID_SUPPBUTTONS:
  1687. command_suppbuttons(client, cmd, len);
  1688. break;
  1689. case COMMAND_ID_SUPPBUTFUNCS:
  1690. command_suppbutfuncs(client, cmd, len);
  1691. break;
  1692. case COMMAND_ID_GETBUTFUNC:
  1693. command_getbutfunc(client, cmd, len);
  1694. break;
  1695. case COMMAND_ID_SETBUTFUNC:
  1696. command_setbutfunc(client, cmd, len);
  1697. break;
  1698. case COMMAND_ID_SUPPAXES:
  1699. command_suppaxes(client, cmd, len);
  1700. break;
  1701. case COMMAND_ID_RECONFIGMICE:
  1702. command_reconfigmice(client, cmd, len);
  1703. break;
  1704. case COMMAND_ID_GETMOUSEINFO:
  1705. command_getmouseinfo(client, cmd, len);
  1706. break;
  1707. case COMMAND_ID_GETPROFNAME:
  1708. command_getprofname(client, cmd, len);
  1709. break;
  1710. case COMMAND_ID_SETPROFNAME:
  1711. command_setprofname(client, cmd, len);
  1712. break;
  1713. default:
  1714. /* Unknown command. */
  1715. break;
  1716. }
  1717. }
  1718. static void handle_received_privileged_command(struct client *client,
  1719. const char *_cmd, unsigned int len)
  1720. {
  1721. const struct command *cmd = (const struct command *)_cmd;
  1722. if (len < COMMAND_HDR_SIZE)
  1723. return;
  1724. switch (cmd->hdr.id) {
  1725. case COMMAND_PRIV_FLASHFW:
  1726. command_flashfw(client, cmd, len);
  1727. break;
  1728. case COMMAND_PRIV_CLAIM:
  1729. command_claim(client, cmd, len);
  1730. break;
  1731. case COMMAND_PRIV_RELEASE:
  1732. command_release(client, cmd, len);
  1733. break;
  1734. default:
  1735. /* Unknown command. */
  1736. break;
  1737. }
  1738. }
  1739. static void check_client_connections(void)
  1740. {
  1741. char command[COMMAND_MAX_SIZE + 1] = { 0, };
  1742. int nr;
  1743. struct client *client, *next;
  1744. for (client = clients; client; ) {
  1745. next = client->next;
  1746. nr = recv(client->fd, command, COMMAND_MAX_SIZE, 0);
  1747. if (nr < 0)
  1748. goto next_client;
  1749. if (nr == 0) {
  1750. disconnect_client(&clients, client);
  1751. goto next_client;
  1752. }
  1753. handle_received_command(client, command, nr);
  1754. next_client:
  1755. client = next;
  1756. }
  1757. }
  1758. static void check_privileged_connections(void)
  1759. {
  1760. char command[COMMAND_MAX_SIZE + 1] = { 0, };
  1761. int nr;
  1762. struct client *client, *next;
  1763. for (client = privileged_clients; client; ) {
  1764. next = client->next;
  1765. nr = recv(client->fd, command, COMMAND_MAX_SIZE, 0);
  1766. if (nr < 0)
  1767. goto next_client;
  1768. if (nr == 0) {
  1769. disconnect_client(&privileged_clients, client);
  1770. goto next_client;
  1771. }
  1772. handle_received_privileged_command(client, command, nr);
  1773. next_client:
  1774. client = next;
  1775. }
  1776. }
  1777. static void broadcast_notification(unsigned int notifyId, size_t size)
  1778. {
  1779. struct reply r;
  1780. struct client *client;
  1781. for (client = clients; client; client = client->next) {
  1782. r.hdr.id = notifyId;
  1783. send_reply(client, &r, size);
  1784. }
  1785. }
  1786. static void event_handler(enum razer_event event,
  1787. const struct razer_event_data *data)
  1788. {
  1789. switch (event) {
  1790. case RAZER_EV_MOUSE_ADD:
  1791. logdebug("Broadcasting mouse-add event\n");
  1792. broadcast_notification(NOTIFY_ID_NEWMOUSE,
  1793. REPLY_SIZE(notify_newmouse));
  1794. break;
  1795. case RAZER_EV_MOUSE_REMOVE:
  1796. logdebug("Broadcasting mouse-remove event\n");
  1797. broadcast_notification(NOTIFY_ID_DELMOUSE,
  1798. REPLY_SIZE(notify_delmouse));
  1799. break;
  1800. }
  1801. }
  1802. static int mainloop(void)
  1803. {
  1804. struct client *client;
  1805. int err;
  1806. fd_set wait_fdset;
  1807. loginfo("Razer device service daemon\n");
  1808. setup_sighandler();
  1809. err = setup_environment();
  1810. if (err)
  1811. return 1;
  1812. err = razer_register_event_handler(event_handler);
  1813. if (err) {
  1814. logerr("Failed to register event handler\n");
  1815. cleanup_environment();
  1816. return 1;
  1817. }
  1818. mice = razer_rescan_mice();
  1819. while (1) {
  1820. FD_ZERO(&wait_fdset);
  1821. FD_SET(privsock, &wait_fdset);
  1822. FD_SET(ctlsock, &wait_fdset);
  1823. for (client = clients; client; client = client->next)
  1824. FD_SET(client->fd, &wait_fdset);
  1825. for (client = privileged_clients; client; client = client->next)
  1826. FD_SET(client->fd, &wait_fdset);
  1827. select(FD_SETSIZE, &wait_fdset, NULL, NULL, NULL);
  1828. check_control_socket(privsock, &privileged_clients);
  1829. check_privileged_connections();
  1830. check_control_socket(ctlsock, &clients);
  1831. check_client_connections();
  1832. }
  1833. return 1;
  1834. }
  1835. static void usage(FILE *fd, int argc, char **argv)
  1836. {
  1837. fprintf(fd, "Razer device service daemon\n\n");
  1838. fprintf(fd, "Usage: %s [OPTIONS]\n", argv[0]);
  1839. fprintf(fd, "\n");
  1840. fprintf(fd, " -B|--background Fork into the background (daemon mode)\n");
  1841. fprintf(fd, " -c|--config PATH Use specified config file. Defaults to %s\n",
  1842. RAZER_DEFAULT_CONFIG);
  1843. fprintf(fd, " -C|--no-config Do not load the config file\n");
  1844. fprintf(fd, " -p|--no-profemu Disable profile emulation\n");
  1845. fprintf(fd, " -P|--pidfile PATH Create a PID-file\n");
  1846. fprintf(fd, " -l|--loglevel LEVEL Set the loglevel\n");
  1847. fprintf(fd, " 0=error, 1=warning, 2=info(default), 3=debug\n");
  1848. fprintf(fd, " -f|--force Force remove sockets before starting up\n");
  1849. fprintf(fd, "\n");
  1850. fprintf(fd, " -h|--help Print this help text\n");
  1851. }
  1852. static int parse_args(int argc, char **argv)
  1853. {
  1854. static struct option long_options[] = {
  1855. { "help", no_argument, 0, 'h', },
  1856. { "version", no_argument, 0, 'v', },
  1857. { "background", no_argument, 0, 'B', },
  1858. { "config", required_argument, 0, 'c', },
  1859. { "no-config", no_argument, 0, 'C', },
  1860. { "no-profemu", no_argument, 0, 'p', },
  1861. { "pidfile", required_argument, 0, 'P', },
  1862. { "loglevel", required_argument, 0, 'l', },
  1863. { "force", no_argument, 0, 'f', },
  1864. { 0, },
  1865. };
  1866. int c, idx;
  1867. while (1) {
  1868. c = getopt_long(argc, argv, "hvBc:CpP:l:f",
  1869. long_options, &idx);
  1870. if (c == -1)
  1871. break;
  1872. switch (c) {
  1873. case 'h':
  1874. case 'v':
  1875. usage(stdout, argc, argv);
  1876. return 1;
  1877. case 'B':
  1878. cmdargs.background = 1;
  1879. break;
  1880. case 'c':
  1881. cmdargs.configfile = optarg;
  1882. break;
  1883. case 'C':
  1884. cmdargs.configfile = "";
  1885. break;
  1886. case 'p':
  1887. cmdargs.no_profile_emu = 1;
  1888. break;
  1889. case 'P':
  1890. cmdargs.pidfile = optarg;
  1891. break;
  1892. case 'l':
  1893. if (sscanf(optarg, "%d", &cmdargs.loglevel) != 1) {
  1894. fprintf(stderr, "Failed to parse --loglevel argument\n");
  1895. return -1;
  1896. }
  1897. break;
  1898. case 'f':
  1899. cmdargs.force = 1;
  1900. break;
  1901. default:
  1902. return -1;
  1903. }
  1904. }
  1905. return 0;
  1906. }
  1907. int main(int argc, char **argv)
  1908. {
  1909. int err;
  1910. err = parse_args(argc, argv);
  1911. if (err > 0)
  1912. return 0;
  1913. if (err)
  1914. return err;
  1915. if (cmdargs.background) {
  1916. err = daemon(0, 0);
  1917. if (err) {
  1918. logerr("Failed to fork into the background: %s\n",
  1919. strerror(errno));
  1920. return 1;
  1921. }
  1922. /* Child process */
  1923. return mainloop();
  1924. }
  1925. return mainloop();
  1926. }