Annotation of researchv10no/libj/io.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sgtty.h>
        !             3: #include "/usr/jerq/include/jioctl.h"
        !             4: #include "jerq.h"
        !             5: #include "io.h"
        !             6: #include "balloc.h"
        !             7: #include "commands.h"
        !             8: 
        !             9: extern int lookUpFont();
        !            10: extern Font *fntab[];
        !            11: extern Font *loadFont();
        !            12: 
        !            13: Rectangle Jrect, Drect;
        !            14: int Jin, Jout;
        !            15: 
        !            16: #define mouseQsize 20
        !            17: Mouse mouseQ[mouseQsize];
        !            18: int Qnum = 0;
        !            19: int lookMouse = 0; nextMouse = 0;
        !            20: 
        !            21: struct sgttyb ttyisave, ttyosave, ttymodes;
        !            22: 
        !            23: int jinit()
        !            24: {      
        !            25:        if (system("32ld /usr/jerq/lib/term.m")) {
        !            26:                fprintf(stderr,"main: can't boot terminal program\n");
        !            27:                return(0);
        !            28:        }
        !            29:        Jin = open("/dev/tty",0);
        !            30:        Jout = open("/dev/tty",1);
        !            31:        if (Jin<0 | Jout <0) {
        !            32:                fprintf(stderr,"couldn't open /dev/tty\n");
        !            33:                return(0);
        !            34:        }
        !            35:        ioctl(Jin, TIOCGETP, &ttymodes);
        !            36:        ttyisave = ttymodes;
        !            37:        ttymodes.sg_flags |= RAW;
        !            38:        ttymodes.sg_flags &= ~ECHO;
        !            39:        ioctl(Jin, TIOCSETP, &ttymodes);
        !            40: 
        !            41:        ioctl(Jout, TIOCGETP, &ttymodes);
        !            42:        ttyosave = ttymodes;
        !            43:        ttymodes.sg_flags |= RAW;
        !            44:        ttymodes.sg_flags &= ~ECHO;
        !            45:        ioctl(Jout, TIOCSETP, &ttymodes);
        !            46: 
        !            47:        Jrect.origin.x = Jrect.origin.y = 0;
        !            48:        Jrect.corner.x = XMAX; Jrect.corner.y = YMAX;
        !            49:        Drect.origin.x = recInt();
        !            50:        Drect.origin.y = recInt();
        !            51:        Drect.corner.x = recInt();
        !            52:        Drect.corner.y = recInt();
        !            53:        BMT[0].rect = Drect;
        !            54: 
        !            55:        fntab[0] = loadFont("/usr/jerq/font/defont");
        !            56:        return(1);
        !            57: }
        !            58: 
        !            59: #define BUFsize 32
        !            60: char obuf[BUFsize], ibuf[BUFsize];
        !            61: int pobuf = 0, pibuf = 0, pinum = 0;
        !            62: 
        !            63: void flush()
        !            64: {
        !            65:        if (pobuf>0) write(Jout,obuf,pobuf);
        !            66:        pobuf = 0;
        !            67: }
        !            68:        
        !            69: put(c) char c;
        !            70: {
        !            71:        if (pobuf == BUFsize) flush();
        !            72:        obuf[pobuf++] = c;
        !            73: }
        !            74: 
        !            75: char get()
        !            76: {
        !            77:        if (pibuf==pinum) {
        !            78:                pinum = read(Jin,ibuf,BUFsize);
        !            79:                pibuf = 0;
        !            80:        }
        !            81:        return ibuf[pibuf++];
        !            82: }
        !            83:        
        !            84: void sendChar(c) char c;
        !            85: {
        !            86:        put(c);
        !            87: }
        !            88: 
        !            89: void sendInt(n) int n;
        !            90: {
        !            91:        if (n<0) {
        !            92:                put('-');
        !            93:                n = -n;
        !            94:        }
        !            95:        for ( ; n != 0; n >>= 4) put((n&15)+'A');
        !            96:        put(' ');
        !            97: }
        !            98: 
        !            99: 
        !           100: void sendPoint(p) Point p;
        !           101: {
        !           102:        sendInt(p.x);
        !           103:        sendInt(p.y);
        !           104: }
        !           105: 
        !           106: void sendRectangle(r) Rectangle r;
        !           107: {
        !           108:        sendInt(r.origin.x);
        !           109:        sendInt(r.origin.y);
        !           110:        sendInt(r.corner.x);
        !           111:        sendInt(r.corner.y);
        !           112: }
        !           113: 
        !           114: void sendToBitmap(b) Bitmap *b;
        !           115: {
        !           116:        if (b<BMT || b>BMT+BMTsize || b->jname<0) 
        !           117:                fprintf(stderr,"inappropriate bitmap\n");
        !           118:        else sendInt(b->jname);
        !           119: }
        !           120: 
        !           121: void sendToFont(f) Font *f;
        !           122: {
        !           123:        sendInt(lookUpFont(f));
        !           124: }
        !           125: 
        !           126: 
        !           127: #define TXTsize 20
        !           128: Texture *txp[TXTsize];
        !           129: Texture txtab[TXTsize];
        !           130: 
        !           131: void sendTexture(i,t) int i; Texture *t;
        !           132: {      int j;
        !           133:        sendInt(i);
        !           134:        txp[i] = t;
        !           135:        txtab[i] = *t;
        !           136:        for (j=0; j<16; j++) sendInt(t->bits[j]);
        !           137: }
        !           138: #define TNULL (Texture *)NULL
        !           139:                
        !           140: void sendToTexture(t) Texture *t;
        !           141: {      int i, j;
        !           142:        if (t==TNULL) {
        !           143:                sendInt(-1);
        !           144:                sendInt(-1);
        !           145:        }
        !           146:        else {
        !           147:                for (i=0; i<TXTsize && txp[i]!=TNULL && txp[i]!=t; i++) ;
        !           148:                if (i>=TXTsize) {
        !           149:                        fprintf(stderr,"out of space in TXTAB\n");
        !           150:                        exit();
        !           151:                }
        !           152:                else if (txp[i]==TNULL) sendTexture(i,t);
        !           153:                else {
        !           154:                        for (j=0; j<16 && t->bits[j]==txp[i]->bits[j]; j++) ;
        !           155:                        if (j==16) {
        !           156:                                sendInt(-1);
        !           157:                                sendInt(i);
        !           158:                        }
        !           159:                        else sendTexture(i,t);
        !           160:                }
        !           161:        }
        !           162: }
        !           163: 
        !           164: void sendToChar(str) char *str;
        !           165: {      int i = 0;
        !           166:        do { put(str[i]); } while (str[i++] != 0); 
        !           167: }
        !           168: 
        !           169: void sendToToChar(cpp) char **cpp;
        !           170: {
        !           171:        sendToChar(*cpp);
        !           172: }
        !           173: 
        !           174: void sendUnsignedInt(n) unsigned int n;
        !           175: {
        !           176:        for ( ; n != 0; n >>= 4) put((n&15)+'A');
        !           177:        put(' ');
        !           178: }
        !           179: 
        !           180: Point recPoint()
        !           181: {      Point p;
        !           182:        p.x = recInt();
        !           183:        p.y = recInt();
        !           184:        return(p);
        !           185: }
        !           186: 
        !           187: Rectangle recRectangle()
        !           188: {      Rectangle r;
        !           189:        r.origin.x = recInt();
        !           190:        r.origin.y = recInt();
        !           191:        r.corner.x = recInt();
        !           192:        r.corner.y = recInt();
        !           193:        return(r);
        !           194: }
        !           195: 
        !           196: int recChar()
        !           197: {      int c;
        !           198:        return get();
        !           199: }
        !           200: 
        !           201: int recInt()
        !           202: {      char c; 
        !           203:        register n, i;
        !           204:        int sign = 1;
        !           205:        
        !           206:        if ((c = get()) == '-') {
        !           207:                sign = -1;
        !           208:                c = get();
        !           209:        }
        !           210:        for (n=0, i=0; (c >= 'A' && c <= 'P'); c = get(), i+=4) 
        !           211:                n += (c - 'A')<<i;
        !           212:        return (n * sign);
        !           213: }
        !           214: 
        !           215: Texture *recToTexture()
        !           216: {      int t;
        !           217:        t = recInt();
        !           218:        return(&txtab[t]);
        !           219: }
        !           220: 
        !           221: void readMouse()
        !           222: {      int i, j;
        !           223:        sendCommand(CCGETMOUSE);
        !           224:        flush();
        !           225:        Qnum = recInt();
        !           226:        for (j=0; j<Qnum; j++) {
        !           227:                for (mouseQ[j].xy.x=0, i=0; i<4*3; i+=4) 
        !           228:                        mouseQ[j].xy.x |= (get() << i);
        !           229:                for (mouseQ[j].xy.y=0, i=0; i<4*3; i+=4) 
        !           230:                        mouseQ[j].xy.y |= (get() << i);
        !           231:                mouseQ[j].buttons = get();
        !           232:        }
        !           233:        get(); /* dump the carriage return */
        !           234: 
        !           235: }
        !           236: 
        !           237: clearMouseQ()
        !           238: {
        !           239:        Qnum = 0;
        !           240: }
        !           241: 
        !           242: Point mousexy()
        !           243: {
        !           244:        if (nextMouse >= Qnum) {
        !           245:                readMouse();
        !           246:                lookMouse = nextMouse = 0;
        !           247:        }
        !           248:        lookMouse  = 0;
        !           249:        return(mouseQ[nextMouse++].xy);
        !           250: }
        !           251: 
        !           252: int mousebuttons()
        !           253: {      
        !           254:        if (lookMouse) nextMouse++;
        !           255:        if (nextMouse >= Qnum) {
        !           256:                readMouse();
        !           257:                lookMouse = nextMouse = 0;
        !           258:        }
        !           259:        lookMouse = 1;
        !           260:        return(mouseQ[nextMouse].buttons);
        !           261: }
        !           262: 
        !           263: int kbdbuf[50];
        !           264: int kbdp = 0;
        !           265: int kbdn = 0;
        !           266: 
        !           267: int kbdchar()
        !           268: {      int c;
        !           269:        if (kbdp >= kbdn) {
        !           270:                sendCommand(CCKBDCHAR);
        !           271:                flush();
        !           272:                kbdn = kbdp = 0;
        !           273:                while ((c = recInt()) != -1) kbdbuf[kbdn++] = c;
        !           274:        }
        !           275:        return((kbdn == 0)? -1 : kbdbuf[kbdp++]);       
        !           276: }
        !           277:                
        !           278: void jexit()
        !           279: {      struct sgttyb buf;
        !           280:        sendCommand(CCEXIT);
        !           281:        flush();
        !           282:        ioctl(Jin, TIOCSETP, &ttyisave);
        !           283:        ttyosave.sg_flags &= ~RAW;
        !           284:        ttyosave.sg_flags |= ECHO;
        !           285:        ioctl(Jout, TIOCSETP, &ttyosave);
        !           286:        ioctl(Jout, JTERM, &ttyosave);
        !           287:        close(Jin);
        !           288:        close(Jout);
        !           289:        exit();
        !           290: }
        !           291:        

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.