// xchars interface,it just does a redraw on the scroll buffer.
//
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#define COMMON_MAPS
#include "/home/so/maps.h"
#define SCROLLBUF 0x800
#define SCROLLMAX 0x600
#define SCROLLMASK 0x7ff
char scroll_buff[SCROLLBUF ];
int scroll_index=0;
int empty_fd;
int flush_fd;
#define add(a,b) a = (a+b) & SCROLLMASK
#define incr(a) a = (a+1) & SCROLLMASK
#define decr(a) a = (a+1) & SCROLLMASK
char * FlushDev = "default";
#define FLUSH_FNAME "//home/so/flusher"
#define FLUSH_VERSION " --- Flush 1.0.2 --"
EntryType PrintReturn;
EntryType * FlushReturn;
EntryType * ExecCommand;
char char_buff[10];
char * ItoA(int x);
int Init(int * argc,Command * c) {
char out[20];
char in[20];
int i,j,k,l;
strcpy(out,"Success");
if((i= mkfifo(FLUSH_FNAME, O_RDWR | O_NONBLOCK | O_CREAT)) < 0) {
puts(ItoA(i));
perror("Fifo:\n");
}
empty_fd = open(FLUSH_FNAME, O_RDONLY | O_NONBLOCK);
flush_fd = open(FLUSH_FNAME, O_WRONLY | O_NONBLOCK);
puts("FD: "); puts(ItoA(flush_fd)); puts(ItoA(empty_fd));
fcntl(flush_fd,F_SETFL ,O_NONBLOCK);
i = write(flush_fd,out,strlen(out));
ioctl(flush_fd, FIONREAD, &j);
k = read(empty_fd,in,20);
ioctl(flush_fd, FIONREAD, &l);
printf(" %d %d %d %d\n",i,j,k,l);
puts(FLUSH_VERSION);
ExecCommand = c->arg1.entry;
if(!c->arg2.src)
FlushReturn =PrintReturn;
else
{
if(!(FlushReturn = arg_key(c->arg2).entry))
FlushReturn = PrintReturn;
else if(c->arg3.src)
FlushDev = c->arg3.src;
}
*argc+=3;
return(SUCCESS);
}
int FlushFifo() {
Scalar s;
int i = SCROLLBUF- scroll_index;
int l;
char temp[80];
do {
l = read(empty_fd,temp,80);
if(l > 0) {// make room
for(i=0;i < scroll_index;i++)
scroll_buff[scroll_index+l-i]
= scroll_buff[scroll_index-i;]
add(scroll_index,l);
memcpy(scroll_buff,temp,l);
scroll_buff[scroll_index]=0;
}
} while(l > 0);
s.cmd.src ="ReDraw";
s.key.src = FlushDev;
s.ptr=scroll_buff; //
if(s.ptr && FlushReturn)
FlushReturn(&base,(void **) &s);
return(SUCCESS);
}
int WriteFifo(int * argc, void * args[]) {
(*argc)++;
while(args[*argc] && ((char*) args[*argc])[0] !=';') {
write(flush_fd,args[*argc],strlen(args[*argc]));
*argc +=1;
}
return(SUCCESS);
}
int flush(int * argc,void *args[])
{
Command * c = (Command * ) (args + *argc);
if(!strcmp(args[*argc],"Init")) return(Init(argc,c));
if(!strcmp(args[*argc],"Write")) return(WriteFifo(argc,args));
if(!strcmp(args[*argc],"Flush")) {(*argc)++;return(FlushFifo());}
}
return(NOTFOUND);
}
int PrintReturn(int * argc, void * args[]){
int i;
XYString * p= (XYString *) (args + (*argc));
ioctl(flush_fd, FIONREAD, &i);
printf("Len %d x %ld y %ld ",i,(long) p->x, (long) p->y);
puts(p->cmd.src);
puts("\n");
while(p && *p->ptr != '\n'){
putchar(*p->ptr);
p->ptr++;
}
putchar(*p->ptr);
return(SUCCESS);
}
#ifdef TEST
char * InitString =
int main() {
void * args[10];
int argc=0;
char * args[]= {"Init",PrintReturn,"xhars", "flusher"};
Command * c = (Command *) args;
memset(scroll_buff,0,sizeof(scroll_buff));
Init(&argc,args);
FlushReturn = PrintReturn;
printf("%d\n",(int) write(flush_fd,"Lin1\n",5));
printf("%d\n",(int)write(flush_fd,"Lin2\n",5));
printf("%d\n",(int)write(flush_fd,"Lin3\n",5));
printf("%d\n",(int)write(flush_fd,"Lin4\n",5));
Flush(&argc,args);
close(empty_fd);
close(flush_fd);
puts("FlushText");
putchar('\n');
FlushText();
}
#endif
// putting ints to string
char * ItoA(int x) {
char * n="0123456789";
char* str=char_buff;
int i=0;
int k;
while(x) {
for(k=i;k > 0;k--) str[k]=str[k-1];
k = x % 10;
str[0]= n[k];
x/=10;
i++;
}
str[i]=0;
return(str);
}
No comments:
Post a Comment