1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <bio.h>
#include <event.h>
#include <keyboard.h>
int newwin(char*);
int nokill;
int textmode;
char *title = nil;
char *message = nil;
Biobuf *bout;
Image *light;
Image *text;
Rectangle rtext;
void
initcolor(void)
{
text = display->black;
light = allocimagemix(display, DPalegreen, DWhite);
if(light == nil) sysfatal("initcolor: %r");
}
void
drawmsg(void)
{
if(textmode){
static int last = 0;
while(last-- > 0)
Bputc(bout, '\b');
Bwrite(bout, message, strlen(message));
Bflush(bout);
last = utflen(message);
return;
}
draw(screen, rtext, light, nil, ZP);
string(screen, rtext.min, text, ZP, display->defaultfont, message);
flushimage(display, 1);
}
void
eresized(int new)
{
if(new && getwindow(display, Refnone) < 0)
fprint(2,"can't reattach to window");
rtext = screen->r;
draw(screen, rtext, light, nil, ZP);
rtext.min.x += 4;
rtext.min.y += 4;
if(title){
string(screen, rtext.min, text, ZP, display->defaultfont, title);
rtext.min.y += 8+display->defaultfont->height;
}
rtext.max.y = rtext.min.y + display->defaultfont->height;
drawmsg();
}
void
msg(Biobuf *b)
{
char *p;
Event e;
int k, die, parent, child;
parent = getpid();
die = 0;
if(textmode){
child = -1;
if(title){
Bwrite(bout, title, strlen(title));
Bwrite(bout, ": ", 2);
Bflush(bout);
}
} else
switch(child = rfork(RFMEM|RFPROC)) {
case 0:
sleep(1000);
while(!die && (k = eread(Ekeyboard|Emouse, &e))) {
if(nokill==0 && k == Ekeyboard && (e.kbdc == Kdel || e.kbdc == Ketx)) {
die = 1;
postnote(PNPROC, parent, "interrupt");
_exits("interrupt");
}
}
_exits(0);
}
while(!die && (p = Brdline(b, '\n'))){
snprint(message, Bsize, "%.*s", utfnlen(p, Blinelen(b)-1), p);
drawmsg();
}
if(textmode){
Bwrite(bout, "\n", 1);
Bterm(bout);
}
postnote(PNPROC, child, "kill");
}
void
usage(void)
{
fprint(2, "usage: %s [-kt] [-w minx,miny,maxx,maxy] [title]\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
Biobuf b;
char *p, *q;
int lfd;
p = "0,0,200,60";
ARGBEGIN{
case 'w':
p = ARGF();
break;
case 't':
textmode = 1;
break;
case 'k':
nokill = 1;
break;
default:
usage();
}ARGEND;
switch(argc){
default:
usage();
case 1:
title = argv[0];
case 0:
break;
}
lfd = dup(0, -1);
while(q = strchr(p, ','))
*q = ' ';
Binit(&b, lfd, OREAD);
if((message = malloc(Bsize)) == nil)
sysfatal("malloc: %r");
memset(message, 0, Bsize);
if(textmode || newwin(p) < 0){
textmode = 1;
if((bout = Bfdopen(1, OWRITE)) == nil)
sysfatal("Bfdopen: %r");
}else{
if(initdraw(0, 0, title ? title : argv0) < 0)
sysfatal("initdraw: %r");
initcolor();
einit(Emouse|Ekeyboard);
eresized(0);
}
msg(&b);
exits(0);
}
int
newwin(char *win)
{
char spec[100];
int cons;
if(win != nil){
snprint(spec, sizeof(spec), "-r %s", win);
win = spec;
}
if(newwindow(win) < 0){
fprint(2, "%s: newwindow: %r", argv0);
return -1;
}
if((cons = open("/dev/cons", OREAD)) < 0){
NoCons:
fprint(2, "%s: can't open /dev/cons: %r", argv0);
return -1;
}
dup(cons, 0);
close(cons);
if((cons = open("/dev/cons", OWRITE)) < 0)
goto NoCons;
dup(cons, 1);
dup(cons, 2);
close(cons);
return 0;
}
|