x11.h
branchv_0
changeset 0 bb715a82a8f1
child 29 dc3c102e1264
equal deleted inserted replaced
-1:000000000000 0:bb715a82a8f1
       
     1 /**
       
     2  * ShaderShark
       
     3  * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #pragma once
       
    19 
       
    20 #include <cstring>
       
    21 
       
    22 #include <X11/Xatom.h>
       
    23 #include <X11/XKBlib.h>
       
    24 #include <X11/keysymdef.h>
       
    25 
       
    26 /**
       
    27  * Publish our PID (process ID) through the X11 window property.
       
    28  */
       
    29 inline
       
    30 void setX11PID(Display* dpy, Window win) {
       
    31 	Atom _NET_WM_PID = XInternAtom(dpy, "_NET_WM_PID", False);
       
    32 	pid_t pid = getpid();
       
    33 	XChangeProperty(dpy, win, _NET_WM_PID, XA_CARDINAL,
       
    34 			32, PropModeReplace, (unsigned char *) &pid, 1);
       
    35 }
       
    36 
       
    37 inline
       
    38 bool setFullscreen(Display* dpy, Window win, bool fullscreen) {
       
    39 	XEvent ev;
       
    40 	Atom wmState = XInternAtom(dpy, "_NET_WM_STATE", False);
       
    41 	Atom wmFullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
       
    42 	ev.xclient.type = ClientMessage;
       
    43 	ev.xclient.serial = 0; // TODO: serial?
       
    44 	ev.xclient.send_event = True;
       
    45 	ev.xclient.display = dpy;
       
    46 	ev.xclient.window = win;
       
    47 	ev.xclient.message_type = wmState;
       
    48 	ev.xclient.format = 32;
       
    49 	ev.xclient.data.l[0] = fullscreen;
       
    50 	ev.xclient.data.l[1] = wmFullscreen;
       
    51 	ev.xclient.data.l[2] = None;
       
    52 	XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask, &ev);
       
    53 	return fullscreen;
       
    54 }
       
    55 
       
    56 class DecodedKey {
       
    57 public:
       
    58 	unsigned int code;
       
    59 	KeySym symbol;
       
    60 	const char* name;
       
    61 
       
    62 	bool matches(
       
    63 			KeySym s1,
       
    64 			KeySym s2 = XK_VoidSymbol,
       
    65 			KeySym s3 = XK_VoidSymbol,
       
    66 			KeySym s4 = XK_VoidSymbol,
       
    67 			KeySym s5 = XK_VoidSymbol,
       
    68 			KeySym s6 = XK_VoidSymbol,
       
    69 			KeySym s7 = XK_VoidSymbol,
       
    70 			KeySym s8 = XK_VoidSymbol
       
    71 			) {
       
    72 		return symbol == s1
       
    73 				|| symbol == s2
       
    74 				|| symbol == s3
       
    75 				|| symbol == s4
       
    76 				|| symbol == s5
       
    77 				|| symbol == s6
       
    78 				|| symbol == s7
       
    79 				|| symbol == s8;
       
    80 	}
       
    81 };
       
    82 
       
    83 inline
       
    84 const DecodedKey decodeKeycode(Display * dpy, KeyCode keyCode) {
       
    85 	DecodedKey dk = {};
       
    86 	dk.code = keyCode;
       
    87 	dk.symbol = XkbKeycodeToKeysym(dpy, keyCode, 0, 0);
       
    88 	dk.name = XKeysymToString(dk.symbol);
       
    89 	return dk;
       
    90 }