logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Integrating-a-VT220-into-my-life.md (7489B)


  1. ---
  2. date: 2016-03-22
  3. # vim: tw=80
  4. title: Integrating a VT220 into my life
  5. layout: post
  6. tags: [hardware]
  7. ---
  8. I bought a DEC VT220 terminal a while ago, and put it next to my desk at work. I
  9. use it to read emails on mutt now, and it's actually quite pleasant. There was
  10. some setup involved in making it as comfortable as possible, though.
  11. [![My desk at work](https://sr.ht/BnAH.jpg)](https://sr.ht/BnAH.jpg)
  12. Here's the terminal up close:
  13. [![The terminal itself](https://sr.ht/TnC6.jpg)](https://sr.ht/TnC6.jpg)
  14. ## Hardware
  15. First, I have several pieces of hardware involved in this:
  16. * VT220 terminal
  17. * LK201 keyboard (later made obsolete)
  18. * [USB to serial adapter](http://amzn.com/B00IDSM6BW)
  19. * [DB9->DB29 null modem cable](http://amzn.com/B00066HL50)
  20. It took a while to get all of these things, but I was able to get a nice
  21. refurbished terminal and a couple of crappy LK201 keyboards. Luckily I was able
  22. to eventually remove the need for the keyboard.
  23. ## Basic Setup
  24. Getting this working on Linux is actually pretty simple thanks to decades of
  25. backwards compatability. Plug all of the cords together, turn on the machine,
  26. and (on Arch, at least) run:
  27. systemctl start serial-agetty@ttyUSB0.service
  28. This will start up a getty for you to log into on your terminal. For a while I
  29. would use the LK201 to log in to this getty and spin up a mail cilent.
  30. I did have to make a couple of changes to serial-agetty@.service, though:
  31. ExecStart=-/sbin/agetty -h -L 19200 %I vt220
  32. This specifies the TERM variable as "vt220" and sets the baud rate to 19200. I
  33. had to also set the baud rate in the terminal's settings to 19200 baud as well,
  34. to get the fastest possible terminal.
  35. I eventually got into the habit of logging into the terminal with the LK201,
  36. then running tmux and attaching to tmux from my desktop session. I would then
  37. hide this tmux terminal in the upper left corner of my display, and move my
  38. mouse over to it when I wanted to interact with the terminal. This let me use
  39. the same keyboard I used for the rest of my computer experience to interact with
  40. the VT220, instead of trying to use the LK201 as well. This was a bit annoying,
  41. so eventually I did some more customization.
  42. ## Removing the keyboard
  43. I wanted to be able to make everything automatic, so I could just boot my
  44. computer and log in normally and treat the VT220 almost like a fourth monitor. I
  45. started by automating the process of logging in and running tmux.
  46. First, I created a user for the terminal:
  47. useradd vt220
  48. Then, I wrote a shell script that would serve as the user's login shell and
  49. would start tmux:
  50. ```bash
  51. #!/usr/bin/env bash
  52. if [[ $TERM == "screen" ]]
  53. then
  54. sudo /usr/local/bin/login-sircmpwn
  55. else
  56. tmux -S /var/tmux/vt220.sock
  57. fi
  58. ```
  59. I made that directory, `/var/tmux/`, and made sure both the vt220 user and my
  60. normal user had access to it. I also edited my sudoers file so that vt220 could
  61. run that command as root:
  62. vt220 ALL=(ALL) NOASSWD: /usr/local/bin/login-sircmpwn
  63. I put the script into `/usr/local/bin` and added it to `/etc/shells`, then made
  64. it the login shell for the vt220 user with `chsh`. I then moved to my own
  65. systemd unit for starting the getty on ttyUSB0, this time with autologin:
  66. # This file is part of systemd.
  67. #
  68. # systemd is free software; you can redistribute it and/or modify it
  69. # under the terms of the GNU Lesser General Public License as published by
  70. # the Free Software Foundation; either version 2.1 of the License, or
  71. # (at your option) any later version.
  72. [Unit]
  73. Description=Serial Getty on %I
  74. Documentation=man:agetty(8) man:systemd-getty-generator(8)
  75. Documentation=http://0pointer.de/blog/projects/serial-console.html
  76. BindsTo=dev-%i.device
  77. After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
  78. # If additional gettys are spawned during boot then we should make
  79. # sure that this is synchronized before getty.target, even though
  80. # getty.target didn't actually pull it in.
  81. Before=getty.target
  82. IgnoreOnIsolate=yes
  83. [Service]
  84. ExecStart=-/sbin/agetty -a vt220 -h -L 19200 %I vt220
  85. Type=idle
  86. Restart=always
  87. UtmpIdentifier=%I
  88. TTYPath=/dev/%I
  89. TTYReset=yes
  90. TTYVHangup=yes
  91. KillMode=process
  92. IgnoreSIGPIPE=no
  93. SendSIGHUP=yes
  94. [Install]
  95. WantedBy=getty.target
  96. The only difference here is that it invokes agetty with `-a vt220` to autologin
  97. as that user. `systemctl enable vtgetty@ttyUSB0.service` makes it so that on
  98. boot, the getty would run on ttyUSB0 and autologin as vt220. Then the script
  99. from earlier will run tmux, and within tmux will run `sudo
  100. /usr/local/bin/login-sircmpwn`, which is this shell script:
  101. ```bash
  102. #!/usr/bin/env bash
  103. until who | grep sircmpwn 2>&1 >/dev/null
  104. do
  105. sleep 1
  106. done
  107. sudo -iu sircmpwn
  108. ```
  109. What this does is pretty straightforward - it loops until I log in as sircmpwn,
  110. then enters an interactive session with sudo as sircmpwn.
  111. The net of all of this is that now, I can boot up my machine, and when I log in,
  112. the VT220 starts up with tmux running and logged in as me. Then I went back to
  113. the old way of attaching to this tmux session with a terminal on my desktop
  114. session hidden in a corner of the screen. And now I could ditch the clunky old
  115. LK201 keyboard!
  116. ## Treating the terminal as another output
  117. I said earlier that my goal was to treat the terminal as a fake "output" that I
  118. could switch to from my desktop session just like I switch between my three
  119. graphical outputs. I run [sway](https://github.com/SirCmpwn/sway), of course, so
  120. I decided to add a fake output in sway and see where that went. I made a
  121. somewhat complicated [branch](https://github.com/SirCmpwn/sway/compare/vt220)
  122. for this purpose, but the important change is here:
  123. ```diff
  124. diff --git a/sway/handlers.c b/sway/handlers.c
  125. index cec6319..60f8406 100644
  126. --- a/sway/handlers.c
  127. +++ b/sway/handlers.c
  128. @@ -704,6 +704,21 @@ static void handle_wlc_ready(void) {
  129. free(line);
  130. list_del(config->cmd_queue, 0);
  131. }
  132. + // VT220 stuff
  133. + // Adds a made up output that we can use for a tmux window
  134. + // connected to my vt220
  135. + swayc_t *output = new_swayc(C_OUTPUT);
  136. + output->name = "VT220";
  137. + output->handle = UINTPTR_MAX;
  138. + output->width = 1000;
  139. + output->height = 1000;
  140. + output->unmanaged = create_list();
  141. + output->bg_pid = -1;
  142. + add_child(&root_container, output);
  143. + output->x = -1000;
  144. + output->y = 0;
  145. + new_workspace(output, "__VT220");
  146. + // End VT220 stuff
  147. }
  148. ```
  149. This creates a fake output and puts it to the far left, then adds a workspace to
  150. it called __VT220. I assigned it the output handle of UINTPTR_MAX and everywhere
  151. in sway that it would try to use the output handle to manipulate a real output,
  152. I changed to to avoid doing so if the handle is UINTPTR_MAX. Then I added this
  153. to my sway config:
  154. for_window [title="__VT220"] move window to workspace __VT220
  155. And run this command when sway starts:
  156. urxvt -T "__VT220" -e tmux -S /var/tmux/vt220.sock a
  157. Which spawns a terminal whose window title is __VT220 running tmux attached to
  158. the session running on the terminal. The for_window rule I added to my sway
  159. config automatically moves this to the VT220 fake output and tada! It works. Now
  160. I have a nice and comfortable way to use my terminal to read emails at work. Now
  161. if only I could convince people to stop sending me HTML emails! I just bought a
  162. second VT220 for use at home, too. Life's good~
  163. [Discussion on Hacker News](https://news.ycombinator.com/item?id=11339909)