Generate X events (Xlib)

Alexandre Quessy listes at sourcelibre.com
Thu Jun 22 02:38:59 PDT 2006


Hi all,

I am trying to generate X events. My main concern is about using that on
Linux (Debian), but if I could do it on other platforms, namely Mac, this
could be great.

I tried to generate events using Xlib. I get no error, but my mouse doesn't
move, and no key seems typed in my X. I wonder if I have done the right
things. Also, it seems like I would have to create very big structs in order
to generate some mouse events or such.

Here is a piece of code trying to generate an event. The makefile is
included. It is intended to build on Debian GNU/Linux.

What am I doing wrong ? How can I generate X events in order to control
graphical application on Linux ? Is there a protection flag in the kernel at
compile time ? Thanks for helping.

-- 
Alexandre Quessy
http://alexandre.quessy.net

======================================================
======================================================
========================= x11test.c =================

/**
 * Generates X11 mouse and keyboard events
 * @author <alex at sourcelibre.com>
 */

#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
    Display *display;
    KeySym keysym;
    int result;
    int i;

    keysym = XK_q;

    /* should be hostname:number.screen_number */
    display = XOpenDisplay(NULL); /* defaults to $DISPLAY */
    if (display == NULL) {
        printf("Error : could not open display.\n");
        return 1;
    }
    printf("Display opened successfully.\n");
    KeyCode keycode = XKeysymToKeycode(display, keysym);
    for (i=0;i<100;i++) {
        result = XTestFakeKeyEvent(display, keycode, True, 0);
        //if (result == 0) printf("failed\n"); else printf("success\n");

        usleep(100);
        result = XTestFakeKeyEvent(display, keycode, False, 0);
        if (result!=0) printf(".");
        //if (result == 0) printf("failed\n"); else printf("success\n");
    }
    printf("\n");
    return 0;
}

======================================================
======================================================
========================= makefile =================


MACOSX_INCLUDES = -L/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/lib \
    -L/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include \
    -L/usr/include/X11

LINUX_INCLUDES = -L/usr/lib/X11 -I/usr/include/X11 -L/usr/X11R6/lib

MACOSX_LINKS = -lXtst -lX11
LINUX_LINKS = -lX11 -lXtst

# noms des executables resultants

FILEZ =  x11test


all: $(FILEZ)
    echo DONE

%: %.c
    gcc $*.c $(LINUX_INCLUDES) $(LINUX_LINKS) -o $*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x.org/archives/xorg/attachments/20060622/09673fa0/attachment.html>


More information about the xorg mailing list