Index: kbd.c =================================================================== --- kbd.c (revision 48) +++ kbd.c (working copy) @@ -46,6 +46,12 @@ #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0])) +#define TRACE_DEBUG(flag, fmt, ...) \ + do { \ + if(flag) \ + fprintf(stderr,"** DEBUG: "__FILE__":%d "fmt, __LINE__, ## __VA_ARGS__);\ + } while(0); + char debug = 0; int uindev = 0; static int reinit = 0; @@ -64,17 +70,17 @@ int fd, res; struct termios ssetup; - fd=open(port, O_RDWR /*| O_NONBLOCK*/); - if (fd <= 0) { + fd = open(port, O_RDWR /*| O_NONBLOCK*/); + if (fd < 0) { perror("open serial"); fprintf(stderr,"opening %s failed\n", port); - return (-1); + return -1; } res = tcgetattr(fd, &ssetup); if (res < 0) { perror("tcgetattr"); - return (-1); + return -1; } ssetup.c_iflag = 0L; @@ -87,47 +93,46 @@ cfsetispeed(&ssetup, baud); cfsetospeed(&ssetup, baud); - res=tcsetattr(fd, TCSANOW, &ssetup); + res = tcsetattr(fd, TCSANOW, &ssetup); if (res < 0) { perror("tcsetattr"); - return (-1); + return -1; } -return (fd); +return fd; } #ifdef USELIRC int lirc(char *progname) { -struct lirc_config *config; +struct lirc_config *config = NULL; - if (debug) - fprintf(stderr, "progname %s\n",progname); + TRACE_DEBUG(debug, "progname %s\n", progname); if (lirc_init(progname,1) == -1) { printf("failed to init lirc\n"); exit(EXIT_FAILURE); } - if (debug) - fprintf(stderr, "in lirc\n"); + + TRACE_DEBUG(debug, "in lirc\n"); + if (lirc_readconfig(NULL,&config,NULL) == 0) { int i, ret; - char *code, *c; + char *code = NULL, *c = NULL; while (lirc_nextcode(&code) == 0) { if (code == NULL) continue; - while ((ret = lirc_code2char(config, code, &c)) == 0 && - c != NULL) { + + while ((ret = lirc_code2char(config, code, &c)) == 0 && c != NULL) { sscanf(c,"%x",&i); - if (debug) - fprintf(stderr, "%s %u\n",c,i); + TRACE_DEBUG(debug, "%s %u\n",c,i); dev_uinput_key(uindev, i, KEY_PRESSED); dev_uinput_key(uindev, i, KEY_RELEASED); - if (debug) - fprintf(stderr, "Code : %s\n",c); + TRACE_DEBUG(debug, "Code : %s\n",c); } free(code); + if (ret == -1) break; } @@ -142,18 +147,20 @@ int compaq_foldable(void) { int fd; -unsigned char buf[16]; +unsigned char buf[2]; char fn=0; fd = open_serial(TTY_PORT, B4800); - if (fd <= 0) - return (-1); + if (fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 2); - // fprintf(stderr, "0x%02x 0x%02x 0x%02x | ", buf[0], buf[1], ~buf[0]); - if (buf[0] == (unsigned char)~buf[1]) { - if (debug) fprintf(stderr, "press: %d ", buf[0]); + do { + if(read (fd, buf, 2) < 0) + return -1; + + TRACE_DEBUG(debug, "0x%02x 0x%02x 0x%02x | ", buf[0], buf[1], ~buf[0]); + if (buf[0] == ~buf[1]) { + TRACE_DEBUG(debug, "press: %d ", buf[0]); if (buf[0] == 0x02) { fn=1; continue; @@ -162,27 +169,26 @@ buf[0] = foldable_function[buf[0]]; else buf[0] = foldable_normal[buf[0]]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); + + TRACE_DEBUG(debug, "= 0x%02x\n", buf[0]); if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); - } else if (((unsigned char)buf[0] & (unsigned char)~0x80) == (unsigned char)~buf[1]) { - if (debug) - fprintf(stderr, "rel. : %d ", buf[0] & ~0x80); + dev_uinput_key(uindev, buf[0], KEY_PRESSED); + } else if ((buf[0] & ~0x80) == ~buf[1]) { + TRACE_DEBUG(debug, "rel. : %d ", buf[0] & ~0x80); if ((buf[0] & ~0x80) == 0x02) { fn = 0; continue; } if (fn) - buf[0] = foldable_function[(unsigned char)buf[0] & (unsigned char)~0x80]; + buf[0] = foldable_function[buf[0] & ~0x80]; else - buf[0] = foldable_normal[(unsigned char)buf[0] & (unsigned char)~0x80]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); + buf[0] = foldable_normal[buf[0] & ~0x80]; + + TRACE_DEBUG(debug, "= 0x%02x\n", buf[0]); if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + dev_uinput_key(uindev, buf[0], KEY_RELEASED); } - } + } while(fd >= 0); return 0; } @@ -191,33 +197,33 @@ int belkin_infrared(void) { int fd; -unsigned char buf[16]; +unsigned char buf[2]; unsigned char key; unsigned int key_down; unsigned char keycode; fd = open_serial(TTY_PORT, B9600); - if (fd <= 0) - return (-1); + if (fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 2); + do { + if(read (fd, buf, 2) < 0) + return -1; + key = buf[1] & 0x7f; - key_down = !(buf[1] & 0x80); - keycode = belkin_irda_normal[key]; - if (debug) - fprintf(stderr, "0x%02x 0x%02x 0x%02x\n", buf[0], buf[1], key); + key_down = !(buf[1] & 0x80); + keycode = belkin_irda_normal[key]; - if ( key_down ) { - if (debug) - fprintf(stderr,"press %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_PRESSED); + TRACE_DEBUG(debug, "0x%02x 0x%02x 0x%02x\n", buf[0], buf[1], key); + + if (key_down) { + TRACE_DEBUG(debug, "press %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_PRESSED); } else { - if (debug) - fprintf(stderr,"release %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_RELEASED); + TRACE_DEBUG(debug, "release %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_RELEASED); } - } + } while(fd >= 0); return 0; } @@ -226,36 +232,35 @@ int freedom_keyboard(void) { int fd; -unsigned char buf[16]; +unsigned char buf; unsigned char key; unsigned int key_down; unsigned char keycode; fd = open_serial(TTY_PORT, B9600); - if (fd <= 0) - return (-1); + if (fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - key = buf[0]; - //keyboard sends n when pressing a key + do { + if(read (fd, &buf, 1) < 0) + return -1; + + key = buf; + // keyboard sends n when pressing a key // and n+63 when releasing the key - key_down = ( key < 63 ); + key_down = (key < 63); if (!key_down) - key = (key-63)&0x3F; // convert key code for key up - keycode = freedom_kbd[key]; - if (debug) - fprintf(stdout, "0x%02x 0x%02x\n", buf[0], keycode); - if ( key_down ) { - if (debug) - fprintf(stdout,"press %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_PRESSED); + key = (key - 63) & 0x3F; // convert key code for key up + keycode = freedom_kbd[key]; + TRACE_DEBUG(debug, "0x%02x 0x%02x\n", buf, keycode); + if (key_down) { + TRACE_DEBUG(debug, "press %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_PRESSED); } else { - if (debug) - fprintf(stdout,"release %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_RELEASED); + TRACE_DEBUG(debug, "release %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_RELEASED); } - } + } while(fd >= 0); return 0; } @@ -276,7 +281,7 @@ int stowaway_init(int fd) { int status; -unsigned char buf[16]; +unsigned char buf[2]; ioctl(fd, TIOCMGET, &status); status |= TIOCM_DTR; /* Set DTR */ @@ -293,10 +298,11 @@ ioctl(fd, TIOCMSET, &status); /* Stowaway will send back 0xFA 0xFD indicating successful init */ if (select_read(fd, 2, 0)) { - read(fd, buf, 2); + if(read(fd, buf, 2) < 0) + return -1; + if ((buf[0] == 0xFA) && (buf[0] == 0xFD)) - if (debug) - fprintf(stderr, "keyboard initialised\n"); + TRACE_DEBUG(debug, "keyboard initialised\n"); } return 0; @@ -308,7 +314,7 @@ int fd; fd = open( "/dev/apm_bios", O_RDONLY | O_NONBLOCK ); - if (fd <= 0) + if (fd < 0) return -1; return fd; @@ -339,14 +345,14 @@ int stowaway(void) { int fd, apm_fd; -unsigned char buf[16]; +unsigned char buf; char fn=0; struct sigaction act; int rc; fd = open_serial(TTY_PORT, B9600); - if (fd <= 0) - return (-1); + if (fd < 0) + return -1; /* Make SIGHUP cause a reinit of the keyboard */ act.sa_handler = stowaway_sig; @@ -357,11 +363,10 @@ /* Open APM so we can listen for resume events */ apm_fd = open_apm(); - while (fd > 0) { - + while (fd >= 0) { stowaway_init(fd); - while (fd > 0) { + while (fd >= 0) { rc = select_read(fd, 0, 100000); if (rc == -1) { if (reinit) { @@ -378,8 +383,8 @@ continue; } - rc = read (fd, buf, 1); - if (rc == -1) { + rc = read (fd, &buf, 1); + if (rc < 0) { if (reinit) { reinit = 0; break; @@ -389,36 +394,32 @@ } } - if ( ((unsigned char)buf[0] & (unsigned char)0x80) == 0 ) { - if (debug) - fprintf(stderr, "press: %d\n", buf[0]); - if (buf[0] == 0x08) { + if ((buf & 0x80) == 0) { + TRACE_DEBUG(debug, "press: %d\n", buf); + if (buf == 0x08) { fn = 1; continue; } if (fn) - buf[0] = stowaway_function[buf[0]]; + buf = stowaway_function[buf]; else - buf[0] = stowaway_normal[buf[0]]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + buf = stowaway_normal[buf]; + TRACE_DEBUG(debug, "= 0x%02x\n", buf); + if (buf > 0) + dev_uinput_key(uindev, buf, KEY_PRESSED); } else { - if (debug) - fprintf(stderr, "rel. : %d\n", buf[0] & ~0x80); - if ((buf[0] & ~0x80) == 0x08) { + TRACE_DEBUG(debug, "rel. : %d\n", buf & ~0x80); + if ((buf & ~0x80) == 0x08) { fn = 0; continue; } if (fn) - buf[0] = stowaway_function[(unsigned char)buf[0] & (unsigned char)~0x80]; + buf = stowaway_function[buf & ~0x80]; else - buf[0] = stowaway_normal[(unsigned char)buf[0] & (unsigned char)~0x80]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + buf = stowaway_normal[buf & ~0x80]; + TRACE_DEBUG(debug, "= 0x%02x\n", buf); + if (buf > 0) + dev_uinput_key(uindev, buf, KEY_RELEASED); } } } @@ -433,13 +434,13 @@ #define STOWAWAYXT_BL_FN 34 int fd, apm_fd; -unsigned char buf[16]; +unsigned char buf; char bluefn=0,greenfn=0; struct sigaction act; int rc; fd = open_serial(TTY_PORT, B9600); - if (fd <= 0) + if (fd < 0) return (-1); /* Make SIGHUP cause a reinit of the keyboard */ @@ -451,12 +452,12 @@ /* Open APM so we can listen for resume events */ apm_fd = open_apm(); - while (fd > 0) { + while (fd >= 0) { stowaway_init(fd); - while (fd > 0) { + while (fd >= 0) { rc = select_read(fd, 0, 100000); - if (rc == -1) { + if (rc < 0) { if (reinit) { reinit = 0; break; @@ -471,8 +472,8 @@ continue; } - rc = read (fd, buf, 1); - if (rc == -1) { + rc = read (fd, &buf, 1); + if (rc < 0) { if (reinit) { reinit = 0; break; @@ -482,97 +483,93 @@ } } - if ( ((unsigned char)buf[0] & (unsigned char)0x80) == 0 ) { + if ( (buf & 0x80) == 0 ) { /* KEY PRESSED */ - if (debug) - fprintf(stderr, "press: %d\n", buf[0]); - if (buf[0] == STOWAWAYXT_BL_FN) { + TRACE_DEBUG(debug, "press: %d\n", buf); + if (buf == STOWAWAYXT_BL_FN) { bluefn = 1; continue; } - if (buf[0] == STOWAWAYXT_GR_FN) { + if (buf == STOWAWAYXT_GR_FN) { greenfn = 1; dev_uinput_key(uindev,42,KEY_PRESSED); continue; } if (bluefn) - buf[0] = stowawayxt_function[buf[0]]; + buf = stowawayxt_function[buf]; else if (greenfn) { - buf[0] = stowawayxt_function[buf[0]]; + buf = stowawayxt_function[buf]; /* fixup where green function is not shift blue function */ - switch (buf[0]) { + switch (buf) { case KEY_UP: - buf[0] = KEY_PAGEUP; + buf = KEY_PAGEUP; break; case KEY_LEFT: - buf[0] = KEY_HOME; + buf = KEY_HOME; break; case KEY_DOWN: - buf[0] = KEY_PAGEDOWN; + buf = KEY_PAGEDOWN; break; case KEY_RIGHT: - buf[0] = KEY_END; + buf = KEY_END; break; case KEY_INTL2: - buf[0] = KEY_INTL3; + buf = KEY_INTL3; break; } /* /switch */ } else - buf[0]=stowawayxt_normal[buf[0]]; + buf=stowawayxt_normal[buf]; + + TRACE_DEBUG(debug, "= 0x%02x\n", buf); + if (buf != KEY_RESERVED) + dev_uinput_key(uindev, buf, KEY_PRESSED); - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] != KEY_RESERVED) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); - } else { /* KEY RELEASED */ - if (debug) - fprintf(stderr, "rel. : %d\n", buf[0] & ~0x80); + TRACE_DEBUG(debug, "rel. : %d\n", buf & ~0x80); - if ((buf[0] & ~0x80) == STOWAWAYXT_BL_FN) { + if ((buf & ~0x80) == STOWAWAYXT_BL_FN) { bluefn = 0; continue; } - if ((buf[0] & ~0x80) == STOWAWAYXT_GR_FN) { + if ((buf & ~0x80) == STOWAWAYXT_GR_FN) { greenfn = 0; dev_uinput_key(uindev,42,KEY_RELEASED); continue; } if (bluefn) - buf[0] = stowawayxt_function[(unsigned char) buf[0] & (unsigned char)~0x80]; + buf = stowawayxt_function[buf & ~0x80]; else if (greenfn) { - buf[0] = stowawayxt_function[(unsigned char) buf[0] & (unsigned char)~0x80]; + buf = stowawayxt_function[buf & ~0x80]; /* fixup where green function is not shift blue function */ - switch(buf[0]) { + switch(buf) { case KEY_UP: - buf[0] = KEY_PAGEUP; + buf = KEY_PAGEUP; break; case KEY_LEFT: - buf[0] = KEY_HOME; + buf = KEY_HOME; break; case KEY_DOWN: - buf[0] = KEY_PAGEDOWN; + buf = KEY_PAGEDOWN; break; case KEY_RIGHT: - buf[0] = KEY_END; + buf = KEY_END; break; case KEY_INTL2: - buf[0] = KEY_INTL3; + buf = KEY_INTL3; break; } /* /switch */ } else - buf[0] = stowawayxt_normal[(unsigned char)buf[0] & (unsigned char)~0x80]; + buf = stowawayxt_normal[buf & ~0x80]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] != KEY_RESERVED) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + TRACE_DEBUG(debug, "= 0x%02x\n", buf); + if (buf != KEY_RESERVED) + dev_uinput_key(uindev, buf, KEY_RELEASED); } } } @@ -584,45 +581,48 @@ int snapntype(void) { int fd; -unsigned char buf[16]; +unsigned char buf; char symb=0; fd = open_serial(TTY_PORT, B2400); + if(fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - if (debug) - fprintf(stderr, "got %d\n", buf[0]); - if (buf[0] & 0x80) { /* release */ - read(fd,buf+1,1); - buf[0] = buf[0] & 0x7f; - if (buf[0] == 27) { + do { + if(read (fd, &buf, 1) < 0) + return -1; + + TRACE_DEBUG(debug, "got %d\n", buf); + if (buf & 0x80) { /* release */ + if(read(fd, &buf, 1) < 0) + return -1; + + buf = buf & 0x7f; + if (buf == 27) { symb = 0; continue; } if (symb) - buf[0] = snapntype_symbol[buf[0]]; + buf = snapntype_symbol[buf]; else - buf[0] = snapntype_normal[buf[0]]; - if (debug) - fprintf(stderr, " release %d\n", buf[0]); - if (buf[0] != 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + buf = snapntype_normal[buf]; + TRACE_DEBUG(debug, " release %d\n", buf); + if (buf != 0) + dev_uinput_key(uindev, buf, KEY_RELEASED); } else { /* press */ - if (buf[0] == 27) { + if (buf == 27) { symb=1; continue; } if (symb) - buf[0] = snapntype_symbol[buf[0]]; + buf = snapntype_symbol[buf]; else - buf[0] = snapntype_normal[buf[0]]; - if (debug) - fprintf(stderr, " press %d\n", buf[0]); - if (buf[0] != 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + buf = snapntype_normal[buf]; + TRACE_DEBUG(debug, " press %d\n", buf); + if (buf != 0) + dev_uinput_key(uindev, buf, KEY_PRESSED); } - } + } while(fd >= 0); return 0; } @@ -638,25 +638,28 @@ fd = open_serial(TTY_PORT, B9600); - while (fd > 0) { - read (fd, buf, 1); - read (fd, buf+1, 1); - if (debug) - fprintf(stderr, "got %02x%02x\n", buf[0],buf[1]); + do { + /* FIXME: really need this? */ + if(read (fd, buf, 1) < 0) + return -1; + + if(read (fd, buf+1, 1) < 0) + return -1; + + TRACE_DEBUG(debug, "got %02x%02x\n", buf[0],buf[1]); if (buf[1] & 0x80) { /* release */ key = buf[1] & 0x7F; if (key == 30) { fn = 0; if (last_key) { - dev_uinput_key(uindev, (unsigned short)snapntypebt_fn[last_key], KEY_RELEASED); + dev_uinput_key(uindev, snapntypebt_fn[last_key], KEY_RELEASED); } continue; } - pressed = KEY_RELEASED; - if (debug) - fprintf(stderr, " release:"); + pressed = KEY_RELEASED; + TRACE_DEBUG(debug, " release:"); } else { key = buf[1]; @@ -667,11 +670,9 @@ } pressed = KEY_PRESSED; - if (debug) - fprintf(stderr, " press: "); + TRACE_DEBUG(debug, " press: "); } - if (debug) - fprintf(stderr, " %d (%d)\n",key,snapntypebt_normal[key]); + TRACE_DEBUG(debug, " %d (%d)\n",key,snapntypebt_normal[key]); if (!fn) key = snapntypebt_normal[key]; @@ -680,8 +681,8 @@ key = snapntypebt_fn[key]; } if (key != 0 && !debug) - dev_uinput_key(uindev, (unsigned short)key, pressed); - } + dev_uinput_key(uindev, key, pressed); + } while(fd >= 0); return 0; } @@ -706,8 +707,7 @@ perror(TTY_PORT); return -1; } - if (debug) - fprintf(stderr, "got %d 0x%x\n", cin,cin); + TRACE_DEBUG(debug, "got %d 0x%x\n", cin,cin); if (cin & 0x80) { /* release */ cin &= 0x7f; switch (cin) { @@ -728,12 +728,11 @@ else cnew = hpslim_normal[cin]; } - if (debug) - fprintf(stderr, " release cnew=%d 0x%x\n", cnew,cnew); + TRACE_DEBUG(debug, " release cnew=%d 0x%x\n", cnew,cnew); if (cnew != 0) { - dev_uinput_key(uindev, (unsigned short)cnew, KEY_RELEASED); + dev_uinput_key(uindev, cnew, KEY_RELEASED); if (symshft) - dev_uinput_key(uindev, (unsigned short)KEY_RIGHTSHIFT, KEY_RELEASED); + dev_uinput_key(uindev, KEY_RIGHTSHIFT, KEY_RELEASED); symshft = 0; // toggle numlck on release if (cnew == KEY_NUMLOCK) numlck = !numlck; @@ -759,12 +758,11 @@ else cnew = hpslim_normal[cin]; } - if (debug) - fprintf(stderr, " press cnew=%d 0x%x\n", cnew,cnew); + TRACE_DEBUG(debug, " press cnew=%d 0x%x\n", cnew,cnew); if (cnew != 0) { if (symshft) - dev_uinput_key(uindev, (unsigned short)KEY_RIGHTSHIFT, KEY_PRESSED); - dev_uinput_key(uindev, (unsigned short)cnew, KEY_PRESSED); + dev_uinput_key(uindev, KEY_RIGHTSHIFT, KEY_PRESSED); + dev_uinput_key(uindev, cnew, KEY_PRESSED); } } } @@ -776,38 +774,41 @@ int smartbt(void) { int fd; -unsigned char buf[16]; +unsigned char buf; fd = open_serial(TTY_PORT, B2400); + if(fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - if (buf[0] > 126) { + do { + if(read (fd, &buf, 1) < 0) + return -1; + + if (buf > 126) { if (debug) - fprintf(stderr, "error: unexpected char %d\n", buf[0]); + fprintf(stderr, "error: unexpected char %d\n", buf); continue; } - if (buf[0] > 63) { + if (buf > 63) { /* release */ - buf[0] = buf[0] - 63; - if (debug) - fprintf(stderr, "rel.: %d\n", buf[0]); - buf[0] = smartbt_normal[buf[0]]; - if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + buf = buf - 63; + + TRACE_DEBUG(debug, "rel.: %d\n", buf); + buf = smartbt_normal[buf]; + TRACE_DEBUG(debug, "= 0x%02x\n", buf); + if (buf > 0) + dev_uinput_key(uindev, buf, KEY_RELEASED); } else { /* press */ if (debug) - fprintf(stderr, "press: %d\n", buf[0]); - buf[0] = smartbt_normal[buf[0]]; + fprintf(stderr, "press: %d\n", buf); + buf = smartbt_normal[buf]; if (debug) - fprintf(stderr,"= 0x%02x\n", buf[0]); - if (buf[0] > 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + fprintf(stderr,"= 0x%02x\n", buf); + if (buf > 0) + dev_uinput_key(uindev, buf, KEY_PRESSED); } - } + } while(fd >= 0); return 0; } @@ -816,16 +817,18 @@ int flexis(void) { int fd; -unsigned char buf[16]; +unsigned char buf[2]; char symb=0; fd = open_serial(TTY_PORT, B9600); + if(fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - if (debug) - fprintf(stderr, "got %d\n", buf[0]); - + do { + if(read (fd, buf, 1) < 0) + return -1; + + TRACE_DEBUG(debug, "got %d\n", buf[0]); if (buf[0] & 0x80) { /* key press */ buf[0] = buf[0] & 0x7f; @@ -840,13 +843,13 @@ if (buf[0] == KEY_LEFTSHIFT || buf[0] == KEY_RIGHTSHIFT || buf[0] == KEY_LEFTCTRL || buf[0] == KEY_RIGHTCTRL || buf[0] == KEY_LEFTALT || buf[0] == KEY_RIGHTALT ) { - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + dev_uinput_key(uindev, buf[0], KEY_PRESSED); } else { - read(fd,buf+1,1); - if (debug) - fprintf(stderr, "got %d\n", buf[1]); - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + if(read(fd, buf+1, 1) < 0) + return -1; + TRACE_DEBUG(debug, "got %d\n", buf[1]); + dev_uinput_key(uindev, buf[0], KEY_PRESSED); + dev_uinput_key(uindev, buf[0], KEY_RELEASED); } } else { /* release of key from Set 2 */ if (symb) @@ -854,37 +857,39 @@ else buf[0] = flexis_fx100_normal[buf[0]]; if (buf[0] != 0) - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + dev_uinput_key(uindev, buf[0], KEY_RELEASED); } - } + } while(fd >= 0); return 0; } int benqgamepad(void) { +unsigned char buf[2]; +unsigned char keycode; +int i; int fd; -unsigned char buf[16]; -int i; -unsigned char keycode; fd = open_serial(TTY_PORT, B4800); + if(fd < 0) + return -1; - while (fd > 0) { + do { keycode = 0; - read (fd, buf, 2); + if(read (fd, buf, 2) < 0) + return -1; if (buf[0] & 0x80) { /* release */ - if (debug) - fprintf(stderr, "release: %d %d\n", buf[0], buf[1]); + TRACE_DEBUG(debug, "release: %d %d\n", buf[0], buf[1]); buf[0] = buf[0] & 0x7f; - for (i=0; i<10; i++) { + for (i=0; i < 10; i++) { if (benq_gamepad_map[i][0] == buf[0]) { keycode = benq_gamepad_map[i][1]; break; } } if (keycode != 0) - dev_uinput_key(uindev, (unsigned short)keycode, KEY_RELEASED); + dev_uinput_key(uindev, keycode, KEY_RELEASED); } else { if (debug) @@ -896,9 +901,9 @@ } } if (keycode != 0) - dev_uinput_key(uindev, (unsigned short)keycode, KEY_PRESSED); + dev_uinput_key(uindev, keycode, KEY_PRESSED); } - } + } while(fd >= 0); return 0; } @@ -914,6 +919,8 @@ int i; fd = open_serial(TTY_PORT, B57600); + if(fd < 0) + return -1; /* PocketVIK needs CRTSCTS */ res = tcgetattr(fd, &ssetup); @@ -928,16 +935,20 @@ return (-1); } - while (fd > 0) { - read (fd, buf, 1); + do { + if(read (fd, buf, 1) < 0) + return -1; + if (debug) fprintf(stderr, "got %d\n", buf[0]); if (buf[0] & 0x80) { /* press with modifiers */ mods = buf[0] & 0x7f; - read (fd, buf, 1); - } else + if(read (fd, buf, 1) < 0) + return -1; + } else { mods = 0; + } if (mods & POCKETVIK_Fn) buf[0] = pocketvik_function[buf[0]]; @@ -955,9 +966,9 @@ } /* Key down */ - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + dev_uinput_key(uindev, buf[0], KEY_PRESSED); /* Key up */ - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_RELEASED); + dev_uinput_key(uindev, buf[0], KEY_RELEASED); /* Modifiers up */ for (i = 0; i < ARRAY_SIZE(pocketvik_modifiers); i++ ) { @@ -966,7 +977,7 @@ } } - } + } while(fd >= 0); return 0; } @@ -974,42 +985,45 @@ int micro_foldaway(void) { int fd; -unsigned char buf[16]; +unsigned char buf; unsigned char lastkey = 0; // char symb=0; int checkkey = 0; fd = open_serial(TTY_PORT, B9600); + if(fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - if (debug) - fprintf(stderr, "got: %d\n", buf[0]); - if (buf[0] & 0x80) { /* possible release */ - checkkey = (159 - buf[0]); + do { + if(read(fd, &buf, 1) < 0) + return -1; + TRACE_DEBUG(debug, "got: %d\n", buf); + if (buf & 0x80) { /* possible release */ + checkkey = (159 - buf); if (checkkey > 0 && checkkey <= 4) { - buf[0] = micro_foldaway_normal[159 - buf[0]]; - dev_uinput_key(uindev, buf[0], KEY_RELEASED); - } else if (buf[0] == 133 && lastkey > 0) { + buf = micro_foldaway_normal[159 - buf]; + dev_uinput_key(uindev, buf, KEY_RELEASED); + } else if (buf == 133 && lastkey > 0) { dev_uinput_key(uindev, lastkey, KEY_RELEASED); lastkey = 0; } /* Eat repeated code */ - read (fd, buf, 1); + if(read (fd, &buf, 1) < 0) + return -1; } else { - checkkey = buf[0]; + checkkey = buf; /* if (symb) buf[0] = micro_foldaway_function[buf[0]]; else*/ - buf[0] = micro_foldaway_normal[buf[0]]; + buf = micro_foldaway_normal[buf]; - if (buf[0] != 0) { + if (buf != 0) { if (checkkey > 4) - lastkey = buf[0]; /* Not a modifier, record key */ - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + lastkey = buf; /* Not a modifier, record key */ + dev_uinput_key(uindev, buf, KEY_PRESSED); } } - } + } while(fd >= 0); return 0; } @@ -1017,41 +1031,45 @@ int micro_datapad(void) { int fd; -unsigned char buf[16]; +unsigned char buf; unsigned char lastkey = 0; int checkkey = 0; fd = open_serial(TTY_PORT, B9600); + if(fd < 0) + return -1; - while (fd > 0) { - read (fd, buf, 1); - if (debug) - fprintf(stderr, "got: %d\n", buf[0]); - if (buf[0] & 0x80) { /* possible release */ - checkkey = (159 - buf[0]); + do { + if(read (fd, &buf, 1) < 0) + return -1; + + TRACE_DEBUG(debug, "got: %d\n", buf); + if (buf & 0x80) { /* possible release */ + checkkey = (159 - buf); if (checkkey > 0 && checkkey <= 4) { - buf[0] = micro_foldaway_normal[159 - buf[0]]; - dev_uinput_key(uindev, buf[0], KEY_RELEASED); - } else if (buf[0] == 133 && lastkey > 0) { + buf = micro_foldaway_normal[159 - buf]; + dev_uinput_key(uindev, buf, KEY_RELEASED); + } else if (buf == 133 && lastkey > 0) { dev_uinput_key(uindev, lastkey, KEY_RELEASED); lastkey = 0; } /* Eat repeated code */ - read (fd, buf, 1); + if(read (fd, &buf, 1) < 0) + return -1; } else { - checkkey = buf[0]; + checkkey = buf; /* if (symb) buf[0]=micro_datapad_function[buf[0]]; else*/ - buf[0] = micro_datapad_normal[buf[0]]; + buf = micro_datapad_normal[buf]; - if (buf[0] != 0) { + if (buf != 0) { if (checkkey > 4) - lastkey = buf[0]; /* Not a modifier, record key */ - dev_uinput_key(uindev, (unsigned short)buf[0], KEY_PRESSED); + lastkey = buf; /* Not a modifier, record key */ + dev_uinput_key(uindev, buf, KEY_PRESSED); } } - } + } while(fd >= 0); return 0; } @@ -1059,7 +1077,7 @@ int hp_bt_foldable(void){ int fd; -unsigned char buf[1]; +unsigned char buf; unsigned char cnew; char symb=0; @@ -1068,40 +1086,37 @@ return -1; for (;;) { - if (read (fd, buf, 1) < 0) { + if (read (fd, &buf, 1) < 0) { perror(TTY_PORT); return -1; } - if (debug) - fprintf(stderr, "got %d (0x%x)\n", buf[0], buf[0]); - if (buf[0] & 0x80) { /* release */ - buf[0] &= 0x7f; - if (buf[0] == 2) { + TRACE_DEBUG(debug, "got %d (0x%x)\n", buf, buf); + if (buf & 0x80) { /* release */ + buf &= 0x7f; + if (buf == 2) { symb = 0; continue; } if (symb) - cnew = btfoldable_function[buf[0]]; + cnew = btfoldable_function[buf]; else - cnew = btfoldable_normal[buf[0]]; + cnew = btfoldable_normal[buf]; - if (debug) - fprintf(stderr, " release cnew=%d (0x%x)\n", cnew, cnew); + TRACE_DEBUG(debug, " release cnew=%d (0x%x)\n", cnew, cnew); if (cnew != 0) - dev_uinput_key(uindev, (unsigned short)cnew, KEY_RELEASED); + dev_uinput_key(uindev, cnew, KEY_RELEASED); } else { /* press */ - if (buf[0] == 2) { + if (buf == 2) { symb = 1; continue; } if (symb) - cnew = btfoldable_function[buf[0]]; + cnew = btfoldable_function[buf]; else - cnew = btfoldable_normal[buf[0]]; - if (debug) - fprintf(stderr, " press cnew=%d (0x%x)\n", cnew, cnew); + cnew = btfoldable_normal[buf]; + TRACE_DEBUG(debug, " press cnew=%d (0x%x)\n", cnew, cnew); if (cnew != 0) - dev_uinput_key(uindev, (unsigned short)cnew, KEY_PRESSED); + dev_uinput_key(uindev, cnew, KEY_PRESSED); } } return 0; @@ -1137,10 +1152,9 @@ else cnew = compaq_normal[buf[0]]; - if (debug) - fprintf(stderr, " release cnew=%d (0x%x)\n", cnew, cnew); + TRACE_DEBUG(debug, " release cnew=%d (0x%x)\n", cnew, cnew); if (cnew != 0) - dev_uinput_key(uindev, (unsigned short)cnew, KEY_RELEASED); + dev_uinput_key(uindev, cnew, KEY_RELEASED); } else { /* press */ if (buf[0] == 2) { symb = 1; @@ -1150,10 +1164,9 @@ cnew = compaq_function[buf[0]]; else cnew = compaq_normal[buf[0]]; - if (debug) - fprintf(stderr, " press cnew=%d (0x%x)\n", cnew, cnew); + TRACE_DEBUG(debug, " press cnew=%d (0x%x)\n", cnew, cnew); if (cnew != 0) - dev_uinput_key(uindev, (unsigned short)cnew, KEY_PRESSED); + dev_uinput_key(uindev, cnew, KEY_PRESSED); } } @@ -1170,28 +1183,27 @@ unsigned char keycode; fd = open_serial(TTY_PORT, B9600); - if (fd <= 0) - return (-1); + if (fd < 0) + return -1; - while (fd > 0) { - read (fd, &buf, 1); - key = buf & 0x7f; - key_down = !(buf & 0x80); - /* keycode = targus_irda_normal[key]; */ + do { + if(read (fd, &buf, 1) < 0) + return -1; + + key = buf & 0x7f; + key_down = !(buf & 0x80); + /* keycode = targus_irda_normal[key]; */ keycode = key; - if (debug) - fprintf(stderr, "0x%02x 0x%02x\n", buf, key); + TRACE_DEBUG(debug, "0x%02x 0x%02x\n", buf, key); - if ( key_down ) { - if (debug) - fprintf(stderr,"press %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_PRESSED); + if (key_down) { + TRACE_DEBUG(debug, "press %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_PRESSED); } else { - if (debug) - fprintf(stderr,"release %d\n", keycode); - dev_uinput_key(uindev, (unsigned short)keycode, KEY_RELEASED); + TRACE_DEBUG(debug, "release %d\n", keycode); + dev_uinput_key(uindev, keycode, KEY_RELEASED); } - } + } while(fd >= 0); return 0; } @@ -1203,6 +1215,7 @@ fprintf (stderr, "Usage:\n"); fprintf (stderr, "%s [-d] [-h] [-c ] -p -t \n", arg0); fprintf (stderr, "-d\tenable debugging output\n"); + fprintf (stderr, "-b\tusing fork() for starting in background\n"); fprintf (stderr, "-h\tprint this help\n"); fprintf (stderr, "-c \n"); fprintf (stderr, "\tRead port and type from config file\n"); @@ -1304,14 +1317,15 @@ FILE *fd; char buf[PATH_MAX]; - fd = fopen(path, "r"); + fd = fopen(path, "r"); if (!fd) { fprintf(stderr, "could not open config file %s\n", path); return; } - while (!feof(fd)) { - fgets(buf, PATH_MAX, fd); + while (!feof(fd)) { + if(!fgets(buf, PATH_MAX, fd)) + continue; if (*buf == '#' || *buf == '\0') { /* It's a comment or a blank line */ @@ -1346,9 +1360,10 @@ int main(int argc, char **argv) { int optc; +int background = 0; int kbdtype=KBD_TYPE_NONE; - while ((optc = getopt(argc, argv, "c:t:p:dh")) != -1) { + while ((optc = getopt(argc, argv, "c:t:p:dhb")) != -1) { switch ((char)optc) { case 'h': print_usage(argv[0]); @@ -1366,6 +1381,8 @@ case 'c': parse_config(optarg, &kbdtype, TTY_PORT); break; + case 'b': + background = 1; } } @@ -1374,57 +1391,77 @@ exit(1); } - uindev = dev_uinput_init(); - if (uindev <= 0) { - fprintf(stderr, "init uinput failed\n"); - exit (1); - } + if(background) { +#ifdef HAVE_FORK + int pid = fork(); + if(pid < 0) { + return -1; + } else if (pid > 0) { + return 0; + } + TRACE_DEBUG(debug, "Daemonized, pid=%d\n", getpid()); +#else + fprintf(stderr, "fork() is not enabled/supported in this version, exiting...\n"); + exit(1); +#endif + } - signal(SIGTERM, handle_sigterm); + uindev = dev_uinput_init(); + if (uindev <= 0) { + fprintf(stderr, "init uinput failed\n"); + exit (1); + } - if (kbdtype == KBD_TYPE_FOLDABLE) - compaq_foldable(); - else if (kbdtype == KBD_TYPE_BTFOLDABLE) - hp_bt_foldable(); - else if (kbdtype == KBD_TYPE_SNAPNTYPE) - snapntype(); - else if (kbdtype == KBD_TYPE_SNAPNTYPEBT) - snapntypebt(); - else if (kbdtype == KBD_TYPE_STOWAWAY) - stowaway(); - else if (kbdtype == KBD_TYPE_STOWAWAYXT) - stowawayxt(); - else if (kbdtype == KBD_TYPE_HPSLIM) - hpslim(); - else if (kbdtype == KBD_TYPE_SMARTBT) - smartbt(); - else if (kbdtype == KBD_TYPE_FLEXIS) - flexis(); - else if (kbdtype == KBD_TYPE_BENQ_GAMEPAD) - benqgamepad(); - else if (kbdtype == KBD_TYPE_POCKETVIK) - pocketvik(); - else if (kbdtype == KBD_TYPE_MICRO_FOLDAWAY) - micro_foldaway(); - else if (kbdtype == KBD_TYPE_MICRO_DATAPAD) - micro_datapad(); - else if (kbdtype == KBD_TYPE_COMPAQ_MICROKBD) - compaq_microkbd(); - else if (kbdtype == KBD_TYPE_LIRC) -#ifdef USELIRC - lirc(basename(argv[0]); -#else - { - fprintf(stderr, "keyboard type 'lirc' is not supported in this version\n"); - exit(1); - } + signal(SIGTERM, handle_sigterm); + + if (kbdtype == KBD_TYPE_FOLDABLE) + compaq_foldable(); + else if (kbdtype == KBD_TYPE_BTFOLDABLE) + hp_bt_foldable(); + else if (kbdtype == KBD_TYPE_SNAPNTYPE) + snapntype(); + else if (kbdtype == KBD_TYPE_SNAPNTYPEBT) + snapntypebt(); + else if (kbdtype == KBD_TYPE_STOWAWAY) + stowaway(); + else if (kbdtype == KBD_TYPE_STOWAWAYXT) + stowawayxt(); + else if (kbdtype == KBD_TYPE_HPSLIM) + hpslim(); + else if (kbdtype == KBD_TYPE_SMARTBT) + smartbt(); + else if (kbdtype == KBD_TYPE_FLEXIS) + flexis(); + else if (kbdtype == KBD_TYPE_BENQ_GAMEPAD) + benqgamepad(); + else if (kbdtype == KBD_TYPE_POCKETVIK) + pocketvik(); + else if (kbdtype == KBD_TYPE_MICRO_FOLDAWAY) + micro_foldaway(); + else if (kbdtype == KBD_TYPE_MICRO_DATAPAD) + micro_datapad(); + else if (kbdtype == KBD_TYPE_COMPAQ_MICROKBD) + compaq_microkbd(); + else if (kbdtype == KBD_TYPE_LIRC) + #ifdef USELIRC + lirc(basename(argv[0]); + #else + { + fprintf(stderr, "keyboard type 'lirc' is not supported in this version\n"); + exit(1); + } + #endif + else if (kbdtype == KBD_TYPE_BELKINIR) + belkin_infrared(); + else if (kbdtype == KBD_TYPE_TARGUSIR) + targus_infrared(); + else if (kbdtype == KBD_TYPE_FREEDOM) + freedom_keyboard(); + +#ifdef HAVE_FORK + if(background) + TRACE_DEBUG(debug, "Exiting of daemon...\n"); #endif - else if (kbdtype == KBD_TYPE_BELKINIR) - belkin_infrared(); - else if (kbdtype == KBD_TYPE_TARGUSIR) - targus_infrared(); - else if (kbdtype == KBD_TYPE_FREEDOM) - freedom_keyboard(); return 0; } Index: ChangeLog =================================================================== --- ChangeLog (revision 53) +++ ChangeLog (working copy) @@ -0,0 +1,12 @@ +2009-05-03 Jorge Pereira + + * Makefile: + * kbd.c: (open_serial), (lirc), (compaq_foldable), + (belkin_infrared), (freedom_keyboard), (stowaway_init), (open_apm), + (stowaway), (stowawayxt), (snapntype), (snapntypebt), (hpslim), + (smartbt), (flexis), (benqgamepad), (pocketvik), (micro_foldaway), + (micro_datapad), (hp_bt_foldable), (compaq_microkbd), + (targus_infrared), (print_usage), (parse_config), (main): + Remove a lot cast for (unsigned char), optimize size of bufs for read + data from uinput, added support for daemonize using "-b", better support + for show debug messages and remove some build warnings Index: Makefile =================================================================== --- Makefile (revision 48) +++ Makefile (working copy) @@ -1,18 +1,28 @@ #CC = gcc -CFLAGS = -Wall -Os - +CFLAGS = -Wall -Os VERSION = \"V0.12\" -CFLAGS += -DVERSION=$(VERSION) +CFLAGS += -DVERSION=$(VERSION) +ifeq ($(DEBUG),1) + CFLAGS += -g3 +endif + +ifeq ($(HAVE_FORK),1) + CFLAGS += -DHAVE_FORK=1 +endif + # for N770 add dbus glib bindings -#CFLAGS+= -DN770=1 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -#`pkg-config dbus-glib-1 --cflags` -#LDFLAGS+= -ldbus-glib-1 -ldbus-1 -lglib-2.0 -# `pkg-config dbus-glib-1 --libs` +ifeq ($(N770),1) + CFLAGS+= -DN770=1 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include \ + `pkg-config dbus-glib-1 --cflags` + LDFLAGS+= -ldbus-glib-1 -ldbus-1 -lglib-2.0 `pkg-config dbus-glib-1 --libs` +endif # for use with LIRC, uncomment the following two lines -# CFLAGS += -DUSELIRC -# LDFLAGS += -llirc_client +ifeq ($(LIRC),1) + CFLAGS += -DUSELIRC + LDFLAGS += -llirc_client +endif ####################################################################### @@ -21,6 +31,17 @@ all: kbdd +help: + @echo "make [options]" + @echo "" + @echo "options:" + @echo "" + @echo " N770=1 - Enable rules build for N770" + @echo " LIRC=1 - Enable using of lirc" + @echo " DEBUG=1 - Add -g for enable debug" + @echo " HAVE_FORK=1 - Enable using of fork() if avaiable" + @echo "" + kbdd: $(OBJ) $(CC) -s -o kbdd $(OBJ) $(LDFLAGS)