Unable to catch ButtonRelease event - Xwindows

This is a discussion on Unable to catch ButtonRelease event - Xwindows ; Hi group, I'm learning to program in Xlib, and written a program in C on Fedora Core 5 (AMD64) Linux. The objective of my program is to close its window, when a mouse button is released on its window. -- ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Unable to catch ButtonRelease event

  1. Unable to catch ButtonRelease event

    Hi group,

    I'm learning to program in Xlib, and written a program in C on Fedora
    Core 5 (AMD64) Linux. The objective of my program is to close its
    window, when a mouse button is released on its window.

    -- begin dump --
    #include
    #include
    #include

    #define WIDTH 300
    #define HEIGHT 200
    #define BORDER_WIDTH 12

    static void main_loop(Display*);

    int
    main()
    {
    Display* display = NULL;
    int scr_height, scr_width, scr_num;
    Window win_root, win_me;
    unsigned long pix_white, pix_black;

    display = XOpenDisplay(NULL);
    if(display == NULL)
    {
    fprintf(stderr, "Can't connect to X server: %s\n",
    XDisplayName(NULL));
    exit(-1);
    }

    scr_num = DefaultScreen(display);
    scr_width = DisplayWidth(display, scr_num);
    scr_height = DisplayHeight(display, scr_num);
    win_root = RootWindow(display, scr_num);
    pix_white = WhitePixel(display, scr_num);
    pix_black = BlackPixel(display, scr_num);

    win_me = XCreateSimpleWindow(display, win_root,
    0, 0, WIDTH, HEIGHT, BORDER_WIDTH, pix_black,
    pix_white);

    XMapWindow(display, win_me);
    XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask);

    main_loop(display);

    XUnmapWindow(display, win_me);
    XDestroyWindow(display, win_me);

    XCloseDisplay(display);
    }

    void
    main_loop(Display* display)
    {
    XEvent event;
    int running = 1;

    while(running)
    {
    XNextEvent(display, &event);
    printf("event, type=%d\n", event.type);
    switch(event.type)
    {
    case Expose:
    break;
    case ButtonRelease:
    printf("released\n");
    running = 0;
    break;
    default:
    fprintf(stderr, "unexpected event: %d\n",
    event.type);
    }
    }
    }
    -- end dump --

    Even after releasing mouse button several times on the window, I'm
    unable to receive a single ButtonRelease event. Can anybody tell me,
    what I'm doing wrong here ?

    Thanks in advance,

    Ashish Shukla
    --
    http://www.geocities.com/wah_java_dotnet/


  2. Re: Unable to catch ButtonRelease event


    "???? ??????" wrote in message
    news:1147858340.202183.22130@i40g2000cwc.googlegro ups.com...
    > Hi group,
    >
    > I'm learning to program in Xlib, and written a program in C on Fedora
    > Core 5 (AMD64) Linux. The objective of my program is to close its
    > window, when a mouse button is released on its window.
    >
    > -- begin dump --
    > #include
    > #include
    > #include
    >
    > #define WIDTH 300
    > #define HEIGHT 200
    > #define BORDER_WIDTH 12
    >
    > static void main_loop(Display*);
    >
    > int
    > main()
    > {
    > Display* display = NULL;
    > int scr_height, scr_width, scr_num;
    > Window win_root, win_me;
    > unsigned long pix_white, pix_black;
    >
    > display = XOpenDisplay(NULL);
    > if(display == NULL)
    > {
    > fprintf(stderr, "Can't connect to X server: %s\n",
    > XDisplayName(NULL));
    > exit(-1);
    > }
    >
    > scr_num = DefaultScreen(display);
    > scr_width = DisplayWidth(display, scr_num);
    > scr_height = DisplayHeight(display, scr_num);
    > win_root = RootWindow(display, scr_num);
    > pix_white = WhitePixel(display, scr_num);
    > pix_black = BlackPixel(display, scr_num);
    >
    > win_me = XCreateSimpleWindow(display, win_root,
    > 0, 0, WIDTH, HEIGHT, BORDER_WIDTH, pix_black,
    > pix_white);
    >
    > XMapWindow(display, win_me);
    > XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask);
    >
    > main_loop(display);
    >
    > XUnmapWindow(display, win_me);
    > XDestroyWindow(display, win_me);
    >
    > XCloseDisplay(display);
    > }
    >
    > void
    > main_loop(Display* display)
    > {
    > XEvent event;
    > int running = 1;
    >
    > while(running)
    > {
    > XNextEvent(display, &event);
    > printf("event, type=%d\n", event.type);
    > switch(event.type)
    > {
    > case Expose:
    > break;
    > case ButtonRelease:
    > printf("released\n");
    > running = 0;
    > break;
    > default:
    > fprintf(stderr, "unexpected event: %d\n",
    > event.type);
    > }
    > }
    > }
    > -- end dump --
    >
    > Even after releasing mouse button several times on the window, I'm
    > unable to receive a single ButtonRelease event. Can anybody tell me,
    > what I'm doing wrong here ?
    >
    > Thanks in advance,
    >
    > Ashish Shukla
    > --
    > http://www.geocities.com/wah_java_dotnet/
    >

    Works fine for me as you wrote it.
    Are you pressing the button while the cursor is in the window, then
    releasing it? Or are you pressing the button OUTSIDE the window,
    moving into the window and then releasing it? For the latter
    case it will not work. That is because the window in which you
    press the button "grabs" the next button release event.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project




  3. Re: Unable to catch ButtonRelease event

    Hi,

    First of all thanks for replying.

    After, changing the following line

    -- begin dump --
    XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask);
    -- end dump --

    to

    -- begin dump --
    XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask |
    ButtonPressMask);
    -- end dump --

    my code works fine. and produces the following output:

    -- begin dump --
    event, type=12
    event, type=12
    event, type=12
    event, type=4
    unexpected event: 4
    event, type=5
    released
    -- end dump --

    Previously (before changing) it produces this (window is closed from
    the close button in the win decoration):

    -- begin dump --
    event, type=12
    event, type=12
    event, type=12
    X connection to :0.0 broken (explicit kill or server shutdown).
    -- end dump --

    I've even tried changing window managers (GNOME and KDE).

    > Works fine for me as you wrote it.
    > Are you pressing the button while the cursor is in the window, then
    > releasing it? Or are you pressing the button OUTSIDE the window,
    > moving into the window and then releasing it? For the latter
    > case it will not work. That is because the window in which you
    > press the button "grabs" the next button release event.


    Well, I was clicking in former way, i.e. pressing and releasing button
    in the same window. BTW, which you're on which platform and running
    which X server.

    This is my command line I used to compile with:

    -- begin dump --
    cc -o paint paint.c -lX11
    -- end dump --

    And output of "X -version" is below:

    -- begin dump --
    X Window System Version 7.0.0
    Release Date: 21 December 2005
    X Protocol Version 11, Revision 0, Release 7.0
    Build Operating System:Linux 2.6.9-22.18.bz155725.ELsmp x86_64Red Hat,
    Inc.
    Current Operating System: Linux myhome 2.6.16-1.2111_FC5 #1 SMP Thu May
    4 21:16:04 EDT 2006 x86_64
    Build Date: 02 May 2006
    Before reporting problems, check http://wiki.x.org
    to make sure that you have the latest version.
    Module Loader present
    -- end dump --

    Is this a bug in my X server ??

    > --
    > Fred L. Kleinschmidt
    > Boeing Associate Technical Fellow
    > Technical Architect, Software Reuse Project


    Thanks again,

    Ashish Shukla
    --
    http://www.geocities.com/wah_java_dotnet/


  4. Re: Unable to catch ButtonRelease event


    "???? ??????" wrote in message
    news:1147890185.490913.248220@j55g2000cwa.googlegr oups.com...
    > Hi,
    >
    > First of all thanks for replying.
    >
    > After, changing the following line
    >
    > -- begin dump --
    > XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask);
    > -- end dump --
    >
    > to
    >
    > -- begin dump --
    > XSelectInput(display, win_me, ExposureMask | ButtonReleaseMask |
    > ButtonPressMask);
    > -- end dump --
    >
    > my code works fine. and produces the following output:
    >
    > -- begin dump --
    > event, type=12
    > event, type=12
    > event, type=12
    > event, type=4
    > unexpected event: 4
    > event, type=5
    > released
    > -- end dump --
    >
    > Previously (before changing) it produces this (window is closed from
    > the close button in the win decoration):
    >
    > -- begin dump --
    > event, type=12
    > event, type=12
    > event, type=12
    > X connection to :0.0 broken (explicit kill or server shutdown).
    > -- end dump --
    >
    > I've even tried changing window managers (GNOME and KDE).
    >
    >> Works fine for me as you wrote it.
    >> Are you pressing the button while the cursor is in the window, then
    >> releasing it? Or are you pressing the button OUTSIDE the window,
    >> moving into the window and then releasing it? For the latter
    >> case it will not work. That is because the window in which you
    >> press the button "grabs" the next button release event.

    >
    > Well, I was clicking in former way, i.e. pressing and releasing button
    > in the same window. BTW, which you're on which platform and running
    > which X server.
    >
    > This is my command line I used to compile with:
    >
    > -- begin dump --
    > cc -o paint paint.c -lX11
    > -- end dump --
    >
    > And output of "X -version" is below:
    >
    > -- begin dump --
    > X Window System Version 7.0.0
    > Release Date: 21 December 2005
    > X Protocol Version 11, Revision 0, Release 7.0
    > Build Operating System:Linux 2.6.9-22.18.bz155725.ELsmp x86_64Red Hat,
    > Inc.
    > Current Operating System: Linux myhome 2.6.16-1.2111_FC5 #1 SMP Thu May
    > 4 21:16:04 EDT 2006 x86_64
    > Build Date: 02 May 2006
    > Before reporting problems, check http://wiki.x.org
    > to make sure that you have the latest version.
    > Module Loader present
    > -- end dump --
    >
    > Is this a bug in my X server ??
    >

    I tried it on 8 different Unix/Linux platforms, plus Cygwin on a PC using
    exceed,
    and they all work as expected - no need for the ButtonPressMask if
    press/release
    is in the same window.

    It may be a bug. However, you will almost always want to mask for
    both press and release in your window. on PRESS you set a flag that
    gets ready to act, then on RELEASE check the flag. If set, act.
    Then always reset the flag.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project




  5. Re: Unable to catch ButtonRelease event

    Hi,

    Fred Kleinschmidt wrote:
    > I tried it on 8 different Unix/Linux platforms, plus Cygwin on a PC using
    > exceed,
    > and they all work as expected - no need for the ButtonPressMask if
    > press/release
    > is in the same window.


    I'm grateful to you for this. And sorry for any inconvenience.

    >
    > It may be a bug. However, you will almost always want to mask for
    > both press and release in your window. on PRESS you set a flag that
    > gets ready to act, then on RELEASE check the flag. If set, act.
    > Then always reset the flag.


    Anyways, I've tried it also on FreeBSD 6.0 (AMD64), and the same
    result.

    -- begin dump --
    X Window System Version 6.8.2
    Release Date: 9 February 2005
    X Protocol Version 11, Revision 0, Release 6.8.2
    Build Operating System: FreeBSD 6.0 amd64 [ELF]
    Current Operating System: FreeBSD myhome 6.0-RELEASE FreeBSD
    6.0-RELEASE #0: Sat Feb 11 20:41:16 IST 2006
    wahjava@myhome:/usr/obj/usr/src/sys/MYHOME amd64
    Build Date: 13 October 2005
    Before reporting problems, check http://wiki.X.Org
    to make sure that you have the latest version.
    Module Loader present
    -- end dump --

    It will be great if you post versions (and architectures) of X servers.
    Do you suspect any portability (across architecture, i.e. 32bit to 64
    bit) error, coz I think all my OSs are 64bit (AMD64) and one of the OS
    mentioned (Cygwin on Windows, is 32-bit).

    I'll reinspect my source code for any portability issues.

    > --
    > Fred L. Kleinschmidt
    > Boeing Associate Technical Fellow
    > Technical Architect, Software Reuse Project


    But anyways, thanks for your co-operation.
    Ashish Shukla
    --
    http://www.geocities.com/wah_java_dotnet/


  6. Re: Unable to catch ButtonRelease event

    Hi,

    I've also tried the above program on Ubuntu Linux "Breezy" (AMD64) but
    same result.Anyways here is my xorg.conf , may be that might help in
    troubleshoot:

    -- begin dump --
    # Xorg configuration created by system-config-display

    Section "ServerLayout"
    Identifier "single head configuration"
    Screen 0 "Screen0" 0 0
    InputDevice "Mouse0" "CorePointer"
    InputDevice "Keyboard0" "CoreKeyboard"
    EndSection

    Section "Files"
    FontPath "unix/:7100"
    EndSection

    Section "Module"
    Load "dbe"
    Load "extmod"
    Load "fbdevhw"
    Load "glx"
    Load "record"
    Load "freetype"
    Load "type1"
    Load "dri"
    EndSection

    Section "InputDevice"
    Identifier "Keyboard0"
    Driver "kbd"
    Option "XkbModel" "pc105"
    Option "XkbLayout" "us"
    EndSection

    Section "InputDevice"
    Identifier "Mouse0"
    Driver "mouse"
    Option "Protocol" "IMPS/2"
    Option "Device" "/dev/input/mice"
    Option "ZAxisMapping" "4 5"
    Option "Emulate3Buttons" "yes"
    EndSection

    Section "Monitor"
    Identifier "Monitor0"
    VendorName "Monitor Vendor"
    ModelName "Monitor 1024x768"
    HorizSync 31.5 - 54.0
    VertRefresh 50.0 - 120.0
    Option "dpms"
    Option "SyncOnGreen"
    Modeline "1024x768@60" 0 1024 0 0 0 768 0 0 0
    EndSection

    Section "Device"
    Identifier "Videocard0"
    Driver "i810"
    VendorName "Videocard vendor"
    BoardName "Intel Corporation 945G Integrated Graphics Controller"
    EndSection

    Section "Screen"
    Identifier "Screen0"
    Device "Videocard0"
    Monitor "Monitor0"
    DefaultDepth 24
    SubSection "Display"
    Viewport 0 0
    Depth 16
    Modes "800x600" "640x480"
    EndSubSection
    SubSection "Display"
    Viewport 0 0
    Depth 24
    Modes "1024x768" "800x600" "640x480"
    EndSubSection
    EndSection

    Section "DRI"
    Mode 0666
    EndSection

    Section "Extensions"
    Option "Composite" "Enable"
    EndSection
    -- end dump --

    All the X servers I've tested on, have identical configuration files
    except on FreeBSD (AMD64) where I'm using "vesa" driver.

    Ashish Shukla
    --
    http://www.geocities.com/wah_java_dotnet/


  7. Re: Unable to catch ButtonRelease event

    Hi,

    Well that was the bug in the Window Managers metacity, kwin, mwm. When
    I ran the program without any window manager it worked fine.

    Thanks
    Ashish Shukla
    --
    http://www.geocities.com/wah_java_dotnet/


+ Reply to Thread