keyboard.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  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 system keyboard)
  19. #:use-module (guix gexp)
  20. #:use-module ((gnu packages xorg)
  21. #:select (xkeyboard-config console-setup))
  22. #:use-module (srfi srfi-9 gnu)
  23. #:use-module (ice-9 match)
  24. #:export (keyboard-layout?
  25. keyboard-layout
  26. keyboard-layout-name
  27. keyboard-layout-variant
  28. keyboard-layout-model
  29. keyboard-layout-options
  30. keyboard-layout->console-keymap))
  31. ;;; Commentary:
  32. ;;;
  33. ;;; This module provides a data structure to represent keyboard layouts
  34. ;;; according to the XKB naming and classification (see the 'xkeyboard-config'
  35. ;;; package).
  36. ;;;
  37. ;;; Code:
  38. (define-immutable-record-type <keyboard-layout>
  39. (%keyboard-layout name variant model options)
  40. keyboard-layout?
  41. (name keyboard-layout-name) ;string
  42. (variant keyboard-layout-variant) ;#f | string
  43. (model keyboard-layout-model) ;#f | string
  44. (options keyboard-layout-options)) ;list of strings
  45. (define* (keyboard-layout name #:optional variant
  46. #:key model (options '()))
  47. "Return a new keyboard layout with the given NAME and VARIANT.
  48. NAME must be a string such as \"fr\"; VARIANT must be a string such as
  49. \"bepo\" or \"nodeadkeys\". See the 'xkeyboard-config' package for valid
  50. options."
  51. (%keyboard-layout name variant model options))
  52. (define* (keyboard-layout->console-keymap layout
  53. #:key
  54. (xkeyboard-config xkeyboard-config))
  55. "Return a Linux console keymap file for LAYOUT, a <keyboard-layout> record.
  56. Layout information is taken from the XKEYBOARD-CONFIG package."
  57. (define build
  58. (with-imported-modules '((guix build utils))
  59. #~(begin
  60. (use-modules (guix build utils)
  61. (ice-9 popen)
  62. (ice-9 match))
  63. (define pipe
  64. (open-pipe* OPEN_READ
  65. #+(file-append console-setup "/bin/ckbcomp")
  66. (string-append "-I"
  67. #+(file-append xkeyboard-config
  68. "/share/X11/xkb"))
  69. "-rules" "base"
  70. #$@(match (keyboard-layout-model layout)
  71. (#f '())
  72. (model `("-model" ,model)))
  73. #$(keyboard-layout-name layout)
  74. #$(or (keyboard-layout-variant layout)
  75. "")
  76. #$(string-join (keyboard-layout-options layout) ",")))
  77. (call-with-output-file #$output
  78. (lambda (output)
  79. (dump-port pipe output)))
  80. ;; Note: ckbcomp errors out when the layout name is unknown, but
  81. ;; merely emits a warning when the variant is unknown.
  82. (unless (zero? (close-pipe pipe))
  83. (error "failed to create console keymap for keyboard layout"
  84. #$(keyboard-layout-name layout))))))
  85. (computed-file (string-append "console-keymap."
  86. (string-map (match-lambda
  87. (#\, #\-)
  88. (chr chr))
  89. (keyboard-layout-name layout)))
  90. build))