1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

Add basic PTY support.

For now, there are four hard-coded PTYs: /dev/pt{m,s}[0123]
Use this in the Terminal to open a pty pair and spawn a shell.
This commit is contained in:
Andreas Kling 2019-01-15 06:30:19 +01:00
parent ecb4ab0943
commit 2f74c2f430
21 changed files with 287 additions and 10 deletions

View file

@ -11,12 +11,50 @@
#include <gui.h>
#include "Terminal.h"
static void paint(GraphicsBitmap& bitmap, int width, int height);
int main(int argc, char** argv)
static void make_shell(int ptm_fd)
{
int fd = open("/dev/gui_events", O_RDONLY);
if (fd < 0) {
pid_t pid = fork();
if (pid == 0) {
const char* tty_name = ptsname(ptm_fd);
if (!tty_name) {
perror("ptsname");
exit(1);
}
close(ptm_fd);
int pts_fd = open(tty_name, O_RDWR);
dbgprintf("*** In child (%d), opening slave pty %s, pts_fd=%d\n", getpid(), tty_name, pts_fd);
close(0);
close(1);
close(2);
dup2(pts_fd, 0);
dup2(pts_fd, 1);
dup2(pts_fd, 2);
close(pts_fd);
int rc = execve("/bin/sh", nullptr, nullptr);
if (rc < 0) {
perror("execve");
exit(1);
}
ASSERT_NOT_REACHED();
} else {
dbgprintf("*** In parent, child is %d\n", pid);
}
}
int main(int, char**)
{
int ptm_fd = open("/dev/ptm0", O_RDWR);
if (ptm_fd < 0) {
perror("open");
return 1;
}
dbgprintf("ptm_fd = %d\n", ptm_fd);
make_shell(ptm_fd);
int event_fd = open("/dev/gui_events", O_RDONLY);
if (event_fd < 0) {
perror("open");
return 1;
}
@ -26,8 +64,17 @@ int main(int argc, char** argv)
terminal.paint();
for (;;) {
byte buffer[1024];
ssize_t ptm_nread = read(ptm_fd, buffer, sizeof(buffer));
if (ptm_nread > 0) {
for (ssize_t i = 0; i < ptm_nread; ++i) {
terminal.on_char(buffer[i]);
}
terminal.paint();
}
#if 0
GUI_Event event;
ssize_t nread = read(fd, &event, sizeof(event));
ssize_t nread = read(event_fd, &event, sizeof(event));
if (nread < 0) {
perror("read");
return 1;
@ -39,10 +86,13 @@ int main(int argc, char** argv)
case GUI_Event::Type::MouseDown: dbgprintf("WID=%x MouseDown %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y); break;
case GUI_Event::Type::MouseUp: dbgprintf("WID=%x MouseUp %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y); break;
case GUI_Event::Type::MouseMove: dbgprintf("WID=%x MouseMove %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y); break;
default:
ASSERT_NOT_REACHED();
}
if (event.type == GUI_Event::Type::MouseDown)
terminal.paint();
#endif
}
return 0;
}