This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository terminology.
View the commit online.
commit 55510ddb3a44d25c70dfa12b169af91aec2b21f6
Author: [email protected] <[email protected]>
AuthorDate: Fri Sep 25 17:23:34 2026 -0600
tymuxd: keep a terminal running in the background
tymuxd owns one session: it runs the shell through termpty, the parser
Terminology itself uses, and publishes the screen to its clients.
It links Elementary like tytest does but never calls elm_init(), so
termpty and the escape sequence parser build unchanged instead of
growing #if guards to avoid Elementary. That costs about 3 MB of RSS
for an idle daemon (12.4 MB to 15.3 MB). Without a textgrid, the
palette lookup is redirected to a stub at compile time, as tytest.h
does, so true colours and colour queries still work. It does not load
Ecore's system modules: a daemon hosting a shell has no use for their
power and locale events, and they would open a D-Bus connection.
One client is the primary: its input and size drive the session. A
client attaching in the default hijack mode takes over, and the
previous primary is paused until the hijacker leaves. Shared clients
send input alongside the primary, read-only clients only watch.
All clients are served from one thread, so a client that stops reading
must not stall the others: each one has its own output queue and is
dropped once more than 1 MiB is waiting in it.
NOTIFY is sent at most once per animator tick, and not while the
application holds its output back with synchronized output (mode
2026). With the shell printing 100000 lines one write at a time, this
sends 21 notifications instead of 1236 in the same time, 0.33 s either
way (tybenchd -l, added later in this series, median of 7 runs).
---
DESIGN.md | 3 +
src/bin/meson.build | 30 +++
src/bin/termd_tymux.c | 525 +++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/termd_tymux.h | 18 ++
src/bin/tymuxd.c | 162 +++++++++++++++
src/bin/tymuxd_stubs.c | 219 +++++++++++++++++++++
6 files changed, 957 insertions(+)
diff --git a/DESIGN.md b/DESIGN.md
index f6e3961e..d6f38d1f 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -26,6 +26,7 @@ A simple documentation to navigate through the C files:
* `src/bin/options_wallpaper.c`: the settings panel to configure a wallpaper
* `src/bin/sel.c`: the tab selector
* `src/bin/termcmd.c` handles custom terminology commands
+* `src/bin/termd_tymux.c`: publishes a terminal running in `tymuxd` to its clients
* `src/bin/termio.c`: the core term widget with the textgrid
* `src/bin/termiolink.c`: link detection in the terminal
* `src/bin/termpty.c`: the PTY interaction
@@ -41,6 +42,8 @@ A simple documentation to navigate through the C files:
* `src/bin/tybg.c`: the `tybg` tool
* `src/bin/tycat.c`: the `tycat` tool
* `src/bin/tyls.c`: the `tyls` tool
+* `src/bin/tymuxd.c`: the `tymuxd` daemon, keeps a terminal running in the background
+* `src/bin/tymuxd_stubs.c`: stands in for the `termio` functions `tymuxd` has no widget for
* `src/bin/typop.c`: the `typop` tool
* `src/bin/tyq.c`: the `tyq` tool
* `src/bin/utf8.c`: handles conversion between Eina_Unicode and char *
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 8b82c41d..ecf436fa 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -204,6 +204,36 @@ executable('tysend',
include_directories: config_dir,
dependencies: terminology_dependencies)
+if host_os == 'linux'
+ tymuxd_sources = ['tymuxd.c',
+ 'tymuxd_stubs.c',
+ 'color_names.h', color_names,
+ 'termd_tymux.c', 'termd_tymux.h',
+ 'termsrv.c', 'termsrv.h',
+ 'termpty.c', 'termpty.h',
+ 'termptydbl.c', 'termptydbl.h',
+ 'termptyesc.c', 'termptyesc.h',
+ 'termptyops.c', 'termptyops.h',
+ 'termptygfx.c', 'termptygfx.h',
+ 'termptyext.c', 'termptyext.h',
+ 'termptystatus.c', 'termptystatus.h',
+ 'backlog.c', 'backlog.h',
+ 'colors.c', 'colors.h',
+ 'sb.c', 'sb.h',
+ 'utf8.c', 'utf8.h',
+ 'utils.c', 'utils.h',
+ 'uri_decode.c', 'uri_decode.h',
+ simd_sources]
+ # With no textgrid to hold the palette, the escape sequence parser gets it
+ # from tymuxd_stubs.c, to map true colors and answer color queries.
+ tymuxd = executable('tymuxd',
+ tymuxd_sources,
+ c_args: '-Devas_object_textgrid_palette_get=tymuxd_textgrid_palette_get',
+ install: true,
+ include_directories: config_dir,
+ dependencies: terminology_dependencies)
+endif
+
if fuzzing
executable('tyfuzz',
tyfuzz_sources,
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
new file mode 100644
index 00000000..20e560ee
--- /dev/null
+++ b/src/bin/termd_tymux.c
@@ -0,0 +1,525 @@
+#include "private.h"
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <Elementary.h>
+#include "config.h"
+#include "termpty.h"
+#include "termsrv.h"
+#include "termd_tymux.h"
+
+_Static_assert(sizeof(Termcell) == sizeof(TermSrvCell),
+ "TermSrvCell must mirror Termcell");
+
+typedef enum
+{
+ CLIENT_PENDING, /* connected, ATTACH not received yet */
+ CLIENT_PRIMARY,
+ CLIENT_SHARED,
+ CLIENT_READONLY,
+ CLIENT_PAUSED
+} Client_Role;
+
+/* What may wait in the output queue of a client that does not keep up
+ * before it is dropped: it must not stall the shell and the other
+ * clients. */
+#define CLIENT_OUT_MAX (1 << 20)
+
+typedef struct
+{
+ int fd;
+ Ecore_Fd_Handler *handler;
+ TermDTymux *td;
+ Client_Role role;
+ /* messages the socket did not take yet */
+ Eina_Binbuf *out;
+ TermSrvReader reader;
+ Eina_Bool dispatching : 1;
+ Eina_Bool dead : 1;
+} Client;
+
+struct tag_TermDTymux
+{
+ Termpty *pty;
+ Config *config;
+ int shm_fd;
+ TermSrvShmHeader *shm;
+ Eina_List *clients;
+ /* clients paused by a hijack, most recent first */
+ Eina_List *paused;
+ Ecore_Animator *notify_anim;
+ Eina_Bool dirty : 1;
+};
+
+static uint64_t
+_screen_flush(TermDTymux *td)
+{
+ Termpty *ty = td->pty;
+ TermSrvShmHeader *shm = td->shm;
+ TermSrvCell *dst = TERMSRV_SHM_CELLS(shm);
+ int cols = MIN(ty->w, TERMSRV_MAX_COLS);
+ int rows = MIN(ty->h, TERMSRV_MAX_ROWS);
+ int y;
+
+ termsrv_shm_write_begin(shm);
+ shm->cols = cols;
+ shm->rows = rows;
+ shm->cursor_x = MIN(ty->cursor_state.cx, cols - 1);
+ shm->cursor_y = MIN(ty->cursor_state.cy, rows - 1);
+ shm->cursor_visible = !ty->termstate.hide_cursor;
+
+ /* termpty_screen_swap() swaps the pointers, so ty->screen is the active
+ * screen whether or not the alternate one is in use. */
+ for (y = 0; y < rows; y++)
+ memcpy(dst + y * cols,
+ ty->screen + ((y + ty->circular_offset) % ty->h) * ty->w,
+ cols * sizeof(TermSrvCell));
+ return termsrv_shm_write_end(shm);
+}
+
+static void _client_del(Client *client);
+static void _client_free(Client *client);
+
+/* For a client that missed a message, when it cannot be deleted right away:
+ * its fd handler sees the end of file and deletes it. */
+static void
+_client_hang_up(Client *client)
+{
+ WRN("dropping client %d", client->fd);
+ shutdown(client->fd, SHUT_RDWR);
+}
+
+/* Send what the socket takes without blocking, watching for it to take
+ * more if needed. Returns EINA_FALSE if the client is gone. */
+static Eina_Bool
+_client_flush(Client *client)
+{
+ size_t len = eina_binbuf_length_get(client->out);
+ ssize_t n;
+
+ if (len > 0)
+ {
+ n = send(client->fd, eina_binbuf_string_get(client->out), len,
+ MSG_DONTWAIT | MSG_NOSIGNAL);
+ if (n < 0)
+ {
+ if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
+ return EINA_FALSE;
+ n = 0;
+ }
+ eina_binbuf_remove(client->out, 0, n);
+ len -= n;
+ }
+ ecore_main_fd_handler_active_set(client->handler,
+ ECORE_FD_READ | (len ? ECORE_FD_WRITE : 0));
+ return EINA_TRUE;
+}
+
+/* Returns EINA_FALSE if the client is gone or does not keep up */
+static Eina_Bool
+_client_send(Client *client, TermSrvMsgType type,
+ const void *payload, uint32_t size)
+{
+ TermSrvMsgHdr hdr = { .type = type, .size = size };
+
+ if (eina_binbuf_length_get(client->out) + sizeof(hdr) + size >
+ CLIENT_OUT_MAX)
+ {
+ WRN("client %d does not keep up", client->fd);
+ return EINA_FALSE;
+ }
+ eina_binbuf_append_length(client->out, (unsigned char *)&hdr, sizeof(hdr));
+ if (size > 0)
+ eina_binbuf_append_length(client->out, payload, size);
+ return _client_flush(client);
+}
+
+static void
+_broadcast(TermDTymux *td, TermSrvMsgType type,
+ const void *payload, uint32_t size)
+{
+ Eina_List *l, *l_next;
+ Client *client;
+
+ EINA_LIST_FOREACH_SAFE(td->clients, l, l_next, client)
+ {
+ if (client->role == CLIENT_PENDING)
+ continue;
+ if (!_client_send(client, type, payload, size))
+ _client_del(client);
+ }
+}
+
+static void
+_flush_and_notify(TermDTymux *td)
+{
+ Termpty *ty = td->pty;
+ TermSrvMsgNotify notify = {
+ .appcursor = ty->termstate.appcursor,
+ .appkeypad = ty->termstate.alt_kp,
+ .mouse_mode = ty->mouse_mode,
+ .mouse_ext = ty->mouse_ext,
+ .bracketed_paste = ty->bracketed_paste,
+ .focus_reporting = ty->focus_reporting,
+ };
+
+ td->dirty = EINA_FALSE;
+ if (td->notify_anim)
+ {
+ ecore_animator_del(td->notify_anim);
+ td->notify_anim = NULL;
+ }
+
+ notify.write_seq = _screen_flush(td);
+ _broadcast(td, TSRV_MSG_NOTIFY, ¬ify, sizeof(notify));
+}
+
+static Eina_Bool
+_cb_notify_anim(void *data)
+{
+ TermDTymux *td = data;
+
+ td->notify_anim = NULL;
+ _flush_and_notify(td);
+ return ECORE_CALLBACK_CANCEL;
+}
+
+/* Called after every read from the pty: ship at most one frame per animator
+ * tick instead of one per read. */
+static void
+_cb_pty_change(void *data)
+{
+ TermDTymux *td = data;
+
+ td->dirty = EINA_TRUE;
+ /* The application is drawing a synchronized update (mode 2026): wait for
+ * its end, which calls us again, instead of shipping a partial frame. */
+ if (td->pty->sync_output.active)
+ return;
+ if (!td->notify_anim)
+ td->notify_anim = ecore_animator_add(_cb_notify_anim, td);
+}
+
+static void
+_cb_pty_title(void *data)
+{
+ TermDTymux *td = data;
+ const char *title = td->pty->prop.title ? td->pty->prop.title : "";
+ size_t size = strlen(title) + 1;
+
+ if (size <= TERMSRV_MAX_MSG_PAYLOAD)
+ _broadcast(td, TSRV_MSG_TITLE, title, size);
+}
+
+static void
+_cb_pty_bell(void *data)
+{
+ TermDTymux *td = data;
+
+ _broadcast(td, TSRV_MSG_BELL, NULL, 0);
+}
+
+static void
+_cb_pty_exited(void *data)
+{
+ TermDTymux *td = data;
+ TermSrvMsgExit msg_exit = { .exit_code = td->pty->exit_code };
+
+ if (td->dirty)
+ _flush_and_notify(td);
+ _broadcast(td, TSRV_MSG_EXIT, &msg_exit, sizeof(msg_exit));
+ ecore_main_loop_quit();
+}
+
+static void
+_client_attach(Client *client, TermSrvAttachMode mode)
+{
+ TermDTymux *td = client->td;
+ Termpty *ty = td->pty;
+ Eina_List *l;
+ Client *other;
+
+ if (termsrv_send_attached(client->fd, td->shm_fd, ty->w, ty->h,
+ ty->pid > 0 ? ty->pid : -1) < 0)
+ {
+ _client_del(client);
+ return;
+ }
+
+ switch (mode)
+ {
+ case TSRV_ATTACH_HIJACK:
+ EINA_LIST_FOREACH(td->clients, l, other)
+ {
+ if (other->role != CLIENT_PRIMARY)
+ continue;
+ other->role = CLIENT_PAUSED;
+ td->paused = eina_list_prepend(td->paused, other);
+ if (!_client_send(other, TSRV_MSG_PAUSE, NULL, 0))
+ _client_hang_up(other);
+ break;
+ }
+ client->role = CLIENT_PRIMARY;
+ break;
+ case TSRV_ATTACH_SHARED:
+ client->role = CLIENT_SHARED;
+ break;
+ case TSRV_ATTACH_READONLY:
+ client->role = CLIENT_READONLY;
+ break;
+ }
+}
+
+static void
+_client_resize(Client *client, const TermSrvMsgResize *resize)
+{
+ TermDTymux *td = client->td;
+
+ if (client->role != CLIENT_PRIMARY)
+ return;
+ if (resize->cols == 0 || resize->rows == 0 ||
+ resize->cols > TERMSRV_MAX_COLS || resize->rows > TERMSRV_MAX_ROWS)
+ return;
+ termpty_resize(td->pty, resize->cols, resize->rows);
+ /* termpty_resize() ends any synchronized update without calling the
+ * change callback: flush now so that clients get the new size. */
+ _flush_and_notify(td);
+}
+
+/* Returns EINA_FALSE if the client must go */
+static Eina_Bool
+_client_msg_handle(Client *client, int type, void *payload, uint32_t size)
+{
+ TermSrvAttachMode mode;
+
+ if (type == TSRV_MSG_DETACH)
+ return EINA_FALSE;
+ if (client->role == CLIENT_PENDING)
+ {
+ if (type != TSRV_MSG_ATTACH ||
+ termsrv_attach_mode_parse(payload, size, &mode) < 0)
+ {
+ ERR("invalid ATTACH from client %d", client->fd);
+ return EINA_FALSE;
+ }
+ _client_attach(client, mode);
+ return EINA_TRUE;
+ }
+ switch (type)
+ {
+ case TSRV_MSG_INPUT:
+ if (client->role == CLIENT_PRIMARY || client->role == CLIENT_SHARED)
+ termpty_write(client->td->pty, payload, size);
+ break;
+ case TSRV_MSG_RESIZE:
+ if (size == sizeof(TermSrvMsgResize))
+ _client_resize(client, payload);
+ break;
+ default:
+ break;
+ }
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+_cb_client_data(void *data, Ecore_Fd_Handler *fd_handler)
+{
+ Client *client = data;
+ void *payload;
+ uint32_t size;
+ int type = 0;
+
+ /* handling a message can delete the client: _client_del() then leaves
+ * freeing it to us */
+ client->dispatching = EINA_TRUE;
+ if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE) &&
+ !_client_flush(client))
+ _client_del(client);
+ if (!client->dead &&
+ ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
+ {
+ if (termsrv_reader_fill(&client->reader, client->fd) < 0)
+ _client_del(client);
+ while (!client->dead &&
+ (type = termsrv_reader_next(&client->reader,
+ &payload, &size)) > 0)
+ {
+ if (!_client_msg_handle(client, type, payload, size))
+ _client_del(client);
+ free(payload);
+ }
+ if (type < 0 || client->reader.eof)
+ _client_del(client);
+ }
+ client->dispatching = EINA_FALSE;
+
+ if (!client->dead)
+ return ECORE_CALLBACK_RENEW;
+ client->handler = NULL;
+ _client_free(client);
+ return ECORE_CALLBACK_CANCEL;
+}
+
+static void
+_client_free(Client *client)
+{
+ if (client->handler)
+ ecore_main_fd_handler_del(client->handler);
+ close(client->fd);
+ eina_binbuf_free(client->out);
+ termsrv_reader_clear(&client->reader);
+ free(client);
+}
+
+static void
+_client_del(Client *client)
+{
+ TermDTymux *td = client->td;
+
+ if (client->dead)
+ return;
+ client->dead = EINA_TRUE;
+ td->clients = eina_list_remove(td->clients, client);
+ if (client->role == CLIENT_PAUSED)
+ td->paused = eina_list_remove(td->paused, client);
+ else if (client->role == CLIENT_PRIMARY && td->paused)
+ {
+ Client *resumed = eina_list_data_get(td->paused);
+
+ td->paused = eina_list_remove_list(td->paused, td->paused);
+ resumed->role = CLIENT_PRIMARY;
+ if (!_client_send(resumed, TSRV_MSG_RESUME, NULL, 0))
+ _client_hang_up(resumed);
+ }
+ if (!client->dispatching)
+ _client_free(client);
+}
+
+void
+termd_tymux_client_add(TermDTymux *td, int cfd)
+{
+ Client *client;
+
+ client = calloc(1, sizeof(*client));
+ if (!client)
+ {
+ close(cfd);
+ return;
+ }
+ client->fd = cfd;
+ client->td = td;
+ client->role = CLIENT_PENDING;
+ client->out = eina_binbuf_new();
+ client->handler = ecore_main_fd_handler_add(cfd, ECORE_FD_READ,
+ _cb_client_data, client,
+ NULL, NULL);
+ if (!client->out || !client->handler)
+ {
+ if (client->handler)
+ ecore_main_fd_handler_del(client->handler);
+ eina_binbuf_free(client->out);
+ close(cfd);
+ free(client);
+ return;
+ }
+ td->clients = eina_list_append(td->clients, client);
+}
+
+TermDTymux *
+termd_tymux_new(const char *name, const char *cmd)
+{
+ TermDTymux *td;
+ char memfd_name[128];
+
+ td = calloc(1, sizeof(*td));
+ if (!td)
+ return NULL;
+ td->shm_fd = -1;
+
+ td->config = calloc(1, sizeof(Config));
+ if (!td->config)
+ goto err;
+ td->config->scrollback = 10000;
+ td->config->term_type = TERM_TYPE_XTERM_256COLOR;
+
+ snprintf(memfd_name, sizeof(memfd_name), "tymux-%s", name);
+ td->shm_fd = memfd_create(memfd_name, MFD_CLOEXEC);
+ if (td->shm_fd < 0)
+ {
+ ERR("memfd_create: %s", strerror(errno));
+ goto err;
+ }
+ if (ftruncate(td->shm_fd, TERMSRV_SHM_TOTAL_SIZE) < 0)
+ {
+ ERR("ftruncate: %s", strerror(errno));
+ goto err;
+ }
+ td->shm = mmap(NULL, TERMSRV_SHM_TOTAL_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED, td->shm_fd, 0);
+ if (td->shm == MAP_FAILED)
+ {
+ ERR("mmap: %s", strerror(errno));
+ td->shm = NULL;
+ goto err;
+ }
+ td->shm->magic = TERMSRV_SHM_MAGIC;
+ td->shm->version = TERMSRV_SHM_VERSION;
+
+ td->pty = termpty_new(cmd, EINA_FALSE, NULL, 80, 24, td->config, name, 0);
+ if (!td->pty)
+ goto err;
+ td->pty->cb.change.func = _cb_pty_change;
+ td->pty->cb.change.data = ""
+ td->pty->cb.set_title.func = _cb_pty_title;
+ td->pty->cb.set_title.data = ""
+ td->pty->cb.bell.func = _cb_pty_bell;
+ td->pty->cb.bell.data = ""
+ td->pty->cb.exited.func = _cb_pty_exited;
+ td->pty->cb.exited.data = ""
+
+ _screen_flush(td);
+ return td;
+
+err:
+ termd_tymux_free(td);
+ return NULL;
+}
+
+void
+termd_tymux_free(TermDTymux *td)
+{
+ Client *client;
+
+ if (!td)
+ return;
+ if (td->notify_anim)
+ ecore_animator_del(td->notify_anim);
+ EINA_LIST_FREE(td->clients, client)
+ {
+ struct timeval timeout = { .tv_sec = 0, .tv_usec = 200 * 1000 };
+ size_t len = eina_binbuf_length_get(client->out);
+
+ /* give the messages still queued, such as EXIT, a short chance */
+ if (len > 0 &&
+ fcntl(client->fd, F_SETFL, 0) == 0 &&
+ setsockopt(client->fd, SOL_SOCKET, SO_SNDTIMEO,
+ &timeout, sizeof(timeout)) == 0)
+ send(client->fd, eina_binbuf_string_get(client->out), len,
+ MSG_NOSIGNAL);
+ _client_free(client);
+ }
+ eina_list_free(td->paused);
+ if (td->pty)
+ termpty_free(td->pty);
+ if (td->shm)
+ munmap(td->shm, TERMSRV_SHM_TOTAL_SIZE);
+ if (td->shm_fd >= 0)
+ close(td->shm_fd);
+ free(td->config);
+ free(td);
+}
diff --git a/src/bin/termd_tymux.h b/src/bin/termd_tymux.h
new file mode 100644
index 00000000..a6e95efc
--- /dev/null
+++ b/src/bin/termd_tymux.h
@@ -0,0 +1,18 @@
+#ifndef TERMINOLOGY_TERMD_TYMUX_H_
+#define TERMINOLOGY_TERMD_TYMUX_H_ 1
+
+typedef struct tag_TermDTymux TermDTymux;
+
+/* Start the shell (cmd, or the user's shell if NULL) on a new pty whose
+ * screen is published in shared memory. The main loop is quit when the
+ * shell exits. Returns NULL on failure. */
+TermDTymux *termd_tymux_new(const char *name, const char *cmd);
+
+/* Disconnect all clients and hang up the shell. */
+void termd_tymux_free(TermDTymux *td);
+
+/* Take ownership of cfd, a freshly accepted connection, and complete the
+ * ATTACH handshake. cfd is closed if the handshake fails. */
+void termd_tymux_client_add(TermDTymux *td, int cfd);
+
+#endif
diff --git a/src/bin/tymuxd.c b/src/bin/tymuxd.c
new file mode 100644
index 00000000..982eb1ad
--- /dev/null
+++ b/src/bin/tymuxd.c
@@ -0,0 +1,162 @@
+#include "private.h"
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <Elementary.h>
+#include "termpty.h"
+#include "termsrv.h"
+#include "termd_tymux.h"
+
+int _log_domain = -1;
+
+static TermDTymux *_tymux = NULL;
+static int _listen_fd = -1;
+
+static Eina_Bool
+_cb_client_connect(void *data EINA_UNUSED,
+ Ecore_Fd_Handler *fd_handler EINA_UNUSED)
+{
+ int cfd = accept4(_listen_fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
+
+ if (cfd < 0)
+ {
+ if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
+ ERR("accept: %s", strerror(errno));
+ return ECORE_CALLBACK_RENEW;
+ }
+ termd_tymux_client_add(_tymux, cfd);
+ return ECORE_CALLBACK_RENEW;
+}
+
+static Eina_Bool
+_cb_signal_exit(void *data EINA_UNUSED, int type EINA_UNUSED,
+ void *event EINA_UNUSED)
+{
+ ecore_main_loop_quit();
+ return ECORE_CALLBACK_DONE;
+}
+
+/* Bind the listening socket at sa, unless a daemon is already serving it.
+ * Returns the socket, or -1. */
+static int
+_listen_socket_new(const struct sockaddr_un *sa, const char *name)
+{
+ int fd;
+
+ fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (fd < 0)
+ {
+ ERR("socket: %s", strerror(errno));
+ return -1;
+ }
+ if (connect(fd, (const struct sockaddr *)sa, sizeof(*sa)) == 0)
+ {
+ fprintf(stderr, "tymuxd: '%s' is already running\n", name);
+ close(fd);
+ return -1;
+ }
+ /* left behind by a daemon that did not exit cleanly */
+ if (errno == ECONNREFUSED)
+ unlink(sa->sun_path);
+ close(fd);
+
+ fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (fd < 0)
+ {
+ ERR("socket: %s", strerror(errno));
+ return -1;
+ }
+ if (bind(fd, (const struct sockaddr *)sa, sizeof(*sa)) < 0)
+ {
+ ERR("bind '%s': %s", sa->sun_path, strerror(errno));
+ close(fd);
+ return -1;
+ }
+ if (listen(fd, 8) < 0)
+ {
+ ERR("listen: %s", strerror(errno));
+ unlink(sa->sun_path);
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+int
+main(int argc, char **argv)
+{
+ struct sockaddr_un sa = { .sun_family = AF_UNIX };
+ Ecore_Fd_Handler *listen_handler = NULL;
+ const char *name;
+ int ret = 1;
+
+ if (argc != 2 || termsrv_name_check(argv[1]) < 0)
+ {
+ fprintf(stderr, "Usage: tymuxd <name>\n"
+ "Runs a shell in the background for tymux(1).\n");
+ return 1;
+ }
+ name = argv[1];
+
+ eina_init();
+ /* no use for power/timezone/D-Bus system monitoring in a headless daemon
+ * hosting a shell */
+ ecore_app_no_system_modules();
+ ecore_init();
+ termpty_init();
+ _log_domain = eina_log_domain_register("tymuxd", NULL);
+ if (_log_domain < 0)
+ EINA_LOG_ERR("Could not create log domain 'tymuxd'");
+
+ signal(SIGPIPE, SIG_IGN);
+ ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, _cb_signal_exit, NULL);
+
+ if (termsrv_ensure_tymux_dir() < 0)
+ {
+ ERR("could not create the tymux directory: %s", strerror(errno));
+ goto end;
+ }
+ if (termsrv_socket_path(name, sa.sun_path, sizeof(sa.sun_path)) < 0)
+ {
+ ERR("socket path too long for '%s'", name);
+ goto end;
+ }
+ _listen_fd = _listen_socket_new(&sa, name);
+ if (_listen_fd < 0)
+ goto end;
+
+ setenv("TYMUX_NAME", name, 1);
+ _tymux = termd_tymux_new(name, NULL);
+ if (!_tymux)
+ goto end;
+
+ listen_handler = ecore_main_fd_handler_add(_listen_fd, ECORE_FD_READ,
+ _cb_client_connect, NULL,
+ NULL, NULL);
+ if (!listen_handler)
+ goto end;
+
+ ecore_main_loop_begin();
+ ret = 0;
+
+end:
+ if (listen_handler)
+ ecore_main_fd_handler_del(listen_handler);
+ if (_listen_fd >= 0)
+ {
+ close(_listen_fd);
+ unlink(sa.sun_path);
+ }
+ termd_tymux_free(_tymux);
+ if (_log_domain >= 0)
+ eina_log_domain_unregister(_log_domain);
+ termpty_shutdown();
+ ecore_shutdown();
+ eina_shutdown();
+ return ret;
+}
diff --git a/src/bin/tymuxd_stubs.c b/src/bin/tymuxd_stubs.c
new file mode 100644
index 00000000..3032cf7c
--- /dev/null
+++ b/src/bin/tymuxd_stubs.c
@@ -0,0 +1,219 @@
+/* The pty code calls back into the termio widget, which tymuxd does not
+ * have: ty->obj is always NULL there. These take its place. */
+
+#include "private.h"
+#include <Elementary.h>
+#include "config.h"
+#include "colors.h"
+#include "termio.h"
+#include "termptynotify.h"
+
+/* The build of tymuxd renames evas_object_textgrid_palette_get() to this:
+ * give the default palette. */
+void
+tymuxd_textgrid_palette_get(const Evas_Object *obj EINA_UNUSED,
+ Evas_Textgrid_Palette pal, int idx,
+ int *r, int *g, int *b, int *a)
+{
+ unsigned char cr, cg, cb, ca;
+
+ if (pal == EVAS_TEXTGRID_PALETTE_EXTENDED)
+ colors_256_get(idx, &cr, &cg, &cb, &ca);
+ else
+ colors_standard_get(idx / 12, idx % 12, &cr, &cg, &cb, &ca);
+ if (r) *r = cr;
+ if (g) *g = cg;
+ if (b) *b = cb;
+ if (a) *a = ca;
+}
+
+void
+termio_scroll(Evas_Object *obj EINA_UNUSED, int direction EINA_UNUSED,
+ int start_y EINA_UNUSED, int end_y EINA_UNUSED)
+{
+}
+
+/* Terminology's own escape sequences drive the widget: turn them off. */
+Config *
+termio_config_get(const Evas_Object *obj EINA_UNUSED)
+{
+ static Config config;
+
+ return &config;
+}
+
+Evas_Object *
+termio_textgrid_get(const Evas_Object *obj EINA_UNUSED)
+{
+ return NULL;
+}
+
+Evas_Object *
+termio_bg_get(const Evas_Object *obj EINA_UNUSED)
+{
+ return NULL;
+}
+
+Evas_Object *
+termio_get_cursor(const Evas_Object *obj EINA_UNUSED)
+{
+ return NULL;
+}
+
+void
+termio_set_cursor_shape(Evas_Object *obj EINA_UNUSED,
+ Cursor_Shape shape EINA_UNUSED)
+{
+}
+
+void
+termio_font_size_set(Evas_Object *obj EINA_UNUSED, int size EINA_UNUSED)
+{
+}
+
+void
+termio_reset_main_colors(Evas_Object *termio EINA_UNUSED)
+{
+}
+
+Eina_Bool
+termio_is_focused(const Evas_Object *obj EINA_UNUSED)
+{
+ return EINA_FALSE;
+}
+
+void
+termio_set_selection_text(Evas_Object *obj EINA_UNUSED,
+ Elm_Sel_Type type EINA_UNUSED,
+ const char *text EINA_UNUSED)
+{
+}
+
+void
+termio_selection_buffer_get_cb(Evas_Object *obj EINA_UNUSED,
+ Elm_Sel_Type type EINA_UNUSED,
+ Elm_Sel_Format format EINA_UNUSED,
+ Elm_Drop_Cb cb EINA_UNUSED,
+ void *data EINA_UNUSED)
+{
+}
+
+const char *
+config_term_type_name(int term_type EINA_UNUSED)
+{
+ return "xterm-256color";
+}
+
+Cursor_Shape
+termio_cursor_shape_get(const Evas_Object *obj EINA_UNUSED)
+{
+ return CURSOR_SHAPE_BLOCK;
+}
+
+Eina_Bool
+termio_bg_is_dark(const Evas_Object *obj EINA_UNUSED)
+{
+ return EINA_TRUE;
+}
+
+void
+termio_fg_color_get(const Evas_Object *obj EINA_UNUSED,
+ int *r, int *g, int *b)
+{
+ *r = 0xaa;
+ *g = 0xaa;
+ *b = 0xaa;
+}
+
+void
+termio_fg_color_set(Evas_Object *obj EINA_UNUSED,
+ int r EINA_UNUSED, int g EINA_UNUSED, int b EINA_UNUSED)
+{
+}
+
+void
+termio_reset_fg_color(Evas_Object *termio EINA_UNUSED)
+{
+}
+
+void
+termio_reset_bg_color(Evas_Object *termio EINA_UNUSED)
+{
+}
+
+void
+termio_reset_cursor_color(Evas_Object *termio EINA_UNUSED)
+{
+}
+
+Eina_Bool
+termio_palette_color_get(const Evas_Object *obj EINA_UNUSED, int idx,
+ int *r, int *g, int *b)
+{
+ unsigned char cr, cg, cb, ca;
+
+ if ((idx < 0) || (idx > 255))
+ return EINA_FALSE;
+ colors_256_get(idx, &cr, &cg, &cb, &ca);
+ *r = cr;
+ *g = cg;
+ *b = cb;
+ return EINA_TRUE;
+}
+
+void
+termio_palette_color_set(Evas_Object *obj EINA_UNUSED, int idx EINA_UNUSED,
+ int r EINA_UNUSED, int g EINA_UNUSED,
+ int b EINA_UNUSED)
+{
+}
+
+void
+termio_palette_color_override_clear(Evas_Object *obj EINA_UNUSED,
+ int idx EINA_UNUSED)
+{
+}
+
+void
+termio_palette_apply(Evas_Object *obj EINA_UNUSED)
+{
+}
+
+void
+termio_palette_reset(Evas_Object *obj EINA_UNUSED)
+{
+}
+
+Eina_Bool
+termio_selection_buffer_exists(const Evas_Object *obj EINA_UNUSED,
+ Elm_Sel_Type type EINA_UNUSED)
+{
+ return EINA_FALSE;
+}
+
+Eina_Bool
+termio_selection_read_ask(Evas_Object *obj EINA_UNUSED)
+{
+ return EINA_FALSE;
+}
+
+/* Notifications are off in the zeroed Config, and Elementary's
+ * sys_notify has no place in the daemon: drop them, queries included,
+ * as Terminology does when notification_escapes is 0. */
+void
+termpty_notify_osc99(Termpty *ty EINA_UNUSED, const char *s EINA_UNUSED,
+ size_t len EINA_UNUSED, Eina_Bool bel EINA_UNUSED)
+{
+}
+
+void
+termpty_notify_show(Termpty *ty EINA_UNUSED, const char *title EINA_UNUSED,
+ const char *body EINA_UNUSED, int urgency EINA_UNUSED,
+ int timeout_ms EINA_UNUSED, int occasion EINA_UNUSED)
+{
+}
+
+void
+termpty_notify_free(Termpty *ty EINA_UNUSED)
+{
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.