src/X11Command.h
branchv_0
changeset 2 8d44cba0a3d1
parent 1 3bbf848b3565
child 3 72384bb5c66e
equal deleted inserted replaced
1:3bbf848b3565 2:8d44cba0a3d1
    16  */
    16  */
    17 #pragma once
    17 #pragma once
    18 
    18 
    19 #include <codecvt>
    19 #include <codecvt>
    20 #include <memory>
    20 #include <memory>
       
    21 #include <algorithm>
    21 #include <iostream>
    22 #include <iostream>
    22 #include <stdexcept>
    23 #include <stdexcept>
    23 
    24 
    24 #include <X11/extensions/XInput.h>
    25 #include <X11/extensions/XInput.h>
    25 
    26 
    56 	};
    57 	};
    57 
    58 
    58 
    59 
    59 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
    60 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
    60 
    61 
       
    62 	relpipe::common::type::StringX getDeviceType(Display& display, XDeviceInfo* device) {
       
    63 		if (device && device->type) {
       
    64 			char* raw = XGetAtomName(display.display, device->type);
       
    65 			if (raw) {
       
    66 				relpipe::common::type::StringX type = convertor.from_bytes(raw);
       
    67 				XFree(raw);
       
    68 				transform(type.begin(), type.end(), type.begin(), ::tolower);
       
    69 				return type;
       
    70 			}
       
    71 		}
       
    72 		return L"";
       
    73 	}
       
    74 
    61 	void listInputDevices(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
    75 	void listInputDevices(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
    62 		writer->startRelation(L"x11_input_device",{
    76 		writer->startRelation(L"x11_input_device",{
    63 			{L"id", relpipe::writer::TypeId::INTEGER},
    77 			{L"id", relpipe::writer::TypeId::INTEGER},
    64 			{L"name", relpipe::writer::TypeId::STRING},
    78 			{L"name", relpipe::writer::TypeId::STRING},
    65 			// TODO: device type name and ID
    79 			{L"type", relpipe::writer::TypeId::STRING},
    66 		}, true);
    80 		}, true);
    67 
    81 
    68 		DeviceList devices;
    82 		DeviceList devices;
    69 		devices.items = XListInputDevices(display.display, &devices.count);
    83 		devices.items = XListInputDevices(display.display, &devices.count);
    70 
    84 
    71 		for (int i = 0; i < devices.count; i++) {
    85 		for (int i = 0; i < devices.count; i++) {
    72 			relpipe::common::type::Integer id = (devices.items + i)->id;
    86 			relpipe::common::type::Integer id = (devices.items + i)->id;
    73 			relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name);
    87 			relpipe::common::type::StringX name = convertor.from_bytes((devices.items + i)->name);
       
    88 			relpipe::common::type::StringX type = getDeviceType(display, devices.items + i);
    74 
    89 
    75 			writer->writeAttribute(&id, typeid (id));
    90 			writer->writeAttribute(&id, typeid (id));
    76 			writer->writeAttribute(&name, typeid (name));
    91 			writer->writeAttribute(&name, typeid (name));
    77 
    92 			writer->writeAttribute(&type, typeid (type));
    78 		}
    93 		}
    79 	}
    94 	}
    80 
    95 
    81 	void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
    96 	void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
    82 		writer->startRelation(L"x11_input_event",{
    97 		writer->startRelation(L"x11_input_event",{