Skip to content

Commit

Permalink
[client] x11: add support for i3 global full screen toggle
Browse files Browse the repository at this point in the history
This commit adds an interface to the X11 display server code to support
various window manage specific features, such as in this case, the i3
global full screen toggle.

This feature specifically uses the i3 IPC to cause looking glass to go
full screen across all monitors if the new option `i3:globalFullScreen`
is enabled.
  • Loading branch information
gnif committed Sep 11, 2023
1 parent 8dba4b6 commit 24d4fce
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 24 deletions.
5 changes: 4 additions & 1 deletion client/displayservers/X11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,5 +31,5 @@ target_link_libraries(displayserver_X11

target_include_directories(displayserver_X11
PRIVATE
src
.
)
39 changes: 39 additions & 0 deletions client/displayservers/X11/wm.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <stdbool.h>

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
71 changes: 71 additions & 0 deletions client/displayservers/X11/wm/default.c
Original file line number Diff line number Diff line change
@@ -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
168 changes: 168 additions & 0 deletions client/displayservers/X11/wm/i3.c
Original file line number Diff line number Diff line change
@@ -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 <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

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
41 changes: 18 additions & 23 deletions client/displayservers/X11/x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,6 +162,8 @@ static void x11DoPresent(uint64_t msc)

static void x11Setup(void)
{
X11WM_Default.setup();
X11WM_i3 .setup();
}

static bool x11Probe(void)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 =
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -781,6 +792,7 @@ static void x11Free(void)
if (x11.keysyms)
XFree(x11.keysyms);

x11.wm->deinit();
XCloseDisplay(x11.display);
}

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 24d4fce

Please sign in to comment.