Advertisement
Guest User

rk.c

a guest
Apr 6th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.06 KB | None | 0 0
  1. #include <X11/Xutil.h>
  2. #include <X11/keysym.h>
  3. #include <X11/keysymdef.h>
  4. #include "rk.h"
  5. #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
  6. #define MAX(X, Y) (((X) < (Y)) ? (Y) : (X))
  7.  
  8. static Display* dpy;     /* X Display */
  9. static GC gc[2];         /* Graphic Context */
  10. static Window root;      /* Main root window */
  11. static int X0=0;         /* main root wintow x-location */
  12. static int Y0=0;         /* main root wintow y-location */
  13. static char mark[20]; /* Marker symbols */
  14. static int X1 = 0;
  15. static int Y1 = 0;
  16. static int dx[4]={0,0,-1,1};
  17. static int dy[4]={-1,1,0,0};
  18. static int BW=45;     /* Box width */
  19. static int BH=45;     /* Box height */
  20. static unsigned AW=40;     /* Arrow width */
  21. static unsigned AH=40;     /* Arrow height */
  22. static int SW = 30;//?
  23. static int SH = 16;//?
  24. static int CPW = 8;
  25. static int IX=0;     /* indicator xcoord */
  26. static int IY=0;     /* indicator ycoord */
  27.  
  28. /* redraw exposed markered box  */
  29. int movebox(int ddx,int ddy) {
  30.     int i=0;
  31.     XMoveWindow(dpy, box[0][0], X1=MAX(MIN(X1+ddx,SW-BW),0),Y1=MAX(MIN(Y1+ddy,SH-BH),0));
  32.     i=sprintf(mark,"%d:%d",X1,Y1);
  33.     XClearWindow(dpy, box[0][0]);
  34.     XDrawString(dpy, box[0][0], gc[0], IX, IY, mark, i);
  35.     return 0;
  36. }
  37.  
  38. int rebox(XEvent* ev) {
  39.     int i, ddx,ddy,bx,by;
  40.     for(i=0; i < NX; i++)
  41.     {
  42.         if(ev->xexpose.window == box[1][i]){
  43.         ddx=dx[i];
  44.         ddy=dy[i];
  45.         movebox(ddx,ddy);
  46.         }
  47.         if(ev->xexpose.window == box[0][0]) {
  48.             bx = ev->xbutton.x;
  49.             by = ev->xbutton.y;
  50.             if ((bx<IX+15&&bx>IX-15)&&(by>IY-7&&by<IY+7))
  51.                 return 1;
  52.         }
  53.     }
  54.     XDrawString(dpy, box[1][0], gc[0], 12, 16, "Up", 2);
  55.     XDrawString(dpy, box[1][1], gc[0], 8, 16, "Down", 4);
  56.     XDrawString(dpy, box[1][2], gc[0], 8, 16, "Left", 4);
  57.     XDrawString(dpy, box[1][3], gc[0], 8, 16, "Right", 5);
  58.     return 0;
  59. }
  60.  
  61. int xcustom() {
  62.     static unsigned XSP=4;     /* x-space between 2 boxes in any row */
  63.     static unsigned YSP=4;     /* y-space between 2 rows */
  64.     int x, y;                  /* windows' location */
  65.     unsigned w, h;             /* windows' size */
  66.     int src;                   /* screen number */
  67.     int depth;                 /* screen depth */
  68.     XSetWindowAttributes attr; /* created Window attributes */
  69.     XSizeHints hint;           /* WM connection structure */
  70.     XFontStruct* fn;           /* Font parameters structure */
  71.     char* fontname = "6x12";   /* default font name */
  72.     int i,j;                   /* windows' index */
  73. /* get X defaults */
  74.     src = DefaultScreen(dpy);
  75.     depth = DefaultDepth(dpy, src);
  76.     gc[0] = DefaultGC(dpy, src);
  77. /* Font custom */
  78.     if((fn = XLoadQueryFont(dpy, fontname)) == NULL)
  79.         return(puts("Incorrect FontStruct id"));
  80.     XSetFont(dpy, gc[0], fn->fid);
  81. /* Main root window */
  82.     SW = MIN(BW*18,800);
  83.     SH = MIN(BH*14,600);
  84.     CPW = AW*3.7;
  85.     IX=BW/2-20;
  86.     IY=BH/2+2;
  87.     w = SW+CPW;
  88.     h = SH;
  89.     attr.override_redirect = False;
  90.     attr.background_pixel = WhitePixel(dpy, src);
  91.     x = X0; y = Y0;
  92.     root = XCreateWindow(dpy, DefaultRootWindow(dpy), x, y, w, h,
  93.                      1, depth, InputOutput, CopyFromParent,
  94.                      (CWOverrideRedirect | CWBackPixel),  &attr);
  95.     hint.flags = (PMinSize | PMaxSize | PPosition);
  96.     hint.min_width = hint.max_width = w;
  97.     hint.min_height = hint.max_height = h;
  98.     hint.x = x; hint.y = y;
  99.     XSetNormalHints(dpy, root, &hint);
  100. /* box row  root's subwindow */
  101.     attr.override_redirect = True;
  102.     attr.background_pixel = WhitePixel(dpy, src);
  103.     attr.event_mask = (ButtonPressMask | KeyPressMask);
  104. /*create main window*/
  105.     row[0] = XCreateWindow(dpy, root, 0, 0, SW, SH, 0, depth,
  106.                          InputOutput, CopyFromParent,
  107.            (CWOverrideRedirect | CWBackPixel | CWEventMask),  &attr);
  108.     attr.background_pixel = 0x00AAAA;
  109. /*create control panel*/
  110.     row[1] = XCreateWindow(dpy, root, SW, 0, CPW, SH, 0, depth,
  111.                          InputOutput, CopyFromParent,
  112.            (CWOverrideRedirect | CWBackPixel | CWEventMask),  &attr);
  113. /* Box row's subwindows */
  114.     attr.override_redirect = True;
  115.     attr.background_pixel = 0x00FFFF; /* WhitePixel(dpy, src); */
  116.     attr.event_mask = (KeyPressMask | ExposureMask |ButtonPressMask);
  117.     w = BW; h = BH;
  118. /*moving box*/
  119.     box[0][0] = XCreateWindow(dpy, row[0], 0, 0, BW, BH, 1,
  120.                               depth, InputOutput, CopyFromParent,
  121.                 (CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
  122. /*arrows*/
  123.     attr.background_pixel = 0xAAFFAA;
  124.     box[1][0] = XCreateWindow(dpy, row[1], AW/5+AW+4, AH, AW, AH, 1,//AW/5+AW+4
  125.                               depth, InputOutput, CopyFromParent,
  126.                 (CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
  127.     XDrawString(dpy, box[1][0], gc[0], 12, 12, "Up", 2);
  128.     box[1][1] = XCreateWindow(dpy, row[1], AW/5+AW+4, AH*2+4, AW, AH, 1,
  129.                               depth, InputOutput, CopyFromParent,
  130.                 (CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
  131.     XDrawString(dpy, box[1][1], gc[0], 2, 16, "Down", 4);
  132.     box[1][2] = XCreateWindow(dpy, row[1], AW/5, AH*2+4, AW, AH, 1,
  133.                               depth, InputOutput, CopyFromParent,
  134.                 (CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
  135.     XDrawString(dpy, box[1][2], gc[0], 2, 16, "Left", 4);
  136.     box[1][3] = XCreateWindow(dpy, row[1], AW/5+2*AW+8, AH*2+4, AW, AH, 1,
  137.                               depth, InputOutput, CopyFromParent,
  138.                 (CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
  139.     XDrawString(dpy, box[1][3], gc[0], 2, 16, "Right", 5);
  140. /* Display windows */
  141.     XMapWindow(dpy, root);
  142.     XMapSubwindows(dpy, root);
  143.     for(i=0; i<NY; i++)
  144.         XMapSubwindows(dpy, row[i]);
  145.     XStoreName(dpy, root, "rkv14");
  146. /* Create clear GC */
  147.     gc[1] = XCreateGC(dpy, root, 0, 0);
  148.     XCopyGC(dpy, gc[0], GCFont, gc[1]);
  149.     XSetForeground(dpy, gc[1], 0x00FFFF); /* WhitePixel(dpy, src)); */
  150.     return(0);
  151. }
  152.  
  153. /* KeyBoard Driver */
  154. int kbdrive(XEvent* ev) {
  155.     KeySym sym;
  156.     int i;
  157.     XLookupString((XKeyEvent*) ev, NULL, 0, &sym, NULL);
  158.     switch(sym) {
  159.         case XK_Escape:
  160.             break;
  161.         case XK_q:
  162.         case XK_Q: if(ev->xkey.state & ControlMask)  
  163.                         return(1);
  164.             break;
  165.         case XK_Down: movebox(0,1);
  166.             return(0);
  167.             break;
  168.         case XK_Up: movebox(0,-1);
  169.             return(0);
  170.             break;
  171.         case XK_Right: movebox(1,0);
  172.             return(0);
  173.             break;
  174.         case XK_Left: movebox(-1,0);
  175.             return(0);
  176.             break;
  177.         default:
  178.             break;
  179.     }
  180.     return(0);
  181. }
  182.  
  183. /* Event dispatcher */
  184. int dispatch() {
  185.     XEvent event;      /* Event structure */
  186.     int flag = 0;      /* exit flag */
  187.     while(flag == 0) {
  188.         XNextEvent(dpy, &event);
  189.         switch(event.type) {
  190.             case Expose: rebox(&event);
  191.                 break;
  192.             case KeyPress: { flag = kbdrive(&event); break; }
  193.             case ButtonPress: flag=rebox(&event);
  194.                 break;
  195.             default:
  196.                 break;
  197.         }
  198.     }
  199.     return(0);
  200. }
  201.  
  202. /* Main function */
  203. int main(int argc, char* argv[]) {
  204.     if(argc != 3 || atoi(argv[1]) < 40 || atoi(argv[2]) < 40)
  205.         fprintf(stderr, "Default: xpat 16x4+0+0\n");  
  206.     else {
  207.         BH=atoi(argv[1]);
  208.         BW=atoi(argv[2]);
  209.     }  
  210.     alloc();
  211.     dpy = XOpenDisplay(NULL);
  212.         if(xcustom() > 0)
  213.         return(1);
  214.     dispatch();
  215.     XDestroySubwindows(dpy, root);
  216.     XDestroyWindow(dpy, root);
  217.     XCloseDisplay(dpy);
  218.     dealloc(box, row);
  219.     return(0);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement