unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: conses <contact@conses.eu>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: contact@conses.eu, 62101@debbugs.gnu.org
Subject: [bug#62101] [PATCH v2] home: services: Add home-xmodmap-service-type.
Date: Thu, 16 Mar 2023 14:03:34 +0100	[thread overview]
Message-ID: <86edpox2zd.fsf@conses.eu> (raw)
In-Reply-To: <867cvo9y6b.fsf@conses.eu>

* gnu/home/services/desktop.scm (home-xmodmap-service-type)
(home-xmodmap-configuration): New variables;
(xmodmap-shepherd-service)
(get-xmodmap-configuration)
(get-xmodmap-file)
(add-xmodmap-config-file)
(add-xmodmap-package): New procedures;
* doc/guix.texi (Desktop Services): Document it.
---
 doc/guix.texi                 | 51 +++++++++++++++++++++
 gnu/home/services/desktop.scm | 85 +++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+)

diff --git a/doc/guix.texi b/doc/guix.texi
index 6671ba9305..c9ec781e8b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -42541,6 +42541,57 @@ Desktop Home Services
 @end table
 @end deftp
 
+@defvar home-xmodmap-service-type
+This is the service type for the
+@uref{https://gitlab.freedesktop.org/xorg/app/xmodmap,xmodmap} utility
+to modify keymaps and pointer button mappings under the Xorg display
+server.  Its associated value must be a
+@code{home-xmodmap-configuration} record, as shown below.
+
+The syntax for the expression grammar is quite straightforward.  You can
+either provide a list of cons cells and strings like this:
+
+@lisp
+(service home-xmodmap-service-type
+         (home-xmodmap-configuration
+          (config '(("add mod4" . "Print")
+                    "clear lock"
+                    "clear control"
+                    ("keycode 66" . "Control_L")
+                    ("add control" . "Control_L Control_R")))))
+@end lisp
+
+Alternatively, there is a more Lisp-like configuration syntax via Scheme
+symbols, lists, and vectors, that you can use like this:
+
+@lisp
+(service home-xmodmap-service-type
+         (home-xmodmap-configuration
+          (config '((#(add mod4) . Print)
+                    (clear lock)
+                    (clear control)
+                    (#(keycode 66) . Control_L)
+                    (#(add control) . #(Control_L Control_R))))))
+@end lisp
+@end defvar
+
+@deftp {Data Type} home-xmodmap-configuration
+The configuration record for @code{home-xmodmap-service-type}.  Its
+available fields are:
+
+@table @asis
+@item @code{xmodmap} (default: @code{xmodmap}) (type: file-like)
+The @code{xmodmap} package to use.
+
+@item @code{config} (default: @code{config}) (type: list)
+The list of expressions to be placed in the
+@file{~/.config/xmodmap/config} configuration file and read on service
+startup.
+
+@end table
+@end deftp
+
+
 @node Guix Home Services
 @subsection Guix Home Services
 
diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm
index cb25b03b64..8bc3d82cba 100644
--- a/gnu/home/services/desktop.scm
+++ b/gnu/home/services/desktop.scm
@@ -22,10 +22,12 @@ (define-module (gnu home services desktop)
   #:use-module (gnu home services shepherd)
   #:use-module (gnu services configuration)
   #:autoload   (gnu packages glib)    (dbus)
+  #:autoload   (gnu packages xorg) (setxkbmap xmodmap)
   #:autoload   (gnu packages xdisorg) (redshift)
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-43)
   #:use-module (ice-9 match)
   #:export (home-redshift-configuration
             home-redshift-configuration?
@@ -226,3 +228,86 @@ (define home-dbus-service-type
    (default-value (home-dbus-configuration))
    (description
     "Run the session-specific D-Bus inter-process message bus.")))
+
+\f
+;;;
+;;; xmodmap
+;;;
+
+(define-configuration/no-serialization home-xmodmap-configuration
+  (xmodmap
+   (file-like xmodmap)
+   "The @code{xmodmap} package to use.")
+  (config
+   (list '())
+   "List of expressions to be inserted in the @file{.config/xmodmap/config}
+configuration file."))
+
+(define (xmodmap-shepherd-service config)
+  (list
+   (shepherd-service
+    (provision '(xmodmap))
+    (start #~(make-system-constructor
+              (string-join
+               (list #$(file-append
+                        (home-xmodmap-configuration-xmodmap config)
+                        "/bin/xmodmap")
+                     #$(get-xmodmap-file config)))))
+    (stop #~(make-system-constructor
+             #$(file-append setxkbmap "/bin/setxkbmap")))
+    (documentation "On startup, run @code{xmodmap} and read the expressions in
+the configuration file.  On stop, reset all the mappings back to the
+defaults."))))
+
+(define (get-xmodmap-configuration field-name val)
+  (define serialize-term
+    (match-lambda
+      ((? vector? e)
+       (string-join
+        (vector-fold (lambda (_ acc e)
+                       (append acc (list (serialize-term e))))
+                     '() e)))
+      ((? symbol? e) (symbol->string e))
+      ((? number? e) (number->string e))
+      (e e)))
+
+  (define serialize-field
+    (match-lambda
+      ((? list? e)
+       (string-join (map serialize-term e)))
+      ((key . value)
+       (format #f "~a = ~a" (serialize-term key) (serialize-term value)))
+      (key (string-append (serialize-term key)))))
+
+  #~(string-append
+     #$@(interpose
+         (map serialize-field val)
+         "\n" 'suffix)))
+
+(define (get-xmodmap-file config)
+  (mixed-text-file
+   "config"
+   (get-xmodmap-configuration
+    #f (home-xmodmap-configuration-config config))))
+
+(define (add-xmodmap-config-file config)
+  `(("xmodmap/config"
+     ,(get-xmodmap-file config))))
+
+(define (add-xmodmap-package config)
+  (list (home-xmodmap-configuration-xmodmap config)))
+
+(define home-xmodmap-service-type
+  (service-type
+   (name 'home-xmodmap)
+   (extensions
+    (list
+     (service-extension home-profile-service-type
+                        add-xmodmap-package)
+     (service-extension home-xdg-configuration-files-service-type
+                        add-xmodmap-config-file)
+     (service-extension home-shepherd-service-type
+                        xmodmap-shepherd-service)))
+   (default-value (home-xmodmap-configuration))
+   (description "Run the @code{xmodmap} utility to modify keymaps and pointer
+buttons under the Xorg display server via user-defined expressions.")))
-- 
2.39.1



-- 
Best regards,
conses




  parent reply	other threads:[~2023-03-16 13:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10 19:57 [bug#62101] [PATCH] home: services: Add xmodmap conses
2023-03-13 14:00 ` Ludovic Courtès
2023-03-16 13:15   ` conses
2023-03-16 13:03 ` conses [this message]
2023-03-16 21:43   ` Ludovic Courtès
2023-03-17 12:06 ` [bug#62101] [PATCH v3] home: services: Add home-xmodmap-service-type conses
2023-03-17 21:50   ` bug#62101: " Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86edpox2zd.fsf@conses.eu \
    --to=contact@conses.eu \
    --cc=62101@debbugs.gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).