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