gui 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env guile
  2. !#
  3. ;;; gui.scm --- Start GUI using Shepherd
  4. ;; Copyright © 2016 Alex Kost
  5. ;; Author: Alex Kost <alezost@gmail.com>
  6. ;; Created: 9 Feb 2016
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;;
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but 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 this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This script is a wrapper around 'herd' command to start X server,
  21. ;; window manager and other things on a free DISPLAY and virtual
  22. ;; terminal.
  23. ;;
  24. ;; My Shepherd config: <https://gitlab.com/alezost-config/shepherd>.
  25. ;;; Code:
  26. (use-modules
  27. (srfi srfi-1)
  28. (srfi srfi-37)
  29. (al files)
  30. (al messages)
  31. (al display))
  32. ;;; Command-line args
  33. (define (show-help)
  34. (format #t "Usage: ~a OPTION...
  35. Start GUI on a free DISPLAY, using the following command:
  36. herd start gui:<DISPLAY> ...
  37. "
  38. (car (command-line)))
  39. (display "
  40. Options:
  41. -h, --help display this help and exit
  42. -s, --simple start only X server and terminal
  43. -t, --ssh-tty update startup TTY for the started X display
  44. -n, --no-ssh-tty do not update startup TTY for the started X display
  45. ")
  46. (display "
  47. By default (without additional options), SSH (gpg-agent) startup TTY
  48. will be updated for the started X display. See description in the GnuPG
  49. manual:
  50. (info \"(gnupg) Agent UPDATESTARTUPTTY\").
  51. If '--simple' option is specified, default action is not updating SSH
  52. TTY. '--[no-]ssh-tty' option sets updating / not updating
  53. uncoditionally.
  54. "))
  55. (define %default-options
  56. '((simple? . #f)
  57. (update-tty? . undefined)))
  58. (define %options
  59. (list
  60. (option '(#\h "help") #f #f
  61. (lambda _
  62. (show-help)
  63. (exit 0)))
  64. (option '(#\s "simple") #f #f
  65. (lambda (opt name arg seed)
  66. (alist-cons 'simple? #t
  67. (alist-delete 'simple? seed eq?))))
  68. (option '(#\t "ssh-tty") #f #f
  69. (lambda (opt name arg seed)
  70. (alist-cons 'update-tty? #t
  71. (alist-delete 'update-tty? seed eq?))))
  72. (option '(#\n "no-ssh-tty") #f #f
  73. (lambda (opt name arg seed)
  74. (alist-cons 'update-tty? #f
  75. (alist-delete 'update-tty? seed eq?))))))
  76. (define (parse-args args)
  77. "Return alist of options from command-line ARGS."
  78. (args-fold args %options
  79. (lambda (opt name arg seed)
  80. (print-error "Unrecognized option: '~a'" name)
  81. seed)
  82. (lambda (arg seed)
  83. (print-error "Useless argument: '~a'" arg)
  84. seed)
  85. %default-options))
  86. ;;; Main
  87. (define* (start-gui display #:key simple?)
  88. "Run 'herd' command for starting GUI on DISPLAY."
  89. (apply system*
  90. "herd" "start" (string-append "gui" display)
  91. (if simple?
  92. '()
  93. (filter identity
  94. (list "xterm"
  95. (first-existing-program "stumpwm" "openbox")
  96. "unclutter" "emacs")))))
  97. (define (update-ssh-tty display)
  98. (setenv "DISPLAY" display)
  99. (system* "gpg-connect-agent" "updatestartuptty" "/bye"))
  100. (define (main arg0 . args)
  101. (let* ((opts (parse-args args))
  102. (simple? (assoc-ref opts 'simple?))
  103. (update-tty? (assoc-ref opts 'update-tty?))
  104. (update-tty? (if (boolean? update-tty?)
  105. update-tty?
  106. (not simple?)))
  107. (display (first-unused-display)))
  108. (start-gui display #:simple? simple?)
  109. (when update-tty? (update-ssh-tty display))))
  110. (when (batch-mode?)
  111. (apply main (command-line)))
  112. ;;; gui.scm ends here