summaryrefslogtreecommitdiff
path: root/appl/wm/logwindow.b
blob: 4d0326b4bb5dad7f36ecca9c3e13701d2e456844 (plain)
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
implement Logwindow;

#
# Copyright © 1999 Vita Nuova Limited.  All rights reserved.
#

include "sys.m";
	sys: Sys;
	stderr: ref Sys->FD;
include "draw.m";
	draw: Draw;
include "tk.m";
	tk: Tk;
	cmd: import tk;
include "tkclient.m";
	tkclient: Tkclient;
include "arg.m";

Logwindow: module {
	init: fn(ctxt: ref Draw->Context, argv: list of string);
};

cfg := array[] of {
	"frame .bf",
	"checkbutton .bf.scroll -text Scroll -variable scroll -command {send cmd scroll}",
	".bf.scroll select",
	"checkbutton .bf.popup -text {Pop up} -variable popup -command {send cmd popup}",
	".bf.popup select",
	"pack .bf.scroll .bf.popup -side left",
	"frame .t",
	"scrollbar .t.scroll -command {.t.t yview}",
	"text .t.t -height 7c -yscrollcommand {.t.scroll set}",
	"pack .t.scroll -side left -fill y",
	"pack .t.t -fill both -expand 1",
	"pack .Wm_t -fill x",
	"pack .bf -anchor w",
	"pack .t -fill both -expand 1",
	"pack propagate . 0",
};

eflag := 0;

badmodule(p: string)
{
	sys->fprint(stderr, "logwindow: cannot load %s: %r\n", p);
	raise "fail:bad module";
}

init(ctxt: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);

	tkclient = load Tkclient Tkclient->PATH;
	if (tkclient == nil)
		badmodule(Tkclient->PATH);
	tkclient->init();

	tk = load Tk Tk->PATH;
	if (tk == nil)
		badmodule(Tk->PATH);

	arg := load Arg Arg->PATH;
	if (arg == nil)
		badmodule(Arg->PATH);

	if (ctxt == nil) {
		sys->fprint(stderr, "logwindow: nil Draw->Context\n");
		raise "fail:no draw context";
	}
	gflag := 0;
	title := "Log Window";
	arg->init(argv);
	while ((opt := arg->opt()) != 0) {
		case opt {
		'e' =>
			eflag = 1;
		'g' =>
			gflag = 1;
		* =>
			sys->fprint(stderr, "usage: logwindow [-ge] [title]\n");
			raise "fail:usage";
		}
	}
	argv = arg->argv();
	if (argv != nil)
		title = hd argv;

	if (!gflag)
		sys->pctl(Sys->NEWPGRP, nil);

	(top, wmchan) := tkclient->toplevel(ctxt, "", title, Tkclient->Hide|Tkclient->Resize);
	if (top == nil) {
		sys->fprint(stderr, "logwindow: couldn't make window\n");
		raise "fail: no window";
	}
	cmd(top, ". unmap");

	for (c:=0; c<len cfg; c++)
		tk->cmd(top, cfg[c]);
	if ((err := tk->cmd(top, "variable lasterror")) != nil) {
		sys->fprint(stderr, "logwindow: tk error: %s\n", err);
		raise "fail: tk error";
	}

	logwin(sys->fildes(0), top, wmchan);
}

scrolling := 1;
popup := 1;

logwin(fd: ref Sys->FD, top: ref Tk->Toplevel, wmchan: chan of string)
{
	cmd := chan of string;
	tk->namechan(top, cmd, "cmd");
	raised := 0;
	ichan := chan of int;
	spawn inputmon(fd, top, ichan);
	tkclient->onscreen(top, nil);
	tkclient->startinput(top, "kbd"::"ptr"::nil);
	tkclient->wmctl(top, "task");
	for (;;) alt {
		s := <-top.ctxt.kbd =>
			tk->keyboard(top, s);
		s := <-top.ctxt.ptr =>
			tk->pointer(top, *s);
		s := <-top.ctxt.ctl or
		s = <-top.wreq or
		s = <-wmchan =>
		case s {
		"task" =>
			raised = 0;
		"untask" =>
			raised = 1;
		}
		tkclient->wmctl(top, s);
	e := <-ichan =>
		if (e == 0 && eflag) {
			tkclient->wmctl(top, "exit");
			exit;
		}
		if (!raised && popup)
			tkclient->wmctl(top, "untask");
	msg := <-cmd =>
		case msg {
		"scroll" =>
			scrolling = int tk->cmd(top, "variable scroll");
		"popup" =>
			popup = int tk->cmd(top, "variable popup");
		}
	}
}

inputmon(fd: ref Sys->FD, top: ref Tk->Toplevel, ichan: chan of int)
{
	buf := array[Sys->ATOMICIO] of byte;
	t := 0;
	while ((n := sys->read(fd, buf[t:], len buf-t)) > 0) {
		t += n;
		cl := 0;
		for (i := t - 1; i >= 0; i--) {
			(nil, cl, nil) = sys->byte2char(buf, i);
			if (cl > 0)
				break;
		}
		if (cl == 0)
			continue;
		logmsg(top, ichan, string buf[0:i+cl]);
		buf[0:] = buf[i+cl:t];
		t -= i + cl;
	}
	if (n < 0)
		logmsg(top, ichan, sys->sprint("Input error: %r\n"));
	else
		logmsg(top, ichan, "Got EOF\n");
	if (eflag)
		ichan <-= 0;
}

logmsg(top: ref Tk->Toplevel, ichan: chan of int, m: string)
{
	tk->cmd(top, ".t.t insert end '"+m);
	if (scrolling)
		tk->cmd(top, ".t.t see end");
	tk->cmd(top, "update");
	ichan <-= 1;
}