SHARE
TWEET

input-event-device switch for vt-console xorg dualscreen

a guest Aug 30th, 2015 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff -ruN evemu-2.2.0/src/evemu.c evemu-2.2.0-switchpatch/src/evemu.c
  2. --- evemu-2.2.0/src/evemu.c     2015-08-30 17:37:39.155134817 +0100
  3. +++ evemu-2.2.0-switchpatch/src/evemu.c 2015-08-30 17:37:39.152134816 +0100
  4. @@ -133,6 +133,31 @@
  5.         return dev;
  6.  }
  7.  
  8. +struct evemu_device *evswitch_enumerate(FILE *fp) {
  9. +       int ret = -ENOMEM;
  10. +       struct evemu_device *dev = calloc(1, sizeof(struct evemu_device));
  11. +       dev = evemu_new(NULL);
  12. +       if (!dev)
  13. +               goto out;
  14. +       ret = evemu_read(dev, fp);
  15. +       if (ret <= 0)
  16. +               goto out;
  17. +
  18. +       if (strlen(evemu_get_name(dev)) == 0) {
  19. +               char name[64];
  20. +               sprintf(name, "evemu-%d", getpid());
  21. +               evemu_set_name(dev, name);
  22. +       }
  23. +
  24. +       ret = evemu_create_managed(dev);
  25. +       if (ret < 0)
  26. +               goto out;
  27. +       return dev;
  28. +out:
  29. +       evemu_delete(dev);
  30. +
  31. +       return ret;
  32. +}
  33.  void evemu_delete(struct evemu_device *dev)
  34.  {
  35.         if (dev->uidev)
  36. @@ -777,6 +802,78 @@
  37.                 }
  38.         }
  39.  
  40. +       return 0;
  41. +}
  42. +
  43. +int evemu_forward(int fdc, int fdo, int fdo2, int fdo3, int fd, int ms)
  44. +{
  45. +       struct pollfd fds[2] ;
  46. +       fds[0].fd = fd;
  47. +       fds[0].events = POLLIN;
  48. +       fds[0].revents = 0;
  49. +       fds[1].fd = fdc;
  50. +       fds[1].events = POLLIN;
  51. +       fds[1].revents = 0;
  52. +       struct input_event ev;
  53. +       char buft[10];
  54. +       char cokb = '1';
  55. +       char xokb = '2';
  56. +       char wikb = '3';
  57. +       int i;
  58. +       char o;
  59. +       o = '1';
  60. +       int ret;
  61. +       int reto;
  62. +       long offset = 0;
  63. +       while (poll(fds, 2, ms) > 0) {
  64. +               if(fds[1].revents & POLLIN) {
  65. +                       error(WARNING, "control failure\n");
  66. +                       while ( i = read( fdc, buft, 1 ) > 0 ) {
  67. +                               char buf = buft[0] ;
  68. +                               error(WARNING, "operator cant controlling\n");
  69. +                               if (fdo3==NULL) {
  70. +                                       if ( buf==cokb || buf==xokb) {
  71. +                                               o = buf;
  72. +                                               error(WARNING, "operator appears to be controlling\n");
  73. +                                       } else {
  74. +                                               error(WARNING, "operator appears to be trolling\n");
  75. +                                               }
  76. +                               } else {
  77. +                                       if ( buf==cokb || buf==xokb || buf==wikb) {
  78. +                                               o = buf;
  79. +                                               error(WARNING, "operator appears to be controlling\n");
  80. +                                       } else {
  81. +                                               error(WARNING, "operator appears to be trolling\n");
  82. +                                       }
  83. +                               }
  84. +                       }
  85. +               }      
  86. +               if(fds[0].revents & POLLIN) {
  87. +                       SYSCALL(ret = read(fd, &ev, sizeof(ev)));
  88. +                       if (ret < 0)
  89. +                               return ret;
  90. +                       if (ret == sizeof(ev)) {
  91. +                              
  92. +                               switch(o) {
  93. +                                       case '1':
  94. +                                               SYSCALL(reto = write(fdo, &ev, sizeof(ev)));
  95. +                                               break;
  96. +                                       case '2':
  97. +                                               SYSCALL(reto = write(fdo2, &ev, sizeof(ev)));
  98. +                                               break;
  99. +                                       case '3':
  100. +                                               SYSCALL(reto = write(fdo3, &ev, sizeof(ev)));
  101. +                                               break;
  102. +                                       default:
  103. +                                               /* If operator is other than +, -, * or /, error message is shown */
  104. +                                               error(WARNING, "operator appears to be an empty file\n");
  105. +                                               break;
  106. +                               }
  107. +      
  108. +                       }
  109. +               }
  110. +       }
  111. +
  112.         return 0;
  113.  }
  114.  
  115. diff -ruN evemu-2.2.0/src/evemu.h evemu-2.2.0-switchpatch/src/evemu.h
  116. --- evemu-2.2.0/src/evemu.h     2015-08-30 17:37:39.155134817 +0100
  117. +++ evemu-2.2.0-switchpatch/src/evemu.h 2015-08-30 17:37:39.152134816 +0100
  118. @@ -64,6 +64,20 @@
  119.   */
  120.  struct evemu_device *evemu_new(const char *name);
  121.  
  122. +
  123. +
  124. +/**
  125. + * evswitch_enumerate() - allocate a new evemu device
  126. + * @fp: input device desctiptor
  127. + * <always-NULL> wanted input device name (or NULL to leave empty)
  128. + *
  129. + * This function allocates a new evemu device structure and
  130. + * initializes all fields to zero. If name is non-null and the length
  131. + * is sane, it is copied to the device name.
  132. + * Then initializes dthe devise from the supplied file.
  133. + * Returns NULL in case of memory failure.
  134. + */
  135. +struct evemu_device *evswitch_enumerate(FILE *fp);
  136.  /**
  137.   * evemu_delete() - free and allocated evemu device
  138.   * @dev: the device to free
  139. @@ -388,6 +402,19 @@
  140.   */
  141.  int evemu_record(FILE *fp, int fd, int ms);
  142.  
  143. +/**
  144. + * evswitch_forward() - read events directly from a kernel device
  145. + * @fdo: file descriptor of kernel device to output from
  146. + * @fd: file descriptor of kernel device to read from
  147. + * @ms: maximum time to wait for an event to appear before reading (ms)
  148. + *
  149. + * Continuously reads events from one kernel device and writes out
  150. + * reiugn the other. The function terminates after ms milliseconds of
  151. + * inactivity.
  152. + *
  153. + * Returns zero if successful, negative error otherwise.
  154. + */
  155. +int evemu_forward(int fdc, int fdo, int fdo2, int dfo3, int fd, int ms);
  156.  
  157.  /**
  158.   * evemu_play_one() - play one event to kernel device
  159. diff -ruN evemu-2.2.0/src/libevemu.ver evemu-2.2.0-switchpatch/src/libevemu.ver
  160. --- evemu-2.2.0/src/libevemu.ver        2015-08-30 17:37:39.155134817 +0100
  161. +++ evemu-2.2.0-switchpatch/src/libevemu.ver    2015-08-30 17:37:39.152134816 +0100
  162. @@ -29,6 +29,7 @@
  163.      evemu_read_event;
  164.      evemu_read_event_realtime;
  165.      evemu_record;
  166. +    evemu_forward;
  167.      evemu_set_abs_flat;
  168.      evemu_set_abs_fuzz;
  169.      evemu_set_abs_maximum;
  170. @@ -41,6 +42,7 @@
  171.      evemu_set_name;
  172.      evemu_write;
  173.      evemu_write_event;
  174. +    evswitch_enumerate;
  175.  
  176.    local:
  177.      *;
  178. diff -ruN evemu-2.2.0/tools/Makefile.am evemu-2.2.0-switchpatch/tools/Makefile.am
  179. --- evemu-2.2.0/tools/Makefile.am       2015-08-30 17:37:39.155134817 +0100
  180. +++ evemu-2.2.0-switchpatch/tools/Makefile.am   2015-08-30 17:41:22.261141511 +0100
  181. @@ -6,7 +6,8 @@
  182.         evemu-device \
  183.         evemu-record \
  184.         evemu-play \
  185. -       evemu-event
  186. +       evemu-event \
  187. +       evemu-switch
  188.  
  189.  AM_CPPFLAGS =-I$(top_srcdir)/src/
  190.  
  191. diff -ruN evemu-2.2.0/tools/evemu-switch.c evemu-2.2.0-switchpatch/tools/evemu-switch.c
  192. --- evemu-2.2.0/tools/evemu-switch.c    1970-01-01 01:00:00.000000000 +0100
  193. +++ evemu-2.2.0-switchpatch/tools/evemu-switch.c        2015-08-30 17:37:39.152134816 +0100
  194. @@ -0,0 +1,394 @@
  195. +/*****************************************************************************
  196. + *
  197. + * evemu - Kernel device emulation
  198. + *
  199. + * Copyright (C) 2010-2012 Canonical Ltd.
  200. + *
  201. + * This program is free software: you can redistribute it and/or modify it
  202. + * under the terms of the GNU General Public License version 3 as published
  203. + * by the Free Software Foundation.
  204. + *
  205. + * This program is distributed in the hope that it will be useful, but
  206. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  207. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  208. + * General Public License for more details.
  209. + *
  210. + * You should have received a copy of the GNU General Public License along
  211. + * with this program.  If not, see <http://www.gnu.org/licenses/>.
  212. + *
  213. + * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
  214. + *
  215. + * Permission is hereby granted, free of charge, to any person obtaining a
  216. + * copy of this software and associated documentation files (the "Software"),
  217. + * to deal in the Software without restriction, including without limitation
  218. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  219. + * and/or sell copies of the Software, and to permit persons to whom the
  220. + * Software is furnished to do so, subject to the following conditions:
  221. + *
  222. + * The above copyright notice and this permission notice (including the next
  223. + * paragraph) shall be included in all copies or substantial portions of the
  224. + * Software.
  225. + *
  226. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  227. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  228. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  229. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  230. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  231. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  232. + * DEALINGS IN THE SOFTWARE.
  233. + *
  234. + ****************************************************************************/
  235. +
  236. +#define _GNU_SOURCE
  237. +#include "evemu.h"
  238. +#include <stdio.h>
  239. +#include <stdlib.h>
  240. +#include <fcntl.h>
  241. +#include <string.h>
  242. +#include <unistd.h>
  243. +#include <signal.h>
  244. +#include <time.h>
  245. +#include <sys/stat.h>
  246. +#include "find_event_devices.h"
  247. +
  248. +#define INFINITE -1
  249. +
  250. +       int fd;
  251. +       int fdo;
  252. +       int fdo2;
  253. +       int fdo3 = NULL;
  254. +       int fdc;
  255. +       FILE *fp;
  256. +       FILE *fp2;
  257. +    FILE *fp3;
  258. +    int devout;
  259. +       struct evemu_device *dev;
  260. +       struct evemu_device *dev1;
  261. +       struct evemu_device *dev2;
  262. +       struct evemu_device *dev3;
  263. +       char *myfifo;
  264. +
  265. +/*    
  266. +static int describe_device(int fd)
  267. +{
  268. +       struct evemu_device *dev;
  269. +       int ret = -ENOMEM;
  270. +
  271. +       dev = evemu_new(NULL);
  272. +       if (!dev)
  273. +               goto out;
  274. +       ret = evemu_extract(dev, fd);
  275. +       if (ret)
  276. +               goto out;
  277. +
  278. +       evemu_write(dev, stdout);
  279. +out:
  280. +       evemu_delete(dev);
  281. +       return ret;
  282. +}
  283. +*/
  284. +
  285. +static void handler (int sig __attribute__((unused)))
  286. +{
  287. +       ioctl(fd, EVIOCGRAB, (void*)0);
  288. +       close(fdo);
  289. +       close(fdo2);
  290. +       close(fdo3);
  291. +       close(fd);
  292. +       close(fdc);
  293. +       unlink(myfifo);
  294. +       evemu_destroy(dev1);
  295. +       evemu_delete(dev1);
  296. +       evemu_destroy(dev2);
  297. +       evemu_delete(dev2);
  298. +       evemu_destroy(dev3);
  299. +       evemu_delete(dev3);
  300. +}
  301. +
  302. +enum mode {
  303. +       EVEMU_RECORD,
  304. +       EVEMU_DESCRIBE
  305. +};
  306. +
  307. +int main(int argc, char *argv[])
  308. +{
  309. +       enum mode mode = EVEMU_RECORD;
  310. +
  311. +       struct sigaction act;
  312. +       char *prgm_name = program_invocation_short_name;
  313. +       int nrouts = 0;
  314. +    char *fifc = "_control";
  315. +    int ret = 1;
  316. +    
  317. +    
  318. +       if (prgm_name && (strcmp(prgm_name, "evemu-describe") == 0 ||
  319. +                       /* when run directly from the sources (not installed) */
  320. +                       strcmp(prgm_name, "lt-evemu-describe") == 0))
  321. +               mode = EVEMU_DESCRIBE;
  322. +/*
  323. +       device = (argc < 2) ? find_event_devices() : strdup(argv[1]);
  324. +
  325. +       if (device == NULL) {
  326. +               fprintf(stderr, "Usage: %s <device> [output file]\n", argv[0]);
  327. +               return -1;
  328. +       }
  329. +*/    
  330. +       if (argc < 4) {
  331. +               fprintf(stderr, "Usage: %s <input-device> <output-descriptor1> <output-descriptor2> [output-descriptor3]\n", argv[0]);
  332. +               return -1;
  333. +       }
  334. +       switch(argc) {
  335. +               case 4:
  336. +                       nrouts = 2;
  337. +                       break;
  338. +               case 5:
  339. +                       nrouts = 3;
  340. +                       break;
  341. +               default:
  342. +                       nrouts = 3;
  343. +                       /* If operator is other than +, -, * or /, error message is shown */
  344. +                       fprintf(stderr, "operator appears to be overly optimistic\n");
  345. +                       fprintf(stderr, "args %d \n",argc);
  346. +                       fprintf(stderr, "%s  %s %s %s %s",argv[0], argv[1], argv[2] ,argv[3],argv[4]);
  347. +                       /*return -1;*/
  348. +                       break;
  349. +       }
  350. +
  351. +/*
  352. +       if (argc != 5) {
  353. +               fprintf(stderr, "Usage: %s <input-device> <output-device1> <output-device2> <output-device3>\n", argv[0]);
  354. +               fprintf(stderr, "\n");
  355. +               fprintf(stderr, "Event data is read from standard input.\n");
  356. +               return -1;
  357. +       }
  358. +*/    
  359. +
  360. +       fp = fopen(argv[2], "r");
  361. +       if (!fp) {
  362. +               fprintf(stderr, "error: could not open file %s\n",argv[2]);
  363. +               return -1;
  364. +       }
  365. +       fp2 = fopen(argv[3], "r");
  366. +       if (!fp2) {
  367. +               fprintf(stderr, "error: could not open file %s\n",argv[3]);
  368. +               goto error1;
  369. +       }
  370. +       if (nrouts == 3) {
  371. +               fp3 = fopen(argv[4], "r");
  372. +               if (!fp3) {
  373. +                       fprintf(stderr, "error: could not open file %s\n",argv[4]);
  374. +                       goto error2;
  375. +               }
  376. +       }
  377. +      
  378. +       dev1 = evswitch_enumerate(fp);
  379. +       if (dev1 <= 0) {
  380. +               fprintf(stderr, "can not enumerate device1\n");
  381. +               goto error3;
  382. +       }
  383. +       const char *device_node1 = evemu_get_devnode(dev1);
  384. +       if (!device_node1)
  385. +       {
  386. +               fprintf(stderr, "can not determine device node1\n");
  387. +               goto error3;
  388. +       }      
  389. +      
  390. +       dev2 = evswitch_enumerate(fp2);
  391. +       if (dev2 <= 0) {
  392. +               fprintf(stderr, "can not enumerate device2\n");
  393. +               goto error4;
  394. +       }      
  395. +       const char *device_node2 = evemu_get_devnode(dev2);
  396. +       if (!device_node2)
  397. +       {
  398. +               fprintf(stderr, "can not determine device node2\n");
  399. +               goto error5;
  400. +       }
  401. +       close(*fp);
  402. +       close(*fp2);
  403. +
  404. +       if (nrouts == 3) {
  405. +               dev3 = evswitch_enumerate(fp3);
  406. +               if (dev3 <= 0) {
  407. +                       fprintf(stderr, "can not enumerate device3\n");
  408. +                       goto error5;
  409. +               }
  410. +               const char *device_node3 = evemu_get_devnode(dev3);
  411. +               if (!device_node3)
  412. +               {
  413. +                       fprintf(stderr, "can not determine device node3\n");
  414. +                       goto error5;
  415. +               }
  416. +               fdo3 = open(device_node3, O_WRONLY);
  417. +               if (fd < 0) {
  418. +                       fprintf(stderr, "error: could not open output device %s\n",device_node3);
  419. +                       goto error6;
  420. +               }
  421. +               close(*fp3);
  422. +       }
  423. +
  424. +      
  425. +      
  426. +       fdo = open(device_node1, O_WRONLY);
  427. +       if (fdo < 0) {
  428. +               fprintf(stderr, "error: could not open output device %s\n",device_node1);
  429. +               goto error11;
  430. +       }
  431. +      
  432. +      
  433. +       fdo2 = open(device_node2, O_WRONLY);
  434. +       if (fd < 0) {
  435. +               fprintf(stderr, "error: could not open output device %s\n",device_node2);
  436. +               goto error12;
  437. +       }
  438. +      
  439. +      
  440. +      
  441. +
  442. +      
  443. +       fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  444. +       if (fd < 0) {
  445. +               fprintf(stderr, "error: could not open input device\n");
  446. +               goto error13;
  447. +       }
  448. +
  449. +
  450. +/*     ret = evemu_device(fp);*/
  451. +       /*if (nrouts == 3) {
  452. +               dev3 = evswitch_enumerate(fp3);
  453. +      
  454. +               const char *device_node3 = evemu_get_devnode(dev3);
  455. +               if (!fdo3)
  456. +               {
  457. +                       fprintf(stderr, "can not determine device node\n");
  458. +                       return -1;
  459. +               }
  460. +       }*/
  461. +       if (ret <= 0) {
  462. +               fprintf(stderr, "error: could not create device: %d\n", ret);
  463. +               fclose(fp);
  464. +               return -1;
  465. +       }
  466. +      
  467. +              
  468. +
  469. +      
  470. +       if (nrouts == 3) {
  471. +              
  472. +       }
  473. +      
  474. +      
  475. +       /* create the FIFO (named pipe) */
  476. +       myfifo = strcat(strdup(argv[1]),fifc);
  477. +    mkfifo(myfifo, 0666);
  478. +    ret = chmod(myfifo,S_IRWXU|S_IRGRP|S_IWGRP|S_IROTH);
  479. +    ret = chown(myfifo,0,10);
  480. +       fdc = open(myfifo, O_RDWR | O_NONBLOCK);
  481. +       if (fdc < 0) {
  482. +               fprintf(stderr, "error: could not open control device\n");
  483. +               fprintf(stderr, "%s \n", myfifo);
  484. +               goto error14;
  485. +       }
  486. +      
  487. +      
  488. +       memset (&act, '\0', sizeof(act));
  489. +       act.sa_handler = &handler;
  490. +
  491. +       if (sigaction(SIGTERM, &act, NULL) < 0) {
  492. +               fprintf (stderr, "Could not attach TERM signal handler.\n");
  493. +               return 1;
  494. +       }
  495. +       if (sigaction(SIGINT, &act, NULL) < 0) {
  496. +               fprintf (stderr, "Could not attach INT signal handler.\n");
  497. +               return 1;
  498. +       }
  499. +
  500. +/*
  501. +       if (argc < 3)
  502. +               output = stdout;
  503. +       else {
  504. +               output = fopen(argv[2], "w");
  505. +               if (!output) {
  506. +                       fprintf(stderr, "error: could not open output file");
  507. +               }
  508. +       }
  509. +*/
  510. +/*
  511. +       if (describe_device(fd)) {
  512. +               fprintf(stderr, "error: could not describe device\n");
  513. +               goto out;
  514. +       }
  515. +*/
  516. +       if (mode == EVEMU_RECORD) {
  517. +#ifdef EVIOCSCLOCKID
  518. +               int clockid = CLOCK_MONOTONIC;
  519. +               ioctl(fd, EVIOCSCLOCKID, &clockid);
  520. +#endif
  521. +               if (ioctl(fd, EVIOCGRAB, (void*)1) < 0) {
  522. +                       fprintf(stderr, "error: this device is grabbed and I cannot record events\n");
  523. +                       fprintf(stderr, "see the evemu-record man page for more information\n");
  524. +                       goto error15;
  525. +               } /*else
  526. +                       ioctl(fd, EVIOCGRAB, (void*)0);*/
  527. +
  528. +               fprintf(stderr,  "################################\n");
  529. +               fprintf(stderr,  "#      Waiting for events      #\n");
  530. +               fprintf(stderr,  "################################\n");
  531. +               if (evemu_forward(fdc, fdo, fdo2, fdo3, fd, INFINITE))
  532. +                       fprintf(stderr, "error: could not describe device\n");
  533. +                       goto error16;
  534. +               }
  535. +
  536. +
  537. +       ioctl(fd, EVIOCGRAB, (void*)0);
  538. +       close(*fdo);
  539. +       close(*fdo2);
  540. +       close(*fdo3);
  541. +       close(*fd);
  542. +       close(*fdc);
  543. +       unlink(myfifo);
  544. +    evemu_destroy(dev1);
  545. +       evemu_delete(dev1);
  546. +       evemu_destroy(dev2);
  547. +       evemu_delete(dev2);
  548. +       evemu_destroy(dev3);
  549. +       evemu_delete(dev3);
  550. +       return 0;
  551. +
  552. +error16:
  553. +       ioctl(fd, EVIOCGRAB, (void*)0);
  554. +error15:
  555. +       close(*fdc);
  556. +       unlink(myfifo);
  557. +error14:
  558. +       close(*fd);
  559. +error13:
  560. +       close(*fdo2);
  561. +error12:
  562. +       close(*fdo);
  563. +error11:      
  564. +       close(fdo3);
  565. +       evemu_destroy(dev3);
  566. +       evemu_delete(dev3);
  567. +       evemu_destroy(dev2);
  568. +       evemu_delete(dev2);
  569. +       evemu_destroy(dev1);
  570. +       evemu_delete(dev1);
  571. +       return -1;
  572. +error6:
  573. +       evemu_destroy(dev3);
  574. +       evemu_delete(dev3);
  575. +error5:
  576. +       evemu_destroy(dev2);
  577. +       evemu_delete(dev2);
  578. +error4:
  579. +    evemu_destroy(dev1);
  580. +       evemu_delete(dev1);
  581. +error3:
  582. +    close(*fp3);
  583. +error2:
  584. +       close(*fp2);
  585. +error1:
  586. +       close(*fp);
  587. +       return -1;
  588. +}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top