Advertisement
Guest User

g15_plugin_uinput.c

a guest
Apr 11th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 28.36 KB | None | 0 0
  1. /*
  2.     This file is part of g15daemon.
  3.  
  4.     g15daemon is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     g15daemon is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with g15daemon; if not, write to the Free Software
  16.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  
  18.     (c) 2006-2008 Mike Lampard, Philip Lawatsch, and others
  19.    
  20.     $Revision: 388 $ -  $Date: 2008-01-02 12:57:03 +1030 (Wed, 02 Jan 2008) $ $Author: mlampard $
  21.    
  22.     UINPUT key processing plugin.  receives events and sends keycodes to the linux kernel.
  23. */
  24. #include <pthread.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <signal.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34.  
  35. #include <config.h>
  36. #include <g15daemon.h>
  37. #include <pwd.h>
  38.  
  39. #ifdef HAVE_CONFIG_H
  40. #ifdef HAVE_LINUX_UINPUT_H
  41. #include <linux/input.h>
  42. #include <linux/uinput.h>
  43.  
  44. #include <g15daemon_client.h>
  45. #include <libg15.h>
  46.  
  47. int g15screen_fd;
  48. static int uinp_fd = -1;
  49. static config_section_t *uinput_cfg=NULL;
  50. static int map_Lkeys = 0;
  51. static unsigned int mled_state = G15_LED_M1;
  52. static int mkey_state = 0;
  53. /*mkey state 0,1 and 2 = M1, M2 and M3*/
  54.  
  55. #define GKEY_OFFSET 167
  56. #define MKEY_OFFSET 185
  57. #define LKEY_OFFSET 189
  58.  
  59. #define G15KEY_DOWN 1
  60. #define G15KEY_UP 0
  61. /*Set the M leds depending on which mode is on (incomplete code, what is & 0x07?)
  62. if(mkey_state == 0)
  63.     g15_send_cmd (mkey_state, G15DAEMON_MKEYLEDS,G15_LED_M1);
  64. else if(mkey_state == 1)
  65.     g15_send_cmd (mkey_state, G15DAEMON_MKEYLEDS,G15_LED_M2);
  66. else if(mkey_state == 2)
  67.     g15_send_cmd (mkey_state, G15DAEMON_MKEYLEDS,G15_LED_M3);
  68. */
  69.  
  70. static int g15_init_uinput(void *plugin_args) {
  71.    
  72.     int i=0;
  73.     char *custom_filename;
  74.     g15daemon_t *masterlist = (g15daemon_t*) plugin_args;
  75.     struct uinput_user_dev uinp;
  76.     static const char *uinput_device_fn[] = { "/dev/uinput", "/dev/input/uinput","/dev/misc/uinput",0};
  77.    
  78.     uinput_cfg = g15daemon_cfg_load_section(masterlist,"Keyboard OS Mapping (uinput)");
  79.     custom_filename = g15daemon_cfg_read_string(uinput_cfg, "device",(char*)uinput_device_fn[1]);
  80.     map_Lkeys=g15daemon_cfg_read_int(uinput_cfg, "Lkeys.mapped",0);
  81.    
  82.     seteuid(0);
  83.     setegid(0);
  84.     while (uinput_device_fn[i] && (uinp_fd = open(uinput_device_fn[i],O_RDWR))<0){
  85.         ++i;
  86.     }
  87.     if(uinp_fd<0) { /* try reading the users preference in the config */
  88.         uinp_fd = open(custom_filename,O_RDWR);
  89.     }
  90.     if(uinp_fd<0){
  91.         g15daemon_log(LOG_ERR,"Unable to open UINPUT device.  Please ensure the uinput driver is loaded into the kernel and that you have permission to open the device.");
  92.         return -1;
  93.     }
  94.     /* all other processes/threads should be seteuid nobody */
  95.      seteuid(masterlist->nobody->pw_uid);
  96.      setegid(masterlist->nobody->pw_gid);
  97.    
  98.    
  99.     memset(&uinp,0,sizeof(uinp));
  100.     strncpy(uinp.name, "G15 Extra Keys", UINPUT_MAX_NAME_SIZE);
  101.  
  102. #ifdef HAVE_UINPUT_USER_DEV_ID
  103.     uinp.id.version = 4;
  104.     uinp.id.bustype = BUS_USB;
  105. #else
  106.     uinp.idversion = 4;
  107.     uinp.idbus = BUS_USB;
  108. #endif
  109.  
  110.     ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
  111.  
  112.     for (i=0; i<256; ++i)
  113.         ioctl(uinp_fd, UI_SET_KEYBIT, i);
  114.  
  115.     write(uinp_fd, &uinp, sizeof(uinp));
  116.    
  117.     if (ioctl(uinp_fd, UI_DEV_CREATE))
  118.     {
  119.         g15daemon_log(LOG_ERR,"Unable to create UINPUT device.");
  120.         return -1;
  121.     }
  122.     return 0;
  123. }
  124.  
  125. void g15_exit_uinput(void *plugin_args){
  126.     ioctl(uinp_fd, UI_DEV_DESTROY);
  127.     close(uinp_fd);
  128. }
  129.  
  130. static void g15_uinput_keydown(unsigned char code)
  131. {
  132.     struct input_event event;
  133.     memset(&event, 0, sizeof(event));
  134.  
  135.     event.type = EV_KEY;
  136.     event.code = code;
  137.     event.value = G15KEY_DOWN;
  138.    
  139.     write (uinp_fd, &event, sizeof(event));
  140.  
  141.     /* Need to write sync event */
  142.     memset(&event, 0, sizeof(event));
  143.  
  144.     event.type = EV_SYN;
  145.     event.code = SYN_REPORT;
  146.     event.value = 0;
  147.  
  148.     write(uinp_fd, &event, sizeof(event));
  149.  
  150. }
  151.  
  152. static void g15_uinput_keyup(unsigned char code)
  153. {
  154.     struct input_event event;
  155.     memset(&event, 0, sizeof(event));
  156.  
  157.     event.type = EV_KEY;
  158.     event.code = code;
  159.     event.value = G15KEY_UP;
  160.    
  161.     write (uinp_fd, &event, sizeof(event));
  162.  
  163.     /* Need to write sync event */
  164.     memset(&event, 0, sizeof(event));
  165.  
  166.     event.type = EV_SYN;
  167.     event.code = SYN_REPORT;
  168.     event.value = 0;
  169.  
  170.     write(uinp_fd, &event, sizeof(event));
  171.  
  172. }
  173.  
  174.     void (*keyup)(unsigned char code) = &g15_uinput_keyup;
  175.     void (*keydown)(unsigned char code) = &g15_uinput_keydown;
  176. #else
  177.     void keyup(unsigned char code) { printf("Extra Keys not supported due to missing Uinput.h\n"); }
  178.     void keydown(unsigned char code) { printf("Extra Keys not supported due to missing Uinput.h\n"); }
  179. #endif
  180. #endif
  181.    
  182. /*The layout of the 'G' keys is now hard-coded here below. See /usr/include/linux/input.h for details on the keys you can use*/
  183. static void g15_process_keys(g15daemon_t *masterlist, unsigned int currentkeys, unsigned int lastkeys)
  184. {
  185.  
  186.     if(!(currentkeys & G15_KEY_LIGHT))
  187.     {
  188.     /*Send MLED state status*/
  189.     g15_send_cmd (g15screen_fd,G15DAEMON_MKEYLEDS,mled_state);
  190.     switch(mkey_state)
  191.     {
  192.         //M1 and the default layout (This one i use for commands while in X.
  193.         case 0:
  194.         {
  195.         /*set the LED*/
  196.             mled_state=G15_LED_M1;
  197. /*M1*/      /*G Keys 1-6 Unused*/
  198. /*M1*/      if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
  199. /*M1*/          {keydown(GKEY_OFFSET+1);}
  200. /*M1*/      else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
  201. /*M1*/          {keyup(GKEY_OFFSET+1);}
  202. /*M1*/     
  203. /*M1*/      if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
  204. /*M1*/          {keydown(GKEY_OFFSET+2);}
  205. /*M1*/      else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
  206. /*M1*/          {keyup(GKEY_OFFSET+2);}
  207. /*M1*/     
  208. /*M1*/      if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
  209. /*M1*/          {keydown(GKEY_OFFSET+3);}
  210. /*M1*/      else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
  211. /*M1*/          {keyup(GKEY_OFFSET+3);}
  212. /*M1*/     
  213. /*M1*/      if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
  214. /*M1*/          {keydown(GKEY_OFFSET+4);}
  215. /*M1*/      else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
  216. /*M1*/          {keyup(GKEY_OFFSET+4);}
  217. /*M1*/     
  218. /*M1*/      if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
  219. /*M1*/          {keydown(GKEY_OFFSET+5);}
  220. /*M1*/      else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
  221. /*M1*/          {keyup(GKEY_OFFSET+5);}
  222. /*M1*/     
  223. /*M1*/      if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G6))
  224. /*M1*/          {keydown(GKEY_OFFSET+6);}
  225. /*M1*/      else if(!(currentkeys & G15_KEY_G6) && (lastkeys & G15_KEY_G6))
  226. /*M1*/          {keyup(GKEY_OFFSET+6);}
  227. /*M1*/
  228. /*M1*/
  229. /*M1*/          /*G Keys 7-12* Switch workspaces 1-6 with Win(META)+F1-F6 keys.*/
  230. /*M1*/      if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G7))
  231. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F1);}
  232. /*M1*/      else if(!(currentkeys & G15_KEY_G7) && (lastkeys & G15_KEY_G7))
  233. /*M1*/          {keyup(KEY_F1);keyup(KEY_LEFTMETA);}
  234. /*M1*/     
  235. /*M1*/      if((currentkeys & G15_KEY_G8) && !(lastkeys & G15_KEY_G8))
  236. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F2);}
  237. /*M1*/      else if(!(currentkeys & G15_KEY_G8) && (lastkeys & G15_KEY_G8))
  238. /*M1*/          {keyup(KEY_F2);keyup(KEY_LEFTMETA);}
  239. /*M1*/
  240. /*M1*/      if((currentkeys & G15_KEY_G9) && !(lastkeys & G15_KEY_G9))
  241. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F3);}
  242. /*M1*/      else if(!(currentkeys & G15_KEY_G9) && (lastkeys & G15_KEY_G9))
  243. /*M1*/          {keyup(KEY_F3);keyup(KEY_LEFTMETA);}
  244. /*M1*/
  245. /*M1*/      if((currentkeys & G15_KEY_G10) && !(lastkeys & G15_KEY_G10))
  246. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F4);}
  247. /*M1*/      else if(!(currentkeys & G15_KEY_G10) && (lastkeys & G15_KEY_G10))
  248. /*M1*/          {keyup(KEY_F4);keyup(KEY_LEFTMETA);}
  249. /*M1*/
  250. /*M1*/      if((currentkeys & G15_KEY_G11) && !(lastkeys & G15_KEY_G11))
  251. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F5);}
  252. /*M1*/      else if(!(currentkeys & G15_KEY_G11) && (lastkeys & G15_KEY_G11))
  253. /*M1*/          {keyup(KEY_F5);keyup(KEY_LEFTMETA);}
  254. /*M1*/
  255. /*M1*/      if((currentkeys & G15_KEY_G12) && !(lastkeys & G15_KEY_G12))
  256. /*M1*/          {keydown(KEY_LEFTMETA);keydown(KEY_F6);}
  257. /*M1*/      else if(!(currentkeys & G15_KEY_G12) && (lastkeys & G15_KEY_G12))
  258. /*M1*/          {keyup(KEY_F6);keyup(KEY_LEFTMETA);}
  259. /*M1*/
  260. /*M1*/
  261. /*M1*/      /*G Keys 13-18 13(Empty) 16(Mouseclick(Not yet done)) 14(Alt+F4) 17(Ctrl+W or Close Tab) 15 and 18 (Next/Prev tab, Ctrl+PgUp/PgDn)*/
  262. /*M1*/      if((currentkeys & G15_KEY_G13) && !(lastkeys & G15_KEY_G13))
  263. /*M1*/          {keydown(GKEY_OFFSET+13);}
  264. /*M1*/      else if(!(currentkeys & G15_KEY_G13) && (lastkeys & G15_KEY_G13))
  265. /*M1*/          {keyup(GKEY_OFFSET+13);}
  266. /*M1*/     
  267. /*M1*/      if((currentkeys & G15_KEY_G14) && !(lastkeys & G15_KEY_G14))
  268. /*M1*/          {keydown(KEY_LEFTALT);keydown(KEY_F4);}
  269. /*M1*/      else if(!(currentkeys & G15_KEY_G14) && (lastkeys & G15_KEY_G14))
  270. /*M1*/          {keyup(KEY_F4);keyup(KEY_LEFTALT);}
  271. /*M1*/
  272. /*M1*/      if((currentkeys & G15_KEY_G15) && !(lastkeys & G15_KEY_G15))
  273. /*M1*/          {keydown(KEY_LEFTCTRL);keydown(KEY_PAGEUP);}
  274. /*M1*/      else if(!(currentkeys & G15_KEY_G15) && (lastkeys & G15_KEY_G15))
  275. /*M1*/          {keyup(KEY_PAGEUP);keyup(KEY_LEFTCTRL);}
  276. /*M1*/     
  277. /*M1*/      if((currentkeys & G15_KEY_G16) && !(lastkeys & G15_KEY_G16))
  278. /*M1*/          {keydown(GKEY_OFFSET+16);}
  279. /*M1*/      else if(!(currentkeys & G15_KEY_G16) && (lastkeys & G15_KEY_G16))
  280. /*M1*/          {keyup(GKEY_OFFSET+16);}
  281. /*M1*/     
  282. /*M1*/      if((currentkeys & G15_KEY_G17) && !(lastkeys & G15_KEY_G17))
  283. /*M1*/          {keydown(KEY_LEFTCTRL);keydown(KEY_W);}
  284. /*M1*/      else if(!(currentkeys & G15_KEY_G17) && (lastkeys & G15_KEY_G17))
  285. /*M1*/          {keyup(KEY_W);keyup(KEY_LEFTCTRL);}
  286. /*M1*/     
  287. /*M1*/      if((currentkeys & G15_KEY_G18) && !(lastkeys & G15_KEY_G18))
  288. /*M1*/          {keydown(KEY_LEFTCTRL);keydown(KEY_PAGEDOWN);}
  289. /*M1*/      else if(!(currentkeys & G15_KEY_G18) && (lastkeys & G15_KEY_G18))
  290. /*M1*/          {keyup(KEY_PAGEDOWN);keyup(KEY_LEFTCTRL);}
  291.         /*M1 END*/
  292.         break;
  293.         }
  294.         //M2 layout, This one i use for terminal shortcuts
  295.         case 1:
  296.         {
  297.         /*set the LED*/
  298.             mled_state=G15_LED_M2;
  299.         /*G Keys 1-6 unsued*/
  300. /*M2*/      if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
  301. /*M2*/          {keydown(GKEY_OFFSET+1);}
  302. /*M2*/      else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
  303. /*M2*/          {keyup(GKEY_OFFSET+1);}
  304. /*M2*/     
  305. /*M2*/      if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
  306. /*M2*/          {keydown(GKEY_OFFSET+2);}
  307. /*M2*/      else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
  308. /*M2*/          {keyup(GKEY_OFFSET+2);}
  309. /*M2*/     
  310. /*M2*/      if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
  311. /*M2*/          {keydown(GKEY_OFFSET+3);}
  312. /*M2*/      else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
  313. /*M2*/          {keyup(GKEY_OFFSET+3);}
  314. /*M2*/     
  315. /*M2*/      if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
  316. /*M2*/          {keydown(GKEY_OFFSET+4);}
  317. /*M2*/      else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
  318. /*M2*/          {keyup(GKEY_OFFSET+4);}
  319. /*M2*/     
  320. /*M2*/      if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
  321. /*M2*/          {keydown(GKEY_OFFSET+5);}
  322. /*M2*/      else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
  323. /*M2*/          {keyup(GKEY_OFFSET+5);}
  324. /*M2*/     
  325. /*M2*/      if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G6))
  326. /*M2*/          {keydown(GKEY_OFFSET+6);}
  327. /*M2*/      else if(!(currentkeys & G15_KEY_G6) && (lastkeys & G15_KEY_G6))
  328. /*M2*/          {keyup(GKEY_OFFSET+6);}
  329. /*M2*/
  330. /*M2*/
  331. /*M2*/          /*G Keys 7-12, Switch between terms 1-6 with Ctrl+Alt+F1-F6*/
  332. /*M2*/      if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G7))
  333. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F1);}
  334. /*M2*/      else if(!(currentkeys & G15_KEY_G7) && (lastkeys & G15_KEY_G7))
  335. /*M2*/          {keyup(KEY_F1);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  336. /*M2*/     
  337. /*M2*/      if((currentkeys & G15_KEY_G8) && !(lastkeys & G15_KEY_G8))
  338. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F2);}
  339. /*M2*/      else if(!(currentkeys & G15_KEY_G8) && (lastkeys & G15_KEY_G8))
  340. /*M2*/          {keyup(KEY_F2);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  341. /*M2*/
  342. /*M2*/      if((currentkeys & G15_KEY_G9) && !(lastkeys & G15_KEY_G9))
  343. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F3);}
  344. /*M2*/      else if(!(currentkeys & G15_KEY_G9) && (lastkeys & G15_KEY_G9))
  345. /*M2*/          {keyup(KEY_F3);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  346. /*M2*/
  347. /*M2*/      if((currentkeys & G15_KEY_G10) && !(lastkeys & G15_KEY_G10))
  348. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F4);}
  349. /*M2*/      else if(!(currentkeys & G15_KEY_G10) && (lastkeys & G15_KEY_G10))
  350. /*M2*/          {keyup(KEY_F4);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  351. /*M2*/
  352. /*M2*/      if((currentkeys & G15_KEY_G11) && !(lastkeys & G15_KEY_G11))
  353. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F5);}
  354. /*M2*/      else if(!(currentkeys & G15_KEY_G11) && (lastkeys & G15_KEY_G11))
  355. /*M2*/          {keyup(KEY_F5);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  356. /*M2*/
  357. /*M2*/      if((currentkeys & G15_KEY_G12) && !(lastkeys & G15_KEY_G12))
  358. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_LEFTALT);keydown(KEY_F6);}
  359. /*M2*/      else if(!(currentkeys & G15_KEY_G12) && (lastkeys & G15_KEY_G12))
  360. /*M2*/          {keyup(KEY_F6);keyup(KEY_LEFTALT);keyup(KEY_LEFTCTRL);}
  361. /*M2*/
  362. /*M2*/
  363. /*M2*/      /*G Keys 13-18 13(Unused) 16(Unused) 14(Ctrl+C) 17(Q+Enter) 15/18(Shift+PgUp/PgDn for Scroll Up/Dn)*/
  364. /*M2*/      if((currentkeys & G15_KEY_G13) && !(lastkeys & G15_KEY_G13))
  365. /*M2*/          {keydown(GKEY_OFFSET+13);}
  366. /*M2*/      else if(!(currentkeys & G15_KEY_G13) && (lastkeys & G15_KEY_G13))
  367. /*M2*/          {keyup(GKEY_OFFSET+13);}
  368. /*M2*/     
  369. /*M2*/      if((currentkeys & G15_KEY_G14) && !(lastkeys & G15_KEY_G14))
  370. /*M2*/          {keydown(KEY_LEFTCTRL);keydown(KEY_C);}
  371. /*M2*/      else if(!(currentkeys & G15_KEY_G14) && (lastkeys & G15_KEY_G14))
  372. /*M2*/          {keyup(KEY_C);keyup(KEY_LEFTCTRL);}
  373. /*M2*/
  374. /*M2*/      if((currentkeys & G15_KEY_G15) && !(lastkeys & G15_KEY_G15))
  375. /*M2*/          {keydown(KEY_LEFTSHIFT);keydown(KEY_PAGEUP);}
  376. /*M2*/      else if(!(currentkeys & G15_KEY_G15) && (lastkeys & G15_KEY_G15))
  377. /*M2*/          {keyup(KEY_PAGEUP);keyup(KEY_LEFTSHIFT);}
  378. /*M2*/     
  379. /*M2*/      if((currentkeys & G15_KEY_G16) && !(lastkeys & G15_KEY_G16))
  380. /*M2*/          {keydown(GKEY_OFFSET+16);}
  381. /*M2*/      else if(!(currentkeys & G15_KEY_G16) && (lastkeys & G15_KEY_G16))
  382. /*M2*/          {keyup(GKEY_OFFSET+16);}
  383. /*M2*/     
  384. /*M2*/      if((currentkeys & G15_KEY_G17) && !(lastkeys & G15_KEY_G17))
  385. /*M2*/          {keydown(KEY_Q);}
  386. /*M2*/      else if(!(currentkeys & G15_KEY_G17) && (lastkeys & G15_KEY_G17))
  387. /*M2*/          {keyup(KEY_Q);keydown(KEY_ENTER);keyup(KEY_ENTER);}
  388. /*M2*/     
  389. /*M2*/      if((currentkeys & G15_KEY_G18) && !(lastkeys & G15_KEY_G18))
  390. /*M2*/          {keydown(KEY_LEFTSHIFT);keydown(KEY_PAGEDOWN);}
  391. /*M2*/      else if(!(currentkeys & G15_KEY_G18) && (lastkeys & G15_KEY_G18))
  392. /*M2*/          {keyup(KEY_PAGEDOWN);keyup(KEY_LEFTSHIFT);}
  393.         /*M2 END*/
  394.  
  395.         break;
  396.         }
  397.         //M3 layout i don't use this yet.
  398.         case 2:
  399.         {
  400.         /*set the LED*/
  401.             mled_state=G15_LED_M3;
  402.         /*G Keys 1-6*/
  403. /*M3*/      if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
  404. /*M3*/          {keydown(GKEY_OFFSET+1);}
  405. /*M3*/      else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
  406. /*M3*/          {keyup(GKEY_OFFSET+1);}
  407. /*M3*/     
  408. /*M3*/      if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
  409. /*M3*/          {keydown(GKEY_OFFSET+2);}
  410. /*M3*/      else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
  411. /*M3*/          {keyup(GKEY_OFFSET+2);}
  412. /*M3*/     
  413. /*M3*/      if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
  414. /*M3*/          {keydown(GKEY_OFFSET+3);}
  415. /*M3*/      else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
  416. /*M3*/          {keyup(GKEY_OFFSET+3);}
  417. /*M3*/     
  418. /*M3*/      if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
  419. /*M3*/          {keydown(GKEY_OFFSET+4);}
  420. /*M3*/      else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
  421. /*M3*/          {keyup(GKEY_OFFSET+4);}
  422. /*M3*/     
  423. /*M3*/      if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
  424. /*M3*/          {keydown(GKEY_OFFSET+5);}
  425. /*M3*/      else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
  426. /*M3*/          {keyup(GKEY_OFFSET+5);}
  427. /*M3*/     
  428. /*M3*/      if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G6))
  429. /*M3*/          {keydown(GKEY_OFFSET+6);}
  430. /*M3*/      else if(!(currentkeys & G15_KEY_G6) && (lastkeys & G15_KEY_G6))
  431. /*M3*/          {keyup(GKEY_OFFSET+6);}
  432. /*M3*/
  433. /*M3*/
  434. /*M3*/          /*G Keys 7-12*/
  435. /*M3*/      if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G7))
  436. /*M3*/          {keydown(GKEY_OFFSET+7);}
  437. /*M3*/      else if(!(currentkeys & G15_KEY_G7) && (lastkeys & G15_KEY_G7))
  438. /*M3*/          {keyup(GKEY_OFFSET+7);}
  439. /*M3*/     
  440. /*M3*/      if((currentkeys & G15_KEY_G8) && !(lastkeys & G15_KEY_G8))
  441. /*M3*/          {keydown(GKEY_OFFSET+8);}
  442. /*M3*/      else if(!(currentkeys & G15_KEY_G8) && (lastkeys & G15_KEY_G8))
  443. /*M3*/          {keyup(GKEY_OFFSET+8);}
  444. /*M3*/
  445. /*M3*/      if((currentkeys & G15_KEY_G9) && !(lastkeys & G15_KEY_G9))
  446. /*M3*/          {keydown(GKEY_OFFSET+9);}
  447. /*M3*/      else if(!(currentkeys & G15_KEY_G9) && (lastkeys & G15_KEY_G9))
  448. /*M3*/          {keyup(GKEY_OFFSET+9);}
  449. /*M3*/
  450. /*M3*/      if((currentkeys & G15_KEY_G10) && !(lastkeys & G15_KEY_G10))
  451. /*M3*/          {keydown(GKEY_OFFSET+10);}
  452. /*M3*/      else if(!(currentkeys & G15_KEY_G10) && (lastkeys & G15_KEY_G10))
  453. /*M3*/          {keyup(GKEY_OFFSET+10);}
  454. /*M3*/
  455. /*M3*/      if((currentkeys & G15_KEY_G11) && !(lastkeys & G15_KEY_G11))
  456. /*M3*/          {keydown(GKEY_OFFSET+11);}
  457. /*M3*/      else if(!(currentkeys & G15_KEY_G11) && (lastkeys & G15_KEY_G11))
  458. /*M3*/          {keyup(GKEY_OFFSET+11);}
  459. /*M3*/
  460. /*M3*/      if((currentkeys & G15_KEY_G12) && !(lastkeys & G15_KEY_G12))
  461. /*M3*/          {keydown(GKEY_OFFSET+12);}
  462. /*M3*/      else if(!(currentkeys & G15_KEY_G12) && (lastkeys & G15_KEY_G12))
  463. /*M3*/          {keyup(GKEY_OFFSET+12);}
  464. /*M3*/
  465. /*M3*/
  466. /*M3*/      /*G Keys 13-18*/
  467. /*M3*/      if((currentkeys & G15_KEY_G13) && !(lastkeys & G15_KEY_G13))
  468. /*M3*/          {keydown(GKEY_OFFSET+13);}
  469. /*M3*/      else if(!(currentkeys & G15_KEY_G13) && (lastkeys & G15_KEY_G13))
  470. /*M3*/          {keyup(GKEY_OFFSET+13);}
  471. /*M3*/     
  472. /*M3*/      if((currentkeys & G15_KEY_G14) && !(lastkeys & G15_KEY_G14))
  473. /*M3*/          {keydown(GKEY_OFFSET+14);}
  474. /*M3*/      else if(!(currentkeys & G15_KEY_G14) && (lastkeys & G15_KEY_G14))
  475. /*M3*/          {keyup(GKEY_OFFSET+14);}
  476. /*M3*/
  477. /*M3*/      if((currentkeys & G15_KEY_G15) && !(lastkeys & G15_KEY_G15))
  478. /*M3*/          {keydown(GKEY_OFFSET+15);}
  479. /*M3*/      else if(!(currentkeys & G15_KEY_G15) && (lastkeys & G15_KEY_G15))
  480. /*M3*/          {keyup(GKEY_OFFSET+15);}
  481. /*M3*/     
  482. /*M3*/      if((currentkeys & G15_KEY_G16) && !(lastkeys & G15_KEY_G16))
  483. /*M3*/          {keydown(GKEY_OFFSET+16);}
  484. /*M3*/      else if(!(currentkeys & G15_KEY_G16) && (lastkeys & G15_KEY_G16))
  485. /*M3*/          {keyup(GKEY_OFFSET+16);}
  486. /*M3*/     
  487. /*M3*/      if((currentkeys & G15_KEY_G17) && !(lastkeys & G15_KEY_G17))
  488. /*M3*/          {keydown(GKEY_OFFSET+17);}
  489. /*M3*/      else if(!(currentkeys & G15_KEY_G17) && (lastkeys & G15_KEY_G17))
  490. /*M3*/          {keyup(GKEY_OFFSET+17);}
  491. /*M3*/     
  492. /*M3*/      if((currentkeys & G15_KEY_G18) && !(lastkeys & G15_KEY_G18))
  493. /*M3*/          {keydown(GKEY_OFFSET+18);}
  494. /*M3*/      else if(!(currentkeys & G15_KEY_G18) && (lastkeys & G15_KEY_G18))
  495. /*M3*/          {keyup(GKEY_OFFSET+18);}
  496.         /*M3 END*/
  497.  
  498.         break;
  499.         }
  500.         //If something goes wrong and no mkey is selected (should be impossible) layout
  501.         default:
  502.         {
  503.         /*G Keys 1-6*/
  504.         if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
  505.             {keydown(GKEY_OFFSET+1);}
  506.         else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
  507.             {keyup(GKEY_OFFSET+1);}
  508.        
  509.         if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
  510.             {keydown(GKEY_OFFSET+2);}
  511.         else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
  512.             {keyup(GKEY_OFFSET+2);}
  513.        
  514.         if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
  515.             {keydown(GKEY_OFFSET+3);}
  516.         else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
  517.             {keyup(GKEY_OFFSET+3);}
  518.        
  519.         if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
  520.             {keydown(GKEY_OFFSET+4);}
  521.         else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
  522.             {keyup(GKEY_OFFSET+4);}
  523.        
  524.         if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
  525.             {keydown(GKEY_OFFSET+5);}
  526.         else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
  527.             {keyup(GKEY_OFFSET+5);}
  528.        
  529.         if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G6))
  530.             {keydown(GKEY_OFFSET+6);}
  531.         else if(!(currentkeys & G15_KEY_G6) && (lastkeys & G15_KEY_G6))
  532.             {keyup(GKEY_OFFSET+6);}
  533.             /*G Keys 7-12*/
  534.         if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G7))
  535.             {keydown(GKEY_OFFSET+7);}
  536.         else if(!(currentkeys & G15_KEY_G7) && (lastkeys & G15_KEY_G7))
  537.             {keyup(GKEY_OFFSET+7);}
  538.        
  539.         if((currentkeys & G15_KEY_G8) && !(lastkeys & G15_KEY_G8))
  540.             {keydown(GKEY_OFFSET+8);}
  541.         else if(!(currentkeys & G15_KEY_G8) && (lastkeys & G15_KEY_G8))
  542.             {keyup(GKEY_OFFSET+8);}
  543.  
  544.         if((currentkeys & G15_KEY_G9) && !(lastkeys & G15_KEY_G9))
  545.             {keydown(GKEY_OFFSET+9);}
  546.         else if(!(currentkeys & G15_KEY_G9) && (lastkeys & G15_KEY_G9))
  547.             {keyup(GKEY_OFFSET+9);}
  548.  
  549.         if((currentkeys & G15_KEY_G10) && !(lastkeys & G15_KEY_G10))
  550.             {keydown(GKEY_OFFSET+10);}
  551.         else if(!(currentkeys & G15_KEY_G10) && (lastkeys & G15_KEY_G10))
  552.             {keyup(GKEY_OFFSET+10);}
  553.  
  554.         if((currentkeys & G15_KEY_G11) && !(lastkeys & G15_KEY_G11))
  555.             {keydown(GKEY_OFFSET+11);}
  556.         else if(!(currentkeys & G15_KEY_G11) && (lastkeys & G15_KEY_G11))
  557.             {keyup(GKEY_OFFSET+11);}
  558.  
  559.         if((currentkeys & G15_KEY_G12) && !(lastkeys & G15_KEY_G12))
  560.             {keydown(GKEY_OFFSET+12);}
  561.         else if(!(currentkeys & G15_KEY_G12) && (lastkeys & G15_KEY_G12))
  562.             {keyup(GKEY_OFFSET+12);}
  563.         /*G Keys 13-18*/
  564.         if((currentkeys & G15_KEY_G13) && !(lastkeys & G15_KEY_G13))
  565.             {keydown(GKEY_OFFSET+13);}
  566.         else if(!(currentkeys & G15_KEY_G13) && (lastkeys & G15_KEY_G13))
  567.             {keyup(GKEY_OFFSET+13);}
  568.        
  569.         if((currentkeys & G15_KEY_G14) && !(lastkeys & G15_KEY_G14))
  570.             {keydown(GKEY_OFFSET+14);}
  571.         else if(!(currentkeys & G15_KEY_G14) && (lastkeys & G15_KEY_G14))
  572.             {keyup(GKEY_OFFSET+14);}
  573.  
  574.         if((currentkeys & G15_KEY_G15) && !(lastkeys & G15_KEY_G15))
  575.             {keydown(GKEY_OFFSET+15);}
  576.         else if(!(currentkeys & G15_KEY_G15) && (lastkeys & G15_KEY_G15))
  577.             {keyup(GKEY_OFFSET+15);}
  578.        
  579.         if((currentkeys & G15_KEY_G16) && !(lastkeys & G15_KEY_G16))
  580.             {keydown(GKEY_OFFSET+16);}
  581.         else if(!(currentkeys & G15_KEY_G16) && (lastkeys & G15_KEY_G16))
  582.             {keyup(GKEY_OFFSET+16);}
  583.        
  584.         if((currentkeys & G15_KEY_G17) && !(lastkeys & G15_KEY_G17))
  585.             {keydown(GKEY_OFFSET+17);}
  586.         else if(!(currentkeys & G15_KEY_G17) && (lastkeys & G15_KEY_G17))
  587.             {keyup(GKEY_OFFSET+17);}
  588.        
  589.         if((currentkeys & G15_KEY_G18) && !(lastkeys & G15_KEY_G18))
  590.             {keydown(GKEY_OFFSET+18);}
  591.         else if(!(currentkeys & G15_KEY_G18) && (lastkeys & G15_KEY_G18))
  592.             {keyup(GKEY_OFFSET+18);}
  593.         /*END*/
  594.  
  595.         break;
  596.         }
  597.     }
  598.    
  599.         /* 'M' keys... */
  600.    
  601.         if((currentkeys & G15_KEY_M1) && !(lastkeys & G15_KEY_M1)){
  602.             keydown(MKEY_OFFSET);
  603.         mkey_state = 0;
  604.     }
  605.         else if(!(currentkeys & G15_KEY_M1) && (lastkeys & G15_KEY_M1))
  606.             keyup(MKEY_OFFSET);
  607.    
  608.         if((currentkeys & G15_KEY_M2) && !(lastkeys & G15_KEY_M2)){
  609.             keydown(MKEY_OFFSET+1);
  610.         mkey_state = 1;
  611.     }
  612.         else if(!(currentkeys & G15_KEY_M2) && (lastkeys & G15_KEY_M2))
  613.             keyup(MKEY_OFFSET+1);
  614.    
  615.         if((currentkeys & G15_KEY_M3) && !(lastkeys & G15_KEY_M3)){
  616.             keydown(MKEY_OFFSET+2);
  617.         mkey_state = 2;
  618.         }
  619.         else if(!(currentkeys & G15_KEY_M3) && (lastkeys & G15_KEY_M3))
  620.             keyup(MKEY_OFFSET+2);
  621.    
  622.         if((currentkeys & G15_KEY_MR) && !(lastkeys & G15_KEY_MR))
  623.             keydown(MKEY_OFFSET+3);
  624.         else if(!(currentkeys & G15_KEY_MR) && (lastkeys & G15_KEY_MR))
  625.             keyup(MKEY_OFFSET+3);
  626.  
  627.        
  628.         if(map_Lkeys){
  629.             /* 'L' keys...  */
  630.             if((currentkeys & G15_KEY_L1) && !(lastkeys & G15_KEY_L1))
  631.                 keydown(LKEY_OFFSET);
  632.             else if(!(currentkeys & G15_KEY_L1) && (lastkeys & G15_KEY_L1))
  633.                 keyup(LKEY_OFFSET);
  634.    
  635.             if((currentkeys & G15_KEY_L2) && !(lastkeys & G15_KEY_L2))
  636.                 keydown(LKEY_OFFSET+1);
  637.             else if(!(currentkeys & G15_KEY_L2) && (lastkeys & G15_KEY_L2))
  638.                 keyup(LKEY_OFFSET+1);
  639.    
  640.             if((currentkeys & G15_KEY_L3) && !(lastkeys & G15_KEY_L3))
  641.                 keydown(LKEY_OFFSET+2);
  642.             else if(!(currentkeys & G15_KEY_L3) && (lastkeys & G15_KEY_L3))
  643.                 keyup(LKEY_OFFSET+2);
  644.    
  645.             if((currentkeys & G15_KEY_L4) && !(lastkeys & G15_KEY_L4))
  646.                 keydown(LKEY_OFFSET+3);
  647.             else if(!(currentkeys & G15_KEY_L4) && (lastkeys & G15_KEY_L4))
  648.                 keyup(LKEY_OFFSET+3);
  649.    
  650.             if((currentkeys & G15_KEY_L5) && !(lastkeys & G15_KEY_L5))
  651.                 keydown(LKEY_OFFSET+4);
  652.             else if(!(currentkeys & G15_KEY_L5) && (lastkeys & G15_KEY_L5))
  653.                 keyup(LKEY_OFFSET+4);
  654.         }
  655.  
  656.     }
  657.  
  658.  
  659.      else
  660.      {
  661.         // G15_KEY_LIGHT - Key modifier for Logitech G510 Media Keys implementation
  662.  
  663.         // XF86AudioPlay
  664.         if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
  665.             keydown(KEY_PLAYPAUSE);
  666.         else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
  667.             keyup(KEY_PLAYPAUSE);
  668.  
  669.         // XF86AudioStop
  670.         if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
  671.             keydown(KEY_STOPCD);
  672.         else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
  673.             keyup(KEY_STOPCD);
  674.  
  675.         // XF86AudioPrev
  676.         if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
  677.             keydown(KEY_PREVIOUSSONG);
  678.         else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
  679.             keyup(KEY_PREVIOUSSONG);
  680.  
  681.         // XF86AudioNext
  682.         if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
  683.             keydown(KEY_NEXTSONG);
  684.         else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
  685.             keyup(KEY_NEXTSONG);
  686.  
  687.         // XF86AudioMute
  688.         if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
  689.             keydown(KEY_MUTE);
  690.         else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
  691.             keyup(KEY_MUTE);
  692.  
  693.         // XF86AudioRaise/LowerVolume
  694.         if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G7))
  695.             keydown(KEY_VOLUMEUP);
  696.         else if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G6))
  697.             keydown(KEY_VOLUMEDOWN);
  698.         else if((!(currentkeys & G15_KEY_G6) || !(currentkeys & G15_KEY_G7)) && ((lastkeys & G15_KEY_G7) || (lastkeys & G15_KEY_G6))){
  699.         keyup(KEY_VOLUMEUP);
  700.         keyup(KEY_VOLUMEDOWN); 
  701.         }
  702.     }
  703. }
  704.  
  705.  
  706.  
  707.  
  708. static int keyevents(plugin_event_t *myevent) {
  709.     lcd_t *lcd = (lcd_t*) myevent->lcd;
  710.     static int lastkeys;
  711.     switch (myevent->event)
  712.     {
  713.         case G15_EVENT_KEYPRESS:{
  714.             g15_process_keys(lcd->masterlist, myevent->value,lastkeys);
  715.             lastkeys = myevent->value;
  716.             break;
  717.         }
  718.         case G15_EVENT_VISIBILITY_CHANGED:
  719.         case G15_EVENT_USER_FOREGROUND:
  720.     case G15_EVENT_MLED:
  721.         case G15_EVENT_BACKLIGHT:
  722.         case G15_EVENT_CONTRAST:
  723.         case G15_EVENT_REQ_PRIORITY:
  724.         case G15_EVENT_CYCLE_PRIORITY:
  725.         default:
  726.             break;
  727.     }
  728.     return G15_PLUGIN_OK;
  729. }
  730.  
  731.  
  732.     /* if no exitfunc or eventhandler, member should be NULL */
  733. plugin_info_t g15plugin_info[] = {
  734.         /* TYPE, name, initfunc, updatefreq, exitfunc, eventhandler, initfunc */
  735.    {G15_PLUGIN_CORE_OS_KB, "Linux UINPUT Keyboard Output"   , NULL, 500, (void*)g15_exit_uinput, (void*)keyevents, (void*)g15_init_uinput},
  736.    {G15_PLUGIN_NONE,               ""                   , NULL,   0,            NULL,            NULL,           NULL}
  737. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement