src/X11Command.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 26 Mar 2021 22:29:46 +0100
branchv_0
changeset 1 3bbf848b3565
parent 0 3493d6e7016e
child 2 8d44cba0a3d1
permissions -rw-r--r--
Configuration + CLIParser + command for listing X11 devices
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>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <stdexcept>
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <X11/extensions/XInput.h>
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace x11 {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class X11Command {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
	class Display {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    36
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    37
		::Display* display = nullptr;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
		// TODO: more OOP
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    39
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
		virtual ~Display() {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
			if (display) XCloseDisplay(display);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
		}
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
	class DeviceList {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
		XDeviceInfo* items = nullptr;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		int count = 0;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		// TODO: more OOP
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		virtual ~DeviceList() {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
			if (items) XFreeDeviceList(items);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
		}
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
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	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
    60
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
	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
    62
		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
    63
			{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
    64
			{L"name", relpipe::writer::TypeId::STRING},
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
			// TODO: device type name and ID
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
		}, true);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		DeviceList devices;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
		devices.items = XListInputDevices(display.display, &devices.count);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		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
    72
			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
    73
			relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
			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
    76
			writer->writeAttribute(&name, typeid (name));
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		}
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
	}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
	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
    82
		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
    83
			{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
    84
		}, true);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
		// TODO: list events
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
public:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	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
    92
		Display display;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
		display.display = XOpenDisplay(nullptr);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		if (display.display) {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
			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
    97
			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
    98
		} else {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    99
			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
   100
		}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	}
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
}
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
}