diff --git a/client/displayservers/X11/CMakeLists.txt b/client/displayservers/X11/CMakeLists.txt index 6ade1e19..9c4daa16 100644 --- a/client/displayservers/X11/CMakeLists.txt +++ b/client/displayservers/X11/CMakeLists.txt @@ -17,6 +17,9 @@ add_library(displayserver_X11 STATIC x11.c atoms.c clipboard.c + + wm/default.c + wm/i3.c ) add_definitions(-D GLX_GLXEXT_PROTOTYPES) @@ -28,5 +31,5 @@ target_link_libraries(displayserver_X11 target_include_directories(displayserver_X11 PRIVATE - src + . ) diff --git a/client/displayservers/X11/wm.h b/client/displayservers/X11/wm.h new file mode 100644 index 00000000..fdfa4f37 --- /dev/null +++ b/client/displayservers/X11/wm.h @@ -0,0 +1,39 @@ +/** + * Looking Glass + * Copyright © 2017-2023 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _H_X11DS_WM_ +#define _H_X11DS_WM_ + +#include +#include + +typedef struct X11WM +{ + void (*setup)(void); + bool (*init)(void); + void (*deinit)(void); + void (*setFullscreen)(bool enable); +} +X11WM; + +extern X11WM X11WM_Default; +extern X11WM X11WM_i3; + +#endif diff --git a/client/displayservers/X11/wm/default.c b/client/displayservers/X11/wm/default.c new file mode 100644 index 00000000..87f7fe17 --- /dev/null +++ b/client/displayservers/X11/wm/default.c @@ -0,0 +1,71 @@ +/** + * Looking Glass + * Copyright © 2017-2023 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _H_X11DS_WM_DEFAULT_ +#define _H_X11DS_WM_DEFAULT_ + +#include "wm.h" +#include "x11.h" +#include "atoms.h" + +static void wm_default_setup(void) +{ +} + +static bool wm_default_init(void) +{ + return true; +} + +static void wm_default_deinit(void) +{ +} + +static void wm_default_setFullscreen(bool enable) +{ + XEvent e = + { + .xclient = { + .type = ClientMessage, + .send_event = true, + .message_type = x11atoms._NET_WM_STATE, + .format = 32, + .window = x11.window, + .data.l = { + enable ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE, + x11atoms._NET_WM_STATE_FULLSCREEN, + 0 + } + } + }; + + XSendEvent(x11.display, DefaultRootWindow(x11.display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &e); +}; + +X11WM X11WM_Default = +{ + .setup = wm_default_setup, + .init = wm_default_init, + .deinit = wm_default_deinit, + .setFullscreen = wm_default_setFullscreen +}; + +#endif diff --git a/client/displayservers/X11/wm/i3.c b/client/displayservers/X11/wm/i3.c new file mode 100644 index 00000000..7443fc94 --- /dev/null +++ b/client/displayservers/X11/wm/i3.c @@ -0,0 +1,168 @@ +/** + * Looking Glass + * Copyright © 2017-2023 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _H_X11DS_WM_DEFAULT_ +#define _H_X11DS_WM_DEFAULT_ + +#include "wm.h" +#include "x11.h" +#include "atoms.h" +#include "common/debug.h" +#include "common/option.h" + +#include +#include +#include +#include +#include + +static struct Option options[] = +{ + // app options + { + .module = "i3", + .name = "globalFullScreen", + .description = "Use i3's global full screen feature (spans all monitors)", + .type = OPTION_TYPE_BOOL, + .value.x_bool = false, + }, + {0} +}; + +struct i3state +{ + int sock; + bool globalFullScreen; +}; + +static struct i3state i3; + +static void wm_i3_setup(void) +{ + option_register(options); +} + +static bool wm_i3_init(void) +{ + memset(&i3, 0, sizeof(i3)); + + i3.globalFullScreen = option_get_bool("i3", "globalFullScreen"); + + FILE * fd = popen("i3 --get-socketpath", "r"); + if (!fd) + return false; + + struct sockaddr_un addr = { .sun_family = AF_UNIX }; + char path[sizeof(addr.sun_path)]; + int pathLen; + if ((pathLen = fread(path, 1, sizeof(path), fd)) <= 0) + { + pclose(fd); + return false; + } + pclose(fd); + + if(path[pathLen-1] == '\n') + --pathLen; + path[pathLen] = '\0'; + + i3.sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (i3.sock < 0) + { + DEBUG_ERROR("Failed to create socket for i3 IPC"); + return false; + } + + strncpy(addr.sun_path, path, sizeof(addr.sun_path)); + if (connect(i3.sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) + { + DEBUG_ERROR("Failed to connect to the i3 IPC socket"); + perror("connect"); + goto err_socket; + } + + DEBUG_INFO("i3 IPC Connected"); + return true; + +err_socket: + close(i3.sock); + return false; +} + +static void wm_i3_deinit(void) +{ + close(i3.sock); +} + +static void wm_i3_setFullscreen(bool enable) +{ + if (!i3.globalFullScreen) + { + X11WM_Default.setFullscreen(enable); + return; + } + + struct i3Msg + { + char magic[6]; + uint32_t length; + uint32_t type; + char payload[0]; + } + __attribute__((packed)); + + #define I3_IPC_TYPE_RUN_COMMAND 0 + + char cmd[128]; + int cmdLen = snprintf(cmd, sizeof(cmd), + "[id=%lu] fullscreen toggle global", + x11.window); + + struct i3Msg *msg = alloca(sizeof(struct i3Msg) + cmdLen); + memcpy(msg->magic, "i3-ipc", 6); + msg->length = cmdLen; + msg->type = I3_IPC_TYPE_RUN_COMMAND; + memcpy(msg->payload, cmd, cmdLen); + + int msgSize = sizeof(*msg) + msg->length; + char * buf = (char *)msg; + while(msgSize) + { + int wrote = write(i3.sock, buf, msgSize); + if (wrote < 0) + { + DEBUG_WARN("i3 IPC communication failure"); + return; + } + + buf += wrote; + msgSize -= wrote; + } +}; + +X11WM X11WM_i3 = +{ + .setup = wm_i3_setup, + .init = wm_i3_init, + .deinit = wm_i3_deinit, + .setFullscreen = wm_i3_setFullscreen +}; + +#endif diff --git a/client/displayservers/X11/x11.c b/client/displayservers/X11/x11.c index 77143c93..e4dc5fd3 100644 --- a/client/displayservers/X11/x11.c +++ b/client/displayservers/X11/x11.c @@ -53,10 +53,6 @@ #include "common/event.h" #include "util.h" -#define _NET_WM_STATE_REMOVE 0 -#define _NET_WM_STATE_ADD 1 -#define _NET_WM_STATE_TOGGLE 2 - struct X11DSState x11; struct MwmHints @@ -166,6 +162,8 @@ static void x11DoPresent(uint64_t msc) static void x11Setup(void) { + X11WM_Default.setup(); + X11WM_i3 .setup(); } static bool x11Probe(void) @@ -230,6 +228,8 @@ static void x11CheckEWMHSupport(void) DEBUG_INFO("EWMH-complient window manager detected: %s", wmName); x11.ewmhSupport = true; + if (strcmp(wmName, "i3") == 0) + x11.wm = &X11WM_i3; XFree(wmName); out_supported: @@ -270,8 +270,9 @@ static bool x11Init(const LG_DSInitParams params) memset(&x11, 0, sizeof(x11)); x11.xValuator = -1; x11.yValuator = -1; - x11.display = XOpenDisplay(NULL); + x11.display = XOpenDisplay(NULL); x11.jitRender = params.jitRender; + x11.wm = &X11WM_Default; XSetWindowAttributes swa = { @@ -369,6 +370,16 @@ static bool x11Init(const LG_DSInitParams params) // check for Extended Window Manager Hints support x11CheckEWMHSupport(); + if (!x11.wm->init()) + { + x11.wm = &X11WM_Default; + if (!x11.wm->init()) + { + DEBUG_ERROR("Failed to initialize the X11 window manager subsystem"); + goto fail_window; + } + } + if (x11atoms._NET_WM_PID) { pid_t pid = getpid(); @@ -781,6 +792,7 @@ static void x11Free(void) if (x11.keysyms) XFree(x11.keysyms); + x11.wm->deinit(); XCloseDisplay(x11.display); } @@ -1964,24 +1976,7 @@ static void x11SetFullscreen(bool fs) if (x11.fullscreen == fs) return; - XEvent e = - { - .xclient = { - .type = ClientMessage, - .send_event = true, - .message_type = x11atoms._NET_WM_STATE, - .format = 32, - .window = x11.window, - .data.l = { - fs ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE, - x11atoms._NET_WM_STATE_FULLSCREEN, - 0 - } - } - }; - - XSendEvent(x11.display, DefaultRootWindow(x11.display), False, - SubstructureNotifyMask | SubstructureRedirectMask, &e); + x11.wm->setFullscreen(fs); } static bool x11GetFullscreen(void) diff --git a/client/displayservers/X11/x11.h b/client/displayservers/X11/x11.h index acde1937..ea276a0b 100644 --- a/client/displayservers/X11/x11.h +++ b/client/displayservers/X11/x11.h @@ -33,6 +33,7 @@ #include "interface/displayserver.h" #include "common/thread.h" #include "common/types.h" +#include "wm.h" enum Modifiers { @@ -48,11 +49,16 @@ enum Modifiers #define MOD_COUNT (MOD_SUPER_RIGHT + 1) +#define _NET_WM_STATE_REMOVE 0 +#define _NET_WM_STATE_ADD 1 +#define _NET_WM_STATE_TOGGLE 2 + struct X11DSState { Display * display; Window window; XVisualInfo * visual; + X11WM * wm; int minKeycode, maxKeycode; int symsPerKeycode;