Xlib Code- why not drawing Rectangle???

pihu m.richa at gmail.com
Mon Jul 18 14:24:19 EEST 2005


Hi,

I have pasted my code below....I want to draw rectangle in any
selected window including desktop(whichever is selected) in the motion
event of my pointer but its not drawing . Can anybody tell me what can
be the problem???its showing me the pointer motion but no drawing.....

Please tell me what could be the reason????

regards,
Pihu......

/***********************************************************/


main(int argc, char **argv)
{
        register int i;
        int tree = 0, stats = 0, bits = 0, events = 0, wm = 0, size  =
0, shape = 0;
        int frame = 0, children = 0;
        Window window;

        window = 0;
        char *displayname = NULL;

        dpy = XOpenDisplay(0);
        screen = DefaultScreen(dpy);
        /* If no window selected on command line, let user pick one
the hard way */
        if (!window)
        {
                printf("\n");
                printf("xwininfo: Please select the window about which you\n");
                printf("          would like information by clicking the\n");
                printf("          mouse in that window.\n");
                window = Select_Window(dpy);
        printf("The target Window address is 0x%x \n",window);
        }

/*
 * Routine to let user select a window using the mouse
 */

Window Select_Window(dpy)
   Display *dpy;
{
        int status;
        Cursor cursor;
        XEvent event;
        Window target_win = None, root = RootWindow(dpy,screen),child;
GC gc;                                // handle of newly created GC
        XGCValues values;                     // initial values for the GC

        unsigned long valuemask = 0;          // which values in 'values' to  /
                                        // check when creating the GC.  /

        int buttons = 0;
        XPoint p1,p2;
        int index = 1;
        int pos_x, pos_y;
        int prev_x, prev_y;
                int flag1 = 0;
        int flag = 0;
        int x = 0,y = 0;
        int root_x, root_y;
        unsigned int keys_buttons;


        /* Make the target cursor */
        cursor = XCreateFontCursor(dpy, XC_crosshair);

        /* Grab the pointer using target cursor, letting it room all over */
        status = XGrabPointer(dpy, root,
False,ButtonPressMask|ButtonReleaseMask|PointerMotionMask|ButtonMotionMask,
GrabModeSync,
        GrabModeAsync, root, cursor, CurrentTime);

        if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");
         gc = XCreateGC(dpy, root, valuemask , &values);
        if (gc < 0)
        {
                fprintf(stderr, "XCreateGC: \n");
        }
/* Let the user select a window... */
        while ((target_win == None) || (buttons != 0))
        {
                /* allow one more event */

                XAllowEvents(dpy, SyncPointer, CurrentTime);
                XWindowEvent(dpy, root,
ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
&event);
                switch (event.type)
                {
                        case ButtonPress:
                        if (target_win == None)
                        {
                                target_win = event.xbutton.subwindow;
/* window selected */
                                p1.x = event.xbutton.x;
                                p1.y = event.xbutton.y;
                                printf("BUTTON PRESSED \n");

                                if (target_win == None) target_win = root;

                                if (target_win )
                                {

                                        int dummyi;
                                        unsigned int dummy;

                                        if (XGetGeometry (dpy,
target_win, &root, &dummyi, &dummyi,
                                                &dummy, &dummy,
&dummy, &dummy) &&
                                                target_win != root)
                                        target_win = XmuClientWindow
(dpy, target_win);
                                }
                                 printf("The target Window address is
0x%x with in BUTTON PRESS after getting
XmuClientWindow\n",target_win);
                        }
 buttons++;
                        break;
case ButtonRelease:
                        index++;
                        p2.x = event.xbutton.x;
                        p2.y = event.xbutton.y;
                        flag1 = 1;
                        printf("BUTTON RELEASED\n");
                        printf("top    left  coordinates are  X : %d 
Y : %d\n",p1.x,p1.y);
                        printf("top    right coordinates are  X : %d 
Y : %d \n",p2.x,p1.y);
                        printf("bottom left  coordinates are  X : %d 
Y : %d \n",p1.x,p2.y);
                        printf("bottom right coordinates are  X : %d 
Y : %d \n",p2.x,p2.y);
                        if (buttons > 0) /* there may have been some
down before we started */
                                buttons--;
                        break;
                        case MotionNotify:
                printf("Mouse POINTER Motion\n");
                while (XCheckMaskEvent(dpy,
            PointerMotionHintMask | ButtonMotionMask, &event));
                if (!XQueryPointer(dpy, event.xmotion.window,
               &root, &child, &root_x, &root_y,
               &pos_x, &pos_y, &keys_buttons))
            {

                   //printf("After motion notify \n");
                // printf("motion %d %d \n",pos_x,pos_y);

            }
             if (flag)
                        {
                         XClearWindow(dpy, target_win);
                 if(pos_y < p1.y && pos_x > p1.x) //  fourth quadrant
                {
             XDrawRectangle(dpy, target_win, gc, p1.x-1 ,pos_y+1 ,p1.x
+1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y +
1) - pos_y: pos_y -(p1.y +1) );
 }
             else if(pos_y < p1.y && pos_x < p1.x)//Third quadrant
             {
             XDrawRectangle(dpy, target_win, gc, pos_x-1 ,pos_y-1
,p1.x +1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1
> pos_y ? (p1.y + 1) - pos_y: pos_y -(p1.y +1) );
             }
             else if(pos_y > p1.y && pos_x < p1.x) //Second quadrant
             {
             XDrawRectangle(dpy, target_win, gc, pos_x-1 ,p1.y+1 ,p1.x
+1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y +
1) - pos_y: pos_y -(p1.y +1) );
             }
             else  if(pos_y > p1.y && pos_x > p1.x)//first quadrant
              {
              XDrawRectangle(dpy, target_win, gc, p1.x +1,p1.y +1,p1.x
+1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y +
1) - pos_y: pos_y -(p1.y +1) );
              }
             }
            break;

                }
        }
        XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */
        return(target_win);
}



More information about the xdg mailing list