29 #include <sys/ioctl.h> 39 #include "pedigree/native/input/Input.h" 42 pid_t g_RunningPid = -1;
47 #if defined(LIVECD) && !defined(TRAVIS) 48 #define FIRST_PROGRAM "/applications/live" 50 #define FIRST_PROGRAM "/applications/login" 53 #define ALT_KEY (1ULL << 60) 54 #define SHIFT_KEY (1ULL << 61) 55 #define CTRL_KEY (1ULL << 62) 56 #define SPECIAL_KEY (1ULL << 63) 68 extern int login(
int uid,
char *password);
79 int fb = open(
"/dev/fb", O_RDWR);
82 int result = ioctl(fb, PEDIGREE_FB_GETMODE, ¤t_mode);
87 if (current_mode.width && current_mode.height && current_mode.depth)
91 "ttyterm: dropping input, currently in graphics mode!");
97 klog(LOG_INFO,
"ttyterm: system input (type=%d)", note.type);
99 if (note.type & Input::Key)
101 uint64_t c = note.data.key.key;
103 ActualKey realKey = None;
106 uint32_t k = c & 0xFFFFFFFFULL;
108 memcpy(str, reinterpret_cast<char *>(&k), 4);
111 if (!strcmp(str,
"left"))
115 else if (!strcmp(str,
"righ"))
119 else if (!strcmp(str,
"up"))
123 else if (!strcmp(str,
"down"))
133 else if (c & CTRL_KEY)
147 write(g_MasterPty,
"\e[D", 3);
150 write(g_MasterPty,
"\e[C", 3);
153 write(g_MasterPty,
"\e[A", 3);
156 write(g_MasterPty,
"\e[B", 3);
162 else if (c & ALT_KEY)
166 char buf[2] = {
'\e',
static_cast<char>(c & 0xFF)};
167 write(g_MasterPty, buf, 2);
171 uint32_t utf32 = c & 0xFFFFFFFF;
178 buf[0] = utf32 & 0x7F;
181 else if (utf32 <= 0x7FF)
183 buf[0] = 0xC0 | ((utf32 >> 6) & 0x1F);
184 buf[1] = 0x80 | (utf32 & 0x3F);
187 else if (utf32 <= 0xFFFF)
189 buf[0] = 0xE0 | ((utf32 >> 12) & 0x0F);
190 buf[1] = 0x80 | ((utf32 >> 6) & 0x3F);
191 buf[2] = 0x80 | (utf32 & 0x3F);
194 else if (utf32 <= 0x10FFFF)
196 buf[0] = 0xF0 | ((utf32 >> 18) & 0x07);
197 buf[1] = 0x80 | ((utf32 >> 12) & 0x3F);
198 buf[2] = 0x80 | ((utf32 >> 6) & 0x3F);
199 buf[3] = 0x80 | (utf32 & 0x3F);
204 write(g_MasterPty, buf, nbuf);
209 int main(
int argc,
char **argv)
211 klog(LOG_INFO,
"ttyterm: starting up...");
214 int fd = open(
"runtimeĀ»/ttyterm.lck", O_WRONLY | O_EXCL | O_CREAT);
217 fprintf(stderr,
"ttyterm: lock file exists, terminating.\n");
223 signal(SIGINT, sigint);
227 int fb = open(
"/dev/fb", O_RDWR);
231 klog(LOG_INFO,
"ttyterm: forcing text mode");
233 int rc = ioctl(fb, PEDIGREE_FB_SETMODE, &mode);
238 klog(LOG_INFO,
"ttyterm: couldn't force text mode, exiting");
244 int tty = open(
"/dev/textui", O_WRONLY);
248 LOG_ALERT,
"ttyterm: couldn't open /dev/textui: %s",
253 g_MasterPty = posix_openpt(O_RDWR);
258 LOG_ALERT,
"ttyterm: couldn't get a pseudo-terminal to use: %s",
264 struct winsize ptySize;
267 ioctl(g_MasterPty, TIOCSWINSZ, &ptySize);
269 char slavename[64] = {0};
270 strncpy(slavename, ptsname(g_MasterPty), 64);
274 write(tty,
"\e[2J", 5);
277 Input::installCallback(Input::Key, handle_input);
280 g_RunningPid = fork();
281 if (g_RunningPid == -1)
283 klog(LOG_ALERT,
"ttyterm: couldn't fork: %s", strerror(errno));
286 else if (g_RunningPid == 0)
295 int slave = open(slavename, O_RDWR);
301 setenv(
"TERM",
"pedigree", 1);
304 setenv(
"LC_ALL",
"en_US.UTF-8", 1);
310 memset(&ut, 0,
sizeof(ut));
311 gettimeofday(&tv, NULL);
312 ut.ut_type = LOGIN_PROCESS;
313 ut.ut_pid = getpid();
315 strncpy(ut.ut_id,
"/", UT_LINESIZE);
316 strncpy(ut.ut_line,
"console", UT_LINESIZE);
321 write(slave,
"\e[?7h", 5);
323 klog(LOG_INFO,
"Starting up '" FIRST_PROGRAM
"' on pty %s", slavename);
324 execl(FIRST_PROGRAM, FIRST_PROGRAM, 0);
326 LOG_ALERT,
"Launching " FIRST_PROGRAM
327 " failed (next line is the error in errno...)");
328 klog(LOG_ALERT, strerror(errno));
333 const size_t maxBuffSize = 32768;
334 char buffer[maxBuffSize];
339 FD_SET(g_MasterPty, &fds);
342 int nReady = select(g_MasterPty + 1, &fds, NULL, NULL, NULL);
346 if (FD_ISSET(g_MasterPty, &fds))
352 Input::inhibitEvents();
353 size_t len = read(g_MasterPty, buffer, maxBuffSize);
354 Input::uninhibitEvents();
356 write(tty, buffer, len);
360 if (FD_ISSET(tty, &fds))
362 size_t len = read(tty, buffer, maxBuffSize);
368 Input::inhibitEvents();
369 write(g_MasterPty, buffer, len);
370 Input::uninhibitEvents();
All zeroes = 'revert to text mode'.