razerd.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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 RUNDIR "/run"
  72. #define RUNDIR_RAZERD RUNDIR "/razerd"
  73. #define SOCKPATH RUNDIR_RAZERD "/socket"
  74. #define PRIV_SOCKPATH RUNDIR_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(RUNDIR_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 /run subdirectory. */
  433. err = mkdir(RUNDIR_RAZERD, 0755);
  434. if (err && errno != EEXIST) {
  435. logerr("Failed to create directory %s: %s\n",
  436. RUNDIR_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(RUNDIR_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 int 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 -1;
  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 -1;
  577. }
  578. client = new_client(&remoteaddr, socklen, fd);
  579. if (!client) {
  580. close(fd);
  581. return -1;
  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. return 0;
  589. }
  590. static void disconnect_client(struct client **client_list, struct client *client)
  591. {
  592. client_list_del(client_list, client);
  593. if (client_list == &privileged_clients)
  594. logdebug("Privileged client disconnected (fd=%d)\n", client->fd);
  595. else
  596. logdebug("Client disconnected (fd=%d)\n", client->fd);
  597. close(client->fd);
  598. free_client(client);
  599. }
  600. static int send_reply(struct client *client, struct reply *r, size_t len)
  601. {
  602. const uint8_t *buf = (const uint8_t *)r;
  603. int ret;
  604. while (len) {
  605. ret = send(client->fd, buf, len, 0);
  606. if (ret < 0) {
  607. if (errno == EAGAIN ||
  608. errno == EINTR) {
  609. razer_msleep(1);
  610. continue;
  611. }
  612. logerr("send() failed: %s", strerror(errno));
  613. return -errno;
  614. }
  615. len -= ret;
  616. buf += ret;
  617. }
  618. return 0;
  619. }
  620. static int send_u32(struct client *client, uint32_t v)
  621. {
  622. struct reply r;
  623. r.hdr.id = REPLY_ID_U32;
  624. r.u32.val = cpu_to_be32(v);
  625. return send_reply(client, &r, REPLY_SIZE(u32));
  626. }
  627. static int send_string(struct client *client, const char *str)
  628. {
  629. struct reply *r;
  630. size_t i, len = strlen(str);
  631. int err;
  632. r = malloc(len + REPLY_SIZE(string));
  633. if (!r) {
  634. logerr("Out of memory\n");
  635. return -ENOMEM;
  636. }
  637. r->hdr.id = REPLY_ID_STR;
  638. r->string.encoding = STRING_ENC_ASCII;
  639. r->string.len = cpu_to_be16(len);
  640. for (i = 0; i < len; i++) {
  641. if ((unsigned char)str[i] <= 0x7Fu)
  642. r->string.str[i] = str[i];
  643. else
  644. r->string.str[i] = '?'; /* Non-ASCII char. */
  645. }
  646. err = send_reply(client, r, REPLY_SIZE(string) + len);
  647. free(r);
  648. return err;
  649. }
  650. static int send_utf16_string(struct client *client, const razer_utf16_t *str)
  651. {
  652. struct reply *r;
  653. size_t i, len = razer_utf16_strlen(str);
  654. int err;
  655. uint16_t value;
  656. r = malloc(len * 2 + REPLY_SIZE(string));
  657. if (!r) {
  658. logerr("Out of memory\n");
  659. return -ENOMEM;
  660. }
  661. r->hdr.id = REPLY_ID_STR;
  662. r->string.encoding = STRING_ENC_UTF16BE;
  663. r->string.len = cpu_to_be16(len);
  664. for (i = 0; i < len; i++) {
  665. value = cpu_to_be16(str[i]);
  666. memcpy(&((uint16_t *)r->string.str)[i], &value, sizeof(value));
  667. }
  668. err = send_reply(client, r, REPLY_SIZE(string) + len * 2);
  669. free(r);
  670. return err;
  671. }
  672. static int recv_bulk(struct client *client, char *buf, unsigned int len)
  673. {
  674. unsigned int next_len, i;
  675. int nr;
  676. //FIXME timeout
  677. for (i = 0; i < len; i += BULK_CHUNK_SIZE) {
  678. next_len = BULK_CHUNK_SIZE;
  679. if (i + next_len > len)
  680. next_len = len - i;
  681. while (1) {
  682. nr = recv(client->fd, buf + i, next_len, 0);
  683. if (nr < 0) {
  684. if (errno == EAGAIN ||
  685. errno == EINTR) {
  686. razer_msleep(1);
  687. continue;
  688. }
  689. }
  690. if (nr > 0)
  691. break;
  692. }
  693. if (nr < 0 || (unsigned int)nr != next_len) {
  694. send_u32(client, ERR_PAYLOAD);
  695. return -1;
  696. }
  697. send_u32(client, ERR_NONE);
  698. }
  699. return 0;
  700. }
  701. struct razer_mouse * find_mouse(const char *idstr)
  702. {
  703. struct razer_mouse *m, *next;
  704. razer_for_each_mouse(m, next, mice) {
  705. if (strncmp(m->idstr, idstr, RAZER_IDSTR_MAX_SIZE) == 0)
  706. return m;
  707. }
  708. return NULL;
  709. }
  710. static struct razer_mouse_profile * find_mouse_profile(struct razer_mouse *mouse,
  711. unsigned int profile_id)
  712. {
  713. struct razer_mouse_profile *list;
  714. unsigned int i;
  715. if (!mouse->get_profiles)
  716. return NULL;
  717. list = mouse->get_profiles(mouse);
  718. if (!list)
  719. return NULL;
  720. for (i = 0; i < mouse->nr_profiles; i++) {
  721. if (list[i].nr == profile_id)
  722. return &list[i];
  723. }
  724. return NULL;
  725. }
  726. static struct razer_axis * find_mouse_axis(struct razer_mouse *mouse,
  727. unsigned int axis_id)
  728. {
  729. struct razer_axis *list;
  730. int i, count;
  731. if (!mouse->supported_axes)
  732. return NULL;
  733. count = mouse->supported_axes(mouse, &list);
  734. if (count <= 0)
  735. return NULL;
  736. for (i = 0; i < count; i++) {
  737. if (list[i].id == axis_id)
  738. return &list[i];
  739. }
  740. return NULL;
  741. }
  742. static struct razer_mouse_dpimapping * find_mouse_dpimapping(struct razer_mouse *mouse,
  743. unsigned int mapping_id)
  744. {
  745. struct razer_mouse_dpimapping *list;
  746. int i, count;
  747. if (!mouse->supported_dpimappings)
  748. return NULL;
  749. count = mouse->supported_dpimappings(mouse, &list);
  750. if (count <= 0)
  751. return NULL;
  752. for (i = 0; i < count; i++) {
  753. if (list[i].nr == mapping_id)
  754. return &list[i];
  755. }
  756. return NULL;
  757. }
  758. static struct razer_button * find_mouse_button(struct razer_mouse *mouse,
  759. unsigned int button_id)
  760. {
  761. struct razer_button *list;
  762. int i, count;
  763. if (!mouse->supported_buttons)
  764. return NULL;
  765. count = mouse->supported_buttons(mouse, &list);
  766. if (count <= 0)
  767. return NULL;
  768. for (i = 0; i < count; i++) {
  769. if (list[i].id == button_id)
  770. return &list[i];
  771. }
  772. return NULL;
  773. }
  774. static struct razer_button_function * find_mouse_button_function(struct razer_mouse *mouse,
  775. unsigned int button_id)
  776. {
  777. struct razer_button_function *list;
  778. int i, count;
  779. if (!mouse->supported_button_functions)
  780. return NULL;
  781. count = mouse->supported_button_functions(mouse, &list);
  782. if (count <= 0)
  783. return NULL;
  784. for (i = 0; i < count; i++) {
  785. if (list[i].id == button_id)
  786. return &list[i];
  787. }
  788. return NULL;
  789. }
  790. static void command_getmice(struct client *client, const struct command *cmd, unsigned int len)
  791. {
  792. unsigned int count;
  793. char str[RAZER_IDSTR_MAX_SIZE + 1];
  794. struct razer_mouse *mouse, *next;
  795. count = 0;
  796. razer_for_each_mouse(mouse, next, mice)
  797. count++;
  798. send_u32(client, count);
  799. razer_for_each_mouse(mouse, next, mice) {
  800. snprintf(str, sizeof(str), "%s", mouse->idstr);
  801. send_string(client, str);
  802. }
  803. }
  804. static void command_getfwver(struct client *client, const struct command *cmd, unsigned int len)
  805. {
  806. struct razer_mouse *mouse;
  807. uint32_t fwver = 0xFFFFFFFF;
  808. int err;
  809. if (len < CMD_SIZE(getfwver))
  810. goto out;
  811. mouse = find_mouse(cmd->idstr);
  812. if (!mouse || !mouse->get_fw_version)
  813. goto out;
  814. err = mouse->claim(mouse);
  815. if (err)
  816. goto out;
  817. fwver = mouse->get_fw_version(mouse);
  818. mouse->release(mouse);
  819. out:
  820. send_u32(client, fwver);
  821. }
  822. static void command_getfreq(struct client *client, const struct command *cmd, unsigned int len)
  823. {
  824. struct razer_mouse *mouse;
  825. struct razer_mouse_profile *profile;
  826. enum razer_mouse_freq freq;
  827. unsigned int profile_id;
  828. if (len < CMD_SIZE(getfreq))
  829. goto error;
  830. mouse = find_mouse(cmd->idstr);
  831. if (!mouse)
  832. goto error;
  833. profile_id = be32_to_cpu(cmd->getfreq.profile_id);
  834. if (profile_id == PROFILE_INVALID) {
  835. if (!mouse->global_get_freq)
  836. goto error;
  837. freq = mouse->global_get_freq(mouse);
  838. } else {
  839. profile = find_mouse_profile(mouse, profile_id);
  840. if (!profile || !profile->get_freq)
  841. goto error;
  842. freq = profile->get_freq(profile);
  843. }
  844. send_u32(client, freq);
  845. return;
  846. error:
  847. send_u32(client, RAZER_MOUSE_FREQ_UNKNOWN);
  848. }
  849. static void command_suppfreqs(struct client *client, const struct command *cmd, unsigned int len)
  850. {
  851. struct razer_mouse *mouse;
  852. enum razer_mouse_freq *freq_list;
  853. int i, count;
  854. if (len < CMD_SIZE(suppfreqs))
  855. goto error;
  856. mouse = find_mouse(cmd->idstr);
  857. if (!mouse || !mouse->supported_freqs)
  858. goto error;
  859. count = mouse->supported_freqs(mouse, &freq_list);
  860. if (count <= 0)
  861. goto error;
  862. send_u32(client, count);
  863. for (i = 0; i < count; i++)
  864. send_u32(client, freq_list[i]);
  865. razer_free_freq_list(freq_list, count);
  866. return;
  867. error:
  868. count = 0;
  869. send_u32(client, count);
  870. }
  871. static void command_suppresol(struct client *client, const struct command *cmd, unsigned int len)
  872. {
  873. struct razer_mouse *mouse;
  874. enum razer_mouse_res *res_list;
  875. int i, count;
  876. if (len < CMD_SIZE(suppresol))
  877. goto error;
  878. mouse = find_mouse(cmd->idstr);
  879. if (!mouse || !mouse->supported_resolutions)
  880. goto error;
  881. count = mouse->supported_resolutions(mouse, &res_list);
  882. if (count <= 0)
  883. goto error;
  884. send_u32(client, count);
  885. for (i = 0; i < count; i++)
  886. send_u32(client, res_list[i]);
  887. razer_free_resolution_list(res_list, count);
  888. return;
  889. error:
  890. count = 0;
  891. send_u32(client, count);
  892. }
  893. static void command_suppdpimappings(struct client *client, const struct command *cmd, unsigned int len)
  894. {
  895. struct razer_mouse *mouse;
  896. struct razer_mouse_dpimapping *list;
  897. int i, j, count;
  898. if (len < CMD_SIZE(suppdpimappings))
  899. goto error;
  900. mouse = find_mouse(cmd->idstr);
  901. if (!mouse || !mouse->supported_dpimappings)
  902. goto error;
  903. count = mouse->supported_dpimappings(mouse, &list);
  904. if (count <= 0)
  905. goto error;
  906. send_u32(client, count);
  907. for (i = 0; i < count; i++) {
  908. send_u32(client, list[i].nr);
  909. send_u32(client, list[i].dimension_mask);
  910. for (j = 0; j < RAZER_NR_DIMS; j++)
  911. send_u32(client, list[i].res[j]);
  912. send_u32(client, (list[i].profile_mask >> 32) & 0xFFFFFFFF);
  913. send_u32(client, (list[i].profile_mask >> 0) & 0xFFFFFFFF);
  914. send_u32(client, list[i].change ? 1 : 0);
  915. }
  916. /* No need to free list. It's statically allocated. */
  917. return;
  918. error:
  919. count = 0;
  920. send_u32(client, count);
  921. }
  922. static void command_changedpimapping(struct client *client, const struct command *cmd, unsigned int len)
  923. {
  924. struct razer_mouse *mouse;
  925. struct razer_mouse_dpimapping *mapping;
  926. int err;
  927. uint32_t errorcode = ERR_NONE;
  928. if (len < CMD_SIZE(changedpimapping)) {
  929. errorcode = ERR_CMDSIZE;
  930. goto error;
  931. }
  932. mouse = find_mouse(cmd->idstr);
  933. if (!mouse) {
  934. errorcode = ERR_NOMOUSE;
  935. goto error;
  936. }
  937. mapping = find_mouse_dpimapping(mouse, be32_to_cpu(cmd->changedpimapping.id));
  938. if (!mapping || !mapping->change) {
  939. errorcode = ERR_FAIL;
  940. goto error;
  941. }
  942. err = mouse->claim(mouse);
  943. if (err) {
  944. errorcode = ERR_CLAIM;
  945. goto error;
  946. }
  947. err = mapping->change(mapping,
  948. be32_to_cpu(cmd->changedpimapping.dimension),
  949. be32_to_cpu(cmd->changedpimapping.new_resolution));
  950. if (err)
  951. errorcode = ERR_FAIL;
  952. mouse->release(mouse);
  953. error:
  954. send_u32(client, errorcode);
  955. }
  956. static void command_getdpimapping(struct client *client, const struct command *cmd, unsigned int len)
  957. {
  958. struct razer_mouse *mouse;
  959. struct razer_mouse_profile *profile;
  960. struct razer_axis *axis;
  961. struct razer_mouse_dpimapping *mapping;
  962. if (len < CMD_SIZE(getdpimapping))
  963. goto error;
  964. mouse = find_mouse(cmd->idstr);
  965. if (!mouse)
  966. goto error;
  967. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getdpimapping.profile_id));
  968. if (!profile)
  969. goto error;
  970. axis = find_mouse_axis(mouse, be32_to_cpu(cmd->getdpimapping.axis_id));
  971. mapping = profile->get_dpimapping(profile, axis);
  972. if (!mapping)
  973. goto error;
  974. send_u32(client, mapping->nr);
  975. return;
  976. error:
  977. send_u32(client, 0xFFFFFFFF);
  978. }
  979. static void command_setdpimapping(struct client *client, const struct command *cmd, unsigned int len)
  980. {
  981. struct razer_mouse *mouse;
  982. struct razer_mouse_profile *profile;
  983. struct razer_axis *axis;
  984. struct razer_mouse_dpimapping *mapping;
  985. uint32_t errorcode = ERR_NONE;
  986. int err;
  987. if (len < CMD_SIZE(setdpimapping)) {
  988. errorcode = ERR_CMDSIZE;
  989. goto error;
  990. }
  991. mouse = find_mouse(cmd->idstr);
  992. if (!mouse) {
  993. errorcode = ERR_NOMOUSE;
  994. goto error;
  995. }
  996. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setdpimapping.profile_id));
  997. if (!profile || !profile->set_dpimapping) {
  998. errorcode = ERR_FAIL;
  999. goto error;
  1000. }
  1001. axis = find_mouse_axis(mouse, be32_to_cpu(cmd->setdpimapping.axis_id));
  1002. mapping = find_mouse_dpimapping(mouse, be32_to_cpu(cmd->setdpimapping.mapping_id));
  1003. if (!mapping) {
  1004. errorcode = ERR_FAIL;
  1005. goto error;
  1006. }
  1007. err = mouse->claim(mouse);
  1008. if (err) {
  1009. errorcode = ERR_CLAIM;
  1010. goto error;
  1011. }
  1012. err = profile->set_dpimapping(profile, axis, mapping);
  1013. mouse->release(mouse);
  1014. if (err) {
  1015. errorcode = ERR_FAIL;
  1016. goto error;
  1017. }
  1018. error:
  1019. send_u32(client, errorcode);
  1020. }
  1021. static void command_rescanmice(struct client *client, const struct command *cmd, unsigned int len)
  1022. {
  1023. mice = razer_rescan_mice();
  1024. }
  1025. static void command_reconfigmice(struct client *client, const struct command *cmd, unsigned int len)
  1026. {
  1027. razer_reconfig_mice();
  1028. }
  1029. static void command_getmouseinfo(struct client *client, const struct command *cmd, unsigned int len)
  1030. {
  1031. struct razer_mouse *mouse;
  1032. struct razer_mouse_profile *profiles;
  1033. unsigned int flags;
  1034. if (len < CMD_SIZE(getmouseinfo))
  1035. goto error;
  1036. mouse = find_mouse(cmd->idstr);
  1037. if (!mouse)
  1038. goto error;
  1039. flags = MOUSEINFOFLG_RESULTOK;
  1040. if (mouse->global_get_leds)
  1041. flags |= MOUSEINFOFLG_GLOBAL_LEDS;
  1042. if (mouse->global_get_freq)
  1043. flags |= MOUSEINFOFLG_GLOBAL_FREQ;
  1044. if (mouse->nr_profiles && mouse->get_profiles) {
  1045. profiles = mouse->get_profiles(mouse);
  1046. if (profiles) {
  1047. if (profiles[0].get_leds)
  1048. flags |= MOUSEINFOFLG_PROFILE_LEDS;
  1049. if (profiles[0].get_freq)
  1050. flags |= MOUSEINFOFLG_PROFILE_FREQ;
  1051. if (profiles[0].set_name)
  1052. flags |= MOUSEINFOFLG_PROFNAMEMUTABLE;
  1053. }
  1054. }
  1055. if (mouse->flags & RAZER_MOUSEFLG_SUGGESTFWUP)
  1056. flags |= MOUSEINFOFLG_SUGGESTFWUP;
  1057. send_u32(client, flags);
  1058. return;
  1059. error:
  1060. send_u32(client, 0);
  1061. }
  1062. static void command_getleds(struct client *client, const struct command *cmd, unsigned int len)
  1063. {
  1064. struct razer_mouse *mouse;
  1065. struct razer_mouse_profile *profile;
  1066. struct razer_led *leds_list, *led;
  1067. int count;
  1068. unsigned int flags, profile_id;
  1069. if (len < CMD_SIZE(getleds))
  1070. goto error;
  1071. mouse = find_mouse(cmd->idstr);
  1072. if (!mouse)
  1073. goto error;
  1074. profile_id = be32_to_cpu(cmd->getleds.profile_id);
  1075. if (profile_id == PROFILE_INVALID) {
  1076. if (!mouse->global_get_leds)
  1077. goto error;
  1078. count = mouse->global_get_leds(mouse, &leds_list);
  1079. } else {
  1080. profile = find_mouse_profile(mouse, profile_id);
  1081. if (!profile)
  1082. goto error;
  1083. if (!profile->get_leds)
  1084. goto error;
  1085. count = profile->get_leds(profile, &leds_list);
  1086. }
  1087. if (count <= 0)
  1088. goto error;
  1089. send_u32(client, count);
  1090. for (led = leds_list; led; led = led->next) {
  1091. flags = 0;
  1092. if (led->color.valid)
  1093. flags |= LED_FLAG_HAVECOLOR;
  1094. if (led->change_color)
  1095. flags |= LED_FLAG_CHANGECOLOR;
  1096. send_u32(client, flags);
  1097. send_string(client, led->name);
  1098. send_u32(client, led->state);
  1099. send_u32(client, led->mode);
  1100. send_u32(client, led->supported_modes_mask);
  1101. send_u32(client, ((uint32_t)led->color.r << 16) |
  1102. ((uint32_t)led->color.g << 8) |
  1103. ((uint32_t)led->color.b << 0));
  1104. }
  1105. razer_free_leds(leds_list);
  1106. return;
  1107. error:
  1108. count = 0;
  1109. send_u32(client, count);
  1110. }
  1111. static struct razer_led * razer_mouse_find_led(struct razer_led *leds_list,
  1112. const char *led_name)
  1113. {
  1114. struct razer_led *led;
  1115. for (led = leds_list; led; led = led->next) {
  1116. if (strncasecmp(led->name, led_name, RAZER_LEDNAME_MAX_SIZE) == 0)
  1117. return led;
  1118. }
  1119. return NULL;
  1120. }
  1121. static void command_setled(struct client *client, const struct command *cmd, unsigned int len)
  1122. {
  1123. struct razer_mouse *mouse;
  1124. struct razer_mouse_profile *profile;
  1125. struct razer_led *leds_list = NULL, *led;
  1126. enum razer_led_state new_state;
  1127. enum razer_led_mode new_mode;
  1128. struct razer_rgb_color new_color;
  1129. int err, count;
  1130. uint32_t errorcode = ERR_NONE;
  1131. unsigned int profile_id, value;
  1132. if (len < CMD_SIZE(setled)) {
  1133. errorcode = ERR_CMDSIZE;
  1134. goto error;
  1135. }
  1136. mouse = find_mouse(cmd->idstr);
  1137. if (!mouse) {
  1138. errorcode = ERR_NOMOUSE;
  1139. goto error;
  1140. }
  1141. profile_id = be32_to_cpu(cmd->setled.profile_id);
  1142. if (profile_id == PROFILE_INVALID) {
  1143. if (!mouse->global_get_leds) {
  1144. errorcode = ERR_NOLED;
  1145. goto error;
  1146. }
  1147. count = mouse->global_get_leds(mouse, &leds_list);
  1148. } else {
  1149. profile = find_mouse_profile(mouse, profile_id);
  1150. if (!profile) {
  1151. errorcode = ERR_NOLED;
  1152. goto error;
  1153. }
  1154. if (!profile->get_leds) {
  1155. errorcode = ERR_NOLED;
  1156. goto error;
  1157. }
  1158. count = profile->get_leds(profile, &leds_list);
  1159. }
  1160. if (count <= 0) {
  1161. errorcode = ERR_NOMEM;
  1162. goto error;
  1163. }
  1164. led = razer_mouse_find_led(leds_list, cmd->setled.led_name);
  1165. if (!led) {
  1166. errorcode = ERR_NOLED;
  1167. goto error;
  1168. }
  1169. err = mouse->claim(mouse);
  1170. if (err) {
  1171. errorcode = ERR_CLAIM;
  1172. goto error;
  1173. }
  1174. new_state = cmd->setled.new_state ? RAZER_LED_ON : RAZER_LED_OFF;
  1175. if (new_state != led->state) {
  1176. err = led->toggle_state(led, new_state);
  1177. if (err) {
  1178. mouse->release(mouse);
  1179. errorcode = ERR_FAIL;
  1180. goto error;
  1181. }
  1182. }
  1183. new_mode = cmd->setled.new_mode;
  1184. if (new_mode != led->mode) {
  1185. if (led->set_mode) {
  1186. err = led->set_mode(led, new_mode);
  1187. if (err) {
  1188. mouse->release(mouse);
  1189. errorcode = ERR_FAIL;
  1190. goto error;
  1191. }
  1192. }
  1193. }
  1194. if (led->change_color) {
  1195. memset(&new_color, 0, sizeof(new_color));
  1196. value = be32_to_cpu(cmd->setled.color);
  1197. new_color.valid = 1;
  1198. new_color.r = (value >> 16) & 0xFF;
  1199. new_color.g = (value >> 8) & 0xFF;
  1200. new_color.b = (value >> 0) & 0xFF;
  1201. if (!!new_color.valid != !!led->color.valid ||
  1202. new_color.r != led->color.r ||
  1203. new_color.g != led->color.g ||
  1204. new_color.b != led->color.b) {
  1205. err = led->change_color(led, &new_color);
  1206. if (err) {
  1207. mouse->release(mouse);
  1208. errorcode = ERR_FAIL;
  1209. goto error;
  1210. }
  1211. }
  1212. }
  1213. mouse->release(mouse);
  1214. error:
  1215. razer_free_leds(leds_list);
  1216. send_u32(client, errorcode);
  1217. }
  1218. static void command_setfreq(struct client *client, const struct command *cmd, unsigned int len)
  1219. {
  1220. struct razer_mouse *mouse;
  1221. struct razer_mouse_profile *profile = NULL;
  1222. int err;
  1223. uint32_t errorcode = ERR_NONE;
  1224. unsigned int profile_id;
  1225. if (len < CMD_SIZE(setfreq)) {
  1226. errorcode = ERR_CMDSIZE;
  1227. goto error;
  1228. }
  1229. mouse = find_mouse(cmd->idstr);
  1230. if (!mouse) {
  1231. errorcode = ERR_NOMOUSE;
  1232. goto error;
  1233. }
  1234. profile_id = be32_to_cpu(cmd->setfreq.profile_id);
  1235. if (profile_id == PROFILE_INVALID) {
  1236. if (!mouse->global_set_freq) {
  1237. errorcode = ERR_NOTSUPP;
  1238. goto error;
  1239. }
  1240. } else {
  1241. profile = find_mouse_profile(mouse, profile_id);
  1242. if (!profile) {
  1243. errorcode = ERR_FAIL;
  1244. goto error;
  1245. }
  1246. if (!profile->set_freq) {
  1247. errorcode = ERR_NOTSUPP;
  1248. goto error;
  1249. }
  1250. }
  1251. err = mouse->claim(mouse);
  1252. if (err) {
  1253. errorcode = ERR_CLAIM;
  1254. goto error;
  1255. }
  1256. if (profile_id == PROFILE_INVALID) {
  1257. err = mouse->global_set_freq(mouse,
  1258. be32_to_cpu(cmd->setfreq.new_frequency));
  1259. } else {
  1260. err = profile->set_freq(profile,
  1261. be32_to_cpu(cmd->setfreq.new_frequency));
  1262. }
  1263. mouse->release(mouse);
  1264. if (err) {
  1265. errorcode = ERR_FAIL;
  1266. goto error;
  1267. }
  1268. error:
  1269. send_u32(client, errorcode);
  1270. }
  1271. static void command_getprofiles(struct client *client, const struct command *cmd, unsigned int len)
  1272. {
  1273. struct razer_mouse *mouse;
  1274. struct razer_mouse_profile *list;
  1275. unsigned int i;
  1276. if (len < CMD_SIZE(getprofiles))
  1277. goto error;
  1278. mouse = find_mouse(cmd->idstr);
  1279. if (!mouse)
  1280. goto error;
  1281. list = mouse->get_profiles(mouse);
  1282. if (!list)
  1283. goto error;
  1284. send_u32(client, mouse->nr_profiles);
  1285. for (i = 0; i < mouse->nr_profiles; i++)
  1286. send_u32(client, list[i].nr);
  1287. return;
  1288. error:
  1289. send_u32(client, 0);
  1290. }
  1291. static void command_getprofname(struct client *client, const struct command *cmd, unsigned int len)
  1292. {
  1293. struct razer_mouse *mouse;
  1294. struct razer_mouse_profile *profile;
  1295. const razer_utf16_t *name;
  1296. razer_utf16_t namebuf[64] = { };
  1297. char asciibuf[64] = { };
  1298. if (len < CMD_SIZE(getprofname))
  1299. goto error;
  1300. mouse = find_mouse(cmd->idstr);
  1301. if (!mouse)
  1302. goto error;
  1303. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getprofname.profile_id));
  1304. if (!profile)
  1305. goto error;
  1306. if (profile->get_name) {
  1307. name = profile->get_name(profile);
  1308. } else {
  1309. snprintf(asciibuf, sizeof(asciibuf),
  1310. "Profile %u", profile->nr + 1);
  1311. razer_ascii_to_utf16(namebuf, ARRAY_SIZE(namebuf), asciibuf);
  1312. name = namebuf;
  1313. }
  1314. if (!name)
  1315. goto error;
  1316. send_utf16_string(client, name);
  1317. return;
  1318. error:
  1319. send_string(client, "");
  1320. }
  1321. static void command_setprofname(struct client *client, const struct command *cmd, unsigned int len)
  1322. {
  1323. struct razer_mouse *mouse;
  1324. struct razer_mouse_profile *profile;
  1325. razer_utf16_t namebuf[sizeof(cmd->setprofname.utf16be_name) / 2 + 1] = { };
  1326. unsigned int i;
  1327. uint32_t errorcode = ERR_NONE;
  1328. uint16_t value;
  1329. if (len < CMD_SIZE(setprofname)) {
  1330. errorcode = ERR_CMDSIZE;
  1331. goto error;
  1332. }
  1333. mouse = find_mouse(cmd->idstr);
  1334. if (!mouse) {
  1335. errorcode = ERR_NOMOUSE;
  1336. goto error;
  1337. }
  1338. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setprofname.profile_id));
  1339. if (!profile) {
  1340. errorcode = ERR_FAIL;
  1341. goto error;
  1342. }
  1343. if (!profile->set_name) {
  1344. errorcode = ERR_NOTSUPP;
  1345. goto error;
  1346. }
  1347. for (i = 0; i < ARRAY_SIZE(namebuf) - 1; i++) {
  1348. memcpy(&value, &((const uint16_t *)cmd->setprofname.utf16be_name)[i], sizeof(value));
  1349. namebuf[i] = be16_to_cpu(value);
  1350. }
  1351. if (profile->set_name(profile, namebuf)) {
  1352. errorcode = ERR_FAIL;
  1353. goto error;
  1354. }
  1355. error:
  1356. send_u32(client, errorcode);
  1357. }
  1358. static void command_getactiveprof(struct client *client, const struct command *cmd, unsigned int len)
  1359. {
  1360. struct razer_mouse *mouse;
  1361. struct razer_mouse_profile *activeprof;
  1362. if (len < CMD_SIZE(getactiveprof))
  1363. goto error;
  1364. mouse = find_mouse(cmd->idstr);
  1365. if (!mouse)
  1366. goto error;
  1367. activeprof = mouse->get_active_profile(mouse);
  1368. if (!activeprof)
  1369. goto error;
  1370. send_u32(client, activeprof->nr);
  1371. return;
  1372. error:
  1373. send_u32(client, 0xFFFFFFFF);
  1374. }
  1375. static void command_setactiveprof(struct client *client, const struct command *cmd, unsigned int len)
  1376. {
  1377. struct razer_mouse *mouse;
  1378. struct razer_mouse_profile *profile;
  1379. int err;
  1380. uint32_t errorcode = ERR_NONE;
  1381. if (len < CMD_SIZE(setactiveprof)) {
  1382. errorcode = ERR_CMDSIZE;
  1383. goto error;
  1384. }
  1385. mouse = find_mouse(cmd->idstr);
  1386. if (!mouse) {
  1387. errorcode = ERR_NOMOUSE;
  1388. goto error;
  1389. }
  1390. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setactiveprof.id));
  1391. if (!profile) {
  1392. errorcode = ERR_FAIL;
  1393. goto error;
  1394. }
  1395. err = mouse->claim(mouse);
  1396. if (err) {
  1397. errorcode = ERR_CLAIM;
  1398. goto error;
  1399. }
  1400. err = mouse->set_active_profile(mouse, profile);
  1401. if (err)
  1402. errorcode = ERR_FAIL;
  1403. mouse->release(mouse);
  1404. error:
  1405. send_u32(client, errorcode);
  1406. }
  1407. static void command_suppbuttons(struct client *client, const struct command *cmd, unsigned int len)
  1408. {
  1409. struct razer_mouse *mouse;
  1410. struct razer_button *list;
  1411. int count, i;
  1412. if (len < CMD_SIZE(suppbuttons))
  1413. goto error;
  1414. mouse = find_mouse(cmd->idstr);
  1415. if (!mouse || !mouse->supported_buttons)
  1416. goto error;
  1417. count = mouse->supported_buttons(mouse, &list);
  1418. if (count <= 0)
  1419. goto error;
  1420. send_u32(client, count);
  1421. for (i = 0; i < count; i++) {
  1422. send_u32(client, list[i].id);
  1423. send_string(client, list[i].name);
  1424. }
  1425. return;
  1426. error:
  1427. send_u32(client, 0);
  1428. }
  1429. static void command_suppbutfuncs(struct client *client, const struct command *cmd, unsigned int len)
  1430. {
  1431. struct razer_mouse *mouse;
  1432. struct razer_button_function *list;
  1433. int count, i;
  1434. if (len < CMD_SIZE(suppbutfuncs))
  1435. goto error;
  1436. mouse = find_mouse(cmd->idstr);
  1437. if (!mouse || !mouse->supported_button_functions)
  1438. goto error;
  1439. count = mouse->supported_button_functions(mouse, &list);
  1440. if (count <= 0)
  1441. goto error;
  1442. send_u32(client, count);
  1443. for (i = 0; i < count; i++) {
  1444. send_u32(client, list[i].id);
  1445. send_string(client, list[i].name);
  1446. }
  1447. return;
  1448. error:
  1449. send_u32(client, 0);
  1450. }
  1451. static void command_getbutfunc(struct client *client, const struct command *cmd, unsigned int len)
  1452. {
  1453. struct razer_mouse *mouse;
  1454. struct razer_mouse_profile *profile;
  1455. struct razer_button_function *func;
  1456. struct razer_button *button;
  1457. if (len < CMD_SIZE(getbutfunc))
  1458. goto error;
  1459. mouse = find_mouse(cmd->idstr);
  1460. if (!mouse)
  1461. goto error;
  1462. button = find_mouse_button(mouse, be32_to_cpu(cmd->getbutfunc.button_id));
  1463. if (!button)
  1464. goto error;
  1465. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->getbutfunc.profile_id));
  1466. if (!profile || !profile->get_button_function)
  1467. goto error;
  1468. func = profile->get_button_function(profile, button);
  1469. if (!func)
  1470. goto error;
  1471. send_u32(client, func->id);
  1472. send_string(client, func->name);
  1473. return;
  1474. error:
  1475. send_u32(client, 0);
  1476. send_string(client, "");
  1477. }
  1478. static void command_setbutfunc(struct client *client, const struct command *cmd, unsigned int len)
  1479. {
  1480. struct razer_mouse *mouse;
  1481. struct razer_mouse_profile *profile;
  1482. struct razer_button_function *func;
  1483. struct razer_button *button;
  1484. uint32_t errorcode = ERR_NONE;
  1485. int err;
  1486. if (len < CMD_SIZE(setbutfunc)) {
  1487. errorcode = ERR_CMDSIZE;
  1488. goto error;
  1489. }
  1490. mouse = find_mouse(cmd->idstr);
  1491. if (!mouse) {
  1492. errorcode = ERR_NOMOUSE;
  1493. goto error;
  1494. }
  1495. button = find_mouse_button(mouse, be32_to_cpu(cmd->setbutfunc.button_id));
  1496. if (!button) {
  1497. errorcode = ERR_FAIL;
  1498. goto error;
  1499. }
  1500. func = find_mouse_button_function(mouse, be32_to_cpu(cmd->setbutfunc.function_id));
  1501. if (!func) {
  1502. errorcode = ERR_FAIL;
  1503. goto error;
  1504. }
  1505. profile = find_mouse_profile(mouse, be32_to_cpu(cmd->setbutfunc.profile_id));
  1506. if (!profile || !profile->set_button_function) {
  1507. errorcode = ERR_FAIL;
  1508. goto error;
  1509. }
  1510. err = mouse->claim(mouse);
  1511. if (err) {
  1512. errorcode = ERR_CLAIM;
  1513. goto error;
  1514. }
  1515. err = profile->set_button_function(profile, button, func);
  1516. mouse->release(mouse);
  1517. if (err) {
  1518. errorcode = ERR_FAIL;
  1519. goto error;
  1520. }
  1521. error:
  1522. send_u32(client, errorcode);
  1523. }
  1524. static void command_suppaxes(struct client *client, const struct command *cmd, unsigned int len)
  1525. {
  1526. struct razer_mouse *mouse;
  1527. struct razer_axis *list;
  1528. int count, i;
  1529. if (len < CMD_SIZE(suppaxes))
  1530. goto error;
  1531. mouse = find_mouse(cmd->idstr);
  1532. if (!mouse || !mouse->supported_axes)
  1533. goto error;
  1534. count = mouse->supported_axes(mouse, &list);
  1535. if (count <= 0)
  1536. goto error;
  1537. send_u32(client, count);
  1538. for (i = 0; i < count; i++) {
  1539. send_u32(client, list[i].id);
  1540. send_string(client, list[i].name);
  1541. send_u32(client, list[i].flags);
  1542. }
  1543. return;
  1544. error:
  1545. send_u32(client, 0);
  1546. }
  1547. static void command_flashfw(struct client *client, const struct command *cmd, unsigned int len)
  1548. {
  1549. struct razer_mouse *mouse;
  1550. uint32_t image_size;
  1551. int err;
  1552. uint32_t errorcode = ERR_NONE;
  1553. char *image = NULL;
  1554. if (len < CMD_SIZE(flashfw)) {
  1555. errorcode = ERR_CMDSIZE;
  1556. goto error;
  1557. }
  1558. image_size = be32_to_cpu(cmd->flashfw.imagesize);
  1559. if (image_size > MAX_FIRMWARE_SIZE) {
  1560. errorcode = ERR_CMDSIZE;
  1561. goto error;
  1562. }
  1563. image = malloc(image_size);
  1564. if (!image) {
  1565. errorcode = ERR_NOMEM;
  1566. goto error;
  1567. }
  1568. err = recv_bulk(client, image, image_size);
  1569. if (err) {
  1570. errorcode = ERR_PAYLOAD;
  1571. goto error;
  1572. }
  1573. mouse = find_mouse(cmd->idstr);
  1574. if (!mouse) {
  1575. errorcode = ERR_NOMOUSE;
  1576. goto error;
  1577. }
  1578. if (!mouse->flash_firmware) {
  1579. errorcode = ERR_NOTSUPP;
  1580. goto error;
  1581. }
  1582. err = mouse->claim(mouse);
  1583. if (err) {
  1584. errorcode = ERR_CLAIM;
  1585. goto error;
  1586. }
  1587. err = mouse->flash_firmware(mouse, image, image_size,
  1588. RAZER_FW_FLASH_MAGIC);
  1589. mouse->release(mouse);
  1590. if (err) {
  1591. errorcode = ERR_FAIL;
  1592. goto error;
  1593. }
  1594. error:
  1595. send_u32(client, errorcode);
  1596. free(image);
  1597. }
  1598. static void command_claim(struct client *client, const struct command *cmd, unsigned int len)
  1599. {
  1600. struct razer_mouse *mouse;
  1601. uint32_t errorcode = ERR_NONE;
  1602. int err;
  1603. if (len < CMD_SIZE(claim)) {
  1604. errorcode = ERR_CMDSIZE;
  1605. goto error;
  1606. }
  1607. mouse = find_mouse(cmd->idstr);
  1608. if (!mouse) {
  1609. errorcode = ERR_NOMOUSE;
  1610. goto error;
  1611. }
  1612. err = mouse->claim(mouse);
  1613. if (err) {
  1614. errorcode = ERR_FAIL;
  1615. goto error;
  1616. }
  1617. error:
  1618. send_u32(client, errorcode);
  1619. }
  1620. static void command_release(struct client *client, const struct command *cmd, unsigned int len)
  1621. {
  1622. struct razer_mouse *mouse;
  1623. uint32_t errorcode = ERR_NONE;
  1624. if (len < CMD_SIZE(release)) {
  1625. errorcode = ERR_CMDSIZE;
  1626. goto error;
  1627. }
  1628. mouse = find_mouse(cmd->idstr);
  1629. if (!mouse) {
  1630. errorcode = ERR_NOMOUSE;
  1631. goto error;
  1632. }
  1633. mouse->release(mouse);
  1634. error:
  1635. send_u32(client, errorcode);
  1636. }
  1637. static void handle_received_command(struct client *client, const char *_cmd, unsigned int len)
  1638. {
  1639. const struct command *cmd = (const struct command *)_cmd;
  1640. if (len < COMMAND_HDR_SIZE)
  1641. return;
  1642. switch (cmd->hdr.id) {
  1643. case COMMAND_ID_GETREV:
  1644. send_u32(client, INTERFACE_REVISION);
  1645. break;
  1646. case COMMAND_ID_RESCANMICE:
  1647. command_rescanmice(client, cmd, len);
  1648. break;
  1649. case COMMAND_ID_GETMICE:
  1650. command_getmice(client, cmd, len);
  1651. break;
  1652. case COMMAND_ID_GETFWVER:
  1653. command_getfwver(client, cmd, len);
  1654. break;
  1655. case COMMAND_ID_SUPPFREQS:
  1656. command_suppfreqs(client, cmd, len);
  1657. break;
  1658. case COMMAND_ID_SUPPRESOL:
  1659. command_suppresol(client, cmd, len);
  1660. break;
  1661. case COMMAND_ID_SUPPDPIMAPPINGS:
  1662. command_suppdpimappings(client, cmd, len);
  1663. break;
  1664. case COMMAND_ID_CHANGEDPIMAPPING:
  1665. command_changedpimapping(client, cmd, len);
  1666. break;
  1667. case COMMAND_ID_GETDPIMAPPING:
  1668. command_getdpimapping(client, cmd, len);
  1669. break;
  1670. case COMMAND_ID_SETDPIMAPPING:
  1671. command_setdpimapping(client, cmd, len);
  1672. break;
  1673. case COMMAND_ID_GETLEDS:
  1674. command_getleds(client, cmd, len);
  1675. break;
  1676. case COMMAND_ID_SETLED:
  1677. command_setled(client, cmd, len);
  1678. break;
  1679. case COMMAND_ID_SETFREQ:
  1680. command_setfreq(client, cmd, len);
  1681. break;
  1682. case COMMAND_ID_GETPROFILES:
  1683. command_getprofiles(client, cmd, len);
  1684. break;
  1685. case COMMAND_ID_GETACTIVEPROF:
  1686. command_getactiveprof(client, cmd, len);
  1687. break;
  1688. case COMMAND_ID_SETACTIVEPROF:
  1689. command_setactiveprof(client, cmd, len);
  1690. break;
  1691. case COMMAND_ID_GETFREQ:
  1692. command_getfreq(client, cmd, len);
  1693. break;
  1694. case COMMAND_ID_SUPPBUTTONS:
  1695. command_suppbuttons(client, cmd, len);
  1696. break;
  1697. case COMMAND_ID_SUPPBUTFUNCS:
  1698. command_suppbutfuncs(client, cmd, len);
  1699. break;
  1700. case COMMAND_ID_GETBUTFUNC:
  1701. command_getbutfunc(client, cmd, len);
  1702. break;
  1703. case COMMAND_ID_SETBUTFUNC:
  1704. command_setbutfunc(client, cmd, len);
  1705. break;
  1706. case COMMAND_ID_SUPPAXES:
  1707. command_suppaxes(client, cmd, len);
  1708. break;
  1709. case COMMAND_ID_RECONFIGMICE:
  1710. command_reconfigmice(client, cmd, len);
  1711. break;
  1712. case COMMAND_ID_GETMOUSEINFO:
  1713. command_getmouseinfo(client, cmd, len);
  1714. break;
  1715. case COMMAND_ID_GETPROFNAME:
  1716. command_getprofname(client, cmd, len);
  1717. break;
  1718. case COMMAND_ID_SETPROFNAME:
  1719. command_setprofname(client, cmd, len);
  1720. break;
  1721. default:
  1722. /* Unknown command. */
  1723. break;
  1724. }
  1725. }
  1726. static void handle_received_privileged_command(struct client *client,
  1727. const char *_cmd, unsigned int len)
  1728. {
  1729. const struct command *cmd = (const struct command *)_cmd;
  1730. if (len < COMMAND_HDR_SIZE)
  1731. return;
  1732. switch (cmd->hdr.id) {
  1733. case COMMAND_PRIV_FLASHFW:
  1734. command_flashfw(client, cmd, len);
  1735. break;
  1736. case COMMAND_PRIV_CLAIM:
  1737. command_claim(client, cmd, len);
  1738. break;
  1739. case COMMAND_PRIV_RELEASE:
  1740. command_release(client, cmd, len);
  1741. break;
  1742. default:
  1743. /* Unknown command. */
  1744. break;
  1745. }
  1746. }
  1747. static int check_client_connections(void)
  1748. {
  1749. char command[COMMAND_MAX_SIZE + 1] = { 0, };
  1750. int nr;
  1751. struct client *client, *next;
  1752. int ret = 0;
  1753. for (client = clients; client; ) {
  1754. next = client->next;
  1755. nr = recv(client->fd, command, COMMAND_MAX_SIZE, 0);
  1756. if (nr < 0) {
  1757. ret = -1;
  1758. goto next_client;
  1759. }
  1760. if (nr == 0) {
  1761. disconnect_client(&clients, client);
  1762. goto next_client;
  1763. }
  1764. handle_received_command(client, command, nr);
  1765. next_client:
  1766. client = next;
  1767. }
  1768. return ret;
  1769. }
  1770. static int check_privileged_connections(void)
  1771. {
  1772. char command[COMMAND_MAX_SIZE + 1] = { 0, };
  1773. int nr;
  1774. struct client *client, *next;
  1775. int ret = 0;
  1776. for (client = privileged_clients; client; ) {
  1777. next = client->next;
  1778. nr = recv(client->fd, command, COMMAND_MAX_SIZE, 0);
  1779. if (nr < 0) {
  1780. ret = -1;
  1781. goto next_client;
  1782. }
  1783. if (nr == 0) {
  1784. disconnect_client(&privileged_clients, client);
  1785. goto next_client;
  1786. }
  1787. handle_received_privileged_command(client, command, nr);
  1788. next_client:
  1789. client = next;
  1790. }
  1791. return ret;
  1792. }
  1793. static void broadcast_notification(unsigned int notifyId, size_t size)
  1794. {
  1795. struct reply r;
  1796. struct client *client;
  1797. for (client = clients; client; client = client->next) {
  1798. r.hdr.id = notifyId;
  1799. send_reply(client, &r, size);
  1800. }
  1801. }
  1802. static void event_handler(enum razer_event event,
  1803. const struct razer_event_data *data)
  1804. {
  1805. switch (event) {
  1806. case RAZER_EV_MOUSE_ADD:
  1807. logdebug("Broadcasting mouse-add event\n");
  1808. broadcast_notification(NOTIFY_ID_NEWMOUSE,
  1809. REPLY_SIZE(notify_newmouse));
  1810. break;
  1811. case RAZER_EV_MOUSE_REMOVE:
  1812. logdebug("Broadcasting mouse-remove event\n");
  1813. broadcast_notification(NOTIFY_ID_DELMOUSE,
  1814. REPLY_SIZE(notify_delmouse));
  1815. break;
  1816. }
  1817. }
  1818. static int mainloop(void)
  1819. {
  1820. struct client *client;
  1821. int err;
  1822. int errcount = 0;
  1823. fd_set wait_fdset;
  1824. loginfo("Razer device service daemon\n");
  1825. setup_sighandler();
  1826. err = setup_environment();
  1827. if (err)
  1828. return 1;
  1829. err = razer_register_event_handler(event_handler);
  1830. if (err) {
  1831. logerr("Failed to register event handler\n");
  1832. cleanup_environment();
  1833. return 1;
  1834. }
  1835. mice = razer_rescan_mice();
  1836. while (1) {
  1837. FD_ZERO(&wait_fdset);
  1838. FD_SET(privsock, &wait_fdset);
  1839. FD_SET(ctlsock, &wait_fdset);
  1840. for (client = clients; client; client = client->next)
  1841. FD_SET(client->fd, &wait_fdset);
  1842. for (client = privileged_clients; client; client = client->next)
  1843. FD_SET(client->fd, &wait_fdset);
  1844. err = select(FD_SETSIZE, &wait_fdset, NULL, NULL, NULL);
  1845. if (err == 0) /* no fd ready */
  1846. err = -1;
  1847. if (err > 0) {
  1848. err = 0;
  1849. err |= check_control_socket(privsock, &privileged_clients);
  1850. err |= check_privileged_connections();
  1851. err |= check_control_socket(ctlsock, &clients);
  1852. err |= check_client_connections();
  1853. }
  1854. if (err) {
  1855. if (errcount >= 3)
  1856. razer_msleep(10);
  1857. else
  1858. errcount++;
  1859. } else
  1860. errcount = 0;
  1861. }
  1862. return 1;
  1863. }
  1864. static void usage(FILE *fd, int argc, char **argv)
  1865. {
  1866. fprintf(fd, "Razer device service daemon\n\n");
  1867. fprintf(fd, "Usage: %s [OPTIONS]\n", argv[0]);
  1868. fprintf(fd, "\n");
  1869. fprintf(fd, " -B|--background Fork into the background (daemon mode)\n");
  1870. fprintf(fd, " -c|--config PATH Use specified config file. Defaults to %s\n",
  1871. RAZER_DEFAULT_CONFIG);
  1872. fprintf(fd, " -C|--no-config Do not load the config file\n");
  1873. fprintf(fd, " -p|--no-profemu Disable profile emulation\n");
  1874. fprintf(fd, " -P|--pidfile PATH Create a PID-file\n");
  1875. fprintf(fd, " -l|--loglevel LEVEL Set the loglevel\n");
  1876. fprintf(fd, " 0=error, 1=warning, 2=info(default), 3=debug\n");
  1877. fprintf(fd, " -f|--force Force remove sockets before starting up\n");
  1878. fprintf(fd, "\n");
  1879. fprintf(fd, " -h|--help Print this help text\n");
  1880. }
  1881. static int parse_args(int argc, char **argv)
  1882. {
  1883. static struct option long_options[] = {
  1884. { "help", no_argument, 0, 'h', },
  1885. { "version", no_argument, 0, 'v', },
  1886. { "background", no_argument, 0, 'B', },
  1887. { "config", required_argument, 0, 'c', },
  1888. { "no-config", no_argument, 0, 'C', },
  1889. { "no-profemu", no_argument, 0, 'p', },
  1890. { "pidfile", required_argument, 0, 'P', },
  1891. { "loglevel", required_argument, 0, 'l', },
  1892. { "force", no_argument, 0, 'f', },
  1893. { 0, },
  1894. };
  1895. int c, idx;
  1896. while (1) {
  1897. c = getopt_long(argc, argv, "hvBc:CpP:l:f",
  1898. long_options, &idx);
  1899. if (c == -1)
  1900. break;
  1901. switch (c) {
  1902. case 'h':
  1903. case 'v':
  1904. usage(stdout, argc, argv);
  1905. return 1;
  1906. case 'B':
  1907. cmdargs.background = 1;
  1908. break;
  1909. case 'c':
  1910. cmdargs.configfile = optarg;
  1911. break;
  1912. case 'C':
  1913. cmdargs.configfile = "";
  1914. break;
  1915. case 'p':
  1916. cmdargs.no_profile_emu = 1;
  1917. break;
  1918. case 'P':
  1919. cmdargs.pidfile = optarg;
  1920. break;
  1921. case 'l':
  1922. if (sscanf(optarg, "%d", &cmdargs.loglevel) != 1) {
  1923. fprintf(stderr, "Failed to parse --loglevel argument\n");
  1924. return -1;
  1925. }
  1926. break;
  1927. case 'f':
  1928. cmdargs.force = 1;
  1929. break;
  1930. default:
  1931. return -1;
  1932. }
  1933. }
  1934. return 0;
  1935. }
  1936. int main(int argc, char **argv)
  1937. {
  1938. int err;
  1939. err = parse_args(argc, argv);
  1940. if (err > 0)
  1941. return 0;
  1942. if (err)
  1943. return err;
  1944. if (cmdargs.background) {
  1945. err = daemon(0, 0);
  1946. if (err) {
  1947. logerr("Failed to fork into the background: %s\n",
  1948. strerror(errno));
  1949. return 1;
  1950. }
  1951. /* Child process */
  1952. return mainloop();
  1953. }
  1954. return mainloop();
  1955. }