summaryrefslogtreecommitdiff
path: root/appl/cmd/ip/ppp/pppgui.b
blob: 40e7e3b43967a93a5482329881dbecd6e7eb5533 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#
# Copyright © 1998 Lucent Technologies Inc.  All rights reserved.
# Revisions copyright © 2000,2001 Vita Nuova Holdings Limited.  All rights reserved.
#
# Originally Written by N. W. Knauft
# Adapted by E. V. Hensbergen (ericvh@lucent.com)
# Further adapted by Vita Nuova
#

implement PPPGUI;

include "sys.m";
	sys: Sys;

include "draw.m";
	draw: Draw;

include "tk.m";
	tk: Tk;

include "tkclient.m";
	tkclient: Tkclient;

include "translate.m";
	translate: Translate;
	Dict: import translate;
	dict: ref Dict;

include "lock.m";
include "modem.m";
include "script.m";
include "pppclient.m";
	ppp: PPPClient;

include "pppgui.m";

#Screen constants
BBG: con "#C0C0C0";             # Background color for button
PBG: con "#808080";             # Background color for progress bar
LTGRN: con "#00FF80";           # Color for progress bar
BARW: con 216;			# Progress bar width
BARH: con " 9";			# Progress bar height
INCR: con 30;			# Progress bar increment size
N_INCR: con 7;			# Number of increments in progress bar width
BSIZE: con 25;			# Icon button size
ISIZE: con BSIZE + 4;		# Icon window size
DIALQUANTA : con 1000;
ICONQUANTA : con 5000;

#Globals
pppquanta := DIALQUANTA;

#Font
FONT: con "/fonts/lucidasans/unicode.6.font";

#Messages
stat_msgs := array[] of {
	"Initializing Modem",
	"Dialling Service Provider",
	"Logging Into Network",
	"Executing Login Script",
	"Script Execution Complete",
	"Logging Into Network",
	"Verifying Password",
	"Connected",
	"",
};

config_icon := array[] of {
	"button .btn -text X -width "+string BSIZE+" -height "+string BSIZE+" -command {send tsk open} -bg "+BBG,
	"pack .btn",

	"pack propagate . no",
	". configure -bd 0",
	". unmap",
	"update",
};


# Create internet connect window, spawn event handler
init(ctxt: ref Draw->Context, stat: chan of int, pppmod: PPPClient, args: list of string): chan of int
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	tk = load Tk Tk->PATH;
	tkclient = load Tkclient Tkclient->PATH;

	if (draw == nil || tk == nil || tkclient == nil) {
		sys->fprint(sys->fildes(2), "pppgui: can't load Draw or Tk: %r\n");
		return nil;
	}

	translate = load Translate Translate->PATH;
	if(translate != nil) {
		translate->init();
		dictname := translate->mkdictname("", "pppgui");
		dicterr: string;
		(dict, dicterr) = translate->opendict(dictname);
		if(dicterr != nil)
			sys->fprint(sys->fildes(2), "pppgui: can't open %s: %s\n", dictname, dicterr);
	}else
		sys->fprint(sys->fildes(2), "pppgui: can't load %s: %r\n", Translate->PATH);
	ppp = pppmod;		# set the global

	tkargs := "";

	if (args != nil) {
		tkargs = hd args;
		args = tl args;
	} else
		tkargs="-x 340 -y 4";

	tkclient->init();
		
	(t, wmctl) := tkclient->toplevel(ctxt, tkargs, "PPP", Tkclient->Plain);

	config_win := array[] of {
		"frame .f",
		"frame .fprog",

		"canvas .cprog -bg "+PBG+" -bd 2 -width "+string BARW+" -height "+BARH+" -relief ridge",	
		"pack .cprog -in .fprog -pady 6",

		"label .stat -text {"+X("Initializing connection...")+"} -width 164 -font "+FONT,
		"pack .stat -in .f -side left -fill y -anchor w",

		"button .done -text {"+X("Cancel")+"} -width 60 -command {send cmd cancel} -bg "+BBG+" -font "+FONT,
		"pack .fprog -side bottom -expand 1 -fill x",
		"pack .done -side right -padx 1 -pady 1 -fill y -anchor e",
		"pack .f -side left -expand 1 -padx 5 -pady 3 -fill both -anchor w",

		"pack propagate . no",
		". configure -bd 2 -relief raised -width "+string WIDTH,
		"update",
	};

	for(i := 0; i < len config_win; i++)
		tk->cmd(t, config_win[i]);

	itkargs := "";
	if (args != nil) {
		itkargs = hd args;
		args = tl args;
	}
	tkclient->onscreen(t, nil);
	tkclient->startinput(t, "ptr" :: nil);

	if (itkargs == "") {
		x := int tk->cmd(t, ". cget x");
		y := int tk->cmd(t, ". cget y");
		x += WIDTH - ISIZE;
		itkargs = "-x "+string x+" -y "+string y;
	}

	(ticon, iconctl) := tkclient->toplevel(ctxt, itkargs, "PPP", Tkclient->Plain);

	for( i = 0; i < len config_icon; i++)
		tk->cmd(ticon, config_icon[i]);

	tk->cmd(ticon, "image create bitmap Network -file network.bit -maskfile network.bit");
	tk->cmd(ticon, ".btn configure -image Network");
	tkclient->startinput(ticon, "ptr"::nil);

	chn := chan of int;
	spawn handle_events(t, wmctl, ticon, iconctl, stat, chn);
	return chn;
}

ppp_timer(sync: chan of int, stat: chan of int)
{
	for(;;) {
		sys->sleep(pppquanta);
		alt {
		<-sync =>
			return;
		stat <-= -1 =>
			;
		}
	}
}

send(cmd: chan of string, msg: string)
{
	cmd <-= msg;
}

