src/X11Command.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 11 Apr 2021 14:37:11 +0200
branchv_0
changeset 11 3948229234cd
parent 10 803beead60c4
permissions -rw-r--r--
list windows: process (PID): inherit PID recursively from parent if the _NET_WM_PID property is missing
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>
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    26
#include <X11/Xutil.h>
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    27
#include <X11/Xatom.h>
0
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
#include "Configuration.h"
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace relpipe {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace in {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace x11 {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
class X11Command {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
private:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
	class Display {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    39
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
		::Display* display = nullptr;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
		// TODO: more OOP
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
		virtual ~Display() {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
			if (display) XCloseDisplay(display);
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
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
	class Device {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
	public:
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
		XDevice* device = nullptr;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
		// TODO: more OOP
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
		virtual ~Device() {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
			if (device) XFree(device);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
		}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
	};
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
	class DeviceInfoList {
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
	public:
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
		XDeviceInfo* items = nullptr;
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
		int size = 0;
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
		// TODO: more OOP
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
		XDeviceInfo* operator[](int index) const {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
			if (index < size) return items + index;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
			else throw std::out_of_range("invalid index of XDeviceInfo");
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
		}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    69
		virtual ~DeviceInfoList() {
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
			if (items) XFreeDeviceList(items);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		}
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
	};
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    75
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: use platform encoding as default
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
	relpipe::common::type::StringX getDeviceType(const Display& display, const XDeviceInfo* device) {
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    78
		if (device && device->type) {
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
			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
    80
			if (raw) {
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
				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
    82
				XFree(raw);
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    83
				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
    84
				return type;
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    85
			}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    86
		}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    87
		return L"";
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
	}
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    90
	void listInputDevices(const Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
		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
    92
			{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
    93
			{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
    94
			{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
    95
		}, true);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    97
		DeviceInfoList devices;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    98
		devices.items = XListInputDevices(display.display, &devices.size);
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   100
		for (int i = 0; i < devices.size; i++) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   101
			relpipe::common::type::Integer id = devices[i]->id;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   102
			relpipe::common::type::StringX name = convertor.from_bytes(devices[i]->name);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
			relpipe::common::type::StringX type = getDeviceType(display, devices[i]);
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   105
			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
   106
			writer->writeAttribute(&name, typeid (name));
2
8d44cba0a3d1 list input devices: add type
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   107
			writer->writeAttribute(&type, typeid (type));
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		}
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
		relationalWriterFlush();
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   111
	}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
	/**
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
	 * Fields of this structure are filled in DeviceKeyPress(), DeviceButtonPress() … macros.
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
	 */
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
	class EventType {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
	private:
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
		const static int UNUSED = -1;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   119
	public:
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   120
		int motion = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   121
		int buttonPress = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   122
		int buttonRelease = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   123
		int keyPress = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   124
		int keyRelease = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   125
		int proximityIn = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   126
		int proximityOut = UNUSED;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
	} eventType;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   128
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   129
	void registerEvents(const Display& display, int screen, const XDeviceInfo* deviceInfo) {
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   130
		bool registerProximityEvents = false; // TODO: proximity?
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   131
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   132
		Device device;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   133
		Window window;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   134
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   135
		int eventCount = 0;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   136
		XEventClass events[7]; // TODO: check array size
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   137
		XInputClassInfo* classInfo;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   138
		int classIndex;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   139
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   140
		window = RootWindow(display.display, screen);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   141
		// TODO: configurable window from which we capture the events or optionally open our own window (can also provide some visual feedback/info)
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   142
		// Currently we can do something like:
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   143
		//   Xephyr  :5 -screen 1920x1080
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   144
		//   DISPLAY=:5 relpipe-in-x11 --list-input-devices false --list-input-events true | …
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   145
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   146
		device.device = XOpenDevice(display.display, deviceInfo->id);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   147
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   148
		if (device.device) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   149
			if (device.device->num_classes > 0) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   150
				for (classInfo = device.device->classes, classIndex = 0; classIndex < deviceInfo->num_classes; classInfo++, classIndex++) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   151
					if (classInfo->input_class == KeyClass) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   152
						DeviceKeyPress(device.device, eventType.keyPress, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   153
						eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   154
						DeviceKeyRelease(device.device, eventType.keyRelease, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   155
						eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   156
					} else if (classInfo->input_class == ButtonClass) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   157
						DeviceButtonPress(device.device, eventType.buttonPress, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   158
						eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   159
						DeviceButtonRelease(device.device, eventType.buttonRelease, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   160
						eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   161
					} else if (classInfo->input_class == ValuatorClass) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   162
						DeviceMotionNotify(device.device, eventType.motion, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   163
						eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   164
						if (registerProximityEvents) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   165
							ProximityIn(device.device, eventType.proximityIn, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   166
							eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   167
							ProximityOut(device.device, eventType.proximityOut, events[eventCount]);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   168
							eventCount++;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   169
						}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   170
					}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   171
				}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   172
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   173
				int result = XSelectExtensionEvent(display.display, window, events, eventCount);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   174
				if (result != Success) throw std::logic_error("Unable to register events from device: " + std::to_string(deviceInfo->id) + " Result: " + std::to_string(result));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   175
			}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   176
		} else {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   177
			throw std::invalid_argument("Unable to open the device: " + std::to_string(deviceInfo->id));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   178
		}
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   179
	}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   181
	void listInputEvents(Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   182
		writer->startRelation(L"x11_input_event",{
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   183
			{L"device", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   184
			{L"type", relpipe::writer::TypeId::STRING},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   185
			{L"state", relpipe::writer::TypeId::STRING},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   186
			{L"key", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   187
			{L"button", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   188
			{L"x", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   189
			{L"y", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   190
			// {L"x_root", relpipe::writer::TypeId::INTEGER},
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   191
			// {L"y_root", relpipe::writer::TypeId::INTEGER},
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   192
		}, true);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   193
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   194
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   195
		{
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   196
			DeviceInfoList devices;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   197
			devices.items = XListInputDevices(display.display, &devices.size);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   198
			for (int i = 0; i < devices.size; i++) {
4
820c4a4a3ed8 register events only for supported devices
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   199
				if (devices[i]->type) {
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   200
					for (int screenCount = XScreenCount(display.display), screen = 0; screen < screenCount; screen++)
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   201
						registerEvents(display, screen, devices[i]);
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   202
				}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   203
			}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   204
		}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   205
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   206
		for (XEvent event; true;) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   207
			XNextEvent(display.display, &event);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   208
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   209
			relpipe::common::type::Integer device = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   210
			relpipe::common::type::StringX type;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   211
			relpipe::common::type::StringX state;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   212
			relpipe::common::type::Integer button = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   213
			relpipe::common::type::Integer key = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   214
			relpipe::common::type::Integer x = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   215
			relpipe::common::type::Integer y = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   216
			// relpipe::common::type::Integer xRoot = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   217
			// relpipe::common::type::Integer yRoot = -1; // TODO: null
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   218
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   219
			if (event.type == eventType.motion) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   220
				XDeviceMotionEvent* e = (XDeviceMotionEvent*) & event;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   221
				device = e->deviceid;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   222
				type = L"motion";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   223
				x = e->x;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   224
				y = e->y;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   225
				// xRoot = e->x_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   226
				// yRoot = e->y_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   227
			} else if (event.type == eventType.buttonPress || event.type == eventType.buttonRelease) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   228
				XDeviceButtonEvent* e = (XDeviceButtonEvent*) & event;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   229
				device = e->deviceid;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   230
				type = L"button";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   231
				state = event.type == eventType.buttonPress ? L"pressed" : L"released";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   232
				button = e->button;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   233
				x = e->x;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   234
				y = e->y;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   235
				// xRoot = e->x_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   236
				// yRoot = e->y_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   237
			} else if (event.type == eventType.keyPress || event.type == eventType.keyRelease) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   238
				XDeviceKeyEvent* e = (XDeviceKeyEvent*) & event;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   239
				device = e->deviceid;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   240
				type = L"key";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   241
				state = event.type == eventType.keyPress ? L"pressed" : L"released";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   242
				key = e->keycode;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   243
				x = e->x;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   244
				y = e->y;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   245
				// xRoot = e->x_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   246
				// yRoot = e->y_root;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   247
			} else if (event.type == eventType.proximityIn || event.type == eventType.proximityOut) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   248
				XProximityNotifyEvent* e = (XProximityNotifyEvent*) & event;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   249
				device = e->deviceid;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   250
				type = L"proximity";
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   251
			}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   252
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   253
			writer->writeAttribute(&device, typeid (device));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   254
			writer->writeAttribute(type);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   255
			writer->writeAttribute(state);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   256
			writer->writeAttribute(&key, typeid (key));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   257
			writer->writeAttribute(&button, typeid (button));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   258
			writer->writeAttribute(&x, typeid (x));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   259
			writer->writeAttribute(&y, typeid (y));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   260
			// writer->writeAttribute(&xRoot, typeid (xRoot));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   261
			// writer->writeAttribute(&yRoot, typeid (yRoot));
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   262
			relationalWriterFlush();
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   263
		}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   264
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   265
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   266
	relpipe::common::type::StringX fetchWindowName(const Display& display, Window window) {
9
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   267
		std::string name;
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   268
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   269
		XTextProperty property;
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   270
		if (XGetWMName(display.display, window, &property) && property.nitems > 0) {
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   271
			int count = 0;
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   272
			int result;
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   273
			char** list = nullptr;
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   274
			result = XmbTextPropertyToTextList(display.display, &property, &list, &count);
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   275
			if ((result == Success || result > 0) && list) for (int i = 0; i < count; i++) name.append(list[i]);
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   276
			// else name = (char*) property.value; // TODO: use property.value?
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   277
			XFreeStringList(list);
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   278
		}
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   279
		XFree(property.value);
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   280
c8d681753bc2 list windows: window names
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   281
		return convertor.from_bytes(name);
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   282
	}
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   283
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   284
	void* findWindowProperty(const Display& display, Window window, Atom atom, long* length, Atom* type, int* size) {
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   285
		// FIXME: review code
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   286
		Atom actualType;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   287
		int actualFormat;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   288
		unsigned long itemCount;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   289
		unsigned long byteCount;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   290
		unsigned long bytesAfter;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   291
		unsigned char* result = nullptr;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   292
		int status;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   293
		unsigned long max_len = 500000;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   294
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   295
		status = XGetWindowProperty(display.display, window, atom, 0, (max_len + 3) / 4, False, AnyPropertyType, &actualType, &actualFormat, &itemCount, &bytesAfter, &result);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   296
		if (status == BadWindow) throw std::invalid_argument("Unable to get window property: BadWindow");
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   297
		if (status != Success) throw std::invalid_argument("Unable to get window property: other error: " + std::to_string(status));
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   298
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   299
		if (actualFormat == 32) byteCount = sizeof (long);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   300
		else if (actualFormat == 16) byteCount = sizeof (short);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   301
		else if (actualFormat == 8) byteCount = 1;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   302
		else if (actualFormat == 0) byteCount = 0;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   303
		else throw std::invalid_argument("Unable to get window property: unexpected format");
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   304
		*length = min(itemCount * byteCount, max_len);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   305
		*type = actualType;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   306
		*size = actualFormat;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   307
		return (void*) result;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   308
	}
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   309
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   310
	relpipe::common::type::Integer findWindowPropertyInteger(const Display& display, Window window, std::string propertyName) {
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   311
		long length = -1;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   312
		Atom type = -1;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   313
		int size = -1;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   314
		void* rawResult = findWindowProperty(display, window, XInternAtom(display.display, propertyName.c_str(), False), &length, &type, &size);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   315
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   316
		relpipe::common::type::Integer result = -1; // TODO: null
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   317
		if (size = 32 && type == XA_CARDINAL) result = *((uint32_t*) rawResult);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   318
		// TODO: review code
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   319
		// TODO: support also other types (other integers, boolean, string etc.)
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   320
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   321
		XFree(rawResult);
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   322
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   323
		return result;
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   324
	}
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   325
11
3948229234cd list windows: process (PID): inherit PID recursively from parent if the _NET_WM_PID property is missing
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   326
	void listWindow(const Display& display, Window window, relpipe::common::type::Integer level, relpipe::common::type::Integer parentProcess, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   327
		Window rootWindow;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   328
		Window parentWindow;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   329
		unsigned int childrenCount;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   330
		Window* childrenList;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   331
		if (!XQueryTree(display.display, window, &rootWindow, &parentWindow, &childrenList, &childrenCount))
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   332
			throw std::invalid_argument("Unable to query the tree for window: " + std::to_string(window));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   333
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   334
		XWindowAttributes windowAttributes;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   335
		if (!XGetWindowAttributes(display.display, window, &windowAttributes))
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   336
			throw std::invalid_argument("Unable to get attributes for window: " + std::to_string(window));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   337
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   338
		XClassHint classHint;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   339
		relpipe::common::type::StringX resClass;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   340
		relpipe::common::type::StringX resName;
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   341
		if (XGetClassHint(display.display, window, &classHint)) {
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   342
			if (classHint.res_class) resClass = convertor.from_bytes(classHint.res_class);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   343
			if (classHint.res_name) resName = convertor.from_bytes(classHint.res_name);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   344
			XFree(classHint.res_class);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   345
			XFree(classHint.res_name);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   346
		}
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   347
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   348
		// The PID as reported by the application itself in the property. Some applications do not report their PID.
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   349
		relpipe::common::type::Integer processId = findWindowPropertyInteger(display, window, "_NET_WM_PID");
11
3948229234cd list windows: process (PID): inherit PID recursively from parent if the _NET_WM_PID property is missing
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   350
		if (configuration.inheritProcessIdInWindowList && processId < 0) processId = parentProcess;
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   351
		// TODO: support also other properties: customizable through options or all in a nested structure/relation/xml
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   352
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   353
		writer->writeAttribute(std::to_wstring(window));
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   354
		writer->writeAttribute(std::to_wstring(windowAttributes.root));
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   355
		writer->writeAttribute(std::to_wstring(parentWindow));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   356
		writer->writeAttribute(&level, typeid (level));
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   357
		writer->writeAttribute(&processId, typeid (processId));
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   358
		writer->writeAttribute(fetchWindowName(display, window));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   359
		writer->writeAttribute(resClass);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   360
		writer->writeAttribute(resName);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   361
		writer->writeAttribute(std::to_wstring(windowAttributes.x));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   362
		writer->writeAttribute(std::to_wstring(windowAttributes.y));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   363
		writer->writeAttribute(std::to_wstring(windowAttributes.width));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   364
		writer->writeAttribute(std::to_wstring(windowAttributes.height));
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   365
11
3948229234cd list windows: process (PID): inherit PID recursively from parent if the _NET_WM_PID property is missing
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   366
		for (unsigned int i = 0; i < childrenCount; i++) listWindow(display, childrenList[i], level + 1, processId, configuration, writer, relationalWriterFlush);
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   367
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   368
		XFree(childrenList);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   369
	}
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   370
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   371
	void listWindows(const Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   372
		writer->startRelation(L"x11_window",{
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   373
			{L"id", relpipe::writer::TypeId::INTEGER},
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   374
			{L"root", relpipe::writer::TypeId::INTEGER},
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   375
			{L"parent", relpipe::writer::TypeId::INTEGER},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   376
			{L"level", relpipe::writer::TypeId::INTEGER},
10
803beead60c4 list windows: process (PID): as reported by the application itself in the _NET_WM_PID property
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   377
			{L"process", relpipe::writer::TypeId::INTEGER},
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   378
			{L"name", relpipe::writer::TypeId::STRING},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   379
			{L"res_class", relpipe::writer::TypeId::STRING},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   380
			{L"res_name", relpipe::writer::TypeId::STRING},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   381
			{L"x", relpipe::writer::TypeId::INTEGER},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   382
			{L"y", relpipe::writer::TypeId::INTEGER},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   383
			{L"width", relpipe::writer::TypeId::INTEGER},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   384
			{L"height", relpipe::writer::TypeId::INTEGER},
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   385
		}, true);
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   386
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   387
		for (int screenCount = XScreenCount(display.display), screen = 0; screen < screenCount; screen++) {
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   388
			Window root = RootWindow(display.display, screen);
11
3948229234cd list windows: process (PID): inherit PID recursively from parent if the _NET_WM_PID property is missing
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   389
			listWindow(display, root, 0, -1, configuration, writer, relationalWriterFlush);
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   390
		}
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   391
	}
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   392
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   393
	void listScreens(const Display& display, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   394
		writer->startRelation(L"x11_screen",{
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   395
			{L"id", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   396
			{L"root", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   397
			{L"width", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   398
			{L"height", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   399
			// {L"width_mm", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   400
			// {L"height_mm", relpipe::writer::TypeId::INTEGER},
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   401
		}, true);
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   402
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   403
		for (int screenCount = XScreenCount(display.display), screen = 0; screen < screenCount; screen++) {
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   404
			Window root = RootWindow(display.display, screen);
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   405
			XWindowAttributes attributes;
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   406
			if (!XGetWindowAttributes(display.display, root, &attributes))
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   407
				throw std::invalid_argument("Unable to get attributes for window: " + std::to_string(root));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   408
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   409
			writer->writeAttribute(std::to_wstring(screen));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   410
			writer->writeAttribute(std::to_wstring(root));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   411
			writer->writeAttribute(std::to_wstring(attributes.width));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   412
			writer->writeAttribute(std::to_wstring(attributes.height));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   413
			// writer->writeAttribute(std::to_wstring(attributes.screen->mwidth));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   414
			// writer->writeAttribute(std::to_wstring(attributes.screen->mheight));
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   415
		}
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   416
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   417
	}
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   418
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   419
	static int handleXError(::Display* display, XErrorEvent* errorEvent) {
5
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   420
		std::wcerr << L"X11 error:"
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   421
				<< L" display=" << errorEvent->display
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   422
				<< L" error_code=" << errorEvent->error_code
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   423
				<< L" minor_code=" << errorEvent->minor_code
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   424
				<< L" request_code=" << errorEvent->request_code
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   425
				<< L" resourceid=" << errorEvent->resourceid
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   426
				<< L" serial=" << errorEvent->serial
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   427
				<< L" type=" << errorEvent->type
7afa994c98f0 print X11 errors
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   428
				<< std::endl;
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   429
		return 0;
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   430
	}
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   431
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   432
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   433
public:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   434
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   435
	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   436
		XSetErrorHandler(handleXError);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   437
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   438
		Display display;
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   439
		display.display = XOpenDisplay(nullptr);
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   440
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   441
		if (display.display) {
3
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   442
			if (configuration.listInputDevices) listInputDevices(display, configuration, writer, relationalWriterFlush);
72384bb5c66e list input events: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   443
			if (configuration.listInputEvents) listInputEvents(display, configuration, writer, relationalWriterFlush);
7
039b3f8a3442 list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   444
			if (configuration.listWindows) listWindows(display, configuration, writer, relationalWriterFlush);
8
b498160cc2ab list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   445
			if (configuration.listScreens) listScreens(display, configuration, writer, relationalWriterFlush);
1
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   446
		} else {
3bbf848b3565 Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   447
			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
   448
		}
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   449
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   450
};
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   451
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   452
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   453
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   454
}