1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2021 Mathieu Othacehe <othacehe@gnu.org>
3 ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu installer keymap)
21 #:use-module (guix records)
22 #:use-module (sxml match)
23 #:use-module (sxml simple)
24 #:use-module (ice-9 binary-ports)
25 #:use-module (ice-9 ftw)
26 #:use-module (ice-9 match)
27 #:use-module (ice-9 regex)
28 #:export (<x11-keymap-model>
33 x11-keymap-model-description
37 make-x11-keymap-layout
39 x11-keymap-layout-name
40 x11-keymap-layout-synopsis
41 x11-keymap-layout-description
42 x11-keymap-layout-variants
46 make-x11-keymap-variant
48 x11-keymap-variant-name
49 x11-keymap-variant-description
51 default-keyboard-model
52 xkb-rules->models+layouts
53 kmscon-update-keymap))
55 (define-record-type* <x11-keymap-model>
56 x11-keymap-model make-x11-keymap-model
58 (name x11-keymap-model-name) ;string
59 (description x11-keymap-model-description)) ;string
61 (define-record-type* <x11-keymap-layout>
62 x11-keymap-layout make-x11-keymap-layout
64 (name x11-keymap-layout-name) ;string
65 (synopsis x11-keymap-layout-synopsis) ;string (e.g., "en")
66 (description x11-keymap-layout-description) ;string (a whole phrase)
67 (variants x11-keymap-layout-variants)) ;list of <x11-keymap-variant>
69 (define-record-type* <x11-keymap-variant>
70 x11-keymap-variant make-x11-keymap-variant
72 (name x11-keymap-variant-name) ;string
73 (description x11-keymap-variant-description)) ;string
75 ;; Assume all modern keyboards have this model.
76 (define default-keyboard-model (make-parameter "pc105"))
78 (define (xkb-rules->models+layouts file)
79 "Parse FILE and return two values, the list of supported X11-KEYMAP-MODEL
80 and X11-KEYMAP-LAYOUT records. FILE is an XML file from the X Keyboard
81 Configuration Database, describing possible XKB configurations."
92 (description ,description)
96 (description description))]))
101 ;; According to xbd-rules DTD, the definition of a
102 ;; configItem is: <!ELEMENT configItem
103 ;; (name,shortDescription*,description*,vendor?,
104 ;; countryList?,languageList?,hwList?)>
106 ;; shortDescription and description are optional elements
107 ;; but sxml-match does not support default values for
108 ;; elements (only attributes). So to avoid writing as many
109 ;; patterns as existing possibilities, gather all the
110 ;; remaining elements but name in REST-VARIANT.
116 (description (maybe-empty
117 (assoc-ref rest-variant 'description))))]))
125 (variantList ,[variant -> v] ...))
128 (synopsis (maybe-empty
129 (assoc-ref rest-layout 'shortDescription)))
130 (description (maybe-empty
131 (assoc-ref rest-layout 'description)))
132 (variants (list v ...)))]
139 (synopsis (maybe-empty
140 (assoc-ref rest-layout 'shortDescription)))
141 (description (maybe-empty
142 (assoc-ref rest-layout 'description)))
145 (let ((sxml (call-with-input-file file
147 (xml->sxml port #:trim-whitespace? #t)))))
154 (modelList ,[model -> m] ...)
155 (layoutList ,[layout -> l] ...)
161 (values models layouts)))))
163 (define (kmscon-update-keymap model layout variant options)
164 "Update kmscon keymap with the provided MODEL, LAYOUT, VARIANT and OPTIONS."
166 (getenv "KEYMAP_UPDATE")
167 (lambda (keymap-file)
168 (unless (file-exists? keymap-file)
169 (error "Unable to locate keymap update file"))
171 ;; See file gnu/packages/patches/kmscon-runtime-keymap-switch.patch.
172 ;; This dirty hack makes possible to update kmscon keymap at runtime by
173 ;; writing an X11 keyboard model, layout and variant to a named pipe
174 ;; referred by KEYMAP_UPDATE environment variable.
175 (call-with-output-file keymap-file
183 (format port (or variant ""))
186 (format port (or options ""))