1 /*
2     This file is part of ydotool.
3     Copyright (C) 2018-2019 ReimuNotMoe
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the MIT License.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 
13 #include "Click.hpp"
14 
15 
16 static const char ydotool_tool_name[] = "click";
17 
18 using namespace ydotool::Tools;
19 
Name()20 const char *Click::Name() {
21 	return ydotool_tool_name;
22 }
23 
24 
ShowHelp()25 static void ShowHelp(){
26 	std::cerr << "Usage: click [--delay <ms>] <button>\n"
27 		<< "  --help                Show this help.\n"
28 		<< "  --delay ms            Delay time before start clicking. Default 100ms.\n"
29 		<< "  button                1: left 2: right 3: middle" << std::endl;
30 }
31 
Exec(int argc,const char ** argv)32 int Click::Exec(int argc, const char **argv) {
33 //	std::cout << "argc = " << argc << "\n";
34 //
35 //	for (int i=1; i<argc; i++) {
36 //		std::cout << "argv[" << i << "] = " << argv[i] << "\n";
37 //	}
38 
39 	int time_delay = 100;
40 
41 	std::vector<std::string> extra_args;
42 
43 	try {
44 
45 		po::options_description desc("");
46 		desc.add_options()
47 			("help", "Show this help")
48 			("delay", po::value<int>())
49 			("extra-args", po::value(&extra_args));
50 
51 
52 		po::positional_options_description p;
53 		p.add("extra-args", -1);
54 
55 
56 		po::variables_map vm;
57 		po::store(po::command_line_parser(argc, argv).
58 			options(desc).
59 			positional(p).
60 			run(), vm);
61 		po::notify(vm);
62 
63 
64 		if (vm.count("help")) {
65 			ShowHelp();
66 			return -1;
67 		}
68 
69 		if (vm.count("delay")) {
70 			time_delay = vm["delay"].as<int>();
71 			std::cerr << "Delay was set to "
72 				  << time_delay << " milliseconds.\n";
73 		}
74 
75 		if (extra_args.size() != 1) {
76 			std::cerr << "Which mouse button do you want to click?\n"
77 				     "Use `ydotool " << argv[0] << " --help' for help.\n";
78 
79 			return 1;
80 		}
81 
82 	} catch (std::exception &e) {
83 		std::cerr << "ydotool: click: error: " << e.what() << std::endl;
84 		return 2;
85 	}
86 
87 	if (time_delay)
88 		usleep(time_delay * 1000);
89 
90 	int keycode = BTN_LEFT;
91 
92 	switch (strtol(extra_args[0].c_str(), NULL, 10)) {
93 		case 2:
94 			keycode = BTN_RIGHT;
95 			break;
96 		case 3:
97 			keycode = BTN_MIDDLE;
98 			break;
99 		default:
100 			break;
101 	}
102 
103 	uInputContext->SendKey(keycode, 1);
104 	uInputContext->SendKey(keycode, 0);
105 
106 	return argc;
107 }
108 
109