src/X11Command.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 27 Mar 2021 00:29:10 +0100
branchv_0
changeset 2 8d44cba0a3d1
parent 1 3bbf848b3565
child 3 72384bb5c66e
permissions -rw-r--r--
list input devices: add type
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <codecvt>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    21
#include <algorithm>
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
#include <stdexcept>
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include <X11/extensions/XInput.h>
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include "Configuration.h"
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace relpipe {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace in {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace x11 {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
class X11Command {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
private:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    36
	class Display {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    37
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
		::Display* display = nullptr;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    39
		// TODO: more OOP
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
		virtual ~Display() {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
			if (display) XCloseDisplay(display);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
		}
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	};
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	class DeviceList {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		XDeviceInfo* items = nullptr;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		int count = 0;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
		// TODO: more OOP
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		virtual ~DeviceList() {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
			if (items) XFreeDeviceList(items);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		}
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
	};
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
	relpipe::common::type::StringX getDeviceType(Display& display, XDeviceInfo* device) {
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    63
		if (device && device->type) {
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    64
			char* raw = XGetAtomName(display.display, device->type);
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    65
			if (raw) {
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
				relpipe::common::type::StringX type = convertor.from_bytes(raw);
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    67
				XFree(raw);
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    68
				transform(type.begin(), type.end(), type.begin(), ::tolower);
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
				return type;
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    70
			}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
		}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    72
		return L"";
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
	}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
	void listInputDevices(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
		writer->startRelation(L"x11_input_device",{
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
			{L"id", relpipe::writer::TypeId::INTEGER},
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
			{L"name", relpipe::writer::TypeId::STRING},
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
			{L"type", relpipe::writer::TypeId::STRING},
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
		}, true);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
		DeviceList devices;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
		devices.items = XListInputDevices(display.display, &devices.count);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
		for (int i = 0; i < devices.count; i++) {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
			relpipe::common::type::Integer id = (devices.items + i)->id;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
			relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name);
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
			relpipe::common::type::StringX type = getDeviceType(display, devices.items + i);
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
			writer->writeAttribute(&id, typeid (id));
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
			writer->writeAttribute(&name, typeid (name));
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
			writer->writeAttribute(&type, typeid (type));
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		}
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
	}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
	void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
		writer->startRelation(L"x11_input_event",{
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
			{L"device_id", relpipe::writer::TypeId::INTEGER},
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    99
		}, true);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
		// TODO: list events
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
public:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
		Display display;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
		display.display = XOpenDisplay(nullptr);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   110
		if (display.display) {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   111
			if (configuration.listInputDevices) listInputDevices(display, configuration, writer);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   112
			if (configuration.listInputEvents) listInputEvents(display, configuration, writer);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
		} else {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
		}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
};
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
}