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

/**
 * 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 <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

	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},
			// TODO: device type name and ID
		}, 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);

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

		}
	}

	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.");
		}
	}
};

}
}
}