#include #include #include #include #include #include #define BUFFERSIZE 20 #define WIDTH 200 #define HIEGHT 200 int charwidth, charascent, chardescent; char buffer[BUFFERSIZE]; int bufsize=BUFFERSIZE; KeySym keysym; long statinout; int charcount; XFontStruct *font_info; Display *display; Window root, window; GC draw, erase; main(argc,argv) int argc; char **argv; { long fgcolor, bgcolor; int screen, pointx, pointy; long eventmask = ExposureMask|KeyPressMask; XEvent event; XGCValues gcval; if (!(display = XOpenDisplay(argv[1]))) { perror("XOpenDisplay"); exit(1); } root = RootWindow(display,screen = DefaultScreen(display)); fgcolor = BlackPixel(display,screen); bgcolor = WhitePixel(display,screen); window = XCreateSimpleWindow(display,root,0,0,WIDTH,HIEGHT,2, fgcolor,bgcolor); gcval.foreground = fgcolor; gcval.background = bgcolor; draw = XCreateGC(display,window,GCForeground|GCBackground,&gcval); gcval.foreground = bgcolor; gcval.background = fgcolor; erase= XCreateGC(display,window,GCForeground|GCBackground,&gcval); load_font(); defineCtrl(); XSelectInput(display,window,eventmask); XMapWindow(display,window); for (;;) { XWindowEvent(display,window,eventmask,&event); switch (event.type) { case Expose: XClearWindow(display,window); break; case KeyPress: charcount = XLookupString(&event,buffer,bufsize, &keysym, &statinout); buffer[charcount]='\0'; if (strcmp (buffer, "quit") == 0 ) exit(0); else if (charcount == 1){ if (keysym >= XK_space && keysym <= XK_asciitilde) PutChar (buffer); else if (keysym == XK_BackSpace || keysym ==XK_Delete) DelChar(buffer); } break; default: fprintf(stderr,"Unexpected event: %d\n",event.type); } } } load_font() { char *fontname = "9x15"; if ((font_info = XLoadQueryFont(display, fontname)) == NULL) { (void) fprintf(stderr,"Could not get font\n"); exit( -1 ); } XSetFont(display, draw, font_info->fid); charwidth = font_info -> max_bounds.width; charascent = font_info -> max_bounds.ascent; chardescent = font_info -> max_bounds.descent; } defineCtrl() { KeySym modlist[2]; unsigned int list_len = 1; modlist[0] = XK_Control_L; XRebindKeysym(display,XK_q,modlist,list_len,"quit",4); } PutChar(buffer) char buffer[]; { int winx, winy; int rwinx, rwiny; unsigned int maskret; Window rootret, childret; int j; XQueryPointer(display, window, &rootret, &childret, &rwinx, &rwiny, &winx, &winy, &maskret); if (winx >= WIDTH-8 || winy >= HIEGHT-8) XBell(display,0); XDrawString(display,window,draw,winx,winy,buffer,strlen(buffer)); XWarpPointer(display, NULL, NULL, 0,0,0,0,charwidth,0); } DelChar( buffer) char buffer[]; { int winx, winy; int rwinx, rwiny; unsigned int maskret; Window rootret, childret; int j; XWarpPointer(display, NULL, NULL, 0,0,0,0,-charwidth,0); XQueryPointer(display, window, &rootret, &childret, &rwinx, &rwiny, &winx, &winy, &maskret); XFillRectangle(display,window,erase,winx, winy-charascent,charwidth,charascent+chardescent); }