Fix C-g handling with multiple ttys.
[bpt/emacs.git] / README.multi-tty
CommitLineData
3224dac1 1 -*- coding: utf-8; mode: text; -*-
28d440ab
KL
2GOAL
3----
4
5The ultimate goal of this branch is to implement support for opening
6multiple, different tty devices and simultaneous X and tty frames from
7a single Emacs session.
8
7b00d185
KL
9Some use cases:
10
11Emacs is notoriously slow at startup, so most people use another
12editor or emacsclient for quick editing jobs from the console.
13Unfortunately, emacsclient was very awkward to use, because it did not
14support opening a new Emacs frame on the current virtual console.
15Now, with multi-tty support, it can do that. (Emacsclient starts up
16faster than vi!)
17
18Some Gnus users (including me) run Gnus in an X frame in its own Emacs
19instance, which they typically leave running for weeks. It would be
20nice if they could connect to this instance from a remote ssh session
21and check their messages without opening a remote X frame or resorting
22to gnus-slave.
23
24
28d440ab
KL
25WHO IS DOING IT
26---------------
27
28I'm Károly Lőrentey. My address: lorentey@elte.hu.
29
30Patches or suggestions are welcome!
31
6548cf00 32Retrieving the latest version of the branch:
4f0359de
KL
33
34 tla register-archive lorentey@elte.hu--2004 http://lorentey.web.elte.hu/arch/2004/
daf01701 35 tla get lorentey@elte.hu--2004/emacs--multi-tty <directory>
4f0359de 36
7b00d185
KL
37(I use a recent arch development snapshot, but any of the released
38versions of arch will do fine, I think.)
39
40If you don't have arch, the branch has a homepage from which you can
41download conventional patches against Emacs CVS HEAD:
4f0359de 42
7b00d185 43 http://lorentey.web.elte.hu/project/emacs.html
28d440ab
KL
44
45STATUS
46------
47
7b00d185
KL
48Multi-tty support is stable, I think most of the problems were fixed.
49(It still needs testing on other architectures, though.) Please let
50me know if you find any bugs in it. Emacsclient has been extended to
51support opening a new terminal frame.
28d440ab 52
7b00d185 53To try it out, compile and run the multi-tty branch with the following
04c3243c 54commands:
bc279d67
KL
55
56 mkdir +build
57 cd +build
daf01701 58 ../configure
bc279d67 59 make bootstrap
7b00d185
KL
60 src/emacs -nw
61 M-x server-start
bc279d67 62
7b00d185
KL
63and then (from a shell prompt on another terminal) start emacsclient
64with
28d440ab 65
7b00d185 66 lib-src/emacsclient -t /optional/file/names...
28d440ab 67
04c3243c 68You'll hopefully have two fully working, independent frames on
7b00d185
KL
69separate terminals. The new frame is closed automatically when you
70have finished editing the specified files (C-x #), but delete-frame
71(C-x 5 0) also works. Of course, you can create frames on more than
72two tty devices.
52c2ee2a 73
7b00d185
KL
74Creating new frames on the same tty with C-x 5 2 works, and they
75behave the same way as in previous Emacs versions. If you exit emacs,
76all terminals should be restored to their previous states.
daf01701 77
7b00d185
KL
78This is work in progress, and probably full of bugs. You should
79always run emacs from gdb, so that you'll have a live instance to
80debug if something goes wrong. Please send me your reports.
04c3243c 81
7b00d185 82Problems:
6548cf00 83
7b00d185
KL
84 * Suspending Emacs is disabled if there are multiple tty
85 devices. Also, there is no way to suspend emacsclient. This
86 will be fixed.
87
88 * X support is (I hope) working, but at the moment there are
89 problems with simultaneous X and tty devices, so don't do
90 that - start a separate Emacs with -nw and run the server
91 there.
92
93 * Mac, Windows and DOS support is broken, probably doesn't
94 even compile -- this will be solved later.
95
96 * Only tested on my GNU/Linux box.
28d440ab
KL
97
98NEWS
99----
100
101For the NEWS file:
102
103** Support for multiple terminal devices has been added. You can
104 specify a terminal device (`tty' parameter) and a terminal type
6548cf00 105 (`tty-type' parameter) to `make-terminal-frame'. `tty' must be a
52c2ee2a 106 terminal device created by the updated emacsclient, or there will
114a8b8c 107 be problems with terminal input and window resizes.
6548cf00
KL
108
109 You can test for the presence of multiple terminal support by
110 testing for the `multi-tty' feature.
111
112** A make-frame-on-tty function has been added to make it easier to
113 create frames on new terminals.
28d440ab 114
819b8f00
KL
115** New functions: frame-tty-name, frame-tty-type for accessing
116 terminal parameters, and delete-tty for closing the terminal
117 device.
118
9628b887
KL
119** Emacsclient has been extended to support opening a new terminal
120 frame.
28d440ab
KL
121
122CHANGELOG
123---------
124
125See arch logs.
126
7b00d185
KL
127THINGS TO DO
128------------
129
428a555e
KL
130** Fix color handling during X+tty combo sessions. (It seems that tty
131 sessions automatically convert the face colors to terminal colors
132 when the face is loaded. This conversion must happen instead on
133 the fly in write_glyphs, which might be problematic, as color
3224dac1
KL
134 approximation is currently done in lisp (term/tty-colors.el).)
135
114a8b8c
KL
136** Fix interactive use of temacs. There are face-related SEGVs, most
137 likely because of changes in realize_default_face, realize_face.
138
4ca927b4
KL
139** Very strange bug: visible-bell does not work on secondary
140 terminals. This might be something xterm (konsole) specific.
141
114a8b8c 142** Allow opening an X session after -nw.
7b00d185
KL
143
144** Find out the best way to support suspending Emacs with multiple
145 ttys. My guess: disable it on the controlling tty, but from other
146 ttys pass it on to emacsclient somehow. (It is (I hope) trivial to
147 extend emacsclient to handle suspend/resume. A `kill -STOP' almost
148 works right now.)
149
4ca927b4 150** Exiting Emacs while there are emacsclient frames doesn't restore the
114a8b8c 151 ttys to their default states.
428a555e
KL
152
153** Move baud_rate to struct display.
7b00d185 154
7b00d185
KL
155** Implement support for starting an interactive Emacs session without
156 an initial frame. (The user would connect to it and open frames
157 later, with emacsclient.) Not necessarily a good idea.
158
3224dac1
KL
159** Fix Mac support (I can't do this myself). Note that the current
160 state of Mac-specific source files in the multi-tty tree are not
161 useful; before starting work on Mac support, revert to pristine,
162 pre-multi-tty versions.
7b00d185 163
3224dac1
KL
164** Fix W32 support (I can't do this myself). Note that the current
165 state of W32-specific source files in the multi-tty tree are not
166 useful; before starting work on W32 support, revert to pristine,
167 pre-multi-tty versions.
7b00d185 168
3224dac1
KL
169** Fix DOS support (I can't do this myself). Note that the current
170 state of DOS-specific source files in the multi-tty tree are not
171 useful; before starting work on DOS support, revert to pristine,
172 pre-multi-tty versions.
7b00d185
KL
173
174** Do a grep on XXX and ?? for more issues.
175
176** Understand Emacs's low-level input system (it seems complicated) :-)
428a555e 177 What does interrupt_input do? I tried to disable it for raw
7b00d185
KL
178 secondary tty support, but it does not seem to do anything useful.
179 (Update: Look again. X unconditionally enables this, maybe that's
180 why raw terminal support is broken again. I really do need to
181 understand input.)
182
7b00d185
KL
183** I have seen a case when Emacs with multiple ttys fell into a loop
184 eating 100% of CPU time. Strace showed this loop:
185
186 getpid() = 30284
187 kill(30284, SIGIO) = 0
188 --- SIGIO (I/O possible) @ 0 (0) ---
189 ioctl(6, FIONREAD, [0]) = -1 EIO (Input/output error)
190 ioctl(5, FIONREAD, [0]) = -1 EIO (Input/output error)
191 ioctl(0, FIONREAD, [0]) = 0
192 sigreturn() = ? (mask now [])
193 gettimeofday({1072842297, 747760}, NULL) = 0
194 gettimeofday({1072842297, 747806}, NULL) = 0
195 select(9, [0 3 5 6], NULL, NULL, {0, 0}) = 2 (in [5 6], left {0, 0})
196 select(9, [0 3 5 6], NULL, NULL, {0, 0}) = 2 (in [5 6], left {0, 0})
197 gettimeofday({1072842297, 748245}, NULL) = 0
198
4d553a13
KL
199 I have seen something similar with a single X frame, but have not
200 been able to reproduce it for debugging.
7b00d185 201
428a555e
KL
202 Update: This may have been caused by checking for nread != 0
203 instead of nread > 0 after calling read_socket_hook in
204 read_avail_input.
205
7b00d185
KL
206** emacsclient -t from an Emacs term buffer does not work, complains
207 about face problems. This can even lock up Emacs (if the recursive
428a555e
KL
208 frame sets single_kboard). Update: the face problems are caused by
209 bugs in term.el, not in multi-tty. The lockup is caused by
4ca927b4
KL
210 single_kboard mode, and is not easily solvable. The best thing to
211 do is to simply refuse to create a tty frame of type `eterm'.
4d553a13 212
6548cf00
KL
213DIARY OF CHANGES
214----------------
215
216(ex-TODO items with explanations.)
28d440ab 217
114a8b8c 218-- Introduce a new struct for terminal devices.
28d440ab 219
daf01701 220 (Done, see struct tty_output. The list of members is not yet
28d440ab
KL
221 complete.)
222
28d440ab
KL
223-- Change the bootstrap procedure to initialize tty_list.
224
225 (Done, but needs review.)
226
28d440ab
KL
227-- Change make-terminal-frame to support specifying another tty.
228
229 (Done, new frame parameters: `tty' and `tty-type'.)
230
9628b887
KL
231-- Implement support for reading from multiple terminals.
232
233 (Done, read_avail_input tries to read from each terminal, until one
28d7d09f 234 succeeds. MULTI_KBOARD is not used. Secondary terminals don't send
6548cf00 235 SIGIO!)
9628b887 236
819b8f00
KL
237 (Update: They do, now.)
238
daf01701
KL
239 (Update2: After enabling X, they don't.)
240
9628b887 241-- other-frame should cycle through the frames on the `current'
114a8b8c 242 terminal only.
9628b887 243
6548cf00 244 (Done, by trivially modifiying next_frame and prev_frame.)
9628b887
KL
245
246-- Support different terminal sizes.
114a8b8c 247
9628b887
KL
248 (Done, no problem.)
249
250-- Make sure terminal resizes are handled gracefully. (Could be
251 problematic.)
252
6548cf00
KL
253 (Done. We don't get automatic SIGWINCH for additional ttys,
254 though.)
9628b887
KL
255
256-- Extend emacsclient to automatically open a new tty when it connects
257 to Emacs.
114a8b8c 258
9628b887
KL
259 (Done. It's an ugly hack, needs more work.)
260
52c2ee2a
KL
261-- Redisplay must refresh the topmost frame on *all* terminals, not
262 just the initial terminal.
114a8b8c 263
52c2ee2a 264 (Done, but introduced an ugly redisplay problems. Ugh.)
9628b887 265
6548cf00
KL
266-- Fix redisplay problems.
267
52c2ee2a
KL
268 (Done; it turned out that the entire Wcm structure must be moved
269 inside tty_output. Why didn't I catch this earlier?)
6548cf00
KL
270
271-- Provide a way for emacsclient to tell Emacs that the tty has been
272 resized.
273
274 (Done, simply forward the SIGWINCH signal.)
275
276-- Each keypress should automatically select the frame corresponding
277 to the terminal that it was coming from. This means that Emacs
278 must know from which terminal the last keyboard event came from.
6548cf00 279
52c2ee2a
KL
280 (Done, it was quite simple, the input event system already
281 supported multiple frames.)
6548cf00
KL
282
283-- Fix SIGIO issue with secondary terminals.
284
285 (Done, emacsclient signals Emacs after writing to the proxy pseudo
52c2ee2a
KL
286 terminal. Note that this means that multi-tty does not work with
287 raw ttys!)
28d440ab 288
0a125897
KL
289 (Update: This is bullshit. There is a read_input_waiting function,
290 extend that somehow.)
291
819b8f00
KL
292 (Update of update: The first update was not right either, extending
293 read_input_waiting was not necessary. Secondary ttys do seem to
294 send signals on input.)
295
daf01701
KL
296 (Update^3: Not any more.)
297
fca177d4
KL
298-- Make make-terminal-frame look up the `tty' and `tty-type' frame
299 parameters from the currently selected terminal before the global
300 default.
301
302 (Done.)
303
304-- Put all cached terminal escape sequences into struct tty_output.
305 Currently, they are still stored in global variables, so we don't
306 really support multiple terminal types.
307
52c2ee2a 308 (Done. It was not fun.)
fca177d4
KL
309
310-- Implement sane error handling after initialization. (Currently
6548cf00
KL
311 emacs exits if you specify a bad terminal type.) The helpful error
312 messages must still be provided when Emacs starts.
313
fca177d4
KL
314 (Done.)
315
316-- Implement terminal deletion, i.e., deleting local frames, closing
317 the tty device and restoring its previous state without exiting
318 Emacs.
9628b887 319
fca177d4
KL
320 (Done, but at the moment only called when an error happens during
321 initialization. There is a memory corruption error around this
daf01701 322 somewhere.) (Update: now it is fully enabled.)
fca177d4 323
8303ba32
KL
324-- Implement automatic deletion of terminals when the last frame on
325 that terminal is closed.
326
327 (Done.)
328
0a125897
KL
329-- Restore tty screen after closing the terminal.
330
331 (Done, we do the same as Emacs 21.2 for all terminals.)
332
333-- 'TERM=dumb src/emacs' does not restore the terminal state.
114a8b8c 334
0a125897 335 (Done.)
fca177d4 336
b2af72d2
KL
337-- C-g should work on secondary terminals.
338
339 (Done, but the binding is not configurable.)
340
341-- Deal with SIGHUP in Emacs and in emacsclient. (After this, the
342 server-frames may be removed from server.el.)
343
344 (Done, nothing to do. It seems that Emacs does not receive SIGHUP
daf01701 345 from secondary ttys, which is actually a good thing.) (Update: I
7b00d185 346 think it would be a bad idea to remove server-frames.)
b2af72d2 347
daf01701 348-- Change emacsclient/server.el to support the -t argument better,
b2af72d2
KL
349 i.e. automatically close the socket when the frame is closed.
350
351 (Seems to be working OK.)
352
16c290d8 353-- Fix mysterious memory corruption error with tty deletion. To
fca177d4
KL
354 trigger it, try the following shell command:
355
356 while true; do TERM=no-such-terminal-definition emacsclient -h; done
357
358 Emacs usually dumps core after a few dozen iterations. (The bug
3224dac1 359 seems to be related to the xfreeing or bzeroing of
9d9f1812
KL
360 tty_output.Wcm. Maybe there are outside references to struct Wcm?
361 Why were these vars collected into a struct before multi-tty
362 support?)
fca177d4 363
16c290d8
KL
364 (Done. Whew. It turned out that the problem had nothing to do
365 with hypothetical external references to Wcm, or any other
366 tty_output component; it was simply that delete_tty closed the
367 filehandles of secondary ttys twice, resulting in fclose doubly
3224dac1 368 freeing memory. Utterly trivial matter. I love the C's memory
16c290d8
KL
369 management, it puts hair on your chest.)
370
819b8f00 371-- Support raw secondary terminals. (Note that SIGIO works only on
3224dac1 372 the controlling terminal.) Hint: extend read_input_waiting for
819b8f00 373 multiple ttys and hopefully this will be fixed.
fca177d4 374
819b8f00
KL
375 (Done, it seems to have been working already for some time. It
376 seems F_SETOWN does work, after all. Not sure what made it fail
377 earlier, but it seems to be fixed (there were several changes
378 around request_sigio, maybe one of them did it).
3224dac1 379 read_input_waiting is only used in sys_select, don't change
daf01701 380 it.) (Update: After adding X support, it's broken again.)
c1c63edb 381
819b8f00 382-- Find out why does Emacs abort when it wants to close its
0a125897 383 controlling tty. Hint: chan_process[] array. Hey, maybe
16c290d8
KL
384 noninterrupt-IO would work, too? Update: no, there is no process
385 for stdin/out.
c33d2d42 386
819b8f00
KL
387 (Done. Added add/delete_keyboard_wait_descriptor to
388 term_init/delete_tty. The hint was right, in a way.)
6548cf00 389
819b8f00 390-- Issue with SIGIO: it needs to be disabled during redisplay. See if
3224dac1 391 fcntl kernel behaviour could be emulated by emacsclient.
0a125897 392
819b8f00 393 (Done. Simply disabled the SIGIO emulation hack in emacsclient.)
daf01701 394 (Update: it was added back.)
22de1e79 395
819b8f00 396-- server.el: There are issues with saving files in buffers of closed
16c290d8
KL
397 clients. Try editing a file with emacsclient -f, and (without
398 saving it) do a delete-frame. The frame is closed without
399 question, and a surprising confirmation prompt appears in another
400 frame.
401
819b8f00
KL
402 (Done. delete-frame now asks for confirmation if it still has
403 pending buffers, and modified buffers don't seem to be deleted.)
404
405-- emacsclient.el, server.el: Handle eval or file open errors when
daf01701 406 doing -t.
16c290d8 407
819b8f00 408 (Done.)
22de1e79 409
819b8f00
KL
410-- Make parts of struct tty_output accessible from Lisp. The device
411 name and the type is sufficient.
22de1e79 412
819b8f00
KL
413 (Done, see frame-tty-name and frame-tty-type.)
414
415-- Export delete_tty to the Lisp environment, for emacsclient.
22de1e79 416
819b8f00
KL
417 (Done, see delete-tty.)
418
daf01701
KL
419-- Get rid of the accessor macros in termchar.h, or define macros for
420 all members.
421
422 (Done.)
423
424-- Move device-specific parameters (like costs) commonly used by
425 device backends to a common, device-dependent structure.
426
427 (Done. See struct display_method in termhooks.h.)
428
429-- Fix X support.
430
431 (Done. Well, it seems to be working.)
432
433-- Allow simultaneous X and tty frames. (Handling input could be
434 tricky. Or maybe not.)
435
436 (Done. Allowed, that is. It is currently extremely unstable, to
437 the point of being unusable. The rif variable causes constant
438 core dumps. Handling input is indeed tricky.)
819b8f00 439
7b00d185 440-- Rewrite multi-tty input in terms of MULTI_KBOARD.
28d440ab 441
7b00d185
KL
442 (Done. In fact, there was no need to rewrite anything, I just
443 added a kboard member to tty_display_info, and initialized the
444 frame's kboard from there.)
819b8f00 445
fa971ac3
KL
446-- Fix rif issue with X-tty combo sessions. IMHO the best thing to do
447 is to get rid of that global variable (and use the value value in
448 display_method, which is guaranteed to be correct).
449
450 (Done, did exactly that. Core dumps during combo sessions became
451 much rarer. In fact, I have not yet met a single one.)
452
17086732
KL
453-- Add multi-tty support to talk.el.
454
455 (Done.)
456
428a555e
KL
457-- Clean up the source of emacsclient. It is a mess.
458
459 (Done, eliminated stupid proxy-pty kludge.)
460
461-- Fix faces on tty frames during X-tty combo sessions. There is an
462 init_frame_faces call in init_sys_modes, see if there is a problem
463 with it.
464
465 (Done, there was a stupid mistake in
466 Ftty_supports_face_attributes_p. Colors are broken, though.)
467
468-- C-x 5 2, C-x 5 o, C-x 5 0 on an emacsclient frame unexpectedly
469 exits emacsclient. This is a result of trying to be clever with
470 delete-frame-functions.
471
472 (Fixed, added delete-tty-after-functions, and changed server.el to
473 use it.)
474
475-- Something with (maybe) multi-keyboard support broke function keys
476 and arrows on ttys during X+tty combo sessions. Debug this.
477
478 (I can't reproduce it, maybe the terminal type was wrong.)
479
480-- Fix input from raw ttys (again).
481
482 (Now it seems to work all right.)
483
484-- During an X-tty combo session, a (message "Hello") from a tty frame
485 goes to the X frame. Fix this.
486
487 (Done. There was a safeguard against writing to the initial
488 terminal frame during bootstrap which prevented echo_area_display
489 from working correctly on a tty frame during a combo session.)
490
114a8b8c
KL
491-- If there are no frames on its controlling terminal, Emacs should
492 exit if the uses presses C-c there.
493
494 (Done, as far as possible. See the SIGTERM comment in
495 interrupt_signal on why this seems to be impossible to solve this
496 in general.)
497
498-- During an X session, Emacs seems to read from stdin. Also, Emacs
499 fails to start without a controlling tty.
500
501 (Fixed by replacing the troublesome termcap display with a dummy
502 bootstrap display during bootstrap.
503
504-- Do tty output through struct display, like graphical display
505 backends.
506
507 (Done.)
508
509-- Define an output_initial value for output_method for the initial
510 frame that is dumped with Emacs. Checking for this frame (e.g. in
511 cmd_error_internal) is ugly.
512
513 (Done, broking interactive temacs.)
514
515-- The command `emacsclient -t -e '(delete-frame)'' fails to exit.
516
517 (Fixed.)
518
4ca927b4
KL
519-- frame-creation-function should always create a frame that is on the
520 same display as the selected frame. Maybe frame-creation-function
521 should simply be removed and make-frame changed to do the right
522 thing.
523
524 (Done, with a nice hack. frame-creation-function is now frame-local.)
525
526-- Fix C-g on raw ttys.
527
528 (Done. I disabled the interrupt/quit keys on all secondary
529 terminals, so Emacs sees C-g as normal input. This looks like an
530 overkill, because emacsclient has extra code to pass SIGINT to
531 Emacs, so C-g should remain the interrupt/quit key on emacsclient
532 frames. See the next entry why implementing this distinction would
533 be a bad idea.)
534
535-- Make sure C-g goes to the right frame with ttys. This is hard, as
536 SIGINT doesn't have a tty parameter. :-(
537
538 (Done, the previous change fixes this as a pleasant side effect.)
539
540
28d440ab 541;;; arch-tag: 8da1619e-2e79-41a8-9ac9-a0485daad17d