Commit | Line | Data |
---|---|---|
db1e2522 | 1 | ;;; GNU Guix --- Functional package management for GNU |
2729cb40 | 2 | ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
db1e2522 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (gnu system keyboard) | |
20 | #:use-module (guix gexp) | |
21 | #:use-module ((gnu packages xorg) | |
22 | #:select (xkeyboard-config console-setup)) | |
23 | #:use-module (srfi srfi-9 gnu) | |
24 | #:use-module (ice-9 match) | |
25 | #:export (keyboard-layout? | |
26 | keyboard-layout | |
27 | keyboard-layout-name | |
28 | keyboard-layout-variant | |
29 | keyboard-layout-model | |
30 | keyboard-layout-options | |
31 | ||
32 | keyboard-layout->console-keymap)) | |
33 | ||
34 | ;;; Commentary: | |
35 | ;;; | |
36 | ;;; This module provides a data structure to represent keyboard layouts | |
37 | ;;; according to the XKB naming and classification (see the 'xkeyboard-config' | |
38 | ;;; package). | |
39 | ;;; | |
40 | ;;; Code: | |
41 | ||
42 | (define-immutable-record-type <keyboard-layout> | |
43 | (%keyboard-layout name variant model options) | |
44 | keyboard-layout? | |
45 | (name keyboard-layout-name) ;string | |
46 | (variant keyboard-layout-variant) ;#f | string | |
47 | (model keyboard-layout-model) ;#f | string | |
48 | (options keyboard-layout-options)) ;list of strings | |
49 | ||
50 | (define* (keyboard-layout name #:optional variant | |
51 | #:key model (options '())) | |
52 | "Return a new keyboard layout with the given NAME and VARIANT. | |
53 | ||
54 | NAME must be a string such as \"fr\"; VARIANT must be a string such as | |
55 | \"bepo\" or \"nodeadkeys\". See the 'xkeyboard-config' package for valid | |
56 | options." | |
57 | (%keyboard-layout name variant model options)) | |
58 | ||
59 | (define* (keyboard-layout->console-keymap layout | |
60 | #:key | |
61 | (xkeyboard-config xkeyboard-config)) | |
62 | "Return a Linux console keymap file for LAYOUT, a <keyboard-layout> record. | |
63 | Layout information is taken from the XKEYBOARD-CONFIG package." | |
64 | (define build | |
65 | (with-imported-modules '((guix build utils)) | |
66 | #~(begin | |
67 | (use-modules (guix build utils) | |
68 | (ice-9 popen) | |
69 | (ice-9 match)) | |
70 | ||
71 | (define pipe | |
72 | (open-pipe* OPEN_READ | |
73 | #+(file-append console-setup "/bin/ckbcomp") | |
74 | (string-append "-I" | |
75 | #+(file-append xkeyboard-config | |
76 | "/share/X11/xkb")) | |
77 | "-rules" "base" | |
78 | #$@(match (keyboard-layout-model layout) | |
79 | (#f '()) | |
80 | (model `("-model" ,model))) | |
81 | #$(keyboard-layout-name layout) | |
82 | #$(or (keyboard-layout-variant layout) | |
83 | "") | |
84 | #$(string-join (keyboard-layout-options layout) ","))) | |
85 | ||
86 | (call-with-output-file #$output | |
87 | (lambda (output) | |
88 | (dump-port pipe output))) | |
89 | ||
90 | ;; Note: ckbcomp errors out when the layout name is unknown, but | |
91 | ;; merely emits a warning when the variant is unknown. | |
92 | (unless (zero? (close-pipe pipe)) | |
93 | (error "failed to create console keymap for keyboard layout" | |
94 | #$(keyboard-layout-name layout)))))) | |
95 | ||
96 | (computed-file (string-append "console-keymap." | |
2729cb40 LC |
97 | (string-map (match-lambda |
98 | (#\, #\-) | |
99 | (chr chr)) | |
100 | (keyboard-layout-name layout))) | |
db1e2522 | 101 | build)) |