linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Modifying keycodes or scancodes of keyboard devices
@ 2012-09-11 15:21 Mathias Laurenz Baumann
  2012-09-12 20:49 ` Bruno Prémont
  0 siblings, 1 reply; 2+ messages in thread
From: Mathias Laurenz Baumann @ 2012-09-11 15:21 UTC (permalink / raw)
  To: linux-input

Hello,

I want to write a kernel module that hooks in somewhere between the  
keyboard driver and whatever
uses the resulting key events (e.g. evdev or xkb).

My goal is to modify the keycodes or scancodes (whatever works best and is  
least amount of work)
reported to the next function in the input processing chain, with the  
intended result of
modifying the keyboard layout.

I could of course just use xkb and create a custom mapping, but my  
experience with those is that
many applications have trouble with some mappings. One example is the  
neo-layout[1] which provides
alternative mappings for the arrow keys or the navigation keys like pos1,  
end, return and the
numblock, only activated when a certain modifier is active (e.g. AltGr).  
Many programs have trouble
when those alternatives are used.

My idea is to do that mapping on a level deeper than xkb can provide, thus  
avoiding those problems and faking
a keyboard that has that layout in hardware, at least from the point of  
view of xkb.

I have no experience with kernel hacking, but I did some reading and..  
well here is my idea:

There are the functions input_set_keycode[0] and input_get_keycode which  
sound like they do what I want to do.
So I figured, I write a simple kernel module that just calls these  
functions with the mappings I want to do.

Trouble is, they also want a pointer to the device that this concerns.
That is of course a reasonable thing to require for that function, just  
that I have no idea where to get that from.

I couldn't find any function that would return me a list of the input  
devices.

I also saw input_grab_* and input_inject_* which sounds like it could also  
be used for my purpose .. but input_set_keycode seemed more reasonable ....

So my questions are:
* Is this approach feasible? Would it work?
* If yes, where to I get the struct input_dev* device, or a list of them,  
from?
* If no, what approach do you suggest?

     --Marenz


[0]  
http://kernel.org/doc/htmldocs/device-drivers/API-input-set-keycode.html
[1] http://neo-layout.org

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Modifying keycodes or scancodes of keyboard devices
  2012-09-11 15:21 Modifying keycodes or scancodes of keyboard devices Mathias Laurenz Baumann
@ 2012-09-12 20:49 ` Bruno Prémont
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Prémont @ 2012-09-12 20:49 UTC (permalink / raw)
  To: Mathias Laurenz Baumann; +Cc: linux-input

Hallo Mathias,

On Tue, 11 September 2012 "Mathias Laurenz Baumann" <marenz@supradigital.org> wrote:
> I want to write a kernel module that hooks in somewhere between the  
> keyboard driver and whatever
> uses the resulting key events (e.g. evdev or xkb).

You can usually just tell the kernel to map scancodes to different keycodes
unless the driver you are targetting does something really weird.


Have a look at the following ioctls to change the scancode-keycode mappings
or even just find out what the current mappings are:
  EVIOCGKEYCODE
  EVIOCSKEYCODE
They both take and int[2] as argument for which the first int is the scancode
and the second one the keycode (there is a new revision of those ioctls that
work differently, for you to look them up).

Just issue the ioctls on your event device.


Sample userspace code for looking up mappings (note that you will eventually
need to adjust the scancode search range, brute-force 0x0..0xffffffff, best is
to capture a few evdev events to determine how the scancodes look like):

  int evdev = open("/dev/input/eventX", O_RDONLY);
  int codes[2];

  printf("Scancode mapping table:\n");
  for (codes[0] = 0x70000; codes[0] < 0x70060; codes[0]++) {
    if (ioctl(evdev, EVIOCGKEYCODE, codes) >= 0) {
      if (codes[1] == 0)
        continue;
      printf("0x%04x        => %s  (%d)\n", codes[0], get_key_name(codes[1]), codes[1]);
    } else if (errno != EINVAL) {
      fprintf(stderr, "Failed to get mapping for scancode %d: %s\n", codes[0], strerror(errno));
    } else
      continue;
  }


For the meaning of the keycodes, see linux/input.h (KEY_*)

Bruno

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-09-12 20:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-11 15:21 Modifying keycodes or scancodes of keyboard devices Mathias Laurenz Baumann
2012-09-12 20:49 ` Bruno Prémont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).