#include #include #include #include #include #include #include #include // #include "gs.xpm" Display *display; Window win, child, root; XEvent event; GC gc, gc2; int screen, depth, white, black, x = 0, y = 0, x1 = 0, y1 = 0, get = 0, tx = 10, ty = 100, cursor = 0, a = 0, i; Cursor pencil; KeySym key; XFontStruct *font; XTextProperty title; Pixmap pixmap; XColor color; Colormap colormap; char keyc[100], *thetitle = "char *thetitle = \"char *the..."; int returncolor(int r, int g, int b); int main(int argc, char *argv[]) { cursor = 86; memset(keyc, 0, sizeof(keyc)); display = XOpenDisplay(getenv("DISPLAY")); if (!display) { fprintf(stderr, "%s: unable to open display %s\n", argv[0], getenv("DISPLAY")); return 1; } else printf("%s: running on display %s\n", argv[0], getenv("DISPLAY")); screen = DefaultScreen(display); root = RootWindow(display, screen); colormap = DefaultColormap(display, screen); depth = DefaultDepth(display, screen); white = WhitePixel(display, screen); black = BlackPixel(display, screen); win = XCreateSimpleWindow(display, root, 0, 0, 320, 240, 0, white, returncolor(0xaaaa, 0xaaaa, 0xaaaa)); child = XCreateSimpleWindow(display, win, 0, 0, 100, 100, 0, white, white); gc = XCreateGC(display, win, 0, 0); gc2 = XCreateGC(display, child, 0, 0); XSetForeground(display, gc, black); XSetForeground(display, gc2, black); XStringListToTextProperty(&thetitle, 1, &title); XSetWMName(display, win, &title); font = XLoadQueryFont(display, "*helveti*med*-r*12*"); if (!font) perror("XLoadQueryFont"); XSetFont(display, gc, font->fid); pencil = XCreateFontCursor(display, cursor); XDefineCursor(display, win, pencil); pixmap = XCreatePixmap(display, root, 45, 45, depth); XSelectInput(display, win, ExposureMask | ButtonPressMask | ButtonMotionMask | KeyPressMask); XSelectInput(display, child, ButtonPressMask); XMapWindow(display, win); // XGrabKeyboard(display, root, False, GrabModeAsync, GrabModeAsync, CurrentTime); // XGrabPointer(display, root, False, ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, root, None, CurrentTime); // sleep(5); // XAllowEvents(display, AsyncPointer, CurrentTime); // printf("pointer freeed\n"); // sleep(5); XGrabServer(display); sleep(10); exit(1); while (1) { XNextEvent(display, &event); switch(event.type) { case Expose: if (event.xexpose.count > 0) break; XDrawLine(display, win, gc, 0, 0, 100, 100); XDrawLine(display, win, gc, 100, 0, 0, 100); break; case ButtonPress: // XDrawPoint(display, win, gc, event.xbutton.x, event.xbutton.y); // XDrawString(display, win, gc, 0, 0, "hello world", strlen("hello world")); if (event.xbutton.window == child) return 0; if (event.xbutton.state == ShiftMask) XDrawLine(display, win, gc, event.xbutton.x, event.xbutton.y, x1, y1); if (event.xbutton.button == Button3) exit(0); x = event.xbutton.x; y = event.xbutton.y; x1 = x; y1 = y; break; case MotionNotify: //XDrawPoint(display, win, gc, event.xmotion.x, event.xmotion.y); if (get == 0) { x = event.xmotion.x; y = event.xmotion.y; get = 1; } else if (get == 1) { x1 = event.xmotion.x; y1 = event.xmotion.y; get = 0; } XDrawLine(display, win, gc, x, y, x1, y1); break; case KeyPress: key = XKeycodeToKeysym(display, event.xkey.keycode, 0); if (key == 65293) { ty += 10; tx = 10; } if (key == 65288) tx -= a; printf("%d\n", key); if ((key > 32 && key < key) || (key > 159)) break; else sprintf(keyc, "%c", key); XDrawString(display, win, gc, tx, ty, keyc, strlen(keyc)); a = XTextWidth(font, keyc, strlen(keyc)); tx += a; switch(key) { case '1': XMapWindow(display, child); break; case '2': XUnmapWindow(display, child); break; case '3': XSetForeground(display, gc, returncolor(0xffff, 0x0000, 0x0000) ); break; case '4': XSetForeground(display, gc, returncolor(0x0000, 0xffff, 0x0000)); break; case '5': XSetForeground(display, gc, returncolor(0x0000, 0x0000, 0xffff)); break; case '6': XSetForeground(display, gc, returncolor(0x0000, 0xffff, 0xffff)); break; case '7': XSetForeground(display, gc, returncolor(0xffff, 0xffff, 0xffff)); break; } break; } } XFlush(display); XCloseDisplay(display); return 0; } int returncolor(int r, int g, int b) { color.red = r; color.green = g; color.blue = b; XAllocColor(display, colormap, &color); return (color.pixel); }