keymap.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. (define-module (gnu installer keymap)
  20. #:use-module (guix records)
  21. #:use-module (sxml match)
  22. #:use-module (sxml simple)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 ftw)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 regex)
  27. #:export (<x11-keymap-model>
  28. x11-keymap-model
  29. make-x11-keymap-model
  30. x11-keymap-model?
  31. x11-keymap-model-name
  32. x11-keymap-model-description
  33. <x11-keymap-layout>
  34. x11-keymap-layout
  35. make-x11-keymap-layout
  36. x11-keymap-layout?
  37. x11-keymap-layout-name
  38. x11-keymap-layout-synopsis
  39. x11-keymap-layout-description
  40. x11-keymap-layout-variants
  41. <x11-keymap-variant>
  42. x11-keymap-variant
  43. make-x11-keymap-variant
  44. x11-keymap-variant?
  45. x11-keymap-variant-name
  46. x11-keymap-variant-description
  47. default-keyboard-model
  48. xkb-rules->models+layouts
  49. kmscon-update-keymap))
  50. (define-record-type* <x11-keymap-model>
  51. x11-keymap-model make-x11-keymap-model
  52. x11-keymap-model?
  53. (name x11-keymap-model-name) ;string
  54. (description x11-keymap-model-description)) ;string
  55. (define-record-type* <x11-keymap-layout>
  56. x11-keymap-layout make-x11-keymap-layout
  57. x11-keymap-layout?
  58. (name x11-keymap-layout-name) ;string
  59. (synopsis x11-keymap-layout-synopsis) ;string (e.g., "en")
  60. (description x11-keymap-layout-description) ;string (a whole phrase)
  61. (variants x11-keymap-layout-variants)) ;list of <x11-keymap-variant>
  62. (define-record-type* <x11-keymap-variant>
  63. x11-keymap-variant make-x11-keymap-variant
  64. x11-keymap-variant?
  65. (name x11-keymap-variant-name) ;string
  66. (description x11-keymap-variant-description)) ;string
  67. ;; Assume all modern keyboards have this model.
  68. (define default-keyboard-model (make-parameter "pc105"))
  69. (define (xkb-rules->models+layouts file)
  70. "Parse FILE and return two values, the list of supported X11-KEYMAP-MODEL
  71. and X11-KEYMAP-LAYOUT records. FILE is an XML file from the X Keyboard
  72. Configuration Database, describing possible XKB configurations."
  73. (define maybe-empty
  74. (match-lambda
  75. ((x) x)
  76. (#f "")))
  77. (define (model m)
  78. (sxml-match m
  79. [(model
  80. (configItem
  81. (name ,name)
  82. (description ,description)
  83. . ,rest))
  84. (x11-keymap-model
  85. (name name)
  86. (description description))]))
  87. (define (variant v)
  88. (sxml-match v
  89. [(variant
  90. ;; According to xbd-rules DTD, the definition of a
  91. ;; configItem is: <!ELEMENT configItem
  92. ;; (name,shortDescription*,description*,vendor?,
  93. ;; countryList?,languageList?,hwList?)>
  94. ;;
  95. ;; shortDescription and description are optional elements
  96. ;; but sxml-match does not support default values for
  97. ;; elements (only attributes). So to avoid writing as many
  98. ;; patterns as existing possibilities, gather all the
  99. ;; remaining elements but name in REST-VARIANT.
  100. (configItem
  101. (name ,name)
  102. . ,rest-variant))
  103. (x11-keymap-variant
  104. (name name)
  105. (description (maybe-empty
  106. (assoc-ref rest-variant 'description))))]))
  107. (define (layout l)
  108. (sxml-match l
  109. [(layout
  110. (configItem
  111. (name ,name)
  112. . ,rest-layout)
  113. (variantList ,[variant -> v] ...))
  114. (x11-keymap-layout
  115. (name name)
  116. (synopsis (maybe-empty
  117. (assoc-ref rest-layout 'shortDescription)))
  118. (description (maybe-empty
  119. (assoc-ref rest-layout 'description)))
  120. (variants (list v ...)))]
  121. [(layout
  122. (configItem
  123. (name ,name)
  124. . ,rest-layout))
  125. (x11-keymap-layout
  126. (name name)
  127. (synopsis (maybe-empty
  128. (assoc-ref rest-layout 'shortDescription)))
  129. (description (maybe-empty
  130. (assoc-ref rest-layout 'description)))
  131. (variants '()))]))
  132. (let ((sxml (call-with-input-file file
  133. (lambda (port)
  134. (xml->sxml port #:trim-whitespace? #t)))))
  135. (match
  136. (sxml-match sxml
  137. [(*TOP*
  138. ,pi
  139. (xkbConfigRegistry
  140. (@ . ,ignored)
  141. (modelList ,[model -> m] ...)
  142. (layoutList ,[layout -> l] ...)
  143. . ,rest))
  144. (list
  145. (list m ...)
  146. (list l ...))])
  147. ((models layouts)
  148. (values models layouts)))))
  149. (define (kmscon-update-keymap model layout variant options)
  150. "Update kmscon keymap with the provided MODEL, LAYOUT, VARIANT and OPTIONS."
  151. (and=>
  152. (getenv "KEYMAP_UPDATE")
  153. (lambda (keymap-file)
  154. (unless (file-exists? keymap-file)
  155. (error "Unable to locate keymap update file"))
  156. ;; See file gnu/packages/patches/kmscon-runtime-keymap-switch.patch.
  157. ;; This dirty hack makes possible to update kmscon keymap at runtime by
  158. ;; writing an X11 keyboard model, layout and variant to a named pipe
  159. ;; referred by KEYMAP_UPDATE environment variable.
  160. (call-with-output-file keymap-file
  161. (lambda (port)
  162. (format port model)
  163. (put-u8 port 0)
  164. (format port layout)
  165. (put-u8 port 0)
  166. (format port (or variant ""))
  167. (put-u8 port 0)
  168. (format port (or options ""))
  169. (put-u8 port 0))))))