2 * fbpad - a small framebuffer virtual terminal
4 * Copyright (C) 2009-2013 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 char pass
[1024];
40 static int cmdmode
; /* execute a command and exit */
41 static int histpos
; /* scrolling history */
43 static int readchar(void)
46 if (read(0, &b
, 1) > 0)
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 histscrl(int pos
)
84 static void switchterm(int oidx
, int nidx
, int show
, int save
, int load
)
88 if (save
&& TERMOPEN(oidx
) && TERMSNAP(oidx
))
89 scr_snap(&terms
[oidx
]);
90 term_save(&terms
[oidx
]);
91 term_load(&terms
[nidx
], show
);
93 term_redraw(load
&& (!TERMOPEN(nidx
) || !TERMSNAP(nidx
) ||
94 scr_load(&terms
[nidx
])));
97 static void showterm(int n
)
99 if (cterm() == n
|| cmdmode
)
101 if (ctag
!= n
% NTAGS
)
103 switchterm(cterm(), n
, !hidden
, !hidden
, !hidden
);
105 tops
[ctag
] = n
/ NTAGS
;
108 static void showtag(int n
)
110 showterm(tops
[n
] * NTAGS
+ n
);
113 static void execterm(char **args
)
119 static void listtags(void)
121 /* colors for tags based on their number of terminals */
122 int colors
[] = {252, FGCOLOR
, FGCOLOR
| FN_B
};
124 int r
= pad_rows() - 1;
126 pad_put('T', r
, c
++, FGCOLOR
, BGCOLOR
);
127 pad_put('A', r
, c
++, FGCOLOR
, BGCOLOR
);
128 pad_put('G', r
, c
++, FGCOLOR
, BGCOLOR
);
129 pad_put('S', r
, c
++, FGCOLOR
, BGCOLOR
);
130 pad_put(':', r
, c
++, FGCOLOR
, BGCOLOR
);
131 pad_put(' ', r
, c
++, FGCOLOR
, BGCOLOR
);
132 for (i
= 0; i
< NTAGS
; i
++) {
136 if (TERMOPEN(altterm(i
)))
138 pad_put(i
== ctag
? '(' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
140 pad_put(tags
[i
], r
, c
++, !nt
? BGCOLOR
: colors
[nt
], colors
[0]);
142 pad_put(tags
[i
], r
, c
++, colors
[nt
], BGCOLOR
);
143 pad_put(i
== ctag
? ')' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
147 static void directkey(void)
149 char *shell
[32] = SHELL
;
150 char *mail
[32] = MAIL
;
151 char *editor
[32] = EDITOR
;
153 if (PASS
&& locked
) {
155 pass
[passlen
] = '\0';
156 if (!strcmp(PASS
, pass
))
161 if (isprint(c
) && passlen
+ 1 < sizeof(pass
))
166 switch ((c
= readchar())) {
178 showterm(altterm(cterm()));
187 if (nextterm() != cterm())
188 showterm(nextterm());
204 histscrl(MIN(NHIST
, histpos
+ pad_rows() / 2));
207 histscrl(MAX(0, histpos
- pad_rows() / 2));
210 if (strchr(tags
, c
)) {
211 showtag(strchr(tags
, c
) - tags
);
219 if (c
!= -1 && mainterm())
223 static void peepterm(int termid
)
225 if (termid
!= cterm())
226 switchterm(cterm(), termid
, 0, 0, 0);
229 static void peepback(int termid
)
231 if (termid
!= cterm())
232 switchterm(termid
, cterm(), !hidden
, 0, 0);
235 static int pollterms(void)
237 struct pollfd ufds
[NTERMS
+ 1];
238 int term_idx
[NTERMS
+ 1];
242 ufds
[0].events
= POLLIN
;
243 for (i
= 0; i
< NTERMS
; i
++) {
245 ufds
[n
].fd
= terms
[i
].fd
;
246 ufds
[n
].events
= POLLIN
;
250 if (poll(ufds
, n
, 1000) < 1)
252 if (ufds
[0].revents
& (POLLFLAGS
& ~POLLIN
))
254 if (ufds
[0].revents
& POLLIN
)
256 for (i
= 1; i
< n
; i
++) {
257 if (!(ufds
[i
].revents
& POLLFLAGS
))
259 peepterm(term_idx
[i
]);
260 if (ufds
[i
].revents
& POLLIN
) {
263 scr_free(&terms
[term_idx
[i
]]);
268 peepback(term_idx
[i
]);
273 static void mainloop(char **args
)
275 struct termios oldtermios
, termios
;
276 tcgetattr(0, &termios
);
277 oldtermios
= termios
;
279 tcsetattr(0, TCSAFLUSH
, &termios
);
280 term_load(&terms
[cterm()], 1);
289 tcsetattr(0, 0, &oldtermios
);
292 static void signalreceived(int n
);
293 static void signalregister(void)
295 signal(SIGUSR1
, signalreceived
);
296 signal(SIGUSR2
, signalreceived
);
297 signal(SIGCHLD
, signalreceived
);
300 static void signalreceived(int n
)
304 /* racy, new signals may arrive before re-registeration */
309 switchterm(cterm(), cterm(), 0, 1, 0);
310 ioctl(0, VT_RELDISP
, 1);
315 switchterm(cterm(), cterm(), 1, 0, 1);
318 while (waitpid(-1, NULL
, WNOHANG
) > 0)
324 static void signalsetup(void)
327 vtm
.mode
= VT_PROCESS
;
329 vtm
.relsig
= SIGUSR1
;
330 vtm
.acqsig
= SIGUSR2
;
333 ioctl(0, VT_SETMODE
, &vtm
);
336 int main(int argc
, char **argv
)
338 char *hide
= "\x1b[?25l";
339 char *clear
= "\x1b[2J\x1b[H";
340 char *show
= "\x1b[?25h";
341 char **args
= argv
+ 1;
342 write(1, clear
, strlen(clear
));
343 write(1, hide
, strlen(hide
));
347 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
348 while (args
[0] && args
[0][0] == '-')
350 mainloop(args
[0] ? args
: NULL
);
353 write(1, show
, strlen(show
));