WEBVTT captioned by Thomas Koch NOTE Introduction 00:00:02.317 --> 00:00:05.698 I am Thomas Koch and I'll be talking about Emacs 00:00:05.938 --> 00:00:07.099 as a window manager. 00:00:08.299 --> 00:00:11.061 My window manager is called Râu. 00:00:11.881 --> 00:00:13.501 I'll explain that name later. 00:00:15.742 --> 00:00:19.064 When I told my wife that after two months of work 00:00:19.464 --> 00:00:24.226 I could now move and resize windows on my screen, 00:00:25.286 --> 00:00:27.047 she was not impressed. 00:00:28.022 --> 00:00:31.205 Maybe today I have more luck and can find people 00:00:31.285 --> 00:00:34.327 who understand what I actually worked on and why 00:00:34.387 --> 00:00:35.608 it was so much fun. 00:00:37.730 --> 00:00:41.052 When I initially switched to Wayland some months 00:00:41.132 --> 00:00:44.455 ago, I first went for Niri as my window manager. 00:00:45.376 --> 00:00:46.617 Niri is fantastic. 00:00:47.137 --> 00:00:49.859 It is fast, beautiful, stable, featureful. 00:00:50.380 --> 00:00:52.902 It has a large fanbase for good reasons. 00:00:54.395 --> 00:00:57.438 But Niri has its own key bindings. 00:00:58.619 --> 00:01:01.843 And there are many more key bindings not shown on 00:01:01.883 --> 00:01:02.483 this slide. 00:01:03.644 --> 00:01:08.269 I did not want to learn all these key bindings. NOTE I already got a window manager 00:01:08.349 --> 00:01:11.733 First, I tried to make Niri behave like Emacs. 00:01:12.834 --> 00:01:15.917 Then I realized that I already got a window 00:01:15.957 --> 00:01:16.337 manager. 00:01:17.256 --> 00:01:19.619 And it has a superb key binding system. 00:01:20.500 --> 00:01:21.522 And it is tiled. 00:01:22.103 --> 00:01:23.364 And it has workspaces. 00:01:23.765 --> 00:01:24.766 And a help system. 00:01:25.387 --> 00:01:27.309 And a functional scripting language. 00:01:27.690 --> 00:01:28.651 And an overview. 00:01:28.992 --> 00:01:31.054 And inter-process communication. 00:01:31.755 --> 00:01:32.656 And much more. NOTE Reka, an Emacs window manager 00:01:33.764 --> 00:01:37.427 My friend Tazjin wrote an Emacs window manager 00:01:37.647 --> 00:01:40.729 based on the Wayland compositor River. 00:01:42.010 --> 00:01:45.231 Since his window manager is based on River, 00:01:45.232 --> 00:01:47.393 he called it Reka, 00:01:47.394 --> 00:01:50.076 which is the Russian word for River. 00:01:51.397 --> 00:01:53.198 On the slide you see Reka. 00:01:54.259 --> 00:01:58.082 The screen is fully occupied by an Emacs frame. 00:01:59.437 --> 00:02:02.619 On the left is one buffer displaying Firefox. 00:02:03.760 --> 00:02:07.662 On the top right is a Magit buffer and on the 00:02:07.742 --> 00:02:10.404 bottom right a Markdown text buffer. 00:02:11.545 --> 00:02:15.007 At the top of the screen there are three Emacs 00:02:15.087 --> 00:02:17.809 tabs and on the bottom is the mini buffer. NOTE Reka is written partly in Rust 00:02:22.312 --> 00:02:25.433 Reka is written partly in Elisp 00:02:25.434 --> 00:02:26.795 and partly in Rust. 00:02:28.065 --> 00:02:30.686 The Rust part is an Emacs module. 00:02:31.667 --> 00:02:35.529 It runs a separate thread and uses libwayland to 00:02:35.589 --> 00:02:38.750 communicate over a Unix socket with River. 00:02:40.071 --> 00:02:43.552 I like Rust. But I do not like the friction 00:02:43.792 --> 00:02:45.693 between Elisp and Rust. 00:02:47.254 --> 00:02:50.696 And I don't like the overhead to set up the Rust 00:02:50.756 --> 00:02:53.237 toolchain and the compilation time. NOTE Râu 00:02:56.093 --> 00:03:01.076 So I found ewc, an Elisp library that could speak 00:03:01.176 --> 00:03:02.377 the Wayland protocol. 00:03:03.978 --> 00:03:09.062 EWC allowed me to replace the Rust part of Reka 00:03:09.242 --> 00:03:10.643 with pure Elisp. 00:03:12.704 --> 00:03:17.768 I named my fork of Reka Râu, which is the 00:03:17.848 --> 00:03:19.629 Romanian word for river. 00:03:22.080 --> 00:03:26.123 After a lot of refactorings, I eventually arrived 00:03:26.423 --> 00:03:28.564 at the architecture shown here: 00:03:30.325 --> 00:03:33.947 a frontend Emacs process that starts a backend 00:03:34.147 --> 00:03:35.048 Emacs process. 00:03:36.329 --> 00:03:38.129 The backend Emacs process 00:03:38.130 --> 00:03:42.451 talks to River. Note that although the backend 00:03:42.452 --> 00:03:44.173 is a child process 00:03:44.273 --> 00:03:48.436 of the frontend, they do not use standard input 00:03:48.696 --> 00:03:51.638 and output as their communication channel. 00:03:53.485 --> 00:03:56.707 I did not find a non-blocking way for Emacs to 00:03:56.787 --> 00:04:00.230 listen to its standard input, and the backend 00:04:00.410 --> 00:04:03.292 must not block because it also needs to 00:04:03.352 --> 00:04:04.813 communicate with River. 00:04:06.854 --> 00:04:09.957 I will also talk about another problem I ran into 00:04:10.017 --> 00:04:13.999 before I arrived at the current state, but first 00:04:14.220 --> 00:04:17.402 some words about the magic involved that lets 00:04:17.601 --> 00:04:20.404 Emacs manage Wayland windows. NOTE River: Separating compositor and WM 00:04:24.027 --> 00:04:26.909 Please do not try to understand the state machine 00:04:27.109 --> 00:04:27.909 on this slide. 00:04:28.430 --> 00:04:31.011 This is only an illustration for River. 00:04:33.092 --> 00:04:36.615 Isaac Freund presented River on this year's 00:04:36.695 --> 00:04:37.235 FOSDEM. 00:04:38.436 --> 00:04:41.498 After working on the window manager River for 00:04:41.678 --> 00:04:45.740 over six years, he made an important observation 00:04:46.020 --> 00:04:48.802 about Wayland window managers in general. 00:04:50.327 --> 00:04:53.751 At this point, it is important to understand that 00:04:53.971 --> 00:04:57.835 so far every Wayland window manager was actually 00:04:58.056 --> 00:05:02.500 a window manager and a compositor in one program. 00:05:04.002 --> 00:05:06.544 So, Isaac noticed that there is 00:05:06.545 --> 00:05:08.546 a similar state machine 00:05:08.547 --> 00:05:11.650 at the core of different Wayland window managers, 00:05:12.770 --> 00:05:15.732 and that this state machine could actually be the 00:05:15.852 --> 00:05:22.116 base for a common API between window managers and 00:05:22.196 --> 00:05:23.337 Wayland compositors. 00:05:24.598 --> 00:05:29.741 So he created this API and since version 4, River 00:05:29.841 --> 00:05:32.623 is now only a Wayland compositor. 00:05:34.125 --> 00:05:37.547 There are now over 20 different Wayland window 00:05:37.587 --> 00:05:41.189 managers in 8 different programming languages 00:05:41.649 --> 00:05:44.110 using River as their compositor. NOTE How? 00:05:47.512 --> 00:05:52.034 Let's look at the core idea of Reka and Râu. 00:05:53.195 --> 00:05:58.357 I have not looked at EXWM yet, but I assume it 00:05:58.437 --> 00:05:59.898 does something similar. 00:06:02.347 --> 00:06:07.408 For every screen or output in Wayland terms, we 00:06:07.488 --> 00:06:09.229 create an Emacs frame. 00:06:10.849 --> 00:06:14.470 Such frames have two important Wayland objects 00:06:14.650 --> 00:06:15.510 related to them: 00:06:16.950 --> 00:06:20.791 the Wayland output that this Emacs frame should 00:06:20.871 --> 00:06:25.952 be shown on and the Wayland window that is 00:06:26.072 --> 00:06:28.533 actually showing the Emacs frame. 00:06:30.345 --> 00:06:35.108 Emacs frames can have parameters that can store 00:06:35.509 --> 00:06:36.630 arbitrary data. 00:06:38.631 --> 00:06:44.635 We store the IDs of the associated Wayland 00:06:44.755 --> 00:06:47.077 objects as frame parameters. 00:06:48.278 --> 00:06:52.661 That is, the window ID and the output ID. 00:06:54.757 --> 00:06:58.199 Then there are the other Wayland windows that are 00:06:58.259 --> 00:07:02.000 not showing Emacs frames associated with outputs. 00:07:03.441 --> 00:07:06.101 For every such Wayland window, 00:07:06.102 --> 00:07:08.162 we create an Emacs buffer 00:07:08.163 --> 00:07:11.564 and store the Wayland window id 00:07:11.565 --> 00:07:14.345 as a buffer-local value 00:07:14.346 --> 00:07:18.328 and the Wayland window title as the buffer name. 00:07:19.389 --> 00:07:23.527 Now every time the buffer is shown, 00:07:23.528 --> 00:07:27.908 we ask Emacs for the edges of the buffer 00:07:27.909 --> 00:07:30.490 and use those to position 00:07:30.590 --> 00:07:35.511 the Wayland window exactly above its associated 00:07:35.751 --> 00:07:36.651 Emacs buffer. 00:07:40.312 --> 00:07:42.493 The same idea visualized: 00:07:44.106 --> 00:07:47.609 For every Wayland output, that means for every 00:07:47.649 --> 00:07:52.153 screen or monitor, there is one maximized Emacs 00:07:52.233 --> 00:07:54.455 frame occupying the whole screen. 00:07:56.937 --> 00:08:01.301 The Emacs frame displays one or more Emacs 00:08:01.401 --> 00:08:05.325 windows with associated Emacs buffers. 00:08:07.277 --> 00:08:11.898 If such an Emacs buffer is a rau-mode buffer, 00:08:12.699 --> 00:08:16.480 then we display the associated Wayland window 00:08:16.760 --> 00:08:20.361 exactly at the position of the Emacs window. NOTE Demo 00:08:21.881 --> 00:08:24.102 Now, let's see this in action. 00:08:25.522 --> 00:08:29.183 For simplicity, we don't use a launcher but start 00:08:29.243 --> 00:08:31.644 LibreOffice as an async process. 00:08:32.904 --> 00:08:35.664 Async is important here to not block Emacs. 00:08:37.155 --> 00:08:40.376 Râu has initial support for floating windows. 00:08:41.275 --> 00:08:44.256 They can be moved around by pressing a modifier 00:08:44.317 --> 00:08:48.259 key together with the left mouse button and 00:08:48.619 --> 00:08:50.980 resized with the right mouse button. 00:08:52.640 --> 00:08:55.401 A middle button click with the modifier key 00:08:55.581 --> 00:08:56.662 closes the window. 00:08:58.162 --> 00:09:02.044 Notice that the Emacs UI is fully functional, 00:09:02.524 --> 00:09:04.024 including pop-up menus. 00:09:04.745 --> 00:09:05.765 I will come back to this. 00:09:07.175 --> 00:09:10.818 Wayland windows are represented as buffers and 00:09:10.838 --> 00:09:13.000 thus can be switched like buffers. 00:09:14.481 --> 00:09:18.644 Splitting the screen automatically resizes, and if 00:09:18.744 --> 00:09:21.607 necessary, repositions the Wayland window. 00:09:23.068 --> 00:09:26.930 Use `C-x b` with autocompletion 00:09:26.931 --> 00:09:28.052 to switch windows. 00:09:29.213 --> 00:09:32.135 You can also close windows by killing their 00:09:32.255 --> 00:09:34.077 associated Emacs buffers. NOTE Concurrency 00:09:37.313 --> 00:09:41.556 As I said, it took multiple iterations to arrive 00:09:41.656 --> 00:09:43.117 at the current architecture. 00:09:44.538 --> 00:09:47.920 The most pressing problem was that accidental 00:09:48.000 --> 00:09:52.663 mouse click could open an x-popup menu and this 00:09:52.744 --> 00:09:54.765 would freeze the whole window manager. 00:09:56.266 --> 00:10:00.108 I can only assume that somehow Emacs and River 00:10:00.289 --> 00:10:02.230 were waiting for each other. 00:10:04.064 --> 00:10:08.407 So I first tried to defer event processing with 00:10:08.547 --> 00:10:12.129 timers and get it out of the process-filter 00:10:12.169 --> 00:10:12.610 function. 00:10:13.350 --> 00:10:14.491 That did not help. 00:10:16.132 --> 00:10:19.994 Then I used artificial intelligence to write me 00:10:20.114 --> 00:10:21.215 an Emacs module. 00:10:22.156 --> 00:10:25.738 The module would do the Unix socket communication 00:10:25.898 --> 00:10:27.259 in a separate thread. 00:10:28.729 --> 00:10:32.529 However, this also did not solve the freezing, 00:10:32.530 --> 00:10:34.231 so I threw the code away. 00:10:36.371 --> 00:10:39.311 Thanks to artificial intelligence, 00:10:39.312 --> 00:10:41.753 this experiment took only about an hour. 00:10:43.513 --> 00:10:46.393 Eventually, I broke Râu 00:10:46.394 --> 00:10:49.655 into two separate Emacs processes. 00:10:50.655 --> 00:10:52.656 That finally fixed the issue. 00:10:54.436 --> 00:10:57.237 I did the coding of this refactoring myself. 00:10:58.601 --> 00:11:01.861 But I discussed the necessary steps 00:11:01.862 --> 00:11:04.803 and code organization with AI. NOTE On concurrency 00:11:07.063 --> 00:11:10.424 I looked at existing packages for concurrency. 00:11:11.344 --> 00:11:14.245 They also use separate Emacs processes. 00:11:15.485 --> 00:11:19.626 Both async and futur, however, only support 00:11:19.806 --> 00:11:21.067 short-lived functions. 00:11:22.247 --> 00:11:25.308 For Râu, I need a long-living process. 00:11:26.943 --> 00:11:31.066 In the end, it took only about 150 lines of code 00:11:31.687 --> 00:11:35.710 to start a child Emacs instance and allow remote 00:11:35.770 --> 00:11:38.232 procedure calls in both directions. 00:11:39.793 --> 00:11:42.935 Both processes just call functions 00:11:42.936 --> 00:11:43.817 on the other side. 00:11:45.198 --> 00:11:47.639 The code could be that simple 00:11:47.640 --> 00:11:50.201 because I do not need return values 00:11:50.202 --> 00:11:51.363 for the function calls. 00:11:53.052 --> 00:11:55.272 Serialization is provided by 00:11:55.273 --> 00:11:57.534 Elisp's symbolic expressions. 00:11:59.055 --> 00:12:02.456 The child Emacs process starts an Emacs server, 00:12:02.996 --> 00:12:06.378 so I can look into the backend with Emacs client. 00:12:07.618 --> 00:12:11.079 So, although my problem is solved, 00:12:11.080 --> 00:12:12.499 it would be very nice to have 00:12:12.500 --> 00:12:15.400 isolated worker threads in Emacs 00:12:15.401 --> 00:12:16.502 like in JavaScript. NOTE Logging: lgr 00:12:22.898 --> 00:12:25.739 A window manager should of course write log 00:12:25.779 --> 00:12:28.520 messages and write them to the same place as 00:12:29.120 --> 00:12:30.260 other services do. 00:12:31.561 --> 00:12:35.222 I found around 15 different logging 00:12:35.342 --> 00:12:38.162 implementations in my Emacs, 00:12:39.343 --> 00:12:41.943 all of them specific to their package. 00:12:43.524 --> 00:12:45.704 Thankfully there is now a logging package 00:12:45.705 --> 00:12:46.805 for Emacs. 00:12:48.102 --> 00:12:51.685 Lgr supports a hierarchy of loggers. 00:12:51.705 --> 00:12:54.427 Each node in the hierarchy can have its own 00:12:54.548 --> 00:12:55.468 configuration, 00:12:56.489 --> 00:12:58.890 so you can raise the logging level 00:12:58.891 --> 00:13:00.932 in only a specific part of your code 00:13:00.933 --> 00:13:04.875 and won't be spammed by log messages 00:13:04.876 --> 00:13:05.697 from other parts. 00:13:06.998 --> 00:13:08.659 Log messages have levels. 00:13:09.140 --> 00:13:11.901 They can be written to different places 00:13:11.902 --> 00:13:14.924 and log messages are only constructed 00:13:14.925 --> 00:13:16.126 when actually needed. 00:13:17.412 --> 00:13:21.153 The latter allows you to craft informative debug 00:13:21.373 --> 00:13:25.835 log messages with helpful data without worrying 00:13:26.015 --> 00:13:27.155 about performance. 00:13:28.576 --> 00:13:32.217 Please adopt Lgr and make it part of Emacs core! NOTE Commodore 64 00:13:36.158 --> 00:13:40.279 When you're around my age, you might have a warm 00:13:40.399 --> 00:13:42.520 feeling when seeing this screen. 00:13:44.490 --> 00:13:48.732 One great thing about the C64 was that 00:13:48.733 --> 00:13:51.153 it welcomed the user directly 00:13:51.154 --> 00:13:52.875 with the programming environment. 00:13:54.256 --> 00:13:56.296 It was very natural to start 00:13:56.297 --> 00:13:57.538 hacking your computer. 00:14:01.100 --> 00:14:04.942 It only occurred to me after some time that Râu 00:14:05.242 --> 00:14:08.044 actually recreates this experience. 00:14:09.261 --> 00:14:12.164 When you use Emacs as your window manager, 00:14:12.165 --> 00:14:14.047 you are booting directly into 00:14:14.048 --> 00:14:16.612 an Elisp programming environment. 00:14:18.354 --> 00:14:20.597 If I were a school teacher, 00:14:20.598 --> 00:14:24.183 I would install Râu in the Computer Science lab. 00:14:28.037 --> 00:14:31.219 Besides Râu, there is of course the old 00:14:31.220 --> 00:14:33.502 Emacs X Window Manager. 00:14:34.182 --> 00:14:37.225 There is Reka, which I already talked about. 00:14:38.165 --> 00:14:42.149 And there is EWM, also a Wayland window manager. 00:14:43.750 --> 00:14:47.173 EWM was initially created with the help of 00:14:47.753 --> 00:14:49.014 artificial intelligence. 00:14:49.935 --> 00:14:53.398 It includes its own compositor and is more 00:14:53.458 --> 00:14:55.840 featureful than Reka or Râu. 00:14:57.503 --> 00:15:01.643 I have created the Category Window Manager 00:15:01.644 --> 00:15:02.844 in the Emacs Wiki. 00:15:03.484 --> 00:15:06.285 Please look there and also update the wiki. NOTE Artificial intelligence 00:15:10.206 --> 00:15:12.186 I have not made up my mind yet. 00:15:13.647 --> 00:15:17.307 I am humble enough to understand that artificial 00:15:17.387 --> 00:15:19.848 intelligence is a complex issue. 00:15:21.897 --> 00:15:25.500 I am convinced though that any meaningful 00:15:25.560 --> 00:15:30.284 discussion on AI should involve at least the 00:15:30.344 --> 00:15:32.146 concepts written on this slide. 00:15:34.247 --> 00:15:37.890 Artificial intelligence is no doubt a tool for 00:15:37.990 --> 00:15:40.693 control over people through surveillance, 00:15:41.353 --> 00:15:43.535 autonomous weapons, and propaganda. 00:15:45.340 --> 00:15:48.321 Artificial intelligence challenges previous 00:15:48.441 --> 00:15:51.723 understanding of property, labor, and capital. 00:15:53.243 --> 00:15:56.725 And none of the above can be talked about without 00:15:56.985 --> 00:16:00.586 also talking about human life and love. 00:16:02.467 --> 00:16:05.368 Unfortunately, that cannot be done in a 00:16:06.029 --> 00:16:08.890 conference talk nor on a mailing list. 00:16:13.268 --> 00:16:18.252 I did use AI right at the start to convert the 00:16:18.312 --> 00:16:20.734 Rust part of Reka into Elisp. 00:16:21.715 --> 00:16:24.918 Since then, however, I have refactored and 00:16:25.158 --> 00:16:30.822 rewritten code so much that I am not aware of any 00:16:30.922 --> 00:16:34.545 remaining trace of the initial AI converted code. 00:16:36.327 --> 00:16:39.009 I do not use AI to generate code. 00:16:40.823 --> 00:16:43.743 The most important reason for me is that 00:16:43.744 --> 00:16:46.584 the brain needs training like a muscle 00:16:46.585 --> 00:16:48.406 and I don't want to become stupid. 00:16:49.986 --> 00:16:53.567 And second, I only want to commit code that I 00:16:53.727 --> 00:16:54.768 fully understand. 00:16:56.908 --> 00:17:02.790 I still do use AI, however, to review my code and 00:17:02.850 --> 00:17:04.590 this is tremendously helpful. 00:17:05.611 --> 00:17:07.791 It regularly catches errors. 00:17:09.512 --> 00:17:12.856 Then AI is a much better search engine. 00:17:14.098 --> 00:17:18.122 I can ask, for example, is there a built-in 00:17:18.402 --> 00:17:21.604 function in Emacs to check whether a value 00:17:21.605 --> 00:17:22.767 is a cons cell. 00:17:25.128 --> 00:17:30.233 And the life-changing benefit of AI shows 00:17:30.234 --> 00:17:31.436 during debugging. 00:17:32.654 --> 00:17:36.517 AI easily finds the root cause that would 00:17:36.718 --> 00:17:39.860 otherwise have taken me hours to dig up. 00:17:42.322 --> 00:17:45.485 Another observation is that AI probably 00:17:45.645 --> 00:17:47.847 diminished the role of testing. 00:17:49.008 --> 00:17:53.291 Writing tests for a graphical, rapidly evolving 00:17:53.371 --> 00:17:57.635 project with multiple network connected 00:17:57.815 --> 00:18:01.218 components is more than a challenge. 00:18:02.619 --> 00:18:06.581 Having AI available for code review and debugging 00:18:07.382 --> 00:18:10.924 was more than a replacement for a test read at 00:18:11.144 --> 00:18:13.306 this stage of the project. 00:18:16.968 --> 00:18:21.010 As I said, AI is a very complex issue. 00:18:23.252 --> 00:18:27.033 In the context of a large and old codebase 00:18:27.034 --> 00:18:27.815 like Emacs, 00:18:28.805 --> 00:18:33.887 it is however very inspiring to think about the 00:18:34.007 --> 00:18:38.789 things that AI could enable and that previously 00:18:39.049 --> 00:18:41.711 would be deemed as infeasible. 00:18:44.312 --> 00:18:49.734 What could you do with 100 junior programmers 00:18:50.134 --> 00:18:52.015 that won't get tired? NOTE Wrapping up 00:18:55.774 --> 00:19:00.802 So, please go ahead and give Râu a try. 00:19:02.004 --> 00:19:04.889 I'm looking forward to user feedback and 00:19:05.109 --> 00:19:06.051 contributions. 00:19:07.613 --> 00:19:08.034 Thank you.