/* A program to track the location of the mouse and report it every 5 second */ void main ( int argc, char *argv[] ) { int windowId, x, y; initialize ( ); while (1) { getMouseLocation ( & windowId, &x, &y); printf ("Inside window %d at (%d, %d)\n", windowId, x, y); sleep ( 5 ); } }
void XDO::mouseMoveRelative(int xDiff, int yDiff) { auto size = getScreenSize(); auto loc = getMouseLocation(); loc.first += xDiff; loc.second += yDiff; if(loc.first < 0) loc.first = 0; else if(loc.first > (signed int)size.first) loc.first = size.first - 1; if(loc.second < 0) loc.second = 0; else if(loc.second > (signed int)size.second) loc.second = size.second - 1; xdo_move_mouse(xdo_, loc.first, loc.second,0); }
/** * This function moves the mouse to moved_mouse_x, moved_mouse_y, clamping * between x_start x_end and y_start y_end. * NOTE: EXPECTS MOVED VARIALBES ALREADY IN WINDOW COORDINATES * This is done in the takeAction function */ bool mouse_move(std::string window_name, int moved_mouse_x, int moved_mouse_y, int x_start, int x_end, int y_start, int y_end, bool no_clamp){ //only move the mouse if the window exists. (get focus and mousemove.) if(windowExists(window_name)){ std::vector<int> current_mouse_position = getMouseLocation(); if(current_mouse_position.size() < 2){ return false; } //Explicit clamping. if(moved_mouse_x > x_end || moved_mouse_x < x_start){ std::cout << "Attempted to move outside of the window bounds." << std::endl; return false; } //Explicit clamping if(moved_mouse_y > y_end || moved_mouse_y < y_start){ std::cout << "Attempted to move outside of the window bounds." << std::endl; return false; } if(current_mouse_position[0] == moved_mouse_x && current_mouse_position[1] == moved_mouse_y){ std::cout << "mouse was already in position. Skipping movement." << std::endl; } else{ std::string command = "wmctrl -R " + window_name + " && xdotool mousemove --sync " + std::to_string(moved_mouse_x) + " " + std::to_string(moved_mouse_y); system(command.c_str()); } return true; } else{ std::cout << "Attempted to move mouse on a nonexistent window." << std::endl; return false; } }