somebar/src/main.cpp

343 lines
11 KiB
C++
Raw Normal View History

2021-10-19 16:46:46 -04:00
// somebar - dwl bar
// See LICENSE file for copyright and license details.
2021-10-25 09:49:01 -04:00
#include <cstdio>
2021-10-22 10:32:53 -04:00
#include <fcntl.h>
2021-10-19 16:46:46 -04:00
#include <math.h>
2021-10-22 10:32:53 -04:00
#include <signal.h>
2021-10-19 16:46:46 -04:00
#include <sys/mman.h>
2021-10-22 10:39:19 -04:00
#include <sys/signalfd.h>
2021-10-22 10:32:53 -04:00
#include <sys/stat.h>
2021-10-19 16:46:46 -04:00
#include <sys/types.h>
#include <unistd.h>
2021-10-22 11:26:05 -04:00
#include <linux/input-event-codes.h>
2021-10-22 10:52:04 -04:00
#include <optional>
2021-10-22 09:34:19 -04:00
#include <QGuiApplication>
2021-10-19 16:46:46 -04:00
#include <QSocketNotifier>
#include <wayland-client.h>
2021-10-22 11:34:21 -04:00
#include <wayland-cursor.h>
2021-10-19 16:46:46 -04:00
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
2021-10-24 13:04:29 -04:00
#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h"
2021-10-20 14:20:27 -04:00
#include "common.hpp"
2021-10-20 14:45:23 -04:00
#include "bar.hpp"
2021-10-19 16:46:46 -04:00
2021-10-25 09:49:01 -04:00
struct Monitor {
uint32_t name;
2021-10-25 13:02:35 -04:00
wl_unique_ptr<wl_output> wlOutput;
wl_unique_ptr<znet_tapesoftware_dwl_wm_monitor_v1> dwlMonitor;
2021-10-25 09:49:01 -04:00
std::optional<Bar> bar;
};
static void waylandFlush();
2021-10-19 16:46:46 -04:00
static void waylandWriteReady();
static void requireGlobal(const void *p, const char *name);
2021-10-22 10:32:53 -04:00
static void setupStatusFifo();
static void onStatus();
static void cleanup();
2021-10-19 16:46:46 -04:00
2021-10-20 14:20:27 -04:00
wl_display *display;
wl_compositor *compositor;
wl_shm *shm;
zwlr_layer_shell_v1 *wlrLayerShell;
2021-10-24 13:04:29 -04:00
znet_tapesoftware_dwl_wm_v1 *dwlWm;
std::vector<QString> tagNames;
2021-10-25 13:02:35 -04:00
static bool ready;
2021-10-25 09:49:01 -04:00
static std::vector<Monitor> monitors;
2021-10-22 10:32:53 -04:00
static std::string statusFifoName;
static int statusFifoFd {-1};
static int statusFifoWriter {-1};
2021-10-19 16:46:46 -04:00
static QSocketNotifier *displayWriteNotifier;
2021-10-22 10:39:19 -04:00
static bool quitting {false};
2021-10-19 16:46:46 -04:00
static xdg_wm_base *xdgWmBase;
static const struct xdg_wm_base_listener xdgWmBaseListener = {
[](void*, xdg_wm_base *sender, uint32_t serial) {
xdg_wm_base_pong(sender, serial);
}
};
2021-10-25 13:02:35 -04:00
struct SeatState {
2021-10-22 11:26:05 -04:00
wl_pointer *pointer;
2021-10-22 11:34:21 -04:00
wl_surface *cursorSurface;
wl_cursor_image *cursorImage;
2021-10-22 11:26:05 -04:00
Bar *focusedBar;
int x, y;
bool leftButtonClick;
};
2021-10-25 13:02:35 -04:00
static SeatState seatState;
static Bar* barFromSurface(const wl_surface *surface)
{
auto fbar = std::find_if(begin(monitors), end(monitors), [surface](const Monitor &mon) {
return mon.bar && mon.bar->surface() == surface;
});
return fbar != end(monitors) && fbar->bar ? &*fbar->bar : nullptr;
}
2021-10-22 11:26:05 -04:00
static const struct wl_pointer_listener pointerListener = {
2021-10-22 11:34:21 -04:00
.enter = [](void*, wl_pointer *pointer, uint32_t serial,
2021-10-25 13:02:35 -04:00
wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
2021-10-22 11:26:05 -04:00
{
2021-10-25 13:02:35 -04:00
seatState.focusedBar = barFromSurface(surface);
wl_pointer_set_cursor(pointer, serial, seatState.cursorSurface,
seatState.cursorImage->hotspot_x, seatState.cursorImage->hotspot_y);
2021-10-22 11:26:05 -04:00
},
.leave = [](void*, wl_pointer*, uint32_t serial, wl_surface*) {
2021-10-25 13:02:35 -04:00
seatState.focusedBar = nullptr;
2021-10-22 11:26:05 -04:00
},
.motion = [](void*, wl_pointer*, uint32_t, wl_fixed_t x, wl_fixed_t y) {
2021-10-25 13:02:35 -04:00
seatState.x = wl_fixed_to_int(x);
seatState.y = wl_fixed_to_int(y);
2021-10-22 11:26:05 -04:00
},
.button = [](void*, wl_pointer*, uint32_t, uint32_t, uint32_t button, uint32_t pressed) {
if (button == BTN_LEFT) {
2021-10-25 13:02:35 -04:00
seatState.leftButtonClick = pressed == WL_POINTER_BUTTON_STATE_PRESSED;
2021-10-22 11:26:05 -04:00
}
},
.axis = [](void*, wl_pointer*, uint32_t, uint32_t, wl_fixed_t) { },
.frame = [](void*, wl_pointer*) {
2021-10-25 13:02:35 -04:00
if (!seatState.focusedBar) return;
if (seatState.leftButtonClick) {
seatState.leftButtonClick = false;
seatState.focusedBar->click(seatState.x, seatState.y);
2021-10-22 11:26:05 -04:00
}
},
.axis_source = [](void*, wl_pointer*, uint32_t) { },
.axis_stop = [](void*, wl_pointer*, uint32_t, uint32_t) { },
.axis_discrete = [](void*, wl_pointer*, uint32_t, int32_t) { },
};
static wl_seat *seat;
static const struct wl_seat_listener seatListener = {
[](void*, wl_seat*, uint32_t cap)
{
2021-10-25 13:02:35 -04:00
if (!seatState.pointer && WL_SEAT_CAPABILITY_POINTER) {
2021-10-22 11:34:21 -04:00
auto cursorTheme = wl_cursor_theme_load(NULL, 24, shm);
auto cursorImage = wl_cursor_theme_get_cursor(cursorTheme, "left_ptr")->images[0];
2021-10-25 13:02:35 -04:00
seatState.cursorImage = cursorImage;
seatState.cursorSurface = wl_compositor_create_surface(compositor);
wl_surface_attach(seatState.cursorSurface,
2021-10-22 11:34:21 -04:00
wl_cursor_image_get_buffer(cursorImage), 0, 0);
2021-10-25 13:02:35 -04:00
wl_surface_commit(seatState.cursorSurface);
seatState.pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(seatState.pointer, &pointerListener, nullptr);
2021-10-22 11:26:05 -04:00
}
},
[](void*, wl_seat*, const char *name) { }
};
2021-10-24 13:04:29 -04:00
static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = {
.tag = [](void*, znet_tapesoftware_dwl_wm_v1*, const char *name) {
tagNames.push_back(name);
},
};
2021-10-25 09:49:01 -04:00
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorListener {
2021-10-25 13:02:35 -04:00
.tag = [](void*, znet_tapesoftware_dwl_wm_monitor_v1*, int tag, int active, int numClients, int urgent) {
printf("tag %s: active=%d, num_clients=%d, urgent=%d\n", qPrintable(tagNames[tag]), active, numClients, urgent);
},
.frame = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
auto mon = static_cast<Monitor*>(mv);
if (!mon->bar) {
mon->bar.emplace(mon->wlOutput.get());
}
2021-10-25 09:49:01 -04:00
}
};
2021-10-25 13:02:35 -04:00
static void setupMonitor(Monitor &monitor) {
monitor.dwlMonitor.reset(znet_tapesoftware_dwl_wm_v1_get_monitor(dwlWm, monitor.wlOutput.get()));
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor);
2021-10-25 09:49:01 -04:00
}
2021-10-25 13:02:35 -04:00
static void onOutput(uint32_t name, wl_output *output) {
auto& m = monitors.emplace_back(Monitor {name, wl_unique_ptr<wl_output> {output}});
if (ready) {
setupMonitor(m);
2021-10-25 09:49:01 -04:00
}
}
2021-10-19 16:46:46 -04:00
// called after we have received the initial batch of globals
static void onReady()
{
requireGlobal(compositor, "wl_compositor");
requireGlobal(shm, "wl_shm");
2021-10-22 11:26:05 -04:00
requireGlobal(seat, "wl_seat");
2021-10-19 16:46:46 -04:00
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
2021-10-25 09:49:01 -04:00
requireGlobal(dwlWm, "znet_tapesoftware_dwl_wm_v1");
2021-10-22 10:32:53 -04:00
setupStatusFifo();
2021-10-25 09:49:01 -04:00
wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc.
2021-10-25 13:02:35 -04:00
ready = true;
2021-10-25 09:49:01 -04:00
for (auto& monitor : monitors) {
2021-10-25 13:02:35 -04:00
setupMonitor(monitor);
2021-10-25 09:49:01 -04:00
}
2021-10-19 16:46:46 -04:00
}
2021-10-22 10:32:53 -04:00
static void setupStatusFifo()
{
for (auto i=0; i<100; i++) {
auto path = std::string{getenv("XDG_RUNTIME_DIR")} + "/somebar-" + std::to_string(i);
auto result = mkfifo(path.c_str(), 0666);
if (result == 0) {
auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY);
if (fd == -1) {
perror("open status fifo reader");
cleanup();
exit(1);
}
statusFifoName = path;
statusFifoFd = fd;
fd = open(path.c_str(), O_CLOEXEC | O_WRONLY);
if (fd == -1) {
perror("open status fifo writer");
cleanup();
exit(1);
}
statusFifoWriter = fd;
auto statusNotifier = new QSocketNotifier(statusFifoFd, QSocketNotifier::Read);
statusNotifier->setEnabled(true);
QObject::connect(statusNotifier, &QSocketNotifier::activated, onStatus);
return;
} else if (errno != EEXIST) {
perror("mkfifo");
}
}
}
static void onStatus()
{
char buffer[512];
auto n = read(statusFifoFd, buffer, sizeof(buffer));
auto str = QString::fromUtf8(buffer, n);
2021-10-25 13:02:35 -04:00
for (auto &monitor : monitors) {
if (monitor.bar) {
monitor.bar->setStatus(str);
}
2021-10-22 10:52:04 -04:00
}
2021-10-22 10:32:53 -04:00
}
2021-10-19 16:46:46 -04:00
struct HandleGlobalHelper {
wl_registry *registry;
uint32_t name;
const char *interface;
template<typename T>
bool handle(T &store, const wl_interface &iface, int version) {
if (strcmp(interface, iface.name)) return false;
store = static_cast<T>(wl_registry_bind(registry, name, &iface, version));
return true;
}
};
static void registryHandleGlobal(void*, wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
{
auto reg = HandleGlobalHelper { registry, name, interface };
printf("got global: %s v%d\n", interface, version);
if (reg.handle(compositor, wl_compositor_interface, 4)) return;
if (reg.handle(shm, wl_shm_interface, 1)) return;
if (reg.handle(wlrLayerShell, zwlr_layer_shell_v1_interface, 4)) return;
if (reg.handle(xdgWmBase, xdg_wm_base_interface, 2)) {
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return;
}
2021-10-22 11:26:05 -04:00
if (seat == nullptr && reg.handle(seat, wl_seat_interface, 7)) {
wl_seat_add_listener(seat, &seatListener, nullptr);
}
2021-10-25 09:49:01 -04:00
if (wl_output *output; reg.handle(output, wl_output_interface, 1)) {
2021-10-25 13:02:35 -04:00
onOutput(name, output);
2021-10-25 09:49:01 -04:00
}
2021-10-24 13:04:29 -04:00
if (reg.handle(dwlWm, znet_tapesoftware_dwl_wm_v1_interface, 1)) {
znet_tapesoftware_dwl_wm_v1_add_listener(dwlWm, &dwlWmListener, nullptr);
}
2021-10-19 16:46:46 -04:00
}
2021-10-25 13:02:35 -04:00
static void registryHandleRemove(void*, wl_registry *registry, uint32_t name)
{
auto it = std::find_if(begin(monitors), end(monitors), [name](const Monitor &m) { return m.name == name; });
if (it != end(monitors)) {
monitors.erase(it);
}
}
static const struct wl_registry_listener registry_listener = {
.global = registryHandleGlobal,
.global_remove = registryHandleRemove,
};
2021-10-19 16:46:46 -04:00
int main(int argc, char **argv)
{
2021-10-22 10:39:19 -04:00
static sigset_t blockedsigs;
sigemptyset(&blockedsigs);
sigaddset(&blockedsigs, SIGINT);
sigaddset(&blockedsigs, SIGTERM);
sigprocmask(SIG_BLOCK, &blockedsigs, nullptr);
2021-10-22 09:34:19 -04:00
QGuiApplication app(argc, argv);
2021-10-19 16:46:46 -04:00
QCoreApplication::setOrganizationName("tape software");
QCoreApplication::setOrganizationDomain("tapesoftware.net");
QCoreApplication::setApplicationName("somebar");
2021-10-22 10:39:19 -04:00
int sfd = signalfd(-1, &blockedsigs, SFD_CLOEXEC | SFD_NONBLOCK);
if (sfd < 0) {
perror("signalfd");
cleanup();
exit(1);
}
QSocketNotifier signalNotifier {sfd, QSocketNotifier::Read};
QObject::connect(&signalNotifier, &QSocketNotifier::activated, []() { quitting = true; });
2021-10-22 10:32:53 -04:00
2021-10-19 16:46:46 -04:00
display = wl_display_connect(NULL);
if (!display) {
fprintf(stderr, "Failed to connect to Wayland display\n");
return 1;
}
auto registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, nullptr);
2021-10-22 10:32:53 -04:00
wl_display_roundtrip(display);
onReady();
2021-10-19 16:46:46 -04:00
QSocketNotifier displayReadNotifier(wl_display_get_fd(display), QSocketNotifier::Read);
displayReadNotifier.setEnabled(true);
2021-10-25 13:02:35 -04:00
QObject::connect(&displayReadNotifier, &QSocketNotifier::activated, [=]() {
auto res = wl_display_dispatch(display);
if (res < 0) {
perror("wl_display_dispatch");
cleanup();
exit(1);
}
});
2021-10-19 16:46:46 -04:00
displayWriteNotifier = new QSocketNotifier(wl_display_get_fd(display), QSocketNotifier::Write);
displayWriteNotifier->setEnabled(false);
QObject::connect(displayWriteNotifier, &QSocketNotifier::activated, waylandWriteReady);
2021-10-22 10:32:53 -04:00
while (!quitting) {
waylandFlush();
app.processEvents(QEventLoop::WaitForMoreEvents);
}
2021-10-22 10:32:53 -04:00
cleanup();
}
void cleanup() {
if (!statusFifoName.empty()) {
unlink(statusFifoName.c_str());
}
2021-10-19 16:46:46 -04:00
}
2021-10-20 14:20:27 -04:00
void waylandFlush()
2021-10-19 16:46:46 -04:00
{
wl_display_dispatch_pending(display);
if (wl_display_flush(display) < 0 && errno == EAGAIN) {
displayWriteNotifier->setEnabled(true);
}
}
static void waylandWriteReady()
{
displayWriteNotifier->setEnabled(false);
waylandFlush();
}
static void requireGlobal(const void *p, const char *name)
{
if (p) return;
fprintf(stderr, "Wayland compositor does not export required global %s, aborting.\n", name);
2021-10-22 10:32:53 -04:00
cleanup();
2021-10-19 16:46:46 -04:00
exit(1);
}