use the -newos toolchain even if -elf is present.
[newos.git] / apps / irc / term.cpp
blob4584e155ad01ab11a83c94b5567491f389515ced
1 /*
2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <sys/syscalls.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <newos/tty_priv.h>
11 #include "term.h"
13 Term::Term(int fd)
14 : mFd(fd)
16 mSem = _kern_sem_create(1, "term lock");
18 tty_flags flags;
19 flags.input_flags = TTY_FLAG_NLCR | TTY_FLAG_CRNL;
20 flags.output_flags = TTY_FLAG_NLCR;
22 ioctl(mFd, _TTY_IOCTL_SET_TTY_FLAGS, &flags, sizeof(flags));
25 Term::~Term()
27 tty_flags flags;
28 flags.input_flags = TTY_FLAG_DEFAULT_INPUT;
29 flags.output_flags = TTY_FLAG_DEFAULT_OUTPUT;
31 ioctl(mFd, _TTY_IOCTL_SET_TTY_FLAGS, &flags, sizeof(flags));
33 Reset();
35 _kern_sem_delete(mSem);
38 void Term::Lock()
40 _kern_sem_acquire(mSem, 1);
43 void Term::Unlock()
45 _kern_sem_release(mSem, 1);
48 void Term::ClearScreen()
50 WriteEscaped("[2J");
51 WriteEscaped("[H");
54 void Term::SaveCursor()
56 WriteEscaped("7");
59 void Term::RestoreCursor()
61 WriteEscaped("8");
64 void Term::Reset()
66 WriteEscaped("c");
69 ssize_t Term::Write(const char *buf)
71 return write(mFd, buf, strlen(buf));
74 ssize_t Term::WriteEscaped(const char *inbuf)
76 char buf[4096];
78 // XXX make safe
79 sprintf(buf, "%c%s", esc, inbuf);
81 return Write(buf);
84 void Term::SetScrollRegion(int y, int height)
86 char buf[256];
88 sprintf(buf, "[%d;%dr", y, y + height - 1);
89 WriteEscaped(buf);
92 void Term::SetCursor(int x, int y)
94 char buf[256];
96 sprintf(buf, "[%d;%dH", y, x);
97 WriteEscaped(buf);
100 void Term::ScrollUp(int count)
102 char buf[256];
104 sprintf(buf, "[%dM", count);
105 WriteEscaped(buf);
108 void Term::ScrollDown(int count)
110 char buf[256];
112 sprintf(buf, "[%dL", count);
113 WriteEscaped(buf);
116 TermWindow::TermWindow(Term *term, int x, int y, int width, int height)
117 : mTerm(term),
118 mX(x),
119 mY(y),
120 mWidth(width),
121 mHeight(height)
125 TermWindow::~TermWindow()
129 void TermWindow::Clear()
131 ScrollUp(mHeight);
134 void TermWindow::SetScrollRegion()
136 mTerm->SetScrollRegion(mY, mHeight);
139 void TermWindow::ScrollUp(int count)
141 mTerm->Lock();
143 SetScrollRegion();
145 mTerm->SaveCursor();
146 mTerm->SetCursor(1, mY + mHeight - 1);
148 mTerm->ScrollUp(count);
150 mTerm->RestoreCursor();
152 mTerm->Unlock();
155 void TermWindow::ScrollDown(int count)
157 mTerm->Lock();
159 SetScrollRegion();
161 mTerm->SaveCursor();
162 mTerm->SetCursor(1, mY);
164 mTerm->ScrollDown(count);
166 mTerm->RestoreCursor();
168 mTerm->Unlock();
171 void TermWindow::Write(const char *buf, bool atLastLine)
173 mTerm->Lock();
175 SetScrollRegion();
176 mTerm->SaveCursor();
178 if(atLastLine)
179 mTerm->SetCursor(1, mY + mHeight - 1);
181 mTerm->Write(buf);
183 mTerm->RestoreCursor();
184 mTerm->Unlock();
187 void TermWindow::SetCursor(int x, int y)
189 mTerm->Lock();
191 if(x < 1)
192 x = 1;
193 if(x > mWidth)
194 x = mWidth;
195 if(y < 1)
196 y = 1;
197 if(y > mHeight)
198 y = mHeight;
200 mTerm->SetCursor(mX + x - 1, mY + y - 1);
202 mTerm->Unlock();