x11.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 26 Nov 2023 16:27:50 +0100
branchv_0
changeset 0 bb715a82a8f1
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * ShaderShark
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
bb715a82a8f1 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
bb715a82a8f1 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
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
bb715a82a8f1 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,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
bb715a82a8f1 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
bb715a82a8f1 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/>.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <cstring>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <X11/Xatom.h>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <X11/XKBlib.h>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <X11/keysymdef.h>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
/**
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 * Publish our PID (process ID) through the X11 window property.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 */
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
inline
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
void setX11PID(Display* dpy, Window win) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	Atom _NET_WM_PID = XInternAtom(dpy, "_NET_WM_PID", False);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	pid_t pid = getpid();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	XChangeProperty(dpy, win, _NET_WM_PID, XA_CARDINAL,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
			32, PropModeReplace, (unsigned char *) &pid, 1);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
inline
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
bool setFullscreen(Display* dpy, Window win, bool fullscreen) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	XEvent ev;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	Atom wmState = XInternAtom(dpy, "_NET_WM_STATE", False);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	Atom wmFullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	ev.xclient.type = ClientMessage;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	ev.xclient.serial = 0; // TODO: serial?
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	ev.xclient.send_event = True;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	ev.xclient.display = dpy;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	ev.xclient.window = win;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	ev.xclient.message_type = wmState;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	ev.xclient.format = 32;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	ev.xclient.data.l[0] = fullscreen;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	ev.xclient.data.l[1] = wmFullscreen;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	ev.xclient.data.l[2] = None;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask, &ev);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	return fullscreen;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
class DecodedKey {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
public:
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	unsigned int code;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	KeySym symbol;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	const char* name;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	bool matches(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			KeySym s1,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			KeySym s2 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			KeySym s3 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			KeySym s4 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			KeySym s5 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			KeySym s6 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			KeySym s7 = XK_VoidSymbol,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			KeySym s8 = XK_VoidSymbol
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		return symbol == s1
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				|| symbol == s2
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				|| symbol == s3
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				|| symbol == s4
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				|| symbol == s5
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				|| symbol == s6
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
				|| symbol == s7
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
				|| symbol == s8;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
inline
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
const DecodedKey decodeKeycode(Display * dpy, KeyCode keyCode) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	DecodedKey dk = {};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	dk.code = keyCode;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	dk.symbol = XkbKeycodeToKeysym(dpy, keyCode, 0, 0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	dk.name = XKeysymToString(dk.symbol);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	return dk;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
}