fbpad: don't load visible terms when tty is hidden
[fbpad.git] / fbpad.c
blob3c2bd775d0e95c9a974de58d70260c2696780cd2
1 /*
2 * fbpad - A small linux framebuffer virtual terminal
4 * Copyright (C) 2009 Ali Gholami Rudi
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, as published by the
8 * Free Software Foundation.
9 */
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <poll.h>
14 #include <pty.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 #include <linux/vt.h>
20 #include <locale.h>
21 #include "config.h"
22 #include "pad.h"
23 #include "term.h"
24 #include "util.h"
26 #define CTRLKEY(x) ((x) - 96)
27 #define BADPOLLFLAGS (POLLHUP | POLLERR | POLLNVAL)
28 #define NTAGS sizeof(tags)
30 static char tags[] = TAGS;
31 static struct term terms[NTAGS * 2];
32 static int cterm; /* current tag */
33 static int lterm; /* last tag */
34 static int exitit;
35 static int hidden;
37 static int readchar(void)
39 char b;
40 if (read(STDIN_FILENO, &b, 1) > 0)
41 return (int) b;
42 return -1;
45 static void showterm(int n)
47 if (cterm % NTAGS != n % NTAGS)
48 lterm = cterm;
49 term_save(&terms[cterm]);
50 cterm = n;
51 term_load(&terms[cterm], hidden ? TERM_HIDDEN : TERM_REDRAW);
54 static struct term *mainterm(void)
56 if (terms[cterm].fd)
57 return &terms[cterm];
58 return NULL;
61 static void exec_cmd(char *file)
63 if (!mainterm())
64 term_exec(file);
67 static int altterm(int n)
69 return n < NTAGS ? n + NTAGS : n - NTAGS;
72 static void nextterm(void)
74 int n = (cterm + 1) % ARRAY_SIZE(terms);
75 while (n != cterm) {
76 if (terms[n].fd) {
77 showterm(n);
78 break;
80 n = (n + 1) % ARRAY_SIZE(terms);
84 static void showterms(void)
86 int colors[] = {FGCOLOR, 4, 2, 5};
87 int c = 0;
88 int r = pad_rows() - 1;
89 int i;
90 pad_put('T', r, c++, FGCOLOR, BGCOLOR);
91 pad_put('A', r, c++, FGCOLOR, BGCOLOR);
92 pad_put('G', r, c++, FGCOLOR, BGCOLOR);
93 pad_put('S', r, c++, FGCOLOR, BGCOLOR);
94 pad_put(':', r, c++, FGCOLOR, BGCOLOR);
95 pad_put(' ', r, c++, FGCOLOR, BGCOLOR);
96 for (i = 0; i < NTAGS; i++) {
97 int nt = 0;
98 int shown = i == cterm || altterm(i) == cterm;
99 if (terms[i].fd)
100 nt = 1;
101 if (terms[altterm(i)].fd)
102 nt = nt ? 2 : 3;
103 pad_put(shown ? '(' : ' ', r, c++, FGCOLOR, BGCOLOR);
104 pad_put(tags[i], r, c++, colors[nt], 7);
105 pad_put(shown ? ')' : ' ', r, c++, FGCOLOR, BGCOLOR);
109 static void directkey(void)
111 int c = readchar();
112 if (c == ESC) {
113 switch ((c = readchar())) {
114 case 'c':
115 exec_cmd(SHELL);
116 return;
117 case 'm':
118 exec_cmd(MAIL);
119 return;
120 case 'e':
121 exec_cmd(EDITOR);
122 return;
123 case 'j':
124 case 'k':
125 showterm(altterm(cterm));
126 return;
127 case 'o':
128 showterm(lterm);
129 return;
130 case 'p':
131 showterms();
132 return;
133 case '\t':
134 nextterm();
135 return;
136 case CTRLKEY('q'):
137 exitit = 1;
138 return;
139 default:
140 if (strchr(tags, c)) {
141 showterm(strchr(tags, c) - tags);
142 return;
144 if (mainterm())
145 term_send(ESC);
148 if (c != -1 && mainterm())
149 term_send(c);
152 static int find_by_fd(int fd)
154 int i;
155 for (i = 0; i < ARRAY_SIZE(terms); i++)
156 if (terms[i].fd == fd)
157 return i;
158 return -1;
161 static int fill_ufds(struct pollfd *ufds)
163 int n = 1;
164 int i;
165 ufds[0].fd = STDIN_FILENO;
166 ufds[0].events = POLLIN;
167 for (i = 0; i < ARRAY_SIZE(terms); i++) {
168 if (terms[i].fd) {
169 ufds[n].fd = terms[i].fd;
170 ufds[n].events = POLLIN;
171 n++;
174 return n;
177 static void temp_switch(int termid)
179 if (termid != cterm) {
180 term_save(&terms[cterm]);
181 term_load(&terms[termid], TERM_HIDDEN);
185 static void switch_back(int termid)
187 if (termid != cterm) {
188 term_save(&terms[termid]);
189 term_load(&terms[cterm], hidden ? TERM_HIDDEN : TERM_VISIBLE);
193 static void check_ufds(struct pollfd *ufds, int n)
195 int i;
196 for (i = 1; i < n; i++) {
197 int idx = find_by_fd(ufds[i].fd);
198 if (ufds[i].revents & BADPOLLFLAGS) {
199 temp_switch(idx);
200 term_end();
201 switch_back(idx);
202 } else if (ufds[i].revents & POLLIN) {
203 temp_switch(idx);
204 term_read();
205 switch_back(idx);
210 static void mainloop(void)
212 struct pollfd ufds[ARRAY_SIZE(terms) + 1];
213 struct termios oldtermios, termios;
214 int rv;
215 int n;
216 tcgetattr(STDIN_FILENO, &termios);
217 oldtermios = termios;
218 cfmakeraw(&termios);
219 tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
220 memset(ufds, 0, sizeof(ufds));
221 term_load(&terms[cterm], TERM_REDRAW);
222 n = fill_ufds(ufds);
223 while (!exitit) {
224 rv = poll(ufds, n, 1000);
225 if (rv == -1 && errno != EINTR)
226 break;
227 if (ufds[0].revents & BADPOLLFLAGS)
228 break;
229 if (ufds[0].revents & POLLIN)
230 directkey();
231 check_ufds(ufds, n);
232 n = fill_ufds(ufds);
234 tcsetattr(STDIN_FILENO, 0, &oldtermios);
237 static void signalreceived(int n)
239 if (exitit)
240 return;
241 switch (n) {
242 case SIGUSR1:
243 hidden = 1;
244 term_save(&terms[cterm]);
245 term_load(&terms[cterm], TERM_HIDDEN);
246 ioctl(STDIN_FILENO, VT_RELDISP, 1);
247 break;
248 case SIGUSR2:
249 hidden = 0;
250 pad_shown();
251 term_save(&terms[cterm]);
252 term_load(&terms[cterm], TERM_REDRAW);
253 break;
254 case SIGCHLD:
255 waitpid(-1, 0, WNOHANG);
256 break;
260 static void setupsignals(void)
262 struct vt_mode vtm;
263 vtm.mode = VT_PROCESS;
264 vtm.waitv = 0;
265 vtm.relsig = SIGUSR1;
266 vtm.acqsig = SIGUSR2;
267 vtm.frsig = 0;
268 ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
270 signal(SIGUSR1, signalreceived);
271 signal(SIGUSR2, signalreceived);
272 signal(SIGCHLD, signalreceived);
275 int main(void)
277 char *hide = "\x1b[?25l";
278 char *clear = "\x1b[2J\x1b[H";
279 char *show = "\x1b[?25h";
280 setlocale(LC_ALL, "");
281 write(STDIN_FILENO, clear, strlen(clear));
282 write(STDIN_FILENO, hide, strlen(hide));
283 pad_init();
284 setupsignals();
285 fcntl(STDIN_FILENO, F_SETFL,
286 fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
287 mainloop();
288 pad_free();
289 write(STDIN_FILENO, show, strlen(show));
290 return 0;