Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ReimuNotMoe authored and ReimuNotMoe committed Nov 24, 2018
0 parents commit 871b559
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 2.8)
project(ydotool)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES
ydotool.cpp CommonIncludes.hpp Commands.hpp Commands.cpp
Commands/Type.cpp)


add_executable(ydotool ${SOURCE_FILES})
target_link_libraries(ydotool boost_program_options uInputPlus)
20 changes: 20 additions & 0 deletions Commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
This file is part of ydotool.
Copyright (C) 2018 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "Commands.hpp"

std::unordered_map<std::string, void *> CommandTable;

void InitCommands() {
CommandTable["type"] = (void *)Command_Type;

}
26 changes: 26 additions & 0 deletions Commands.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of ydotool.
Copyright (C) 2018 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#ifndef YDOTOOL_COMMANDS_HPP
#define YDOTOOL_COMMANDS_HPP

#include "CommonIncludes.hpp"

extern std::unordered_map<std::string, void *> CommandTable;

extern void InitCommands();

namespace po = boost::program_options;

extern int Command_Type(int argc, const char *argv[]);

#endif //YDOTOOL_COMMANDS_HPP
163 changes: 163 additions & 0 deletions Commands/Type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
This file is part of ydotool.
Copyright (C) 2018 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "../Commands.hpp"


using namespace uInputPlus;

static uInput *uInput1 = nullptr;
static int time_keydelay = 12;

static void ShowHelp(){
fprintf(stderr, "Usage: type [--delay milliseconds] [--key-delay milliseconds] [--args N] [--file <filepath>] <things to type>\n"
" --help Show this help\n"
" --delay milliseconds Delay time before start typing\n"
" --key-delay milliseconds Delay time between keystrokes. Default 12ms\n"
" --args N How many arguments to expect in the exec command. This \n"
" is useful for ending an exec and continuing with more \n"
" ydotool commands\n"
" --file filepath Specify a file, the contents of which will be be typed \n"
" as if passed as an argument. The filepath may also be \n"
" '-' to read from stdin\n");

}

static int TypeText(const std::string &text) {
int pos = 0;

for (auto &c : text) {

int isUpper = 0;

int key_code;

auto itk = KeyTextStringTable.find(c);

if (itk != KeyTextStringTable.end()) {
key_code = itk->second;
} else {
auto itku = KeyTextStringTableUpper.find(c);
if (itku != KeyTextStringTableUpper.end()) {
isUpper = 1;
key_code = itku->second;
} else {
return -(pos+1);
}
}

int sleep_time;

if (isUpper) {
sleep_time = 250 * time_keydelay;
uInput1->SendKey(KEY_LEFTSHIFT, 1);
usleep(sleep_time);
} else {
sleep_time = 500 * time_keydelay;
}

uInput1->SendKey(key_code, 1);
usleep(sleep_time);
uInput1->SendKey(key_code, 0);
usleep(sleep_time);


if (isUpper) {
uInput1->SendKey(KEY_LEFTSHIFT, 0);
usleep(sleep_time);
}

}

return pos;
}

int Command_Type(int argc, const char *argv[]) {

printf("argc = %d\n", argc);

for (int i=1; i<argc; i++) {
printf("argv[%d] = %s \n", i, argv[i]);
}

int time_delay = 100;
int args_count = -1;
int text_start = -1;

std::string file_path;
std::vector<std::string> extra_args;

try {

po::options_description desc("");
desc.add_options()
("help", "Show this help")
("delay", po::value<int>())
("key-delay", po::value<int>())
("args", po::value<int>())
("file", po::value<std::string>())
("extra-args", po::value(&extra_args));


po::positional_options_description p;
p.add("extra-args", -1);


po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).
positional(p).
run(), vm);
po::notify(vm);


if (vm.count("help")) {
ShowHelp();
return -1;
}

if (vm.count("delay")) {
time_delay = vm["delay"].as<int>();
std::cerr << "Delay was set to "
<< time_delay << " milliseconds.\n";
}

if (vm.count("key-delay")) {
time_keydelay = vm["key-delay"].as<int>();
std::cerr << "Key delay was set to "
<< time_keydelay << " milliseconds.\n";
}

if (vm.count("args")) {
args_count = vm["args"].as<int>();
std::cerr << "Argument count was set to "
<< args_count << ".\n";
}


} catch (std::exception &e) {
fprintf(stderr, "ydotool: type: error: %s\n", e.what());
return 2;
}

uInput1 = new uInput(uInputSetup(uInputDeviceInfo("ydotool virtual device")));

if (time_delay)
usleep(time_delay * 1000);

for (auto &txt : extra_args) {
TypeText(txt);
}

return argc;

}
25 changes: 25 additions & 0 deletions CommonIncludes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
This file is part of ydotool.
Copyright (C) 2018 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#ifndef YDOTOOL_COMMONINCLUDES_HPP
#define YDOTOOL_COMMONINCLUDES_HPP


#include <iostream>
#include <cstdio>
#include <cstdlib>

#include <uInputPlus/uInput.hpp>

#include <boost/program_options.hpp>

#endif //YDOTOOL_COMMONINCLUDES_HPP
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Reimu NotMoe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ydotool
Generic Linux command-line automation tool (no X!)

## Usage
Replace `x` with `y`. :P

Currently implemented command(s):
- type

## Compatibility
You can use it on anything as long as it accepts keyboard/mouse/whatever input. For example, wayland, text console, etc.

## Build
### Dependencies
- [uInputPlus](https://github.com/YukiWorkshop/libuInputPlus)
- boost::program_options

### Compile
Nearly all my projects use CMake. It's very simple:

mkdir build
cd build
cmake ..
make -j `nproc`
55 changes: 55 additions & 0 deletions ydotool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
This file is part of ydotool.
Copyright (C) 2018 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "CommonIncludes.hpp"
#include "Commands.hpp"

static void ShowHelp() {
fprintf(stderr, "Usage: ydotool <cmd> <args>\n"
"Available commands:\n");

for (auto &it : CommandTable) {
fprintf(stderr, " %s\n", it.first.c_str());
}

}

int main(int argc, const char **argv) {

InitCommands();

if (argc < 2) {
ShowHelp();
exit(1);
}

if (strncmp(argv[1], "-h", 2) == 0 || strncmp(argv[1], "--h", 3) == 0 || strcmp(argv[1], "help") == 0) {
ShowHelp();
exit(1);
}

auto it_cmd = CommandTable.find(argv[1]);

if (it_cmd == CommandTable.end()) {
fprintf(stderr, "ydotool: Unknown command: %s\n"
"Run 'ydotool help' if you want a command list\n", argv[1]);
exit(1);
}

auto command = (int (*)(int, const char *[]))it_cmd->second;

fprintf(stderr, "ydotool: Executing `%s' at %p\n", argv[1], command);

int rc = command(argc-1, &argv[1]);


}

0 comments on commit 871b559

Please sign in to comment.