1998-12-09 16:43:03 +01:00
|
|
|
/* xterm.c */
|
|
|
|
|
|
|
|
/* This "driver" is designed to go on top of an existing driver
|
|
|
|
to provide support for features only present if using an
|
1999-01-17 17:55:11 +01:00
|
|
|
xterm or compatible program for your console output.
|
|
|
|
Currently, it supports resizing and separating debug messages from
|
|
|
|
program output.
|
|
|
|
It does not currently support changing the title bar.
|
|
|
|
*/
|
1998-12-09 16:43:03 +01:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
#include "console.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
1999-01-03 17:15:39 +01:00
|
|
|
#define ESC '\x1b'
|
|
|
|
|
1998-12-09 16:43:03 +01:00
|
|
|
static BOOL32 wine_create_console(FILE **master, FILE **slave, int *pid);
|
1999-02-09 15:14:16 +01:00
|
|
|
FILE *wine_openpty(int *master, int *slave, char *name,
|
1998-12-09 16:43:03 +01:00
|
|
|
struct termios *term, struct winsize *winsize);
|
|
|
|
|
|
|
|
/* The console -- I chose to keep the master and slave
|
|
|
|
* (UNIX) file descriptors around in case they are needed for
|
|
|
|
* ioctls later. The pid is needed to destroy the xterm on close
|
|
|
|
*/
|
|
|
|
typedef struct _XTERM_CONSOLE {
|
|
|
|
FILE *master; /* xterm side of pty */
|
|
|
|
FILE *slave; /* wine side of pty */
|
1999-01-03 17:15:39 +01:00
|
|
|
int pid; /* xterm's pid, -1 if no xterm */
|
1998-12-09 16:43:03 +01:00
|
|
|
} XTERM_CONSOLE;
|
|
|
|
|
|
|
|
static XTERM_CONSOLE xterm_console;
|
|
|
|
|
|
|
|
CONSOLE_device chain;
|
|
|
|
FILE *old_in, *old_out;
|
|
|
|
|
|
|
|
void XTERM_Start()
|
|
|
|
{
|
|
|
|
/* Here, this is a supplementary driver so we should remember to call
|
|
|
|
the chain. */
|
|
|
|
chain.init = driver.init;
|
|
|
|
driver.init = XTERM_Init;
|
|
|
|
|
|
|
|
chain.close = driver.close;
|
|
|
|
driver.close = XTERM_Close;
|
1999-01-03 17:15:39 +01:00
|
|
|
|
|
|
|
chain.resizeScreen = driver.resizeScreen;
|
|
|
|
driver.resizeScreen = XTERM_ResizeScreen;
|
1998-12-09 16:43:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XTERM_Init()
|
|
|
|
{
|
|
|
|
wine_create_console(&xterm_console.master, &xterm_console.slave,
|
|
|
|
&xterm_console.pid);
|
|
|
|
|
|
|
|
old_in = driver.console_in;
|
|
|
|
driver.console_in = xterm_console.slave;
|
|
|
|
|
|
|
|
old_out = driver.console_out;
|
|
|
|
driver.console_out = xterm_console.slave;
|
|
|
|
|
|
|
|
/* Then call the chain... */
|
|
|
|
if (chain.init)
|
|
|
|
chain.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XTERM_Close()
|
|
|
|
{
|
|
|
|
/* Call the chain first... */
|
|
|
|
if (chain.close)
|
|
|
|
chain.close();
|
|
|
|
|
|
|
|
driver.console_in = old_in;
|
|
|
|
driver.console_out = old_out;
|
|
|
|
|
|
|
|
/* make sure a xterm exists to kill */
|
|
|
|
if (xterm_console.pid != -1) {
|
|
|
|
kill(xterm_console.pid, SIGTERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-03 17:15:39 +01:00
|
|
|
void XTERM_ResizeScreen(int x, int y)
|
|
|
|
{
|
|
|
|
char temp[100];
|
|
|
|
|
|
|
|
/* Call the chain first, there shoudln't be any... */
|
|
|
|
if (chain.resizeScreen)
|
|
|
|
chain.resizeScreen(x, y);
|
|
|
|
|
|
|
|
sprintf(temp, "\x1b[8;%d;%dt", y, x);
|
|
|
|
CONSOLE_WriteRawString(temp);
|
|
|
|
|
|
|
|
CONSOLE_NotifyResizeScreen(x, y);
|
|
|
|
}
|
|
|
|
|
1998-12-09 16:43:03 +01:00
|
|
|
|
|
|
|
static BOOL32 wine_create_console(FILE **master, FILE **slave, int *pid)
|
|
|
|
{
|
1999-01-17 17:55:11 +01:00
|
|
|
/* There is definately a bug in this routine that causes a lot
|
|
|
|
of garbage to be written to the screen, but I can't find it...
|
|
|
|
*/
|
1998-12-09 16:43:03 +01:00
|
|
|
struct termios term;
|
|
|
|
char buf[1024];
|
|
|
|
char c = '\0';
|
|
|
|
int status = 0;
|
|
|
|
int i;
|
1999-01-17 17:55:11 +01:00
|
|
|
int tmaster, tslave;
|
1998-12-09 16:43:03 +01:00
|
|
|
|
|
|
|
if (tcgetattr(0, &term) < 0) return FALSE;
|
|
|
|
term.c_lflag |= ICANON;
|
|
|
|
term.c_lflag &= ~ECHO;
|
1999-01-17 17:55:11 +01:00
|
|
|
if (wine_openpty(&tmaster, &tslave, NULL, &term, NULL) < 0)
|
1998-12-09 16:43:03 +01:00
|
|
|
return FALSE;
|
1999-01-17 17:55:11 +01:00
|
|
|
*master = fdopen(tmaster, "r+");
|
|
|
|
*slave = fdopen(tslave, "r+");
|
1998-12-09 16:43:03 +01:00
|
|
|
|
|
|
|
if ((*pid=fork()) == 0) {
|
1998-12-14 15:38:10 +01:00
|
|
|
tcsetattr(fileno(*slave), TCSADRAIN, &term);
|
|
|
|
sprintf(buf, "-Sxx%d", fileno(*master));
|
1999-01-03 17:15:39 +01:00
|
|
|
execlp(CONSOLE_XTERM_PROG, CONSOLE_XTERM_PROG, buf, "-fg",
|
|
|
|
"white", "-bg", "black", NULL);
|
1998-12-14 15:38:10 +01:00
|
|
|
ERR(console, "error creating xterm\n");
|
1998-12-09 16:43:03 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* most xterms like to print their window ID when used with -S;
|
|
|
|
* read it and continue before the user has a chance...
|
|
|
|
* NOTE: this is the reason we started xterm with ECHO off,
|
|
|
|
* we'll turn it back on below
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i=0; c!='\n'; (status=fread(&c, 1, 1, *slave)), i++) {
|
|
|
|
if (status == -1 && c == '\0') {
|
|
|
|
/* wait for xterm to be created */
|
|
|
|
usleep(100);
|
|
|
|
}
|
|
|
|
if (i > 10000) {
|
|
|
|
WARN(console, "can't read xterm WID\n");
|
|
|
|
kill(*pid, SIGKILL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
term.c_lflag |= ECHO;
|
1998-12-14 15:38:10 +01:00
|
|
|
tcsetattr(fileno(*master), TCSADRAIN, &term);
|
1998-12-09 16:43:03 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|