keymap.scm 6.2 KB

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