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

/**
 * Relational pipes
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the 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. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <codecvt>
#include <memory>
#include <algorithm>
#include <iostream>
#include <stdexcept>

#include <X11/extensions/XInput.h>

#include "Configuration.h"

namespace relpipe {
namespace in {
namespace x11 {

class X11Command {
private:

	class Display {
	public:
		::Display* display = nullptr;
		// TODO: more OOP

		virtual ~Display() {
			if (display) XCloseDisplay(display);
		}

	};

	class DeviceList {
	public:
		XDeviceInfo* items = nullptr;
		int count = 0;
		// TODO: more OOP

		virtual ~DeviceList() {
			if (items) XFreeDeviceList(items);
		}

	};


	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default

	relpipe::common::type::StringX getDeviceType(Display& display, XDeviceInfo* device) {
		if (device && device->type) {
			char* raw = XGetAtomName(display.display, device->type);
			if (raw) {
				relpipe::common::type::StringX type = convertor.from_bytes(raw);
				XFree(raw);
				transform(type.begin(), type.end(), type.begin(), ::tolower);
				return type;
			}
		}
		return L"";
	}

	void listInputDevices(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
		writer->startRelation(L"x11_input_device",{
			{L"id", relpipe::writer::TypeId::INTEGER},
			{L"name", relpipe::writer::TypeId::STRING},
			{L"type", relpipe::writer::TypeId::STRING},
		}, true);

		DeviceList devices;
		devices.items = XListInputDevices(display.display, &devices.count);

		for (int i = 0; i < devices.count; i++) {
			relpipe::common::type::Integer id = (devices.items + i)->id;
			relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name);
			relpipe::common::type::StringX type = getDeviceType(display, devices.items + i);

			writer->writeAttribute(&id, typeid (id));
			writer->writeAttribute(&name, typeid (name));
			writer->writeAttribute(&type, typeid (type));
		}
	}

	void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
		writer->startRelation(L"x11_input_event",{
			{L"device_id", relpipe::writer::TypeId::INTEGER},
		}, true);

		// TODO: list events
	}

public:

	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
		Display display;
		display.display = XOpenDisplay(nullptr);

		if (display.display) {
			if (configuration.listInputDevices) listInputDevices(display, configuration, writer);
			if (configuration.listInputEvents) listInputEvents(display, configuration, writer);
		} else {
			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
		}
	}
};

}
}
}