loadkeys: Remove OPT_QUIET option
[platform/upstream/kbd.git] / src / kbdrate.c
index 456fd98..8d0df5a 100644 (file)
@@ -63,8 +63,8 @@ beats rebuilding the kernel!
   1999-03-17
   Linux/SPARC modifications by Jeffrey Connell <ankh@canuck.gen.nz>:
   It seems that the KDKBDREP ioctl is not available on this platform.
-  However, Linux/SPARC has its own ioctl for this, with yet another
-  different measurement system.  Thus, try for KIOCSRATE, too.
+  However, Linux/SPARC has its own ioctl for this (since 2.1.30),
+  with yet another measurement system.  Thus, try for KIOCSRATE, too.
 
 */
 
@@ -74,21 +74,24 @@ beats rebuilding the kernel!
 #include <errno.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
+#include <linux/kd.h>
 
 #ifdef __sparc__
 #include <asm/param.h>
-#include <asm/kbio.h>
 #endif
 
-#ifndef KDKBDREP
-/* usually defined in <linux/kd.h> */
-#define KDKBDREP        0x4B52  /* set keyboard delay/repeat rate;
-                                * actually used values are returned */
-struct kbd_repeat {
+#ifdef COMPAT_HEADERS
+#include "compat/linux-kd.h"
+#endif
+
+/* Equal to kernel version, but field names vary. */
+struct my_kbd_repeat {
         int delay;        /* in msec; <= 0: don't change */
         int period;       /* in msec; <= 0: don't change */
+                         /* earlier this field was misnamed "rate" */
 };
-#endif
+
+#include <signal.h>
 
 #include "nls.h"
 #include "version.h"
@@ -104,15 +107,18 @@ static int valid_delays[] = { 250, 500, 750, 1000 };
 
 static int
 KDKBDREP_ioctl_ok(double rate, int delay, int silent) {
-       /* This ioctl is defined in <linux/kd.h> but is not
-          implemented anywhere - must be in some m68k patches. */
-       struct kbd_repeat kbdrep_s;
+       /*
+        * This ioctl is defined in <linux/kd.h> but is not
+        * implemented anywhere - must be in some m68k patches.
+        * Since 2.4.9 also on i386.
+        */
+       struct my_kbd_repeat kbdrep_s;
 
        /* don't change, just test */
        kbdrep_s.period = -1;
        kbdrep_s.delay = -1;
        if (ioctl( 0, KDKBDREP, &kbdrep_s )) {
-               if (errno == EINVAL)
+               if (errno == EINVAL || errno == ENOTTY)
                        return 0;
                perror( "ioctl(KDKBDREP)" );
                exit( 1 );
@@ -149,11 +155,36 @@ KDKBDREP_ioctl_ok(double rate, int delay, int silent) {
                printf( _("Typematic Rate set to %.1f cps (delay = %d ms)\n"),
                        rate, kbdrep_s.delay );
 
+       kbdrep_s.period = -1;
+       kbdrep_s.delay = -1;
+       if (ioctl( 0, KDKBDREP, &kbdrep_s )) {
+               if (errno == EINVAL)
+                       return 0;
+               perror( "ioctl(KDKBDREP)" );
+               exit( 1 );
+       }
+       printf("old delay %d, period %d\n",
+              kbdrep_s.delay, kbdrep_s.period);
+       if (kbdrep_s.period == 0)
+               rate = 0;
+       else
+               rate = 1000.0 / (double) kbdrep_s.period;
+
+       if (!silent)
+               printf( _("Typematic Rate set to %.1f cps (delay = %d ms)\n"),
+                       rate, kbdrep_s.delay );
+
        return 1;                       /* success! */
 }
 
+#ifndef KIOCSRATE
+#define arg_state attr_unused
+#else
+#define arg_state
+#endif
+
 static int
-KIOCSRATE_ioctl_ok(double rate, int delay, int silent) {
+KIOCSRATE_ioctl_ok(arg_state double rate, arg_state int delay, arg_state int silent) {
 #ifdef KIOCSRATE
        struct kbd_rate kbdrate_s;
        int fd;
@@ -164,10 +195,10 @@ KIOCSRATE_ioctl_ok(double rate, int delay, int silent) {
                exit( 1 );
        }
 
-       kbdrate_s.period = (int) (rate + 0.5);  /* round up */
+       kbdrate_s.rate = (int) (rate + 0.5);  /* round up */
        kbdrate_s.delay = delay * HZ / 1000;  /* convert ms to Hz */
-       if (kbdrate_s.period > 50)
-               kbdrate_s.period = 50;
+       if (kbdrate_s.rate > 50)
+               kbdrate_s.rate = 50;
 
        if (ioctl( fd, KIOCSRATE, &kbdrate_s )) {
                perror( "ioctl(KIOCSRATE)" );
@@ -177,7 +208,7 @@ KIOCSRATE_ioctl_ok(double rate, int delay, int silent) {
 
        if (!silent)
                printf( "Typematic Rate set to %d cps (delay = %d ms)\n",
-                       kbdrate_s.period, kbdrate_s.delay * 1000 / HZ );
+                       kbdrate_s.rate, kbdrate_s.delay * 1000 / HZ );
 
        return 1;
 #else /* no KIOCSRATE */
@@ -185,6 +216,12 @@ KIOCSRATE_ioctl_ok(double rate, int delay, int silent) {
 #endif /* KIOCSRATE */
 }
 
+static void
+sigalrmhandler( attr_unused int sig ) {
+       fprintf( stderr, "kbdrate: Failed waiting for kbd controller!\n" );
+       raise( SIGINT );
+}
+
 int
 main( int argc, char **argv ) {
 #ifdef __sparc__
@@ -200,14 +237,14 @@ main( int argc, char **argv ) {
        int         fd;
        char        data;
        int         c;
-       int         i;
+       unsigned int i;
        extern char *optarg;
 
        set_progname(argv[0]);
 
        setlocale(LC_ALL, "");
-       bindtextdomain(PACKAGE, LOCALEDIR);
-       textdomain(PACKAGE);
+       bindtextdomain(PACKAGE_NAME, LOCALEDIR);
+       textdomain(PACKAGE_NAME);
 
        if (argc == 2 &&
            (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")))
@@ -260,23 +297,40 @@ main( int argc, char **argv ) {
                exit( 1 );
        }
 
+       signal( SIGALRM, sigalrmhandler );
+       alarm( 3 );
+
        do {
                lseek( fd, 0x64, 0 );
-               read( fd, &data, 1 );
+               if (read( fd, &data, 1 ) == -1) {
+                       perror( "read" );
+                       exit( 1 );
+               }
        } while ((data & 2) == 2 );  /* wait */
 
        lseek( fd, 0x60, 0 );
        data = 0xf3;                 /* set typematic rate */
-       write( fd, &data, 1 );
+       if (write( fd, &data, 1 ) == -1) {
+               perror( "write" );
+               exit( 1 );
+       }
 
        do {
                lseek( fd, 0x64, 0 );
-               read( fd, &data, 1 );
+               if (read( fd, &data, 1 ) == -1) {
+                       perror( "read" );
+                       exit( 1 );
+               }
        } while ((data & 2) == 2 );  /* wait */
 
+       alarm( 0 );
+
        lseek( fd, 0x60, 0 );
        sleep( 1 );
-       write( fd, &value, 1 );
+       if (write( fd, &value, 1 ) == -1) {
+               perror( "write" );
+               exit( 1 );
+       }
 
        close( fd );