[Xcb] Xlib Code --- Why not Drawing Rectangle??????

pihu m.richa at gmail.com
Mon Jul 18 21:24:28 EST 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: podøê¸Ã
šÓu}Þ9ħ–ÿ@›=øûÏ*_´—ªà…7O‘ª›õ­ükäSOÞûUÆ­6Ñûj}®pµ­¦ÚÎç$·mí·¦ÄË4œÒsIÍ9£¦\óõS/®Ã0Ã	Ä°ˆ(L/‚ í=»_möß¹íÛ[ÚÕ÷MkµxF&ÇA—xƒ1&ÌlÆÌlL±³1³¿Û, 0,â ‚ ó>|ßXFÜ9[çaÒÛbevbl»18îٍ˜Ù‰–6cf6`p pÀ Cˆ‚ ƒûÄiLé6–VÓb1Ób»ÄرÓe¶cf6&Xٍ˜Ù÷‹	9A—Þ÷Ö îø·¾Sp²¶ŒMŽƒ.ñbM˜Ù˜Ø™cf6cf5A ‡Ahîp{• éo®ƒ•Óbevbl»18îٍ˜Ù‰–6cf6ce†D@Aë.¦t›K+I±é±]âlXé²Û1³,lÆÌlƶ€AAã@AF)œM‹+HÄØè2ïf$ٍ˜Ù‰–6cf6cJ@‚ÂA"  ‚—7}¸í½sÇ·]ûvíß¾öYt—M‰•Ù‰²ìÄã»f6cf6&Xٍ˜Ùâº@h,â ‚ ê6¦v›k+w4:m,Í&ÒÇM–ٍ˜Ø™cf6cf5ù÷ A@ã@A§¦ÚÊás§m:
fi±&ÌlÆÌlL±³1³==Ì	
†D@A½Îd9U΃•¶ÄÊìÄÙvbqݳ1³,lÆÌlÆ^ãÐ À Cˆ‚ ƒÖZtª´›Jëi±é±]âlXé²Û1³;²ÓkM­6´Ö( r ƒ:=ϹÛv·¾“tº®‘‰±ÐeÞ ÌI³1³,lÆÌlÆy8ˆ €‚=òæ–›+³eÙ‰ÇvÌlÆÌlL±³1³$,(â ‚ Ôb™´Ø²´›Ž›Þ&ÅŽ›-³1±2ÆÌlÆÌ P@8äAv×WÛŠ]v«´á†a†a†a†a†a†a†a†a†a†a†ïÏŒÚlW]‘‰±ÐeÞ ÌI³1³,lÆÌlÆä pÀ Cˆ‚ ƒz€Ò`mi±2»16]˜œwlÆÌlÆÄË1³/aÄØ0 dJP¥)åË™ËZY[MˆXØ‹ÌlF:l¶ÌlÆÄË1³1½Íh A@ã@Açªá7U´Ø…Œeæ1˜“f6cf6&Xٍ˜Ùî*†A(K,°²Ë,îoflç3wi«³eÙ‰ÇvÌlÆÌlL±³1³Å@ÂA"  ‚0àõ="Õ-k”Ü®k”Ø…ˆ¼ÆÄc¦ËlÆÌlL±³1³ÕŽ( r ƒˆÅ3‰±ei6!cyŒf$ٍ˜Ù‰–6cf6cDƒ……D@A‰­Mâm-6&Wf&˳Ží˜Ù˜Þ鵦֛ZmcU……D@Aš¢²Ûz¶ÄØé±]âlXé²Û1³,lÆÌlÆ­P À qÈ  ‚.IÕ6‘›¶ÄØè2ïf$ٍ˜Ù‰–6cf6