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 tops
[NTAGS
]; /* top terms of tags */
33 static int ctag
; /* current tag */
34 static int ltag
; /* the last tag */
38 static int readchar(void)
41 if (read(STDIN_FILENO
, &b
, 1) > 0)
46 static int cterm(void)
48 return tops
[ctag
] * NTAGS
+ ctag
;
51 static void showterm(int n
)
55 if (ctag
!= n
% NTAGS
)
57 term_save(&terms
[cterm()]);
59 tops
[ctag
] = n
/ NTAGS
;
60 term_load(&terms
[n
], hidden
? TERM_HIDDEN
: TERM_REDRAW
);
63 static void showtag(int n
)
65 showterm(tops
[n
] * NTAGS
+ n
);
68 static struct term
*mainterm(void)
70 if (terms
[cterm()].fd
)
71 return &terms
[cterm()];
75 static void exec_cmd(char *file
)
81 static int altterm(int n
)
83 return n
< NTAGS
? n
+ NTAGS
: n
- NTAGS
;
86 static void nextterm(void)
88 int n
= (cterm() + 1) % ARRAY_SIZE(terms
);
89 while (n
!= cterm()) {
94 n
= (n
+ 1) % ARRAY_SIZE(terms
);
98 static void showtags(void)
100 int colors
[] = {15, 4, 2};
102 int r
= pad_rows() - 1;
104 pad_put('T', r
, c
++, FGCOLOR
, BGCOLOR
);
105 pad_put('A', r
, c
++, FGCOLOR
, BGCOLOR
);
106 pad_put('G', r
, c
++, FGCOLOR
, BGCOLOR
);
107 pad_put('S', r
, c
++, FGCOLOR
, BGCOLOR
);
108 pad_put(':', r
, c
++, FGCOLOR
, BGCOLOR
);
109 pad_put(' ', r
, c
++, FGCOLOR
, BGCOLOR
);
110 for (i
= 0; i
< NTAGS
; i
++) {
114 if (terms
[altterm(i
)].fd
)
116 pad_put(i
== ctag
? '(' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
117 pad_put(tags
[i
], r
, c
++, colors
[nt
], 7);
118 pad_put(i
== ctag
? ')' : ' ', r
, c
++, FGCOLOR
, BGCOLOR
);
122 static void directkey(void)
126 switch ((c
= readchar())) {
138 showterm(altterm(cterm()));
156 if (strchr(tags
, c
)) {
157 showtag(strchr(tags
, c
) - tags
);
164 if (c
!= -1 && mainterm())
168 static int find_by_fd(int fd
)
171 for (i
= 0; i
< ARRAY_SIZE(terms
); i
++)
172 if (terms
[i
].fd
== fd
)
177 static int fill_ufds(struct pollfd
*ufds
)
181 ufds
[0].fd
= STDIN_FILENO
;
182 ufds
[0].events
= POLLIN
;
183 for (i
= 0; i
< ARRAY_SIZE(terms
); i
++) {
185 ufds
[n
].fd
= terms
[i
].fd
;
186 ufds
[n
].events
= POLLIN
;
193 static void temp_switch(int termid
)
195 if (termid
!= cterm()) {
196 term_save(&terms
[cterm()]);
197 term_load(&terms
[termid
], TERM_HIDDEN
);
201 static void switch_back(int termid
)
203 if (termid
!= cterm()) {
204 term_save(&terms
[termid
]);
205 term_load(&terms
[cterm()], hidden
? TERM_HIDDEN
: TERM_VISIBLE
);
209 static void check_ufds(struct pollfd
*ufds
, int n
)
212 for (i
= 1; i
< n
; i
++) {
213 int idx
= find_by_fd(ufds
[i
].fd
);
214 if (ufds
[i
].revents
& BADPOLLFLAGS
) {
218 } else if (ufds
[i
].revents
& POLLIN
) {
226 static void mainloop(void)
228 struct pollfd ufds
[ARRAY_SIZE(terms
) + 1];
229 struct termios oldtermios
, termios
;
232 tcgetattr(STDIN_FILENO
, &termios
);
233 oldtermios
= termios
;
235 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &termios
);
236 memset(ufds
, 0, sizeof(ufds
));
237 term_load(&terms
[cterm()], TERM_REDRAW
);
240 rv
= poll(ufds
, n
, 1000);
241 if (rv
== -1 && errno
!= EINTR
)
243 if (ufds
[0].revents
& BADPOLLFLAGS
)
245 if (ufds
[0].revents
& POLLIN
)
250 tcsetattr(STDIN_FILENO
, 0, &oldtermios
);
253 static void signalreceived(int n
)
260 term_save(&terms
[cterm()]);
261 term_load(&terms
[cterm()], TERM_HIDDEN
);
262 ioctl(STDIN_FILENO
, VT_RELDISP
, 1);
267 term_save(&terms
[cterm()]);
268 term_load(&terms
[cterm()], TERM_REDRAW
);
271 while (waitpid(-1, 0, WNOHANG
) > 0)
277 static void setupsignals(void)
280 vtm
.mode
= VT_PROCESS
;
282 vtm
.relsig
= SIGUSR1
;
283 vtm
.acqsig
= SIGUSR2
;
285 ioctl(STDIN_FILENO
, VT_SETMODE
, &vtm
);
287 signal(SIGUSR1
, signalreceived
);
288 signal(SIGUSR2
, signalreceived
);
289 signal(SIGCHLD
, signalreceived
);
294 char *hide
= "\x1b[?25l";
295 char *clear
= "\x1b[2J\x1b[H";
296 char *show
= "\x1b[?25h";
297 setlocale(LC_ALL
, "");
298 write(STDOUT_FILENO
, clear
, strlen(clear
));
299 write(STDIN_FILENO
, hide
, strlen(hide
));
302 fcntl(STDIN_FILENO
, F_SETFL
,
303 fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
306 write(STDIN_FILENO
, show
, strlen(show
));