#include #include #include #include #include #define MWX 128 #define MWY 176 char *Version = "Ver. 1.60-Special"; char *Copyright = "Copyright (C) 1993 M.Tsuruta."; char *p_name; int F_draw = 0, lw = 5, d_lw = 5, lw_mode = 8; int x0, y0, x1, y1, xm, ym, x_max, y_max, d2_x_max, d2_y_max; int open_menu = 0, menu_no = 0; char d2_name[100], temp[100]; Display *d, *d2 = NULL; Window w, wm; GC gc, gc2, gcm; XEvent e; XSetWindowAttributes a; Font f; char *menu[6] = {" Xdraw Menu", " All Clear", " All Set", " Refresh", " Exit", " Refresh & Exit"}; int lws[9] = {0, 1, 5, 10, 15, 20, 25, 30, 0}; int xor_flag = 0; void usage_exit(void) { fprintf(stderr, "%s (画面に落書きをするプログラム) %s %s\n", p_name, Version, Copyright); fprintf(stderr, "使い方 : %s [-d2 display [-i]] [-lw line_width] [-xor]\n", p_name); fprintf(stderr, " 左ボタン : 黒\n"); fprintf(stderr, " 中ボタン : 白\n"); fprintf(stderr, " 右ボタン : メニュー\n"); fprintf(stderr, "詳しくは、マニュアルを読んでください。\n"); exit(-1); } void get_option(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { if (strcmp(argv[i], "-d2") == 0) { if (++i >= argc) usage_exit(); d2 = XOpenDisplay(argv[i]); if (d2 == NULL) fprintf(stderr, "%s : %s がオープン出来ません!\n", p_name, argv[i]); else strcpy(d2_name, argv[i]); } else if (strcmp(argv[i], "-i") == 0) { F_draw = 1; fprintf(stderr, "%s : 注意", p_name); fprintf(stderr, "(他人に迷惑をかけないように!!)\n"); } else if (strcmp(argv[i], "-lw") == 0) { if (++i >= argc) usage_exit(); lw = atoi(argv[i]); lw = (lw > 0) ? lw : 5; } else if (strcmp(argv[i], "-xor") == 0) xor_flag = 1; else usage_exit(); } else usage_exit(); } } void init_xdraw(void) { Window root; int x, y, border, depth; a.override_redirect = 1; d = XOpenDisplay(NULL); f = XLoadFont(d, "-bitstream-charter-*-*-normal-*-16-*-*-*-*-*-*-*"); fprintf(stderr, "%s : 右ボタンでメニューが出ます。\n", p_name); XGrabPointer(d, RootWindow(d,0), False, ButtonPressMask | ButtonMotionMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, RootWindow(d,0), XCreateFontCursor(d, XC_pencil), CurrentTime); gc = XCreateGC(d, RootWindow(d,0), 0, 0); XSetSubwindowMode(d, gc, IncludeInferiors); XSetLineAttributes(d, gc, lw, LineSolid, CapRound, JoinRound); XGetGeometry(d, RootWindow(d,0), &root, &x, &y, &x_max, &y_max, &border, &depth); if (d2 != NULL) { gc2 = XCreateGC(d2, RootWindow(d2,0), 0, 0); if (F_draw) XSetSubwindowMode(d2, gc2, IncludeInferiors); XSetLineAttributes(d2, gc2, lw, LineSolid, CapRound, JoinRound); XGetGeometry(d2, RootWindow(d2,0), &root, &x, &y, &d2_x_max, &d2_y_max, &border, &depth); } } void write_menu(int m, unsigned long fore, unsigned long back) { int x, y; x = (m - 1) % 4; y = (m - 1) / 4; XSetForeground(d, gcm, back); if (m < 8) { XFillRectangle(d, wm, gcm, x*32, y*32+24, 32, 32); XSetForeground(d, gcm, fore); XSetLineAttributes(d, gcm, lws[m], LineSolid, CapRound, JoinRound); XDrawLine(d, wm, gcm, 15+x*32, 39+y*32, 15+x*32, 39+y*32); } else if (m == 8) { XFillRectangle(d, wm, gcm, x*32, y*32+24, 32, 32); XSetForeground(d, gcm, fore); XDrawString(d, wm, gcm, x*32+0, y*32+24+24,"Opt.", strlen("Opt.")); } else { m -= 9; XFillRectangle(d, wm, gcm, 0, 16*m+96, MWX, 16); XSetForeground(d, gcm, fore); XDrawString(d, wm, gcm, 0, 16*m+96+15, menu[m+1], strlen(menu[m+1])); } } void write_lw_mode(void) { int x, y; x = (lw_mode - 1) % 4; y = (lw_mode - 1) / 4; XSetForeground(d, gcm, BlackPixel(d,0)); XSetLineAttributes(d, gcm, 1, LineSolid, CapRound, JoinRound); XDrawRectangle(d, wm, gcm, x*32, y*32+24, 31, 31); } void write_menus(void) { int i; XSetForeground(d, gcm, BlackPixel(d,0)); XFillRectangle(d, wm, gcm, 0, 0, MWX, 16); XSetForeground(d, gcm, WhitePixel(d,0)); XDrawString(d, wm, gcm, 0, 15, menu[0], strlen(menu[0])); for (i = 1; i < 14; i++) write_menu(i, BlackPixel(d,0), WhitePixel(d,0)); } void xdraw_menu(void) { xm = e.xbutton.x - MWX / 2; ym = e.xbutton.y; wm = XCreateSimpleWindow(d, RootWindow(d,0), xm, ym, MWX, MWY, 2, BlackPixel(d,0), WhitePixel(d,0)); XChangeWindowAttributes(d, wm , CWOverrideRedirect, &a); gcm = XCreateGC(d, wm, 0, 0); XSetFont(d, gcm, f); XMapWindow(d, wm); write_menus(); write_lw_mode(); open_menu = 1; } #define menu_in(x,y) ((x >= 0) && (x < MWX)\ && (y >= 0) && (y < MWY)) int get_menu(void) { int m = 0, ex = e.xbutton.x - xm, ey = e.xbutton.y - ym; if (menu_in(ex, ey)) { if ((ey > 23) && (ey < 88)) m = ((ey - 24) / 32) * 4 + ex / 32 + 1; else if (ey > 95) m = ((ey - 96) / 16) + 9; } return m; } void menu_sel(void) { int old_menu = menu_no; menu_no = get_menu(); if (old_menu != menu_no) { if (old_menu) write_menu(old_menu, BlackPixel(d,0), WhitePixel(d,0)); if (menu_no) write_menu(menu_no, WhitePixel(d,0), BlackPixel(d,0)); write_lw_mode(); } } void menu_exec(void) { switch (menu_no) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: lw_mode = menu_no; lw = lws[menu_no]; break; case 8: lw_mode = menu_no; lw = d_lw; break; case 9: sleep(1); XSetForeground(d, gc, WhitePixel(d,0)); XFillRectangle(d, RootWindow(d,0), gc, 0, 0, x_max, y_max); if (d2 != NULL) { XSetForeground(d2, gc2, WhitePixel(d2,0)); XFillRectangle(d2, RootWindow(d2,0), gc2, 0, 0, d2_x_max, d2_y_max); XFlush(d2); } XFlush(d); break; case 10: sleep(1); XSetForeground(d, gc, BlackPixel(d,0)); XFillRectangle(d, RootWindow(d,0), gc, 0, 0, x_max, y_max); if (d2 != NULL) { XSetForeground(d2, gc2, BlackPixel(d2,0)); XFillRectangle(d2, RootWindow(d2,0), gc2, 0, 0, d2_x_max, d2_y_max); XFlush(d2); } XFlush(d); break; case 11: system("xrefresh"); if (d2 != NULL) { sprintf(temp, "xrefresh -display %s", d2_name); system(temp); } break; case 12: exit(0); case 13: system("xrefresh"); if (d2 != NULL) { sprintf(temp, "xrefresh -display %s", d2_name); system(temp); } exit(0); } XSetLineAttributes(d, gc, lw, LineSolid, CapRound, JoinRound); if (d2 != NULL) XSetLineAttributes(d2, gc2, lw, LineSolid, CapRound, JoinRound); } void xdraw_main(void) { for (;;) { XNextEvent(d, &e); switch (e.type) { case ButtonPress : switch (e.xbutton.button) { case 1: x0 = e.xbutton.x; y0 = e.xbutton.y; if (!open_menu) { XSetForeground(d, gc, BlackPixel(d,0)); if (xor_flag) XSetFunction(d, gc, GXxor); XDrawLine(d, RootWindow(d,0), gc, x0, y0, x0, y0); if (xor_flag) XSetFunction(d, gc, GXcopy); if (d2 != NULL) { XSetForeground(d2, gc2, BlackPixel(d2,0)); if (xor_flag) XSetFunction(d2, gc2, GXxor); XDrawLine(d2, RootWindow(d2,0), gc, x0, y0, x0, y0); if (xor_flag) XSetFunction(d2, gc2, GXcopy); } } break; case 2: x0 = e.xbutton.x; y0 = e.xbutton.y; if (!open_menu) { XSetForeground(d, gc, WhitePixel(d,0)); if (!xor_flag) XDrawLine(d, RootWindow(d,0), gc, x0, y0, x0, y0); if (d2 != NULL) { XSetForeground(d2, gc2, WhitePixel(d2,0)); if (!xor_flag) XDrawLine(d2, RootWindow(d2,0), gc, x0, y0, x0, y0); XFlush(d2); } } break; case 3: xdraw_menu(); break; } break; case MotionNotify : switch (e.xbutton.button) { case 1: case 2: if (!open_menu) { x1 = e.xbutton.x; y1 = e.xbutton.y; if (xor_flag) { XSetFunction(d, gc, GXxor); XDrawLine(d, RootWindow(d,0), gc, x0, y0, x0, y0); } XDrawLine(d, RootWindow(d,0), gc, x0, y0, x1, y1); if (xor_flag) XSetFunction(d, gc, GXcopy); if (d2 != NULL) { if (xor_flag) { XSetFunction(d2, gc2, GXxor); XDrawLine(d2, RootWindow(d2,0), gc2, x0,y0,x0,y0); } XDrawLine(d2, RootWindow(d2,0), gc2, x0, y0, x1, y1); if (xor_flag) XSetFunction(d2, gc2, GXcopy); XFlush(d2); } x0 = x1; y0 = y1; } break; case 3: if (open_menu) menu_sel(); break; } break; case ButtonRelease: switch (e.xbutton.button) { case 3: XDestroyWindow(d, wm); XFlush(d); menu_exec(); open_menu = 0; menu_no = 0; break; } break; } } } int main(int argc, char *argv[]) { p_name = argv[0]; if (getenv("DISPLAY") == NULL) { fprintf(stderr, "%s : ディスプレイが、オープン出来ません!!\n", p_name); exit(-1); } get_option(argc, argv); d_lw = lw; init_xdraw(); xdraw_main(); }