I'm creating an application on UNIX which uses the command xdotool
to fake mouse/keyboard actions. The command works as expected, and the example line:
xdotool mousemove 20 50 && xdotool mousedown 1 && xdotool mouseup 1
moves the mouse and performs a simple click as expected.
However, when trying to implement a call to this command from a C program requires to split the command and insert a delay between both of them (mousemove
, then wait, then mousedown && mouseup
).
These are the relevant pieces of code involved:
Generation of the command:
button = button + 1;
std::string moveCommand =
"xdotool mousemove "
+ std::to_string(realPosX)
+ " "
+ std::to_string(realPosY);
std::string btnStr = std::to_string(button);
std::string clickCommand =
"xdotool mousedown "
+ btnStr
+ " && xdotool mouseup "
+ btnStr;
exec(moveCommand + " && " + clickCommand);
//std::this_thread::sleep_for(std::chrono::duration<double>(0.1));
//exec(clickCommand);
exec function:
std::string exec(const char * cmd)
{
/*
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), 128, pipe.get()) != nullptr)
{
result += buffer.data();
}
return result;
*/
std::cout << cmd << std::endl;
int retValue = system(cmd);
if(retValue != 0)
{
std::cerr << "Command \"" << cmd << "\" exited with value " << retValue << std::endl;
}
return "";
}
std::string exec(const std::string & st)
{
return exec(st.c_str());
}
So far I have tried:
Executing the commands separately using
popen
Executing the commands together joined by
&&
usingpopen
The same as previous, using
system
since I read it makes a blocking call to the command (Thinking that it could be the problem).
The only way it has work until now is by splitting the commands and putting a delay between both calls (as can be seen in the generation of command).
Without the delay, the mouse is moved, but the click has no effect. No error is returned from the execution.
As you can see in the exec()
function, I print the command to screen, and then I run it manually to make sure the command is well-formed. The command run as expected on the shell.
I dont know if I am missing something when calling the popen()/system()
functions that is causing this behaviour