Thread: Wont run in X, works fine in alt+F1 terminal

  1. #1
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19

    Wont run in X, works fine in alt+F1 terminal

    i have this code to determine which lights are on, on a keyboard.

    but if i run it in x windows it doesn't work, i get the error
    Code:
    ioctl: Invalid Argument
    but if i press ctrl+alt+f1 for example the code will work in one of these terminals fine.

    is there a way to get it to work in x windows, or is ioctl limited to terminals ??

    here is the code
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <linux/kd.h>  /* Keyboard IOCTLs */
    #include <sys/ioctl.h> /* ioctl()         */
    #include <sys/types.h>
      
    #define ERROR -1
     
    void checkleds();
    long int arg;
    int main()
        {
           int fd;       /* Console fd (/dev/tty). Used as fd in ioctl() */
           //long int arg; /* Where the LED states will be put into.       */
     
              /* To use as the fd in ioctl(). */
              if ((fd = open("/dev/tty", O_NOCTTY)) == ERROR) {
                 perror("open");
                 exit(ERROR);
              }
              printf("first %d\n" , arg);
              /* Value stored in arg. */
              if (ioctl(fd, KDGETLED,&arg) == ERROR) {
                 perror("ioctl");
                 printf("%d\n", arg);
                 close(fd);
                 exit(ERROR);
              }
     
              /* Here we print out current LEDS. */
            checkleds();
        }
     
    void checkleds()
        {
            /* LED_SCR = 0x1, LED_NUM = 0x2, LED_CAP = 0x4 */
     
            if (arg == LED_SCR) printf("Scroll Lock LED is on.\n");
            else if (arg == LED_NUM) printf("Numeric Lock LED is on.\n");
            else if (arg == LED_CAP) printf("Caps Lock LED is on.\n");
     
            else if (arg == LED_NUM + LED_CAP)
               printf("Numeric Lock and Caps Lock LEDs are on.\n");
     
            else if (arg == LED_NUM + LED_SCR)
               printf("Numeric Lock and Scroll Lock LEDs are on.\n");
     
            else if (arg == LED_CAP + LED_SCR)
               printf("Caps Lock and Scroll Lock LEDs are on.\n");
     
            else if (arg == LED_NUM + LED_SCR + LED_CAP)
               printf("Numeric Lock, Scroll Lock, and Caps Lock LEDs are on.\n");
     
            else if (arg == 0)
              printf("No LEDs are on. \n");
     
            }
    thanks for any help in this matter
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I would think that X takes over the keyboard functions and does something else with them. You might have to go to another website. but to critique your code
    Code:
    1. First you need to include stdlib.h for exit and unistd.h for close
    2. You need %ld in your printf statements
    3. You need to return 0 in your main function

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Did you try including <sys/kbio.h>, I think that is where KDGETLED is defined.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    GETLED is defined in ioctl.h, i searched for kbio.h and it seems that it is used mainly for SPARC systems
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    oh okay, It should work in X unelss X is doing something to restrict it. ioctl's communicate directly to the kernel and then to the device, X must be restricting that ioctl, don't know why or how, I will try and do mroe research on this topic.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    thanks a lot, this is really baffling me as well
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateFile returns err 5 on 2000, but works fine of XP
    By Yarin in forum Windows Programming
    Replies: 26
    Last Post: 08-10-2008, 09:34 AM
  2. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM
  3. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  4. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM
  5. Thanks Stoned_Coder! ALT key works!!
    By johnnyd in forum C Programming
    Replies: 0
    Last Post: 03-11-2002, 10:39 AM