22
votes

how can I set the mouse cursor position in an X window using a C program under Linux? thanks :) (like setcursorpos() in WIN)

EDIT: I've tried this code, but doesn't work:

#include <curses.h>

main(){
 move(100, 100);
 refresh();
}
6
Your cursor position...in what? An X window? A terminal window? vi?T.J. Crowder
in an X window.. but I don't have to get the cursor position, I have to set it everywhere in the screenfrx08
(I've edited your question for you; you really wanted to do that when you replied to my comment.) You see the value of being specific. :-) You now have three answers completely unrelated to the question (they're all about setting cursor position in terminal windows).T.J. Crowder
thanks but I didn't know if I explain myself, I need to move mouse programmaticlyfrx08
In X Window System, the mouse cursor refers to the graphic icon, whereas you seem to want to move the pointer itself (which moves the "hotspot" as well as also moving the cursor icon).mctylr

6 Answers

33
votes

12.4 - Moving the Pointer

Although movement of the pointer normally should be left to the control of the end user, sometimes it is necessary to move the pointer to a new position under program control.

To move the pointer to an arbitrary point in a window, use XWarpPointer().


Example:

Display *dpy;
Window root_window;

dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);
XFlush(dpy); // Flushes the output buffer, therefore updates the cursor's position. Thanks to Achernar.
13
votes

This is old, but in case someone else comes across this issue. The answer provided by tusbar was correct but the command XFlush(dpy) must be added at the end to update the cursor's position. The libraries needed are: X11/X.h, X11/Xlib.h, X11/Xutil.h.

int main(int argc, char *argv[]){
         //Get system window
         Display *dpy;
         Window root_window;

         dpy = XOpenDisplay(0);
         root_window = XRootWindow(dpy, 0);
         XSelectInput(dpy, root_window, KeyReleaseMask);

         XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, 100, 100);

         XFlush(dpy);

         return 0;}

PS: You can use this command to build the code gcc main.c -lX11

5
votes

You want to write a X11 program that uses the call XWarpPointer function to move the point to a relative or global position. (Xlib Programming Manual, Vol 1)

In general, using Xlib for programming the X Window System, is the most basic, and quite low-level interface for graphical programming on a Unix or Linux system. Most applications developed nowadays using a higher level library such as GTK or Qt for developing their GUI applications.

Curses or NCurses (New Curses) is for programming terminal-oriented interfaces, so are not useful in this case.

3
votes

You can use XWarpPointer to move the mouse cursor in an X window.

XWarpPointer(display, src_w, dest_w, src_x, src_y, src_width, src_height, dest_x, 
                dest_y)
        Display *display;
        Window src_w, dest_w;
        int src_x, src_y;
        unsigned int src_width, src_height;
        int dest_x, dest_y;
3
votes

use Jordan Sissel's excellent utility xdotool.

http://www.semicomplete.com/projects/xdotool/

it provide XWarpPointer wrapper function like xdo_mousemove(), here is some example:

Display *display = NULL;
xdo_t *xdo = NULL;

void mouse_left_down(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0)
  xdo_mousedown(xdo, CURRENTWINDOW, Button1); 
}

void mouse_left_up(int x, int y)
{
  xdo_mouseup(xdo, CURRENTWINDOW, Button1, 1, 0); 
}

void mouse_left_double_click(int x, int y)
{
  xdo_mousemove(xdo, x, y, 0);
  xdo_click_multiple(xdo, CURRENTWINDOW, Button1, 1, 0);
  doubleclick = TRUE;
}

int main()
{

  display = XOpenDisplay(NULL);
  if(display == NULL)
  {
    fprintf(stderr, "can't open display!\n");
    return -1;
  }
  xdo = xdo_new((char*) display);

  //some task here
  // ...

  return 0;
}
2
votes

All modern terminals should support ANSI escape sequences. For anything more complicated (and more portable), however, you should look into using a library such as ncurses.