2 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
4 * Microwindows Terminal Emulator for Linux
6 * Yes, this is just a demo, and doesn't repaint contents on refresh.
15 #define MWINCLUDECOLORS
17 #include "wintern.h" /* for MwRegisterFdInput*/
18 #include "wintools.h" /* Draw3dInset*/
26 #define FONTNAME SYSTEM_FIXED_FONT
27 /*#define FONTNAME OEM_FIXED_FONT*/
28 #define APPCLASS "mterm"
32 #define SIGCHLD 17 /* from Linux, not defined in DJGPP */
36 LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wp,LPARAM lp);
37 void EmOutChar(HWND hwnd, int ch);
38 int CreatePtyShell(void);
39 int ReadPtyShell(int fd, char *buf, int count);
40 int WritePtyShell(int fd, char *buf, int count);
41 void ClosePtyShell(int fd);
44 static int ttyfd = -1;
45 static int xpos = XMARGIN;
46 static int ypos = YMARGIN;
47 static int nCharWidth, nCharHeight;
48 static int nScreenWidth, nScreenHeight;
51 RegisterAppClass(void)
55 wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
56 wc.lpfnWndProc = (WNDPROC)WndProc;
60 wc.hIcon = 0; /*LoadIcon(GetHInstance(), MAKEINTRESOURCE( 1));*/
61 wc.hCursor = 0; /*LoadCursor(NULL, IDC_ARROW);*/
62 wc.hbrBackground = CreateSolidBrush(BKCOLOR);
63 wc.lpszMenuName = NULL;
64 wc.lpszClassName = APPCLASS;
77 GetWindowRect(GetDesktopWindow(), &rc);
81 /* determine TE size from font*/
83 SelectObject(hdc, GetStockObject(FONTNAME));
84 SetRect(&rc, 0, 0, 0, 0);
85 nCharHeight = DrawText(hdc, "m", 1, &rc, DT_CALCRECT);
86 nCharWidth = rc.right;
87 nScreenWidth = min(w, nCharWidth*COLS);
88 nScreenHeight = min(h, nCharHeight*ROWS);
91 hwnd = CreateWindowEx(0L, APPCLASS,
92 "Microwindows Terminal",
93 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
94 CW_USEDEFAULT, CW_USEDEFAULT,
95 nScreenWidth+4, nScreenHeight+24,
96 NULL, (HMENU)1, NULL, NULL);
102 WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
111 ttyfd = CreatePtyShell();
114 MwRegisterFdInput(hwnd, ttyfd);
120 MwUnregisterFdInput(hwnd, ttyfd);
121 ClosePtyShell(ttyfd);
126 /* echo half duplex if CreatePtyShell() failed*/
130 EmOutChar(hwnd, '\n');
132 WritePtyShell(ttyfd, &ch, 1);
136 if(ReadPtyShell(ttyfd, &ch, 1) == 1)
141 hdc = BeginPaint(hwnd, &ps);
142 GetClientRect(hwnd, &rc);
143 Draw3dInset(hdc, 0, 0, rc.right-rc.left, rc.bottom-rc.top);
148 return DefWindowProc(hwnd, msg, wp, lp);
154 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
158 extern MWIMAGEHDR image_car8;
161 MwSetDesktopWallpaper(&image_car8);
165 /* type ESC to quit...*/
166 while(GetMessage(&msg, NULL, 0, 0)) {
167 TranslateMessage(&msg);
168 DispatchMessage(&msg);
174 EmOutChar(HWND hwnd, int ch)
185 GetClientRect(hwnd, &rc);
186 if(ypos > (ROWS-1)*nCharHeight + YMARGIN) {
189 /* scroll window using bitblt ;-)*/
191 BitBlt(hdc, XMARGIN, YMARGIN, rc.right-XMARGIN*2,
192 rc.bottom-nCharHeight-YMARGIN*2,
193 hdc, XMARGIN, nCharHeight+YMARGIN, SRCCOPY);
197 rc.bottom -= YMARGIN;
199 (HBRUSH)GetClassLong(hwnd, GCL_HBRBACKGROUND));
200 ReleaseDC(hwnd, hdc);
203 case '\007': /* bel*/
204 write(STDERR_FILENO, "\007", 1);
208 while((xpos/nCharWidth) & 7)
209 EmOutChar(hwnd, ' ');
215 EmOutChar(hwnd, ' ');
222 SelectObject(hdc, GetStockObject(FONTNAME));
223 SetBkColor(hdc, BKCOLOR);
224 SetTextColor(hdc, FGCOLOR);
225 SetRect(&rc, xpos, ypos, xpos+nCharWidth, ypos+nCharHeight);
226 ExtTextOut(hdc, xpos, ypos, ETO_OPAQUE, &rc, (char *)&ch, 1, NULL);
227 ReleaseDC(hwnd, hdc);
229 if(xpos > (COLS-1)*nCharWidth) {
231 EmOutChar(hwnd, '\n');
236 #define SHELL "/bin/sash"
241 #define SHELL "/bin/sh"
248 ptysignaled(int signo)
251 case SIGINT: /* interrupt*/
253 /* this doesn't work, can anyone fix it?*/
257 case SIGCHLD: /* child status change - child exit*/
258 DestroyWindow(GetActiveWindow());
262 fprintf(stderr, "Uncaught signal %d\n", signo);
266 * Create a shell running through a pseudo tty, return the shell fd.
277 sprintf(pty_name, "/dev/ptyp%d", n);
278 if ((tfd = open(pty_name, O_RDWR | O_NONBLOCK)) < 0) {
279 if ((errno == EBUSY || errno == EIO) && n < 10) {
283 fprintf(stderr, "Can't create pty %s\n", pty_name);
286 signal(SIGCHLD, ptysignaled);
287 signal(SIGINT, ptysignaled);
288 if ((pid = fork()) == -1) {
289 fprintf(stderr, "No processes\n");
294 close(STDOUT_FILENO);
295 close(STDERR_FILENO);
300 if ((tfd = open(pty_name, O_RDWR)) < 0) {
301 fprintf(stderr, "Child: Can't open pty %s\n", pty_name);
304 dup2(tfd, STDIN_FILENO);
305 dup2(tfd, STDOUT_FILENO);
306 dup2(tfd, STDERR_FILENO);
307 /*if(!(argv[0] = getenv("SHELL")))*/
310 execv(argv[0], argv);
317 ReadPtyShell(int fd, char *buf, int count)
319 return read(fd, buf, count);
323 WritePtyShell(int fd, char *buf, int count)
325 return write(fd, buf, count);
329 ClosePtyShell(int fd)