How do we simulate a mouse click with Xlib / C? - c

I want to find C / Xorg code to 'enter' a left mouse button click. I'd expect a single line of code but the only things I've found written in C are about two dozen lines long and they don't work anyway :( It seems it can be done in Windows, but I'm in Linux.
The reason for the question is that I've written a utility that lets me move my mouse pointer between several screens using the keyboard. The only problem is that if I move to a location where window abc used to be but another window xyz has been loaded on top of that same location, the mouse pointer moves to xyz just fine, but xyz doesn't have focus -- until I left click the mouse. So, I want to build the 'click' into my code.
The code I tried that didn't work was based on XSendEvent().

Yes, I've more or less come to understand. Anyway it seems this is the way:
{
#include <X11/extensions/XTest.h>
XTestFakeButtonEvent(display, 1, True, CurrentTime);
XTestFakeButtonEvent(display, 1, False, CurrentTime);
XFlush(display);
}
... and add " -lXtst " to the LDFLAGS line in the Makefile.
Xlib seems to be so bloody difficult. I've had advice to use other libraries, I wish I knew how to go about changing over.
Thanks R.

Why not just directly raise/focus the window rather than trying to make a fake click event? That should be a lot more reliable and work with all window managers, even non-click-to-focus ones.

xdotool is the easy way of doing this. It's a command line tool. You can use it in simple scripts. For example:
#!/bin/sh
xdotool mousemove x y
xdotool click 1

Related

gtk drawing area does not redraw content

I am on an Ubuntu 19.10 64 bit system
Source files:
https://drive.google.com/open?id=1I4ejOHNXqbAOkTbIyJR0lvldsAwiPPqI
Problem:
I am working on a simple drawing program using gtk and glade with c. Right now I am stuck into implementing undoing behavior. I have two problems. There are cases for when I press the undo button.
1)
When I have one stroke or one dot on the canvas, it works perfect as I experienced. Canvas clears itself.
2)
When I have two strokes or two dots on the canvas, if I click undo once, canvas does not redraw itself, unless I draw again; if I click twice, it works perfect like in the first case, canvas clears.
3)
When I have more than two strokes or two dots on the canvas, if I click undo twice, the program stops working waiting me to force quit.
I am a beginner at using glade, gtk3, and cairo libraries. I have been searching about the issue for days. However, the resources are scarce compared to trending frameworks and libraries. Furthermore, I couldn't match the problem I need. I appreciate any help.
EDIT I forgot to add that the halting problem occurs after the function , on_undo_clicked exits the second time (twice undo click).
I solved the problem.
In the function, addPoint I forgot that the next struct pointer's pre pointer may be a dangling pointer. It creates the problem.

Xlib - Two issues with call to XMoveResizeWindow

I am new to Xlib (in C) and am having two issues when calling the XMoveResizeWindow function.
ex.
XMoveResizeWindow(display, window_id, move_x, move_y, resize_x, resize_y);
1) After the call, the window I move will reposition itself correctly, however, if I select the window with the pointer after the move, it will instantly revert back to the position it held prior to the move. I assume I have to somehow 'update' the X11 server after it's moved with the windows new position?
2) Secondly, in regards to the resize of the window. My window is essentially being truncated by the x and y values entered, instead of resized. In other words, instead of the entire window shrinking down, the right and bottom sides of the window are cut off from view. Is there a way to instead resize the entire window?
--I am sorry I am unable to submit my complete code, however, I believe my issues are due solely to my lack of understanding of Xlib and this particular function's operations. As such, I am not neccessarily looking for specific code as a solution, merely an explination or suggestion on how I should go about implementing a solution.
Thank you.
I believe there is something wrong with your code, since that code is not available i'll point to an example.
Examples are given at readme itself.

How to handle mouse motion events in GTK3?

I am trying to implement the following feature using C/GTK3/Cairo:
-Left click on an GtkDrawingArea Widget and printf the coordinates Xo and Yo.
-While keeping the left button down, move the mouse and draw a line conecting (Xo,Yo) to the current mouse position.
-Release the left mouse button and printf("something")
How do I do this? Anyone knows of a good tutorial showing how to handle mouse clicl-move events?
So far, the best I found was this zetcode lines (which shows how to handle mouse click events but not button-down/move/button-up and this , which explains how to change the mouse cursor when hovering over a Widget.
Thanks
Did you see this GtkDrawingArea demo from the Gtk people? This one is written in C, but there is a Python version of the same program (links updated - thanks #kyuuhachi).
Anyway, in the constructor (__init__), calls are connected to the motion_notify_event.
You also need to connect to the button_press_event and the button_release_event.
Then, on button press, you save the coordinates of the start point. (and save it to the end point too, which are the same for now).
On each motion_notify_event, you delete the previous line (by overwriting), and redraw it to the new end point.
Finally, when the button is released, the line is final.
It's much easier if you use a canvas widget, for example GooCanvas, which takes care of most of the updating. You can just update the coordinates of the line object, and it will move itself. Also you can easily remove lines. The 'algorithm' is similar as above:
Connect button_press_event, button_release_event, and motion_notifyevent to the canvas,
When a button press occurs, create a GooCanvas.polyline object, and set begin and endpoint,
Update the endpoint on each motion_notify_event
Finalize with a button_release_event.

mouse click function for linux

I am trying to build a virtual mouse by detecting finger movements with opencv. The finger detection is done. But I am stuck in mouse click function.
My work is similar to this :
http://8a52labs.wordpress.com/tag/colored-finger-tracking
But he has done in Windows. I am working in linux. I just want a library which provides me functions for doing left click, right click, mouse movement etc.
I am working with Fedora 16 , opencv and QT.
P.S - I have already moved the mouse cursor in Qt by QCursor::setPos(mouseX,mouseY);
But there is no function to make left click and right click.
If you want to do this system-wide, rather than just restricted to your Qt application, see this answer.
You need to post a QMouseEvent through QCoreApplication::postEvent(QObject* receiver, QEvent* event).

OpenGL mouse "lock"

How would one "lock" the mouse to a certain OpenGL window. Sort of like how it is done in Minecraft.
Is GameDev a better place to ask?
Like Robert said in the comment, OpenGL doesn't actually do user input.
However, there are libraries that can abstract the platform dependent part away, such as LibSDL. You can use it to grab the mouse to your window.
A similar question has been asked here, where a programmer used a class called robot to change the mouse position.
Code: Robot.moveMouse(x,y)
This code was written in java, however, There are several classes like robot that can do the trick!
One option is to constantly move the mouse to the center of the screen or wherever you want it.

Resources