#!/usr/bin/env bash
# vim: set expandtab ts=4 sw=4:
#
# Generate lircrc files for various applications
#
# Usage: See help function below
#
# BTN_* keys in namespace are ignored.
readonly LIRC_CONF='/etc/lirc/lircd.conf'
readonly OUTPUT_DIR="$HOME/.config/lirc"
declare -a ALL_LIRCD_KEYS # All buttons in /etc/lirc/lircd.conf
declare -a BAD_LIRCD_KEYS # Buttons not listed by irrecord -l
declare -a DUPLICATE_KEYS
declare -a GOOD_KEYS
declare -a GOOD_KEYS=( $( irrecord -l |
egrep '^KEY_|BTN_LEFT|BTN_RIGHT' |
egrep -v 'KEY_KP[0-9]' |
egrep -v 'KEY_NUMERIC_[0-9]')
)
GOOD_KEYS+=('MOUSE_NW' 'MOUSE_N' 'MOUSE_NE'
'MOUSE_W' 'MOUSE_E' 'MOUSE_SW' 'MOUSE_SE')
readonly GOOD_KEYS
declare -A OPTIONS # Option values, with single-letter
# option as key.
function help
{
cat << EOF
Generate lirc configuration files.
Usage:
$( basename $0 ) -s [-c file]
$( basename $0 ) -u [-c file]
$( basename $0 ) -l
$( basename $0 ) [-o directory] [-c file] [files...]
$( basename $0 ) [-d] [-c file] [files...]
Options:
-s Scan lircd file for unusable keycodes and exit.
-u Update existing lircd file to use key symbols in namespace.
-l List available files to generate.
-d Dump file template(s) usable as stdin on stdout.
-o
Output directory, defaults to $OUTPUT_DIR.
-c lircd(8) config file, defaults to $LIRC_CONF.
-h Print this message.
-m Display manpage.
[files] is a list of files to build or dump, as listed by -l. Defaults
to all available files except stdin.
Only one of the options -s, -u, -l and -d can be given.
EOF
}
function manpage()
{
cat << EOF
.TH $(basename $0)"1" "Last change: Sep 2014" "lircd @version@" \
"System Administration Utilities"
.SH NAME
$(basename $0) \- tool to scan and generate lirc config files
.SH SYNOPSIS
.br
.B
.LL 90
$(basename $0) \fR \-s|-u [ -c FILE]
.br
.B
.LL=20
$(basename $0) \fR [-o DIR] [-d] [-c FILE] [FILES]
.br
.B
$(basename $0) \fR \-l
.br
.SH OPTIONS
Without options, generates all available application specific lircrc
files in ~/.config/lirc. The optional [files] is a list of files to
build or dump, as listed by -l.
.TP
\fB\-s\fR
Scan lircd file for key symbols which are duplicated or not in namespace.
.TP
\fB\-u\fR
Update existing lircd file to use key symbols in namespace.
.TP
\fB\-l\fR
List available files to generate.
.TP
\fB\-d\fR
Dump file templates usable as stdin on stdout.
.TP
\fB\-o \\fR
Output directory, defaults to ~/.config/lirc
.TP
\fB\-c \\fR
lircd(8) config file, defaults to /etc/lirc/lircd.conf
.TP
\fB\-h\fR
Print help message
.PP
.SH DESCRIPTION
.PP
The script is designed to assist in the setup of the lirc(8)
configuration files for ordinary remote controls. It is not aimed
to handle full-blown keyboards or joystick devices. The generated
lircrc files represents a starting point which will need further,
manual refinements but might "work" in a basic sense in many cases.
.PP
The setup involves normalizing the lircd.conf to use symbols in the
legal namespace, and to create application specific lircrc files which
uses the normalized symbols.
.PP
To assist normalizing /etc/lircd.conf, the script can report symbols
used which not are in the namespace. It can can also update many of
these symbols to corresponding symbols within the namespace.
.PP
The script also can create application specific lircrc files such as
mythtv.lircrc or vlc.lircrc. It then uses the normalized
key symbols in /etc/lirdc.conf. By default, files are created in
~/.config/lirc.
.PP
Generated lircrc files basically combines known keys from
/etc/lirc/lircd.conf with template data for an application. The process
results in a lircrc file, a list a of unused keys and some entries
which don't match any existing key. The unused keys and unmatched entries
are written as comments into the generated file.
An 'illegal' key i. e., a key not in namespace, will not match any entry
and will thus always be unused. However, it's perfectly possible to
use it later on while manually modifying the lircrc files.
.PP
When updating files, script always makes backup copies.
.PP
The generated lircrc files are intended to be included in a main
~/.config/lircrc file. A minimal such might look like
.IP
include ~/.config/lirc/mythtv.lircrc
.SH EXAMPLES
.PP
Scan /etc/lirc/lircd.conf for duplicated symbols and symbols not in namespace:
.IP
$(basename $0) -s
.PP
Update a local lircd.conf file to use symbols in namespace:
.IP
$(basename $0) -uc ./lircd.conf
.PP
List available files to generate:
.IP
$(basename $0) -l
.PP
Generate one of the files listed by -l, creates ~/.config/lirc/mythtv.lircrc:
.IP
$(basename $0) mythtv
.PP
Dump template data for mythtv:
.IP
$(basename $0) -d mythtv \>mythtv.lircdata
.PP
Use possibly edited data to create a modified mythtv.lircrc in current dir:
.IP
$(basename $0) -o . stdin \< mythtv.lircdata
.SH LIRCD.CONF CONVERSIONS
The -s option simply reports all key symbols not in namespace. It also
reports some symbols which although legal are inappropriate in this
context, notably KEY_NUMERIC_0..KEY_NUMERIC_9. If present, duplicated
keys are also reported.
The -u option converts several known symbols to their corresponding
KEY_* symbols. Symbols which have names like KEY_WINDOWS but not are in
namespace gets a X_ prefix e. g., KEY_WINDOWS -> X_KEY_WINDOWS. The
converted file thus guarantees that any key symbol starting with
KEY_ is a legal symbol.
All known numeric keys such as '1', 'KEY_1', 'KEY_KP_1', 'KEY_NUMERIC_1
are converted to KEY_1.
All modifications are marked by comments in file.
.SH EXTENDING
The script can be extended to support more applications by providing a shell
function named *.lircdata. E. g., to add a new application named
"my_app" based on the built-in vlc application:
.PP
.ft CW
$ $(basename $0) -d vlc >my_app.lircdata
$ function my_app_lircdata()
> {
> cat my_app.lircdata
> }
$ export -f my_app_lircdata
$ $(basename $0) my_app
.ft R
.SH DATA FORMAT
The script uses a simple, line-oriented format to represent the lircrc files.
The purpose of the format is to be compact and thus to give a good overview,
and also to be easy to parse. Each line is divided in comma-separated tokens.
There are four kind of lines.
.PP
Comment lines begins with a '#'. These are copied verbatim to the output file.
.PP
The first data line represents global items, applicable to all entrys. The
first word is the program name. It's used in the first 'program=' line.
Following words, if present, are optional lines which will be appended to each
entry in the file.
.PP
A data line starting with 'begin' or 'end' is a mode block designator. It's
copied verbatim to output file.
.PP
All other data lines represents a button/config entry. The format is
.ft CW