2 * fbpad - a small framebuffer virtual terminal
4 * Copyright (C) 2009-2014 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
14 #include <sys/ioctl.h>
23 #define CTRLKEY(x) ((x) - 96)
24 #define POLLFLAGS (POLLIN | POLLHUP | POLLERR | POLLNVAL)
25 #define NTAGS (sizeof(tags) - 1)
26 #define NTERMS (NTAGS * 2)
27 #define TERMOPEN(i) (terms[i].fd)
28 #define TERMSNAP(i) (strchr(TAGS_SAVED, tags[(i) % NTAGS]))
30 static char tags
[] = TAGS
;
31 static struct term terms
[NTERMS
];
32 static int tops
[NTAGS
]; /* top terms of tags */
33 static int ctag
; /* current tag */
34 static int ltag
; /* the last tag */
36 static int hidden
; /* do not touch the framebuffer */
38 static int taglock
; /* disable tag switching */
39 static char pass
[1024];
41 static int cmdmode
; /* execute a command and exit */
43 static int readchar(void)
46 if (read(0, &b
, 1) > 0)
47 return (unsigned char) b
;
51 static int cterm(void)
53 return tops
[ctag
] * NTAGS
+ ctag
;
56 static int altterm(int n
)
58 return n
< NTAGS
? n
+ NTAGS
: n
- NTAGS
;
61 static int nextterm(void)
63 int n
= (cterm() + 1) % NTERMS
;
64 while (n
!= cterm()) {
72 static struct term
*mainterm(void)
74 return TERMOPEN(cterm()) ? &terms
[cterm()] : NULL
;
77 static void switchterm(int oidx
, int nidx
, int show
, int save
, int load
)
79 if (save
&& TERMOPEN(oidx
) && TERMSNAP(oidx
))
81 term_save(&terms
[oidx
]);
82 term_load(&terms
[nidx
], show
);
84 term_redraw(load
&& (!TERMOPEN(nidx
) || !TERMSNAP(nidx
) ||
88 static void showterm(int n
)
90 if (cterm() == n
|| cmdmode
)
92 if (taglock
&& ctag
!= n
% NTAGS
)
94 if (ctag
!= n
% NTAGS
)
96 switchterm(cterm(), n
, !hidden
, !hidden
, !hidden
);
98 tops
[ctag
] = n
/ NTAGS
;
101 static void showtag(int n
)
103 showterm(tops
[n
] * NTAGS
+ n
);
106 static void execterm(char **args
)
112 static void listtags(void)
114 /* colors for tags based on their number of terminals */
115 int colors
[] = {252, FGCOLOR
, FGCOLOR
| FN_B
};
117 int r
= pad_rows() - 1;
119 pad_put('T', r
, c
++, FGCOLOR
, BGCOLOR
);
120 pad_put('A', r
, c
++, FGCOLOR
, BGCOLOR
);
121 pad_put('G', r
, c
++, FGCOLOR
, BGCOLOR
);
122 pad_put('S', r
, c
++, FGCOLOR
, BGCOLOR
);
123 pad_put(':', r
, c
++, FGCOLOR
, BGCOLOR
);
124 pad_put(' ', r
, c
++, FGCOLOR
, BGCOLOR
);
125 for (i
= 0; i
< NTAGS
; i
++) {
129 if (TERMOPEN(altterm(i
)))
131 pad_put(i
== ctag
? '(' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
133 pad_put(tags
[i
], r
, c
++, !nt
? BGCOLOR
: colors
[nt
], colors
[0]);
135 pad_put(tags
[i
], r
, c
++, colors
[nt
], BGCOLOR
);
136 pad_put(i
== ctag
? ')' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
140 static void directkey(void)
142 char *shell
[32] = SHELL
;
143 char *mail
[32] = MAIL
;
144 char *editor
[32] = EDITOR
;
146 if (PASS
&& locked
) {
148 pass
[passlen
] = '\0';
149 if (!strcmp(PASS
, pass
))
154 if (isprint(c
) && passlen
+ 1 < sizeof(pass
))
159 switch ((c
= readchar())) {
171 showterm(altterm(cterm()));
180 if (nextterm() != cterm())
181 showterm(nextterm());
197 taglock
= 1 - taglock
;
200 term_scrl(pad_rows() / 2);
203 term_scrl(-pad_rows() / 2);
206 if (strchr(tags
, c
)) {
207 showtag(strchr(tags
, c
) - tags
);
214 if (c
!= -1 && mainterm())
218 static void peepterm(int termid
)
220 if (termid
!= cterm())
221 switchterm(cterm(), termid
, 0, 0, 0);
224 static void peepback(int termid
)
226 if (termid
!= cterm())
227 switchterm(termid
, cterm(), !hidden
, 0, 0);
230 static int pollterms(void)
232 struct pollfd ufds
[NTERMS
+ 1];
233 int term_idx
[NTERMS
+ 1];
237 ufds
[0].events
= POLLIN
;
238 for (i
= 0; i
< NTERMS
; i
++) {
240 ufds
[n
].fd
= terms
[i
].fd
;
241 ufds
[n
].events
= POLLIN
;
245 if (poll(ufds
, n
, 1000) < 1)
247 if (ufds
[0].revents
& (POLLFLAGS
& ~POLLIN
))
249 if (ufds
[0].revents
& POLLIN
)
251 for (i
= 1; i
< n
; i
++) {
252 if (!(ufds
[i
].revents
& POLLFLAGS
))
254 peepterm(term_idx
[i
]);
255 if (ufds
[i
].revents
& POLLIN
) {
258 scr_free(term_idx
[i
]);
263 peepback(term_idx
[i
]);
268 static void mainloop(char **args
)
270 struct termios oldtermios
, termios
;
271 tcgetattr(0, &termios
);
272 oldtermios
= termios
;
274 tcsetattr(0, TCSAFLUSH
, &termios
);
275 term_load(&terms
[cterm()], 1);
284 tcsetattr(0, 0, &oldtermios
);
287 static void signalreceived(int n
);
288 static void signalregister(void)
290 signal(SIGUSR1
, signalreceived
);
291 signal(SIGUSR2
, signalreceived
);
292 signal(SIGCHLD
, signalreceived
);
295 static void signalreceived(int n
)
299 /* racy, new signals may arrive before re-registeration */
304 switchterm(cterm(), cterm(), 0, 1, 0);
305 ioctl(0, VT_RELDISP
, 1);
310 switchterm(cterm(), cterm(), 1, 0, 1);
313 while (waitpid(-1, NULL
, WNOHANG
) > 0)
319 static void signalsetup(void)
322 vtm
.mode
= VT_PROCESS
;
324 vtm
.relsig
= SIGUSR1
;
325 vtm
.acqsig
= SIGUSR2
;
328 ioctl(0, VT_SETMODE
, &vtm
);
331 int main(int argc
, char **argv
)
333 char *hide
= "\x1b[?25l";
334 char *clear
= "\x1b[2J\x1b[H";
335 char *show
= "\x1b[?25h";
336 char **args
= argv
+ 1;
337 write(1, clear
, strlen(clear
));
338 write(1, hide
, strlen(hide
));
342 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
343 while (args
[0] && args
[0][0] == '-')
345 mainloop(args
[0] ? args
: NULL
);
349 write(1, show
, strlen(show
));