# Process events and pass disconnect cmd to calling app
handle_events(t: ref Tk->Toplevel, wmctl: chan of string, ticon: ref Tk->Toplevel, iconctl: chan of string, stat, chn: chan of int)
{
	sys->pctl(Sys->NEWPGRP, nil);
	cmd := chan of string;
	tk->namechan(t, cmd, "cmd");

	tsk := chan of string;
	tk->namechan(ticon, tsk, "tsk");

	connected := 0;
	winmapped := 1;
	timecount := 0;
	xmin := 0;
	x := 0;

	iocmd := sys->file2chan("/chan", "pppgui");
	if (iocmd == nil) {
		sys->print("fail: pppgui: file2chan: /chan/pppgui: %r\n");
		return;
	}

	pppquanta = DIALQUANTA;
	sync_chan := chan of int;
	spawn ppp_timer(sync_chan, stat);

Work:
	for(;;) alt {
	s := <-t.ctxt.kbd =>
		tk->keyboard(t, s);

	s := <-t.ctxt.ptr =>
		tk->pointer(t, *s);

	s := <-t.ctxt.ctl or
	s = <-t.wreq or
	s = <-wmctl =>
		tkclient->wmctl(t, s);

	s := <-ticon.ctxt.kbd =>
		tk->keyboard(ticon, s);
	s := <-ticon.ctxt.ptr =>
		tk->pointer(ticon, *s);
	s := <-ticon.ctxt.ctl or
	s = <-ticon.wreq or
	s = <-iconctl =>
		tkclient->wmctl(ticon, s);

	(off, data, fid, wc) := <-iocmd.write =>	# remote io control
		if (wc == nil)
			break;
		spawn send(cmd, string data[0:len data]);
		wc <-= (len data, nil);

	(nil, nbytes, fid, rc) := <-iocmd.read =>
		if (rc != nil)
			rc <-= (nil, "not readable");

	press := <-cmd =>
		case press {
		"cancel" or "disconnect" =>
			tk->cmd(t, ".stat configure -text 'Disconnecting...");
			tk->cmd(t, "update");
			ppp->reset();
			if (!connected) {
				# other end may have gone away
				alt {
					chn <-= 666 => ;
					* => ;
				}
			}
			break Work;
		* => ;
		}

	prs := <-tsk =>
		case prs {
		"open" =>
			tk->cmd(ticon, ". unmap; update");
			tk->cmd(t, ". map; raise .; update");
			winmapped = 1;
			timecount = 0;
		* => ;
		}

	s := <-stat =>
		if (s == -1) {	# just an update event
			if(winmapped){
				if(!connected) {	# increment status bar
					if (x < xmin+INCR) {
						x++;
						tk->cmd(t, ".cprog create rectangle 0 0 "+string x + BARH+" -fill "+LTGRN);
					}
				}else{
					timecount++;
					if(timecount > 1){
						winmapped = 0;
						timecount = 0;
						tk->cmd(t, ". unmap; update");
						tk->cmd(ticon, ". map; raise .; update");
						continue;
					}
				}
				tk->cmd(t, "raise .; update");
			} else {
				tk->cmd(ticon, "raise .; update");
				timecount = 0;
			}
			continue;
		}
		if (s == ppp->s_Error) {
			tk->cmd(t, ".stat configure -text '"+ppp->lasterror);
			if (!winmapped) {
				tk->cmd(ticon, ". unmap; update");
				tk->cmd(t, ". map; raise .");
			}
			tk->cmd(t, "update");
			sys->sleep(3000);	
			ppp->reset();
			if (!connected)
				chn <-= 0;			# Failure	
			break Work;
		}
	
		if (s == ppp->s_Initialized)
			tk->cmd(t,".cprog create rectangle 0 0 "+string BARW + BARH+" -fill "+PBG);
		
		x = xmin = s * INCR;
		if (xmin > BARW)
			xmin = BARW;
		tk->cmd(t, ".cprog create rectangle 0 0 "+string xmin + BARH+" -fill "+LTGRN);
		tk->cmd(t, "raise .; update");
		tk->cmd(t, ".stat configure -text '"+X(stat_msgs[s]));

		if (s == ppp->s_SuccessPPP || s == ppp->s_Done) {
			if(!connected){
				chn <-= 1;
				connected = 1;
			}
			pppquanta = ICONQUANTA;

			# find and display connection speed
			speed := findrate("/dev/modemstat", "rcvrate" :: "baud" :: nil);
			if(speed != nil)
				tk->cmd(t, ".stat configure -text {"+X(stat_msgs[s])+" "+speed+" bps}");
			else
				tk->cmd(t, ".stat configure -text {"+X(stat_msgs[s])+"}");
			tk->cmd(t, ".done configure -text Disconnect -command 'send cmd disconnect");
			tk->cmd(t, "update");
			sys->sleep(2000);	
			tk->cmd(t, ". unmap; pack forget .fprog; update");
			winmapped = 0;
			tk->cmd(ticon, ". map; raise .; update");
		}

		tk->cmd(t, "update");
	}
	sync_chan <-= 1;	# stop ppp_timer
}

findrate(file: string, opt: list of string): string
{
	fd := sys->open(file, sys->OREAD);
	if(fd == nil)
		return nil;
	buf := array [1024] of byte;
	n := sys->read(fd, buf, len buf);
	if(n <= 1)
		return nil;
	(nil, flds) := sys->tokenize(string buf[0:n], " \t\r\n");
	for(; flds != nil; flds = tl flds)
		for(l := opt; l != nil; l = tl l)
			if (hd flds == hd l)
				return hd tl flds;
	return nil;
}



# Translate a string 

X(s : string) : string
{
	if (dict== nil) return s;
	return dict.xlate(s);
}