From: Sergey Reva <rs_rlab@mail.ru>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>
Subject: [9fans] thread, utf
Date: Tue, 17 May 2005 15:53:14 +0300 [thread overview]
Message-ID: <10520507828.20050517155314@mail.ru> (raw)
[-- Attachment #1: Type: text/plain, Size: 239 bytes --]
Hello Fans,
Does I am on a right way of using thread library?
How can I convert Rune string to Utf-8 sequence? I can't find standard
function...
Sergey
--
http://rs-rlab.narod.ru mailto:rs_rlab@mail.ru
[-- Attachment #2: thrd.c --]
[-- Type: APPLICATION/OCTET-STREAM, Size: 6464 bytes --]
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
enum {
Entry=0,
Button,
Radio,
MaxEntryText=50,
MaxButText=50,
ButtonExit=0,
EntryBox1,
RadioPlus,
RadioMinus,
RadioMul,
RadioDiv,
EntryBox2,
ButtonEval,
EntryResult,
WidgEnd,
OpAdd=0,
OpSub,
OpMul,
OpDiv,
AButtons=0,
AResize,
AEnd
};
typedef struct BUT BUT;
struct BUT {
int id;
int grp;
int type;
Rune* txt;
int aux;
int x;
int y;
int cx;
int cy;
void (*pclick)(BUT *b, int but);
void (*pdraw)(BUT *b);
void (*pkbd)(BUT *b, Rune r);
};
BUT widg[];
BUT *focus=nil;
Channel *bc, *redraw;
Rune Entry1[MaxEntryText]={L"1"};
Rune Entry2[MaxEntryText]={L"2"};
Rune Entry3[MaxEntryText]={L""};
/////////////////////////Controls//////////////////////////
void kbddup(BUT *b, Rune r)
{
}
void clickdup(BUT *b, int but)
{
}
void drawradio(BUT *b)
{
Rectangle r;
r.min.x=b->x+screen->r.min.x;
r.min.y=b->y+screen->r.min.y;
r.max.x=r.min.x+b->cy;
r.max.y=r.min.y+b->cy;
draw(screen,r,display->white,display->opaque,ZP);
if (b->aux)
draw(screen,r,display->black,display->opaque,ZP);
else
border(screen,r,1,display->black,ZP);
r.max.y=r.min.y;
r.max.x+=2;
runestringn(screen, r.max, display->black, ZP, font, b->txt, MaxEntryText);
}
void clickradio(BUT *b, int but)
{
int i;
if (!(but & 1))
return;
if (b->aux)
return;
for (i=0;i<WidgEnd;i++)
if ((widg[i].grp==b->grp) && (widg[i].type=Radio))
widg[i].aux=0;
b->aux=1;
sendul(redraw,WidgEnd);
}
void drawentry(BUT *b)
{
Rectangle r;
r.min.x=b->x+screen->r.min.x;
r.min.y=b->y+screen->r.min.y;
r.max.x=r.min.x+b->cx;
r.max.y=r.min.y+b->cy;
draw(screen,r,display->white,display->opaque,ZP);
border(screen,r,1,display->black,ZP);
r.min.x+=2;
runestringn(screen, r.min, display->black, ZP, font, b->txt, MaxEntryText);
}
void kbdentry(BUT *b, Rune r)
{
switch(r)
{
case 8:
b->aux--;
if (b->aux<0)
b->aux=0;
b->txt[b->aux]=0;
break;
default:
if (r>31)
{
b->txt[b->aux]=r;
b->aux++;
b->txt[b->aux]=0;
}
break;
}
sendul(redraw,b->id);
}
void drawbutton(BUT *b)
{
Rectangle r;
r.min.x=b->x+screen->r.min.x;
r.min.y=b->y+screen->r.min.y;
r.max.x=r.min.x+b->cx;
r.max.y=r.min.y+b->cy;
draw(screen,r,display->white,display->opaque,ZP);
border(screen,r,1,display->black,ZP);
r.min.x+=2;
runestringn(screen, r.min, display->black, ZP, font, b->txt, MaxButText);
}
void clickbutton(BUT *b, int but)
{
if (but & 1)
sendul(bc, b->id);
}
/////////////////Main program//////////////
void graphthread(void*)
{
ulong id;
for(;;)
{
id=recvul(redraw);
if (id<WidgEnd)
widg[id].pdraw(&widg[id]);
else
for (id=0;id<WidgEnd;id++)
{
widg[id].pdraw(&widg[id]);
}
flushimage(display,1);
}
}
void keyboardthread(void* v)
{
Rune kbdc;
Channel *c=(Channel*)v;
for(;;)
{
recv(c,&kbdc);
if ((kbdc!=Runeerror) && (focus!=nil))
focus->pkbd(focus,kbdc);
}
}
void mousethread(void *v)
{
int i;
Rectangle r;
Mouse m;
Channel *c=(Channel*)v;
for(;;)
{
recv(c,&m);
if (m.buttons)
for (i=0;i<WidgEnd;i++)
{
r.min.x=screen->r.min.x+widg[i].x;
r.min.y=screen->r.min.y+widg[i].y;
r.max.x=r.min.x+widg[i].cx;
r.max.y=r.min.y+widg[i].cy;
if (ptinrect(m.xy,r))
{
focus=&widg[i];
focus->pclick(focus, m.buttons);
break;
}
}
}
}
void runetocharcpy(char *c, Rune *r)
{
int n=0,i;
for (i=0;r[i]!=0;i++)
n+=runetochar(&c[n],&r[i]);
c[n]=0;
}
void Evaluate(Rune* result, Rune* op1, Rune* op2, int op)
{
char uop1[MaxEntryText*2];
char uop2[MaxEntryText*2];
double res;
runetocharcpy(uop1,op1);
runetocharcpy(uop2,op2);
switch(op)
{
case OpAdd:
res=atof(uop1)+atof(uop2);
break;
case OpSub:
res=atof(uop1)-atof(uop2);
break;
case OpMul:
res=atof(uop1)*atof(uop2);
break;
case OpDiv:
res=atof(uop1)/atof(uop2);
break;
}
runesprint(result,"%f",res);
}
void threadmain(int, char**)
{
Mousectl *mc;
Keyboardctl *kc;
ulong but,i;
Alt alts[AEnd+1]={
[AButtons] {nil,&but,CHANRCV},
[AResize] {nil,nil,CHANRCV},
[AEnd] {nil,nil,CHANEND}
};
initdraw(0,0,"Alt example");
mc=initmouse(nil,screen);
kc=initkeyboard(nil);
bc=chancreate(sizeof(ulong),0);
redraw=chancreate(sizeof(ulong),0);
alts[AButtons].c=bc;
alts[AResize].c=mc->resizec;
threadcreate(graphthread, nil, 8192);
threadcreate(mousethread, (void*)mc->c, 8192);
threadcreate(keyboardthread, (void*)kc->c, 8192);
send(mc->resizec,nil);
for (;;)
switch(alt(alts))
{
case AButtons:
switch(but)
{
case ButtonEval:
Evaluate(widg[EntryResult].txt,widg[EntryBox1].txt,widg[EntryBox2].txt,
((widg[RadioPlus].aux) || (widg[RadioMinus].aux)) ?
((widg[RadioPlus].aux) ? OpAdd : OpSub)
:
((widg[RadioMul].aux) ? OpMul : OpDiv));
sendul(redraw,EntryResult);
break;
case ButtonExit:
chanfree(bc);
chanfree(redraw);
threadexitsall(0);
break;
}
break;
case AResize:
if(getwindow(display, Refnone) < 0)
sysfatal("resize failed: %r");
for(i=0;i<WidgEnd;i++)
if (i!=ButtonExit)
{
if (widg[i].grp!=1)
widg[i].cx=screen->r.max.x-screen->r.min.x-4;
}
else
widg[i].x=screen->r.max.x-screen->r.min.x-4-widg[i].cx;
sendul(redraw,WidgEnd);
break;
}
}
BUT widg[]={
[ButtonExit] {ButtonExit, 0,Button, L"X", 0, 2,0,16,16, clickbutton, drawbutton, kbddup},
[EntryBox1] {EntryBox1, 0,Entry, Entry1, 1, 2,18,50,16, clickdup, drawentry, kbdentry},
[RadioPlus] {RadioPlus, 1,Radio, L"+", 1, 2,36,40,16, clickradio, drawradio, kbddup},
[RadioMinus] {RadioMinus, 1,Radio, L"-", 0, 2,54,40,16, clickradio, drawradio, kbddup},
[RadioMul] {RadioMul, 1,Radio, L"*", 0, 42,36,40,16, clickradio, drawradio, kbddup},
[RadioDiv] {RadioDiv, 1,Radio, L"/", 0, 42,54,40,16, clickradio, drawradio, kbddup},
[EntryBox2] {EntryBox2, 0,Entry, Entry2, 1, 2,72,50,16, clickdup, drawentry, kbdentry},
[ButtonEval] {ButtonEval, 0,Button, L"=", 0, 2,90,50,16, clickbutton, drawbutton, kbddup},
[EntryResult] {EntryResult, 0,Entry, Entry3, 0, 2,108,50,16, clickdup, drawentry, kbdentry},
};
next reply other threads:[~2005-05-17 12:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-05-17 12:53 Sergey Reva [this message]
2005-05-17 13:28 ` [9fans] Question on dial() I RATTAN
2005-05-17 14:04 ` Russ Cox
2005-05-17 14:19 ` I RATTAN
2005-05-17 15:35 ` Robert Raschke
2005-05-17 16:04 ` Christoph Lohmann
2005-05-17 16:17 ` "Nils O. Selåsdal"
2005-05-17 14:04 ` [9fans] thread, utf Russ Cox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=10520507828.20050517155314@mail.ru \
--to=rs_rlab@mail.ru \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).