⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fgui.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
}/* *  free the controls for the confirmation window */static voidteardownconfirm(Request *r){	Controlset *cs;	cs = r->rt->cs;	r->rt->cs = nil;	hide();	closecontrolset(cs);	closekmr();}/* *  get user confirmation of a key */static voidconfirm(Request *r){	Channel *c;	char *s;	int n;	char *args[3];	int remember;	Attr *val;	Memory *m;	/* if it's something that the user wanted us not to ask again about */	m = searchmem(r->a);	if(m != nil){		fprint(r->rt->fd, "%A %A", r->tag, m->val);		return;	}	/* set up the controls */	c = setupconfirm(r);	/* wait for user to reply */	remember = 0;	for(;;){		s = recvp(c);		n = tokenize(s, args, nelem(args));		if(n==3 && strcmp(args[1], "value")==0){			if(strcmp(args[0], "b_remember:") == 0){				remember = atoi(args[2]);			}			if(strcmp(args[0], "b_accept:") == 0){				val = _mkattr(AttrNameval, "answer", "yes", nil);				free(s);				break;			}			if(strcmp(args[0], "b_refuse:") == 0){				val = _mkattr(AttrNameval, "answer", "no", nil);				free(s);				break;			}		}		free(s);	}	teardownconfirm(r);	fprint(r->rt->fd, "%A %A", r->tag, val);	if(remember)		addmem(_copyattr(r->a), val);	else		_freeattr(val);}/* *  confirmations that are remembered */static intmatch(Attr *a, Attr *b){	Attr *x;	for(; a != nil; a = a->next){		x = _findattr(b, a->name);		if(x == nil || strcmp(a->val, x->val) != 0)			return 0;	}	return 1;}static Memory*searchmem(Attr *a){	Memory *m;	for(m = mem; m != nil; m = m->next){		if(match(a, m->a))			break;	}	return m;}static voidaddmem(Attr *a, Attr *val){	Memory *m;	m = malloc(sizeof *m);	if(m == nil)		return;	m->a = a;	m->val = val;	m->next = mem;	mem = m;}/* controls for needkey */Control *msg;Control *b_done;enum {	Pprivate=	1<<0,	Pneed=		1<<1,};typedef struct Entry Entry;struct Entry {	Control *name;	Control *val;	Control *eq;	Attr *a;};static Entry *entry;static int entries;/* *  set up the controls for the confirmation window */static Channel*setupneedkey(Request *r){	Controlset *cs;	Channel *c;	Attr *a;	Attr **l;	char cn[10];	int i;	/* create a new control set for the confirmation */	openkmr();	cs = newcontrolset(screen, kbdc, mousec, resizec);	/* count attributes and allocate entry controls */	entries = 0;	for(l = &r->a; *l; l = &(*l)->next)		entries++;	if(entries == 0){		closecontrolset(cs);		closekmr();		return nil;	}	*l = a = mallocz(sizeof *a, 1);	a->type = AttrQuery;	entries++;	l = &(*l)->next;	*l = a = mallocz(sizeof *a, 1);	a->type = AttrQuery;	entries++;	entry = malloc(entries*sizeof(Entry));	if(entry == nil){		closecontrolset(cs);		closekmr();		return nil;	}	namectlimage(display->white, "i_white");	namectlimage(display->black, "i_black");	/* create controls */	msg = createtext(cs, "msg");	chanprint(cs->ctl, "msg image white");	chanprint(cs->ctl, "msg bordercolor white");	chanprint(cs->ctl, "msg add 'You need the following key.  Fill in the blanks'");	chanprint(cs->ctl, "msg add 'and click on the DONE button.'");	for(i = 0, a = r->a; a != nil; i++, a = a->next){		entry[i].a = a;		snprint(cn, sizeof cn, "name_%d", i);		if(entry[i].a->name == nil){			entry[i].name = createentry(cs, cn);			chanprint(cs->ctl, "%s image yellow", cn);			chanprint(cs->ctl, "%s border 1", cn);		} else {			entry[i].name = createtext(cs, cn);			chanprint(cs->ctl, "%s image white", cn);			chanprint(cs->ctl, "%s bordercolor white", cn);			chanprint(cs->ctl, "%s add '%s'", cn, a->name);		}		snprint(cn, sizeof cn, "val_%d", i);		if(a->type == AttrQuery){			entry[i].val = createentry(cs, cn);			chanprint(cs->ctl, "%s image yellow", cn);			chanprint(cs->ctl, "%s border 1", cn);			if(a->name != nil){				if(strcmp(a->name, "user") == 0)					chanprint(cs->ctl, "%s value %q", cn, getuser());				if(*a->name == '!')					chanprint(cs->ctl, "%s font invisible", cn);			}		} else {			entry[i].val = createtext(cs, cn);			chanprint(cs->ctl, "%s image white", cn);			chanprint(cs->ctl, "%s add %q", cn, a->val);		}		snprint(cn, sizeof cn, "eq_%d", i);		entry[i].eq = createtext(cs, cn);		chanprint(cs->ctl, "%s image white", cn);		chanprint(cs->ctl, "%s add ' = '", cn);	}	b_done = createtextbutton(cs, "b_done");	chanprint(cs->ctl, "b_done border 1");	chanprint(cs->ctl, "b_done align center");	chanprint(cs->ctl, "b_done text DONE");	chanprint(cs->ctl, "b_done image green");	chanprint(cs->ctl, "b_done light green");	/* wire controls for input */	c = chancreate(sizeof(char*), 0);	controlwire(b_done, "event", c);	for(i = 0; i < entries; i++)		if(entry[i].a->type == AttrQuery)			controlwire(entry[i].val, "event", c);	/* make the controls interactive */	activate(msg);	activate(b_done);	for(i = 0; i < entries; i++){		if(entry[i].a->type != AttrQuery)			continue;		if(entry[i].a->name == nil)			activate(entry[i].name);		activate(entry[i].val);	}	/* change the display */	r->rt->cs = cs;	unhide();	resizecontrolset(cs);	return c;}/* *  resize the controls for the confirmation window */static voidresizeneedkey(Controlset *cs){	Rectangle r, mr;	int mid, i, n, lasty;	/* get usable window rectangle */	if(getwindow(display, Refnone) < 0)		ctlerror("resize failed: %r");	r = insetrect(screen->r, 10);	/* find largest name */	mid = 0;	for(i = 0; i < entries; i++){		if(entry[i].a->name == nil)			continue;		n = strlen(entry[i].a->name);		if(n > mid)			mid = n;	}	mid = (mid+2) * font->height;	/* top line is the message */	mr = r;	mr.max.y = mr.min.y + 2*font->height + 2;	chanprint(cs->ctl, "msg rect %R\nmsg show", mr);	/* one line per attribute */	mr.min.x += 2*font->height;	lasty = mr.max.y;	for(i = 0; i < entries; i++){		r.min.x = mr.min.x;		r.min.y = lasty+2;		r.max.x = r.min.x + mid - 3*stringwidth(font, "=");		r.max.y = r.min.y + font->height;		chanprint(cs->ctl, "name_%d rect %R\nname_%d show", i, r, i);		r.min.x = r.max.x;		r.max.x = r.min.x + 3*stringwidth(font, "=");		chanprint(cs->ctl, "eq_%d rect %R\neq_%d show", i, r, i);		r.min.x = r.max.x;		r.max.x = mr.max.x;		if(Dx(r) > 32*font->height)			r.max.x = r.min.x + 32*font->height;		chanprint(cs->ctl, "val_%d rect %R\nval_%d show", i, r, i);		lasty = r.max.y;	}	/* done button */	mr.min.x -= 2*font->height;	r.min.x = mr.min.x + mid - 3*font->height;	r.min.y = lasty+10;	r.max.x = r.min.x + 6*font->height;	r.max.y = r.min.y + font->height + 2;	chanprint(cs->ctl, "b_done rect %R\nb_done show", r);}/* *  free the controls for the confirmation window */static voidteardownneedkey(Request *r){	Controlset *cs;	cs = r->rt->cs;	r->rt->cs = nil;	hide();	closecontrolset(cs);	closekmr();	if(entry != nil)		free(entry);	entry = nil;}static voidneedkey(Request *r){	Channel *c;	char *nam, *val;	int i, n;	int fd;	char *args[3];	/* set up the controls */	c = setupneedkey(r);	if(c == nil)		goto out;	/* wait for user to reply */	for(;;){		val = recvp(c);		n = tokenize(val, args, nelem(args));		if(n==3 && strcmp(args[1], "value")==0){	/* user hit 'enter' */			free(val);			break;		}		free(val);	}	/* get entry values */	for(i = 0; i < entries; i++){		if(entry[i].a->type != AttrQuery)			continue;		chanprint(r->rt->cs->ctl, "val_%d data", i);		val = recvp(entry[i].val->data);		if(entry[i].a->name == nil){			chanprint(r->rt->cs->ctl, "name_%d data", i);			nam = recvp(entry[i].name->data);			if(nam == nil || *nam == 0){				free(val);				continue;			}			entry[i].a->val = estrdup(val);			free(val);			entry[i].a->name = estrdup(nam);			free(nam);		} else {			if(val != nil){				entry[i].a->val = estrdup(val);				free(val);			}		}		entry[i].a->type = AttrNameval;	}	/* enter the new key !!!!need to do something in case of error!!!! */	fd = open("/mnt/factotum/ctl", OWRITE);	fprint(fd, "key %A", r->a);	close(fd);	teardownneedkey(r);out:	fprint(r->rt->fd, "%A", r->tag);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -