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.
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 */
37 static int readchar(void)
40 if (read(STDIN_FILENO
, &b
, 1) > 0)
45 static void showterm(int n
)
47 if (cterm
% NTAGS
!= n
% NTAGS
)
49 term_save(&terms
[cterm
]);
51 term_load(&terms
[cterm
], hidden
? TERM_HIDDEN
: TERM_REDRAW
);
54 static struct term
*mainterm(void)
61 static void exec_cmd(char *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
);
80 n
= (n
+ 1) % ARRAY_SIZE(terms
);
84 static void showterms(void)
86 int colors
[] = {FGCOLOR
, 4, 2, 5};
88 int r
= pad_rows() - 1;
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
++) {
98 int shown
= i
== cterm
|| altterm(i
) == cterm
;
101 if (terms
[altterm(i
)].fd
)
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)
113 switch ((c
= readchar())) {
125 showterm(altterm(cterm
));
140 if (strchr(tags
, c
)) {
141 showterm(strchr(tags
, c
) - tags
);
148 if (c
!= -1 && mainterm())
152 static int find_by_fd(int fd
)
155 for (i
= 0; i
< ARRAY_SIZE(terms
); i
++)
156 if (terms
[i
].fd
== fd
)
161 static int fill_ufds(struct pollfd
*ufds
)
165 ufds
[0].fd
= STDIN_FILENO
;
166 ufds
[0].events
= POLLIN
;
167 for (i
= 0; i
< ARRAY_SIZE(terms
); i
++) {
169 ufds
[n
].fd
= terms
[i
].fd
;
170 ufds
[n
].events
= POLLIN
;
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
)
196 for (i
= 1; i
< n
; i
++) {
197 int idx
= find_by_fd(ufds
[i
].fd
);
198 if (ufds
[i
].revents
& BADPOLLFLAGS
) {
202 } else if (ufds
[i
].revents
& POLLIN
) {
210 static void mainloop(void)
212 struct pollfd ufds
[ARRAY_SIZE(terms
) + 1];
213 struct termios oldtermios
, termios
;
216 tcgetattr(STDIN_FILENO
, &termios
);
217 oldtermios
= termios
;
219 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &termios
);
220 memset(ufds
, 0, sizeof(ufds
));
221 term_load(&terms
[cterm
], TERM_REDRAW
);
224 rv
= poll(ufds
, n
, 1000);
225 if (rv
== -1 && errno
!= EINTR
)
227 if (ufds
[0].revents
& BADPOLLFLAGS
)
229 if (ufds
[0].revents
& POLLIN
)
234 tcsetattr(STDIN_FILENO
, 0, &oldtermios
);
237 static void signalreceived(int n
)
244 term_save(&terms
[cterm
]);
245 term_load(&terms
[cterm
], TERM_HIDDEN
);
246 ioctl(STDIN_FILENO
, VT_RELDISP
, 1);
251 term_save(&terms
[cterm
]);
252 term_load(&terms
[cterm
], TERM_REDRAW
);
255 waitpid(-1, 0, WNOHANG
);
260 static void setupsignals(void)
263 vtm
.mode
= VT_PROCESS
;
265 vtm
.relsig
= SIGUSR1
;
266 vtm
.acqsig
= SIGUSR2
;
268 ioctl(STDIN_FILENO
, VT_SETMODE
, &vtm
);
270 signal(SIGUSR1
, signalreceived
);
271 signal(SIGUSR2
, signalreceived
);
272 signal(SIGCHLD
, signalreceived
);
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
));
285 fcntl(STDIN_FILENO
, F_SETFL
,
286 fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
289 write(STDIN_FILENO
, show
, strlen(show
));