Hatherly.com

Blog



MythTV and “Cypress” remote control that emulates a keyboard

Posted on May 2, 2016 by Adam under MythTV, Ubuntu

Some time ago I bought a new home theatre PC case for my mythtv box, and it came with an in-built IR remote control. I didn’t use it at first, preferring to use a pulse-eight CEC adapter with my normal TV remote, but when that stopped working the other day I thought I would see if I could get the case remote working with MythTV.
02-05-2016 08-49-28_0003
I had assumed it would just be a case of configuring lircd (the usual Linux infra-red remote control daemon, but it turned out it was actually showing up as a keyboard and mouse – as you can see from the output from running xinput list:

⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ Cypress Cypress USB Keyboard / PS2 Mouse	id=14	[slave  pointer  (2)]
⎜   ↳ MCE IR Keyboard/Mouse (saa716x)         	id=17	[slave  pointer  (2)]
⎜   ↳ Pulse-Eight USB-CEC Adapter             	id=15	[slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver                   	id=9	[slave  pointer  (2)]
⎜   ↳ Logitech USB Receiver                   	id=10	[slave  pointer  (2)]
⎜   ↳ SONiX USB Device                        	id=11	[slave  pointer  (2)]
⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
    ↳ Power Button                            	id=6	[slave  keyboard (3)]
    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
    ↳ Power Button                            	id=8	[slave  keyboard (3)]
    ↳ Cypress Cypress USB Keyboard / PS2 Mouse	id=13	[slave  keyboard (3)]
    ↳ saa716x IR (TurboSight TBS 6280)        	id=16	[slave  keyboard (3)]
    ↳ SONiX USB Device                        	id=12	[slave  keyboard (3)]

The device we are interested in is “Cypress Cypress USB Keyboard / PS2 Mouse”. The first job is to see what keyboard events the remote is generating when we press keys. To do that, use xev, which will show you a whole load of keyboard and mouse events (each key press includes a keydown event and a keyup event). For example, when we press the “Back” button on our remote, we get this:

KeyPress event, serial 37, synthetic NO, window 0x4800001,
    root 0xe4, subw 0x0, time 48537039, (150,532), root:(150,584),
    state 0x0, keycode 22 (keysym 0xff08, BackSpace), same_screen YES,
    XLookupString gives 1 bytes: (08) "
    XmbLookupString gives 1 bytes: (08) "
    XFilterEvent returns: False

The keycode from our remote we want to remap is “keycode 22”, currently mapped to a key event of “Backspace”. I want this button to actually send an Escape key rather than Backspace, so on my main keyboard I press Escape to see what the action is:

KeyPress event, serial 38, synthetic NO, window 0x4c00001,
    root 0xe4, subw 0x0, time 48697777, (492,-51), root:(492,1),
    state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,
    XLookupString gives 1 bytes: (1b) ""
    XmbLookupString gives 1 bytes: (1b) ""
    XFilterEvent returns: False

So, the action we want is called (unsurprisingly) “Escape”.

This page show how to remap keys globally, but we only want to remap it for this device so we don’t also remap the keys on the keyboard! So this link can help us do that.

So, using the link in the above page we can map our keycode 22 to an XKB code of <BKSP>. Now, we can make a script like the one in the above article to remap this key (and others we want to remap). The line in the script for our back key will look like this:

    key <BKSP>  { [ Escape ]       };

Which maps the <BKSP> key to the “Escape” key event. I was able to remap other keys in the same way to get keypresses I could use in MythTV.

One challenge I had was with some of the buttons on the remote that send multiple keypresses. For example, the menu button sends Alt+Super+Enter. This caused me some head-scratching as I couldn’t get MythTV to properly process any keypresses with Alt, Ctrl or Super (a.k.a. the Windows key) pressed (I suspect Gnome was intercepting them). In the end I remapped both Alt and Ctrl to Shift, and rather sneakily I mapped Super to “m” (the key for opening the Menu in MythTV), which works quite nicely for my purposes. I few tweaks to the MythTV keyboard bindings (which you can do in MythWeb) and I now have a fully working remote!

This is the full script to remap the keys:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
# Find the ID of our keyboard (using the name "Cypress")
remote_id=$(
    xinput list |
    sed -n 's/.*Cypress.*id=\([0-9]*\).*keyboard.*/\1/p'
)

# Remap our keys:
mkdir -p /tmp/xkb/symbols
cat >/tmp/xkb/symbols/custom <<\EOF
xkb_symbols "remote" {
    key <BKSP>  { [ Escape ]       };
    key <COMP>  { [ i ]       };
    key <LCTL>  { [ Shift_L ]       };
    key <LALT>  { [ Shift_L ]       };
    key <LWIN>  { [ m ]         };
};
EOF

setxkbmap -device $remote_id -print | sed 's/\(xkb_symbols.*\)"/\1+custom(remote)"/' \
 | xkbcomp -I/tmp/xkb -i $remote_id -synch - $DISPLAY 2>/dev/null

And I added this script as 99remapRemoteControlButtons.sh in /etc/X11/Xsession.d so it runs when X11 starts.



  |  Leave a comment


No comments




Leave a Comment