6

I'd like to write a x11 terminal emulator, but I don't know how I should spawn and communicate with the shell, is there any basic (pseudo- or C) code for that? like what sort of PTY to create, how to bind the shell to it, what signals I have to catch or send, etc. don't really feel like sorting through the whole xterm sources.

EDIT: oh and I want to implement a way of communicating with any applications in it, how shall I do the feature discovery? some hidden ansi sequence in the "clients", hoping it's not colliding with other terminal emulators? some environment variable, hoping it's not colliding with the "clients" or removed by the shell?

nonchip
  • 1,084
  • 1
  • 18
  • 36
  • 4
    Start by reading [Advanced Linux Programming](http://advancedlinuxprogramming.com/) and [the tty demystified](http://www.linusakesson.net/programming/tty/) then study the source code of e.g. `xterm` or `gnome-terminal` (study such code is much less time-consuming that reinventing it) – Basile Starynkevitch Mar 21 '14 at 22:18
  • I kinda hoped to avoid the foreign code study :P – nonchip Mar 21 '14 at 22:18
  • 1
    Then you probably won't have enough time to achieve your ambitious goals. Perhaps study `rxvt` source code (it probably is smaller). And why do you want to reinvent another terminal emulator? Why not improve an existing one? – Basile Starynkevitch Mar 21 '14 at 22:20
  • because I don't just want the terminal emulator application, but it embedded inside a bigger, kinda ide-like program of mine. – nonchip Mar 21 '14 at 22:27
  • is there any library doing this kind of stuff already? EDIT: libvterm, testing that. – nonchip Mar 21 '14 at 22:39
  • libvterm's curses dependency is terrible -_- – nonchip Mar 21 '14 at 23:38
  • ok, as I'm using luajit anyway, I'm now just using http://www.tset.de/lpty, which works fine. – nonchip Mar 22 '14 at 00:13

2 Answers2

7

YAT (yet another terminal) https://github.com/jorgen/yat is suitable for embedding in Qt Quick programs. Contributions for improvement are welcome. (Disclaimer: a friend started that project, and I work on it sometimes.) It takes a mostly correct approach (e.g. it uses a Linux pseudo-terminal properly, something I didn't know about before my friend was explaining that), and has a lot of features; however the parser is written from scratch and is not feature-complete or bug-free yet.

Unfortunately most terminal implementations so far have been starting from scratch, or with a one-off monolithic fork (from rxvt for example), which is a lot of work and results in all of them being incomplete. So I think a better alternative would be to use a reusable logic-only library called libvterm: http://www.leonerd.org.uk/code/libvterm/ or to base your terminal on one which already uses that. That way if you find bugs and fix them, you'll improve the whole ecosystem.

https://github.com/timmoorhouse/imgui-terminal is interesting, and works (at least somewhat) but is a prime candidate to be rewritten with libvterm, IMO. If you are into immediate-mode rendering in OpenGL, it might be a good choice anyway.

http://41j.com/hterm/ does use libvterm, and adds a few features which libvterm doesn't have, for inline graphics rendering (ReGIS and PNG). But the code is not elegant enough or portable enough, IMO, and the graphics rendering "floats" over the text rather than being truly inline. It still might be an adequate starting point for some use cases. In my fork https://github.com/ec1oud/hackterm I got it to build with mostly modern system libraries, however it still depends on an outdated version of SDL, which is included.

ecloud
  • 326
  • 2
  • 3
2

OK, if anyone also need this, and is using lua, I found the http://www.tset.de/lpty library works fine. still testing ansi escapes and stuff, but should work.

nonchip
  • 1,084
  • 1
  • 18
  • 36