Skip to content

Commit

Permalink
Redirect program output to separate window
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Nahnybida committed Aug 30, 2023
1 parent 2b048cf commit f8c7213
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gf2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <fcntl.h>
#include <poll.h>
#include <time.h>
#include <pty.h>

extern "C" {
#define UI_FONT_PATH
Expand All @@ -33,6 +34,7 @@ extern "C" {
#define MSG_RECEIVED_DATA ((UIMessage) (UI_MSG_USER + 1))
#define MSG_RECEIVED_CONTROL ((UIMessage) (UI_MSG_USER + 2))
#define MSG_RECEIVED_LOG ((UIMessage) (UI_MSG_USER + 3))
#define MSG_RECEIVED_STDOUT ((UIMessage) (UI_MSG_USER + 4))

// Data structures:

Expand Down Expand Up @@ -142,6 +144,9 @@ FILE *commandLog;
char emptyString;
bool programRunning = true;
const char *vimServerName = "GVIM";

char setPttyCMD[PATH_MAX];
int pseudoTerminalMasterFD = -1;
const char *logPipePath;
const char *controlPipePath;
Array<INIState> presetCommands;
Expand Down Expand Up @@ -558,6 +563,8 @@ void *DebuggerThread(void *) {
const char *setPrompt = "set prompt (gdb) \n";
write(pipeToGDB, setPrompt, strlen(setPrompt));

write(pipeToGDB, setPttyCMD, strlen(setPttyCMD));

char *catBuffer = NULL;
size_t catBufferUsed = 0;
size_t catBufferAllocated = 0;
Expand Down Expand Up @@ -1310,6 +1317,7 @@ void InterfaceAddBuiltinWindowsAndCommands() {
interfaceWindows.Add({ "Files", FilesWindowCreate, nullptr });
interfaceWindows.Add({ "Console", ConsoleWindowCreate, nullptr });
interfaceWindows.Add({ "Log", LogWindowCreate, nullptr });
interfaceWindows.Add({ "STDOUT", STDOUTWindowCreate, nullptr });
interfaceWindows.Add({ "Thread", ThreadWindowCreate, ThreadWindowUpdate });
interfaceWindows.Add({ "Exe", ExecutableWindowCreate, nullptr });

Expand Down Expand Up @@ -1482,6 +1490,8 @@ int WindowMessage(UIElement *, UIMessage message, int di, void *dp) {
free(input);
} else if (message == MSG_RECEIVED_LOG) {
LogReceived(dp);
} else if (message == MSG_RECEIVED_STDOUT) {
STDOUTReceived(dp);
} else if (message == UI_MSG_WINDOW_ACTIVATE) {
DisplaySetPosition(currentFileFull, currentLine, false);
}
Expand Down
57 changes: 57 additions & 0 deletions windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,63 @@ UIElement *CommandsWindowCreate(UIElement *parent) {
return &panel->e;
}

//////////////////////////////////////////////////////
// STDOUT window:
//////////////////////////////////////////////////////

void *STDOUTWindowThread(void *context) {
if (pseudoTerminalMasterFD < 0) {
fprintf(stderr, "Warning: ptty fd invalid\n");
return nullptr;
}

struct pollfd p = { .fd = pseudoTerminalMasterFD, .events = POLLIN };

while (true) {
poll(&p, 1, 10000);

if (p.revents & POLLHUP) {
struct timespec t = { .tv_nsec = 10000000 };
nanosleep(&t, 0);
}

while (true) {
char input[16384];
int length = read(pseudoTerminalMasterFD, input, sizeof(input) - 1);
if (length <= 0) break;
input[length] = 0;
void *buffer = malloc(strlen(input) + sizeof(context) + 1);
memcpy(buffer, &context, sizeof(context));
strcpy((char *) buffer + sizeof(context), input);
UIWindowPostMessage(windowMain, MSG_RECEIVED_STDOUT, buffer);
}
}
}

void STDOUTReceived(void *buffer) {
UICodeInsertContent(*(UICode **) buffer, (char *) buffer + sizeof(void *), -1, false);
UIElementRefresh(*(UIElement **) buffer);
free(buffer);
}

UIElement *STDOUTWindowCreate(UIElement *parent) {
int master_tty, slave_tty;
char pseudoTerminalPath[PATH_MAX];
int ret = openpty(&master_tty, &slave_tty, pseudoTerminalPath, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Warning: cannot open pseudo tty");
/* TODO(Oleksandr): What should I do here, is it allowed to return NULL from window create? */
}
StringFormat(setPttyCMD, sizeof(setPttyCMD), "tty %s\n", pseudoTerminalPath);
pseudoTerminalMasterFD = master_tty;


UICode *code = UICodeCreate(parent, 0);
pthread_t thread;
pthread_create(&thread, nullptr, STDOUTWindowThread, code);
return &code->e;
}

//////////////////////////////////////////////////////
// Log window:
//////////////////////////////////////////////////////
Expand Down

0 comments on commit f8c7213

Please sign in to comment.