2.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="generator" content="Hugo 0.88.0" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="/assets/css/theme.css">
  8. <link rel="alternate" href="/rss.xml" type="application/rss+xml" title="Pleasant Programmer">
  9. <script type="text/javascript" src="//use.typekit.net/iwm5axp.js"></script>
  10. <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
  11. <title>Pleasant Programmer</title>
  12. </head>
  13. <body>
  14. <header id="header" role="banner">
  15. <div id="thomas">
  16. <img src="/assets/img/thomas.gif" alt="DJ THOMAS IN DA HAUS">
  17. <img src="/assets/img/thomas.png" alt="Pleasant Programmer">
  18. </div>
  19. <h1 class="site-title"><a href="/">Pleasant Programmer</a></h1>
  20. <nav id="menu" role="navigation">
  21. <ul>
  22. <li class="twitter">
  23. <a href="http://twitter.com/pleasantprog">@pleasantprog</a>
  24. </li>
  25. <li><a href="/pages/projects.html">projects</a></li>
  26. <li><a href="/posts.html">archives</a></li>
  27. <li><a href="/tags.html">tags</a></li>
  28. <li><a href="/rss.xml">rss</a></li>
  29. </ul>
  30. </nav>
  31. </header>
  32. <div id="container">
  33. <main id="content" role="main">
  34. <div class="postindex">
  35. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  36. <header>
  37. <h1 class="p-name entry-title" itemprop="headline">
  38. <a href="/posts/cloudflare-shenanigans.html" class="u-url">Cloudflare Shenanigans</a>
  39. </h1>
  40. </header>
  41. <div class="e-content entry-content">
  42. <p>An old client of ours managed to convince a telco to zero-rate the data for their app. In order to whitelist it though, we needed to use plain HTTP for domain whitelisting. For HTTPS, they can only whitelist by IP address. Like any good developer, we were using HTTPS. Also, like any good developer, we put our server behind Cloudflare.</p>
  43. <p>Now the problem is that Cloudflare can put you behind <a href="https://www.cloudflare.com/ips/">any IP they own</a>, which is a huge range. There&rsquo;s no guarantee that the IP we have now is going to be the same later on. So we did the reasonable thing and asked them to whitelist all of the Cloudflare IPs. And the telco agreed! We were in total disbelief when that happened. But hey, if life gives you free internet, you take it.</p>
  44. <p>We never actually empirically tested whether other sites hosted on Cloudflare were also actually zero-rated. But I like to think that we saved a lot of people on their data costs from browsing Reddit and 4chan. But alas, good things must come to an end.</p>
  45. <p>A few months after we started beta testing the app, Cloudflare added more IPs to their range. Unfortunately, our server got moved to those new IPs which were not whitelisted yet. Apparently, the telco whitelisting process was incredibly convoluted and time consuming. Our client didn&rsquo;t want to bother asking them to whitelist more IPs. We also tried asking Cloudflare to move us back to the original IP range, but they could only do that if we were in their enterprise tier. We couldn&rsquo;t really afford that, so we looked for other options.</p>
  46. <p>Since Cloudflare was essentially just a giant reverse proxy, theoretically there should be no distinction between one IP address from another. The specific IP we get is probably just for load balancing. So we tried accessing the IPs in the range directly and just setting the Host header and it worked! But we get SSL errors because the IP itself doesn&rsquo;t have its own certificate.</p>
  47. <p>After more testing, we figured out that you could actually use any Cloudflare backed domain so long as we properly set the Host header. We just needed to find one still in the old range. Coincidentally, 4chan.org was. Which led to this wonderful commit</p>
  48. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-diff" data-lang="diff">commit 123456789abcdef
  49. Author: ~~~~~~
  50. Date: ~~~~~~
  51. 4chan hack
  52. <span style="font-weight:bold">diff --git a/src/com/client/common/Util.java b/src/com/client/common/Util.java
  53. </span><span style="font-weight:bold"></span>--- a/src/com/client/common/Util.java
  54. +++ b/src/com/client/common/Util.java
  55. <span style="font-weight:bold">@@ -210,7 +210,8 @@ public class Util {
  56. </span><span style="font-weight:bold"></span> }
  57. public static String getServerAddress(Context context) {
  58. - String address = &#34;https://backend.client.com&#34;;
  59. + // String address = &#34;https://backend.client.com&#34;;
  60. + String address = &#34;https://4chan.org&#34;;
  61. if(!isDebug(context)) return address;
  62. try {
  63. <span style="font-weight:bold">diff --git a/src/com/client/common/logging/APIClient.java b/src/com/client/common/logging/APIClient.java
  64. </span><span style="font-weight:bold"></span>--- a/src/com/client/common/logging/APIClient.java
  65. +++ b/src/com/client/common/logging/APIClient.java
  66. <span style="font-weight:bold">@@ -101,6 +101,7 @@ public class APIClient {
  67. </span><span style="font-weight:bold"></span> private HttpResponse postInternal(String url, List&lt;NameValuePair&gt; data, boolean forRegistration) throws ClientProtocolException, IOException {
  68. HttpPost request = new HttpPost(Util.getServerAddress(mContext)+&#34;/api/&#34;+url);
  69. request.setHeader(&#34;X-API-VERSION&#34;, apiVersion);
  70. + request.setHeader(&#34;Host&#34;, &#34;backend.client.com&#34;);
  71. if(data == null) {
  72. data = new ArrayList&lt;NameValuePair&gt;();
  73. </code></pre></div><p>Eventually, we did decide to just abandon Cloudflare for the server. We probably weren&rsquo;t going to be the target of a DDOS or anything. This also allowed us to do more secure things like pinning the server certificate in the application itself. Clearly, this is what we should have just done in the first place, but at the time we just wanted a stopgap solution.</p>
  74. <p>I just still find it funny we were making people&rsquo;s phones go to 4chan.org everyday for more than a year.</p>
  75. </div>
  76. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-12-25">2015-12-25</time></small>
  77. | <small class="commentline"><a href="/posts/cloudflare-shenanigans.html#isso-thread">Comments</a></small>
  78. </article>
  79. </article>
  80. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  81. <header>
  82. <h1 class="p-name entry-title" itemprop="headline">
  83. <a href="/posts/tiddlywiki-in-the-sky-or-tiddlyweb-for-tw5.html" class="u-url">TiddlyWiki in the Sky (or TiddlyWeb for TW5)</a>
  84. </h1>
  85. </header>
  86. <div class="e-content entry-content">
  87. <p>I&rsquo;ve always liked <a href="http://tiddlywiki.com">TiddlyWiki</a>. Back when it first came out, it was really amazing. A wiki all in one file, that worked in the browser. It didn&rsquo;t need a backend, it would just save itself as an all new HTML file with all your posts inside. I&rsquo;ve used it a lot over the years, as a personal wiki/journal and a class notebook. I even had a blog with it at one point using one of the server-side forks.</p>
  88. <p>Now, there&rsquo;s TiddlyWiki5 which is a rewrite of the original TiddlyWiki that looks a whole lot snazzier, and I assume has better architecture overall. It also has experimental support for all the server-side platforms (particularly TiddlyWeb) that have cropped up.</p>
  89. <p>If you&rsquo;re just looking for a simple server setup for TiddlyWiki5, it has native support for that on its own. There&rsquo;s plenty of documentation on the site. But if you&rsquo;re looking for more advanced features (like storing your posts in git or a database), then you&rsquo;ll need to use it with TiddlyWeb. The problem is that most of the documentation for TiddlyWeb still refers to the old TiddlyWiki.</p>
  90. <p>To support TiddlyWiki5, we&rsquo;ll need a version of the wiki which has the TiddlyWeb plugin already installed and configured. After that, some tweaking is necessary to get TiddlyWeb to provide what the wiki requires.</p>
  91. <h2 id="setting-up-tiddlywiki">Setting Up TiddlyWiki</h2>
  92. <p>TiddlyWiki5 provides a command line tool via <code>npm</code> that allows building custom versions of the wiki. In fact, it comes with templates, called &ldquo;editions&rdquo;, that we can use for our setup. Assuming you already have it installed, create the wiki using</p>
  93. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">tiddlywiki mywiki --init tw5tank <span style="color:#007f7f"># create wiki from template</span>
  94. </code></pre></div><p>This creates a wiki intended for use with <a href="https://tank.peermore.com/">Tank</a>, which is built on top of TiddlyWeb. From here, you should look in <code>mywiki/tiddlers/system</code> which contain the entries for <code>SiteTitle</code>, <code>SiteSubtitle</code>, <code>DefaultTiddlers</code>, and <code>tiddlyweb-host</code>. The first 3 should be configured however you want. These are necessary because they&rsquo;re needed before the wiki can load them from the server. <code>tiddlyweb-host</code> contains the location of the TiddlyWeb server, this should be <code>http://localhost:8080/</code> if you&rsquo;re just testing locally. With everything configured, you can build the new wiki by running</p>
  95. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">tiddlywiki mywiki --build
  96. </code></pre></div><p>This will output the wiki to <code>mywiki/output/tw5tank.html</code>. You can now serve it using your favorite local webserver, like <code>python -m http.server</code>.</p>
  97. <h2 id="setting-up-tiddlyweb">Setting Up TiddlyWeb</h2>
  98. <p>The TiddlyWeb tutorial recommends using <code>tiddlywebwiki</code> which has all the plugins setup for a nice wiki instance for the old TiddlyWiki. It has a lot of features that aren&rsquo;t really needed, so we won&rsquo;t go with that. So first, we&rsquo;ll need to install TiddlyWeb and any plugins we might want to use.</p>
  99. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">pip install tiddlyweb tiddlywebplugins.status tiddlywebplugins.cherrypy tiddlywebplugins.cors
  100. </code></pre></div><p>Next, we&rsquo;ll need the tiddlyweb configuration in <code>tiddlywebconfig.py</code></p>
  101. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#007f7f"># A basic configuration.</span>
  102. <span style="color:#007f7f"># `pydoc tiddlyweb.config` for details on configuration items.</span>
  103. <span style="color:#fff;font-weight:bold">import</span> tiddlywebplugins.status
  104. config = {
  105. <span style="color:#0ff;font-weight:bold">&#39;system_plugins&#39;</span>: [<span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.status&#39;</span>, <span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.cors&#39;</span>],
  106. <span style="color:#0ff;font-weight:bold">&#39;secret&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;36c98d6d14618c79f0ed2d49cd1b9e272d8d4bd0&#39;</span>,
  107. <span style="color:#0ff;font-weight:bold">&#39;wsgi_server&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;tiddlywebplugins.cherrypy&#39;</span>,
  108. <span style="color:#0ff;font-weight:bold">&#39;cors.enable_non_simple&#39;</span>: <span style="color:#fff;font-weight:bold">True</span>
  109. }
  110. original_gather_data = tiddlywebplugins.status._gather_data
  111. <span style="color:#fff;font-weight:bold">def</span> _status_gather_data(environ):
  112. data = original_gather_data(environ)
  113. data[<span style="color:#0ff;font-weight:bold">&#39;space&#39;</span>] = {<span style="color:#0ff;font-weight:bold">&#39;recipe&#39;</span>: <span style="color:#0ff;font-weight:bold">&#39;default&#39;</span>}
  114. <span style="color:#fff;font-weight:bold">return</span> data
  115. tiddlywebplugins.status._gather_data = _status_gather_data
  116. </code></pre></div><p>The tweaks involved are:</p>
  117. <ul>
  118. <li>using the status plugin which the wiki requires</li>
  119. <li>monkeypatching the status plugin for the wiki to use the correct &ldquo;recipe&rdquo;</li>
  120. <li>using cherrypy server instead of the buggy default one</li>
  121. <li>using cors since we&rsquo;re not hosting the wiki itself on the same server</li>
  122. </ul>
  123. <p>With that, we just need to create the store that will hold our data</p>
  124. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">twanager recipe default <span style="color:#0ff;font-weight:bold">&lt;&lt;EOF
  125. </span><span style="color:#0ff;font-weight:bold">desc: standard TiddlyWebWiki environment
  126. </span><span style="color:#0ff;font-weight:bold">policy: {&#34;read&#34;: [], &#34;create&#34;: [], &#34;manage&#34;: [&#34;R:ADMIN&#34;], &#34;accept&#34;: [], &#34;write&#34;: [&#34;R:ADMIN&#34;], &#34;owner&#34;: &#34;administrator&#34;, &#34;delete&#34;: [&#34;R:ADMIN&#34;]}
  127. </span><span style="color:#0ff;font-weight:bold">
  128. </span><span style="color:#0ff;font-weight:bold">/bags/default/tiddlers
  129. </span><span style="color:#0ff;font-weight:bold">EOF</span>
  130. twanager bag default <span style="color:#0ff;font-weight:bold">&lt;&lt;EOF
  131. </span><span style="color:#0ff;font-weight:bold">{&#34;policy&#34;: {&#34;read&#34;: [], &#34;create&#34;: [], &#34;manage&#34;: [&#34;R:ADMIN&#34;], &#34;accept&#34;: [], &#34;write&#34;: [], &#34;owner&#34;: &#34;administrator&#34;, &#34;delete&#34;: []}}
  132. </span><span style="color:#0ff;font-weight:bold">EOF</span>
  133. </code></pre></div><p>Finally, we can start the TiddlyWeb server</p>
  134. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">twanager server
  135. </code></pre></div><h2 id="putting-it-all-together">Putting it all together</h2>
  136. <p>Once you have the TiddlyWeb server running, you can just go to wherever you&rsquo;re hosting the wiki html and it should work. You can try creating some posts, and the check mark on the sidebar should be red for a while and then turn black. Once that&rsquo;s done it&rsquo;s saved. You can refresh your browser and your posts should still be there.</p>
  137. <p>At this point, you can start customizing your TiddlyWeb instance, by changing your store to something like a database, or adding authorization. You can also tweak the server setup so you won&rsquo;t need CORS anymore.</p>
  138. <p>TiddlyWiki5 is still relatively new. I hope that eventually, support for server-side and the plugin ecosystem grows to be as great as the old TiddlyWiki.</p>
  139. </div>
  140. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-12-24">2015-12-24</time></small>
  141. | <small class="commentline"><a href="/posts/tiddlywiki-in-the-sky-or-tiddlyweb-for-tw5.html#isso-thread">Comments</a></small>
  142. </article>
  143. </article>
  144. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  145. <header>
  146. <h1 class="p-name entry-title" itemprop="headline">
  147. <a href="/posts/is-my-terminal-window-active.html" class="u-url">Is My Terminal Window Active?</a>
  148. </h1>
  149. </header>
  150. <div class="e-content entry-content">
  151. <p>I&rsquo;ve been working in OSX for almost 3 years now, but I recently switched back to Linux because of all the problems people encountered with Yosemite. There are some things I missed from OSX though. One of which is <a href="https://github.com/marzocchi/zsh-notify">zsh-notify</a>. It&rsquo;s a zsh plugin that alerts you if your long-running task is complete, and whether it failed or not.</p>
  152. <p>It&rsquo;s pretty convenient when you&rsquo;re compiling something and then go on to browse reddit while waiting. Usually, I spend too much time just reading and forget about the compilation entirely. With the plugin, I get the notification and maybe go back to work.</p>
  153. <p>One nice feature it has is that if you&rsquo;re currently looking at the terminal window of the job that just finished, it won&rsquo;t notify you. It only notifies on windows that aren&rsquo;t currently in focus. To do this, it has to actually talk to Terminal.app or iTerm2 to see if the window and tab are active.</p>
  154. <p>This is alright in OSX since those 2 are the generally most used terminal emulators. On Linux though, everyone has their own favorite terminal. Given that, I figured I could probably rely on talking to X to see if the window is active instead of each single terminal emulator. X can&rsquo;t tell if the tab is active though, but I don&rsquo;t use tabs in my current setup so it should still be good.</p>
  155. <h2 id="xdotool">xdotool</h2>
  156. <p><a href="http://superuser.com/questions/382616/detecting-currently-active-window">Preliminary research</a> reveals that we can easily get what the active window is with xdotool. <code>xdotool getactivewindow</code> gives us the X window id of the active one. Now all we need is a way to get the window id of the terminal we&rsquo;re in.</p>
  157. <h2 id="first-attempt-windowid">First Attempt: $WINDOWID</h2>
  158. <p>Apparently, xterm and similar terminal emulators define an environment variable called <code>$WINDOWID</code> with the window id of the terminal. Obviously, this is too good to be true. In xterm and konsole the <code>$WINDOWID</code> was correct, but in VTE-based terminal emulators, <code>$WINDOWID</code> had the wrong value. In terminology, it didn&rsquo;t define <code>$WINDOWID</code> altogether. So <code>$WINDOWID</code> wasn&rsquo;t going to work.</p>
  159. <h2 id="second-attempt-xdotool-search-magic">Second Attempt: xdotool search $MAGIC</h2>
  160. <p>My second idea was that you can use zsh to change the window title to a magic number and then just check if the active window is the same one as the window with the magic number. This sort of worked for most terminals, except konsole which does whatever it wants with the window title. There&rsquo;s also the problem of some zsh configs automatically settings the window title to the current command.</p>
  161. <p>In hindsight, I could probably have just done <code>xdotool search --name xdotool</code> since in most cases, when you run the search, zsh or konsole will set the window name to the current command. Maybe that&rsquo;s another option I can explore some day.</p>
  162. <h2 id="third-attempt-ppid">Third Attempt: $PPID</h2>
  163. <p>My third idea was another environment variable called <code>$PPID</code>, which is the process id of the parent of the shell. As it happens, the parent is the window containing the zsh instance. This is actually pretty consistent across most terminals. The only problem was if you launched zsh from another shell since your new zsh&rsquo;s parent will now be another zsh instance instead of an X window.</p>
  164. <p>At first glance, launching zsh within zsh doesn&rsquo;t seem like something most people would do, but this is what happens when you run screen or tmux. To work around this, we can actually just save the original <code>$PPID</code> in a different variable and use that instead.</p>
  165. <p>Now that we have the PID of the window from zsh, we can once again use xdotool to get the PID of the current active window with <code>xdotool getactivewindow getwindowpid</code>. We just simply compare that with our <code>$PPID</code> and we can tell if we&rsquo;re in an active window or not. Overall, this approach worked surprisingly well so that&rsquo;s the final solution I went with.</p>
  166. </div>
  167. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2015-06-07">2015-06-07</time></small>
  168. | <small class="commentline"><a href="/posts/is-my-terminal-window-active.html#isso-thread">Comments</a></small>
  169. </article>
  170. </article>
  171. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  172. <header>
  173. <h1 class="p-name entry-title" itemprop="headline">
  174. <a href="/posts/removing-pldtmydslbiz-from-the-zyxel-p-2612hnu.html" class="u-url">Removing PLDTMyDSLBiz from the ZyXEL P-2612HNU</a>
  175. </h1>
  176. </header>
  177. <div class="e-content entry-content">
  178. <p>I&rsquo;ve always thought that people were just too lazy to change their SSIDs when I see &ldquo;PLDTMyDSLBizCafeJapan&rdquo;. It became apparent when we got our own PLDT line that it was because the bundled router/modem <em>does not</em> allow you to remove the prefix.</p>
  179. <p>This is not the kind of thing you expect as a business customer. Even for home customers, I feel it&rsquo;s still a bit dishonest. I&rsquo;d be fine if it was just the default SSID, but forcing people to have it as part of their SSID is like advertising that your company (I mean PLDT) is a douche.</p>
  180. <p>Of course, we couldn&rsquo;t just leave the SSID prefix there, so we tried a number of things to get rid of it. There are articles for removing it from the <a href="http://www.phandroidinternet.com/2013/06/how-to-remove-on-wifi-name-or-ssid-on.html">Prolink H5004N</a> or the <a href="http://www.symbianize.com/showthread.php?t=730091">ZyXEL P-660HN-T1A</a> but not for the one we got which was the ZyXEL P-2612HNU-F1F.</p>
  181. <p>We did still try the firebug/inspector tricks, but it seems that there is a server-side check that adds in the &ldquo;PLDTMyDSLBiz&rdquo;. We tried a number of things, but the one that ultimately worked (and we had a good laugh about) was to backup the configuration, edit the dumped file and restore it.</p>
  182. <p>The backup is actually just an XML file. You can search for SSID and change the parameter there. It&rsquo;s a bit annoying because the router has to restart after restoring the configuration, but it works!</p>
  183. <p>A minor note, the router doesn&rsquo;t seem to support SSIDs with a comma (,) well. It just gets everything before the comma as the SSID for some reason.</p>
  184. </div>
  185. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2013-11-27">2013-11-27</time></small>
  186. | <small class="commentline"><a href="/posts/removing-pldtmydslbiz-from-the-zyxel-p-2612hnu.html#isso-thread">Comments</a></small>
  187. </article>
  188. </article>
  189. <article class="h-entry post-text" itemscope itemtype="http://schema.org/Blog">
  190. <header>
  191. <h1 class="p-name entry-title" itemprop="headline">
  192. <a href="/posts/console-keymap-switching.html" class="u-url">Console Keymap Switching</a>
  193. </h1>
  194. </header>
  195. <div class="e-content entry-content">
  196. <p>At the office, we have some people who use DVORAK. Normally, this isn&rsquo;t a problem. To each his own after all. It does become a bit problematic though, when we&rsquo;re dealing with the servers around the office.</p>
  197. <p>We normally leave the servers on QWERTY. After all, most people start off as QWERTY typists and migrate to something else. That said, it&rsquo;s apparently difficult to stay fluent in both. People tend to forget how to type in QWERTY once they learn DVORAK or something else. While it is true that they can just look a the keyboard while typing, my coworkers would prefer it to just be in DVORAK.</p>
  198. <p>For the console, they&rsquo;d typically do <code>sudo loadkeys dvorak</code> after logging in. The problem with this is, after they logout, the keymapping is still on DVORAK. This has been quite annoying for a few times since I can&rsquo;t even login to change the keymap. What I wanted was something like you get in the graphical login screens where you can pick your keymap before logging in. Apparently, there isn&rsquo;t a readily available thing for the console.</p>
  199. <p>I googled around for solutions and came across <a href="http://superuser.com/questions/548234/how-can-i-easily-toggle-between-dvorak-and-qwerty-keyboard-layouts-from-a-linux">a nice idea</a>. You could alias <code>asdf</code> to load the DVORAK mapping and <code>aoeu</code> (the equivalent to asdf in DVORAK) to load the QWERTY mapping. This actually makes sense since you don&rsquo;t really have to know where the letters are. The only problem is, you once again have to be logged in to change the key mappings.</p>
  200. <p>After some further searching, I found <a href="http://unix.stackexchange.com/questions/2884/toggle-between-dvorak-and-qwerty">something close to what I wanted</a>. Apparently, Alt+Up sends a KeyboardSignal keycode to the init process, which can act on that. It also works anywhere, even before being logged in. For SysVinit systems, you can just add a line to your inittab for a command to be run when Alt+Up is pressed.</p>
  201. <p>In the office, however, we generally use Arch Linux which uses SystemD. But apparently, it also has a mechanism of accepting the Alt+Up press. It runs the kbrequest target whenever it gets the keypress. <code>kbrequest.target</code> is normally aliased to run the rescue service though, so you have to manually create the file in <code>/etc/systemd/system/kbrequest.target</code> and fill it with a description:</p>
  202. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#fff;font-weight:bold">[Unit]</span>
  203. <span style="color:#007f7f">Description</span>=<span style="color:#0ff;font-weight:bold">kbrequest target</span>
  204. </code></pre></div><p>We can then add a service to be run whenever the target is called. Something like <code>/etc/systemd/system/keymap-switch.service</code>:</p>
  205. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#fff;font-weight:bold">[Unit]</span>
  206. <span style="color:#007f7f">Description</span>=<span style="color:#0ff;font-weight:bold">Keymap Switch Service</span>
  207. <span style="color:#fff;font-weight:bold">[Service]</span>
  208. <span style="color:#007f7f">Type</span>=<span style="color:#0ff;font-weight:bold">oneshot</span>
  209. <span style="color:#007f7f">ExecStart</span>=<span style="color:#0ff;font-weight:bold">/usr/local/bin/keymap-switch</span>
  210. <span style="color:#fff;font-weight:bold">[Install]</span>
  211. <span style="color:#007f7f">WantedBy</span>=<span style="color:#0ff;font-weight:bold">kbrequest.target</span>
  212. </code></pre></div><p>After enabling said service, we only need the actual keymap switcher, <code>/usr/local/bin/keymap-switch</code>. The StackOverflow answer provides different ways of detecting the current keymap so we know which one to switch to. Since we&rsquo;re using SystemD, we can use that instead for managing which keymap we&rsquo;re actually using. It stores the current settings inside <code>/etc/vconsole.conf</code>. We can also then switch keymaps by using <code>localectl set-keymap</code>.</p>
  213. <div class="highlight"><pre tabindex="0" style="color:#e5e5e5;background-color:#000;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#0f0;font-weight:bold">#!/bin/sh
  214. </span><span style="color:#0f0;font-weight:bold"></span><span style="color:#fff;font-weight:bold">source</span> /etc/vconsole.conf
  215. <span style="color:#fff;font-weight:bold">if</span> [ <span style="color:#0ff;font-weight:bold">&#34;</span>$TERM<span style="color:#0ff;font-weight:bold">&#34;</span> = <span style="color:#0ff;font-weight:bold">&#34;dumb&#34;</span> ]; <span style="color:#fff;font-weight:bold">then</span>
  216. <span style="color:#fff;font-weight:bold">if</span> [ <span style="color:#0ff;font-weight:bold">&#34;</span>$KEYMAP<span style="color:#0ff;font-weight:bold">&#34;</span> = <span style="color:#0ff;font-weight:bold">&#34;dvorak&#34;</span> ]; <span style="color:#fff;font-weight:bold">then</span>
  217. localectl set-keymap us
  218. <span style="color:#fff;font-weight:bold">else</span>
  219. localectl set-keymap dvorak
  220. <span style="color:#fff;font-weight:bold">fi</span>
  221. <span style="color:#fff;font-weight:bold">fi</span>
  222. </code></pre></div><p>After putting it all together, it works! We can switch keymaps on the fly by simply pressing Alt+Up.</p>
  223. </div>
  224. <small class="dateline">Posted: <time class="published dt-published" itemprop="datePublished" datetime="2013-10-29">2013-10-29</time></small>
  225. | <small class="commentline"><a href="/posts/console-keymap-switching.html#isso-thread">Comments</a></small>
  226. </article>
  227. </article>
  228. </div>
  229. <nav class="postindexpager">
  230. <ul class="pager clearfix">
  231. <li class="previous">
  232. <a href="/">&larr; Newer posts</a>
  233. </li>
  234. <li class="next">
  235. <a href="/page/3.html">Older posts &rarr;</a>
  236. </li>
  237. </ul>
  238. </nav>
  239. </main>
  240. <footer id="footer" role="contentinfo">
  241. <p>
  242. <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.en_US">
  243. <img alt="CC-BY-SA" style="border-width:0" src="https://licensebuttons.net/l/by-sa/3.0/80x15.png">
  244. </a> &copy; 2020 Thomas Dy - Powered by <a href="http://gohugo.io">Hugo</a></p>
  245. </footer>
  246. </div>
  247. <script src="/assets/js/konami.js"></script>
  248. <script>
  249. var easter_egg = new Konami();
  250. easter_egg.code = function() {
  251. var el = document.getElementById('thomas');
  252. if(el.className == "whoa") {
  253. el.className = "";
  254. }
  255. else {
  256. el.className = "whoa";
  257. }
  258. document.body.scrollTop = document.documentElement.scrollTop = 0;
  259. }
  260. easter_egg.load();
  261. </script>
  262. <script
  263. data-isso="https://isso.pleasantprogrammer.com/"
  264. src="https://isso.pleasantprogrammer.com/js/count.min.js">
  265. </script>
  266. </body>
  267. </html>