2021-10-19 16:46:46 -04:00
|
|
|
// somebar - dwl bar
|
|
|
|
// See LICENSE file for copyright and license details.
|
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
#include <stdio.h>
|
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-27 11:24:47 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <list>
|
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
2021-10-27 11:24:47 -04:00
|
|
|
#include <sys/epoll.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-19 16:46:46 -04:00
|
|
|
#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"
|
2021-10-27 11:24:47 -04:00
|
|
|
#include "xdg-output-unstable-v1-client-protocol.h"
|
2021-10-19 16:46:46 -04:00
|
|
|
#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-27 12:36:57 -04:00
|
|
|
#include "line_buffer.hpp"
|
2021-10-19 16:46:46 -04:00
|
|
|
|
2021-10-25 09:49:01 -04:00
|
|
|
struct Monitor {
|
2021-10-27 11:24:47 -04:00
|
|
|
uint32_t registryName;
|
|
|
|
std::string xdgName;
|
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;
|
2021-10-27 11:24:47 -04:00
|
|
|
bool desiredVisibility {true};
|
|
|
|
bool hasData;
|
2021-10-25 09:49:01 -04:00
|
|
|
};
|
2021-10-27 11:24:47 -04:00
|
|
|
|
2021-10-26 06:59:41 -04:00
|
|
|
struct SeatPointer {
|
|
|
|
wl_unique_ptr<wl_pointer> wlPointer;
|
|
|
|
Bar *focusedBar;
|
|
|
|
int x, y;
|
|
|
|
std::vector<int> btns;
|
|
|
|
};
|
|
|
|
struct Seat {
|
|
|
|
uint32_t name;
|
|
|
|
wl_unique_ptr<wl_seat> wlSeat;
|
|
|
|
std::optional<SeatPointer> pointer;
|
|
|
|
};
|
2021-10-25 09:49:01 -04:00
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
static void updatemon(Monitor &mon);
|
2021-10-22 10:32:53 -04:00
|
|
|
static void setupStatusFifo();
|
|
|
|
static void onStatus();
|
2021-10-27 11:24:47 -04:00
|
|
|
static void cleanup();
|
|
|
|
static void requireGlobal(const void *p, const char *name);
|
|
|
|
static void waylandFlush();
|
2021-10-27 11:24:47 -04:00
|
|
|
[[noreturn]] static void diesys(const char *why);
|
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;
|
2021-10-27 11:24:47 -04:00
|
|
|
std::vector<std::string> tagNames;
|
|
|
|
std::vector<std::string> layoutNames;
|
2021-10-26 05:40:46 -04:00
|
|
|
static xdg_wm_base *xdgWmBase;
|
2021-10-27 11:24:47 -04:00
|
|
|
static zxdg_output_manager_v1 *xdgOutputManager;
|
2021-10-26 06:59:41 -04:00
|
|
|
static wl_surface *cursorSurface;
|
|
|
|
static wl_cursor_image *cursorImage;
|
2021-10-25 13:02:35 -04:00
|
|
|
static bool ready;
|
2021-10-26 06:59:41 -04:00
|
|
|
static std::list<Monitor> monitors;
|
|
|
|
static std::list<Seat> seats;
|
2021-10-27 11:24:47 -04:00
|
|
|
static Monitor *selmon;
|
2021-10-27 11:24:47 -04:00
|
|
|
static std::string lastStatus;
|
2021-10-22 10:32:53 -04:00
|
|
|
static std::string statusFifoName;
|
2021-10-27 11:24:47 -04:00
|
|
|
static int epoll {-1};
|
|
|
|
static int displayFd {-1};
|
2021-10-22 10:32:53 -04:00
|
|
|
static int statusFifoFd {-1};
|
|
|
|
static int statusFifoWriter {-1};
|
2021-10-22 10:39:19 -04:00
|
|
|
static bool quitting {false};
|
2021-10-19 16:46:46 -04:00
|
|
|
|
2021-10-26 05:40:46 -04:00
|
|
|
void view(Monitor &m, const Arg &arg)
|
|
|
|
{
|
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), arg.ui, 1);
|
|
|
|
}
|
2021-10-26 10:17:07 -04:00
|
|
|
void toggleview(Monitor &m, const Arg &arg)
|
|
|
|
{
|
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), arg.ui, 0);
|
|
|
|
}
|
2021-10-26 05:40:46 -04:00
|
|
|
void setlayout(Monitor &m, const Arg &arg)
|
|
|
|
{
|
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_set_layout(m.dwlMonitor.get(), arg.ui);
|
|
|
|
}
|
2021-10-26 10:17:07 -04:00
|
|
|
void tag(Monitor &m, const Arg &arg)
|
|
|
|
{
|
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0, arg.ui);
|
|
|
|
}
|
|
|
|
void toggletag(Monitor &m, const Arg &arg)
|
|
|
|
{
|
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0xffffff, arg.ui);
|
|
|
|
}
|
2021-10-26 10:40:19 -04:00
|
|
|
void spawn(Monitor&, const Arg &arg)
|
|
|
|
{
|
2021-10-27 12:36:57 -04:00
|
|
|
if (fork() == 0) {
|
2021-10-26 10:40:19 -04:00
|
|
|
auto argv = static_cast<char* const*>(arg.v);
|
|
|
|
setsid();
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
fprintf(stderr, "somebar: execvp %s ", argv[0]);
|
|
|
|
perror(" failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 05:40:46 -04:00
|
|
|
|
2021-10-19 16:46:46 -04:00
|
|
|
static const struct xdg_wm_base_listener xdgWmBaseListener = {
|
|
|
|
[](void*, xdg_wm_base *sender, uint32_t serial) {
|
|
|
|
xdg_wm_base_pong(sender, serial);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
static const struct zxdg_output_v1_listener xdgOutputListener = {
|
|
|
|
.logical_position = [](void*, zxdg_output_v1*, int, int) { },
|
|
|
|
.logical_size = [](void*, zxdg_output_v1*, int, int) { },
|
|
|
|
.done = [](void*, zxdg_output_v1*) { },
|
|
|
|
.name = [](void *mp, zxdg_output_v1 *xdgOutput, const char *name) {
|
|
|
|
auto& monitor = *static_cast<Monitor*>(mp);
|
|
|
|
monitor.xdgName = name;
|
|
|
|
zxdg_output_v1_destroy(xdgOutput);
|
|
|
|
},
|
|
|
|
.description = [](void*, zxdg_output_v1*, const char*) { },
|
|
|
|
};
|
|
|
|
|
2021-10-25 13:02:35 -04:00
|
|
|
static Bar* barFromSurface(const wl_surface *surface)
|
|
|
|
{
|
2021-10-25 14:52:21 -04:00
|
|
|
auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor &mon) {
|
2021-10-25 13:02:35 -04:00
|
|
|
return mon.bar && mon.bar->surface() == surface;
|
|
|
|
});
|
2021-10-25 14:52:21 -04:00
|
|
|
return mon != end(monitors) && mon->bar ? &*mon->bar : nullptr;
|
2021-10-25 13:02:35 -04:00
|
|
|
}
|
2021-10-22 11:26:05 -04:00
|
|
|
static const struct wl_pointer_listener pointerListener = {
|
2021-10-26 06:59:41 -04:00
|
|
|
.enter = [](void *sp, 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-26 06:59:41 -04:00
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
seat.pointer->focusedBar = barFromSurface(surface);
|
|
|
|
if (!cursorImage) {
|
|
|
|
auto cursorTheme = wl_cursor_theme_load(NULL, 24, shm);
|
|
|
|
cursorImage = wl_cursor_theme_get_cursor(cursorTheme, "left_ptr")->images[0];
|
|
|
|
cursorSurface = wl_compositor_create_surface(compositor);
|
|
|
|
wl_surface_attach(cursorSurface, wl_cursor_image_get_buffer(cursorImage), 0, 0);
|
|
|
|
wl_surface_commit(cursorSurface);
|
|
|
|
}
|
|
|
|
wl_pointer_set_cursor(pointer, serial, cursorSurface,
|
|
|
|
cursorImage->hotspot_x, cursorImage->hotspot_y);
|
2021-10-22 11:26:05 -04:00
|
|
|
},
|
2021-10-26 06:59:41 -04:00
|
|
|
.leave = [](void *sp, wl_pointer*, uint32_t serial, wl_surface*) {
|
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
seat.pointer->focusedBar = nullptr;
|
2021-10-22 11:26:05 -04:00
|
|
|
},
|
2021-10-26 06:59:41 -04:00
|
|
|
.motion = [](void *sp, wl_pointer*, uint32_t, wl_fixed_t x, wl_fixed_t y) {
|
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
seat.pointer->x = wl_fixed_to_int(x);
|
|
|
|
seat.pointer->y = wl_fixed_to_int(y);
|
2021-10-22 11:26:05 -04:00
|
|
|
},
|
2021-10-26 06:59:41 -04:00
|
|
|
.button = [](void *sp, wl_pointer*, uint32_t, uint32_t, uint32_t button, uint32_t pressed) {
|
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
auto it = std::find(begin(seat.pointer->btns), end(seat.pointer->btns), button);
|
|
|
|
if (pressed == WL_POINTER_BUTTON_STATE_PRESSED && it == end(seat.pointer->btns)) {
|
|
|
|
seat.pointer->btns.push_back(button);
|
|
|
|
} else if (pressed == WL_POINTER_BUTTON_STATE_RELEASED && it != end(seat.pointer->btns)) {
|
|
|
|
seat.pointer->btns.erase(it);
|
2021-10-22 11:26:05 -04:00
|
|
|
}
|
|
|
|
},
|
2021-10-26 06:59:41 -04:00
|
|
|
.axis = [](void *sp, wl_pointer*, uint32_t, uint32_t, wl_fixed_t) { },
|
|
|
|
.frame = [](void *sp, wl_pointer*) {
|
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
if (!seat.pointer->focusedBar) return;
|
|
|
|
for (auto btn : seat.pointer->btns) {
|
2021-10-26 09:32:25 -04:00
|
|
|
seat.pointer->focusedBar->click(seat.pointer->x, seat.pointer->y, btn);
|
2021-10-22 11:26:05 -04:00
|
|
|
}
|
2021-10-26 06:59:41 -04:00
|
|
|
seat.pointer->btns.clear();
|
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 const struct wl_seat_listener seatListener = {
|
2021-10-26 06:59:41 -04:00
|
|
|
.capabilities = [](void *sp, wl_seat*, uint32_t cap)
|
2021-10-22 11:26:05 -04:00
|
|
|
{
|
2021-10-26 06:59:41 -04:00
|
|
|
auto& seat = *static_cast<Seat*>(sp);
|
|
|
|
auto hasPointer = cap & WL_SEAT_CAPABILITY_POINTER;
|
|
|
|
if (!seat.pointer && hasPointer) {
|
2021-10-26 09:32:25 -04:00
|
|
|
auto &pointer = seat.pointer.emplace();
|
|
|
|
pointer.wlPointer = wl_unique_ptr<wl_pointer> {wl_seat_get_pointer(seat.wlSeat.get())};
|
2021-10-26 06:59:41 -04:00
|
|
|
wl_pointer_add_listener(seat.pointer->wlPointer.get(), &pointerListener, &seat);
|
|
|
|
} else if (seat.pointer && !hasPointer) {
|
|
|
|
seat.pointer.reset();
|
2021-10-22 11:26:05 -04:00
|
|
|
}
|
|
|
|
},
|
2021-10-26 06:59:41 -04:00
|
|
|
.name = [](void*, wl_seat*, const char *name) { }
|
2021-10-22 11:26:05 -04:00
|
|
|
};
|
|
|
|
|
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 15:54:14 -04:00
|
|
|
.layout = [](void*, znet_tapesoftware_dwl_wm_v1*, const char *name) {
|
|
|
|
layoutNames.push_back(name);
|
|
|
|
},
|
2021-10-24 13:04:29 -04:00
|
|
|
};
|
|
|
|
|
2021-10-25 09:49:01 -04:00
|
|
|
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorListener {
|
2021-10-26 05:40:46 -04:00
|
|
|
.selected = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t selected) {
|
2021-10-25 15:54:14 -04:00
|
|
|
auto mon = static_cast<Monitor*>(mv);
|
2021-10-27 11:24:47 -04:00
|
|
|
if (selected) {
|
|
|
|
selmon = mon;
|
|
|
|
} else if (selmon == mon) {
|
|
|
|
selmon = nullptr;
|
|
|
|
}
|
2021-10-25 15:54:14 -04:00
|
|
|
mon->bar->setSelected(selected);
|
|
|
|
},
|
2021-10-26 05:40:46 -04:00
|
|
|
.tag = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t tag, uint32_t state, uint32_t numClients, int32_t focusedClient) {
|
2021-10-25 14:52:21 -04:00
|
|
|
auto mon = static_cast<Monitor*>(mv);
|
|
|
|
mon->bar->setTag(tag, static_cast<znet_tapesoftware_dwl_wm_monitor_v1_tag_state>(state), numClients, focusedClient);
|
2021-10-25 13:02:35 -04:00
|
|
|
},
|
2021-10-26 05:40:46 -04:00
|
|
|
.layout = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t layout) {
|
2021-10-25 15:54:14 -04:00
|
|
|
auto mon = static_cast<Monitor*>(mv);
|
|
|
|
mon->bar->setLayout(layout);
|
|
|
|
},
|
|
|
|
.title = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, const char *title) {
|
|
|
|
auto mon = static_cast<Monitor*>(mv);
|
|
|
|
mon->bar->setTitle(title);
|
|
|
|
},
|
2021-10-25 13:02:35 -04:00
|
|
|
.frame = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
|
|
|
|
auto mon = static_cast<Monitor*>(mv);
|
2021-10-27 11:24:47 -04:00
|
|
|
mon->hasData = true;
|
|
|
|
updatemon(*mon);
|
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()));
|
2021-10-26 05:40:46 -04:00
|
|
|
monitor.bar.emplace(&monitor);
|
2021-10-25 14:52:21 -04:00
|
|
|
monitor.bar->setStatus(lastStatus);
|
2021-10-27 11:24:47 -04:00
|
|
|
auto xdgOutput = zxdg_output_manager_v1_get_xdg_output(xdgOutputManager, monitor.wlOutput.get());
|
|
|
|
zxdg_output_v1_add_listener(xdgOutput, &xdgOutputListener, &monitor);
|
2021-10-27 11:24:47 -04:00
|
|
|
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void updatemon(Monitor &mon)
|
|
|
|
{
|
|
|
|
if (!mon.hasData) return;
|
|
|
|
if (mon.desiredVisibility) {
|
|
|
|
if (mon.bar->visible()) {
|
|
|
|
mon.bar->invalidate();
|
|
|
|
} else {
|
|
|
|
mon.bar->show(mon.wlOutput.get());
|
|
|
|
}
|
|
|
|
} else if (mon.bar->visible()) {
|
|
|
|
mon.bar->hide();
|
|
|
|
}
|
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");
|
|
|
|
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
|
2021-10-27 11:24:47 -04:00
|
|
|
requireGlobal(xdgOutputManager, "zxdg_output_manager_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);
|
2021-10-27 11:24:47 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
diesys("open status fifo reader");
|
2021-10-22 10:32:53 -04:00
|
|
|
}
|
|
|
|
statusFifoName = path;
|
|
|
|
statusFifoFd = fd;
|
|
|
|
|
|
|
|
fd = open(path.c_str(), O_CLOEXEC | O_WRONLY);
|
2021-10-27 11:24:47 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
diesys("open status fifo writer");
|
2021-10-22 10:32:53 -04:00
|
|
|
}
|
|
|
|
statusFifoWriter = fd;
|
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
epoll_event ev = {0};
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ev.data.fd = statusFifoFd;
|
|
|
|
if (epoll_ctl(epoll, EPOLL_CTL_ADD, statusFifoFd, &ev) < 0) {
|
|
|
|
diesys("epoll_ctl add status fifo");
|
|
|
|
}
|
2021-10-22 10:32:53 -04:00
|
|
|
return;
|
|
|
|
} else if (errno != EEXIST) {
|
2021-10-27 11:24:47 -04:00
|
|
|
diesys("mkfifo");
|
2021-10-22 10:32:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
const std::string prefixStatus = "status ";
|
|
|
|
const std::string prefixShow = "show ";
|
|
|
|
const std::string prefixHide = "hide ";
|
|
|
|
const std::string prefixToggle = "toggle ";
|
|
|
|
const std::string argAll = "all";
|
2021-10-28 11:22:40 -04:00
|
|
|
const std::string argSelected = "selected";
|
2021-10-27 11:24:47 -04:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void updateVisibility(const std::string &name, T updater)
|
|
|
|
{
|
2021-10-28 11:22:40 -04:00
|
|
|
auto isCurrent = name == argSelected;
|
2021-10-27 11:24:47 -04:00
|
|
|
auto isAll = name == argAll;
|
2021-10-27 11:24:47 -04:00
|
|
|
for (auto& mon : monitors) {
|
|
|
|
if (isAll ||
|
|
|
|
isCurrent && &mon == selmon ||
|
|
|
|
mon.xdgName == name) {
|
|
|
|
auto newVisibility = updater(mon.desiredVisibility);
|
|
|
|
if (newVisibility != mon.desiredVisibility) {
|
|
|
|
mon.desiredVisibility = newVisibility;
|
|
|
|
updatemon(mon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 11:24:47 -04:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:36:57 -04:00
|
|
|
static LineBuffer<512> _statusBuffer;
|
2021-10-22 10:32:53 -04:00
|
|
|
static void onStatus()
|
|
|
|
{
|
2021-10-27 12:36:57 -04:00
|
|
|
_statusBuffer.readLines(
|
|
|
|
[](void *p, size_t size) {
|
|
|
|
return read(statusFifoFd, p, size);
|
|
|
|
},
|
|
|
|
[](const char *buffer, size_t n) {
|
|
|
|
auto str = std::string {buffer, n};
|
|
|
|
if (str.rfind(prefixStatus, 0) == 0) {
|
|
|
|
lastStatus = str.substr(prefixStatus.size());
|
|
|
|
for (auto &monitor : monitors) {
|
|
|
|
if (monitor.bar) {
|
|
|
|
monitor.bar->setStatus(lastStatus);
|
|
|
|
monitor.bar->invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (str.rfind(prefixShow, 0) == 0) {
|
|
|
|
updateVisibility(str.substr(prefixShow.size()), [](bool) { return true; });
|
|
|
|
} else if (str.rfind(prefixHide, 0) == 0) {
|
|
|
|
updateVisibility(str.substr(prefixHide.size()), [](bool) { return false; });
|
|
|
|
} else if (str.rfind(prefixToggle, 0) == 0) {
|
|
|
|
updateVisibility(str.substr(prefixToggle.size()), [](bool vis) { return !vis; });
|
2021-10-27 11:24:47 -04:00
|
|
|
}
|
2021-10-27 12:36:57 -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 };
|
|
|
|
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;
|
2021-10-27 11:24:47 -04:00
|
|
|
if (reg.handle(xdgOutputManager, zxdg_output_manager_v1_interface, 3)) return;
|
2021-10-19 16:46:46 -04:00
|
|
|
if (reg.handle(xdgWmBase, xdg_wm_base_interface, 2)) {
|
|
|
|
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
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-26 06:59:41 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
|
|
|
|
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
|
|
|
|
wl_seat_add_listener(wlSeat, &seatListener, &seat);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (wl_output *output; reg.handle(output, wl_output_interface, 1)) {
|
2021-10-27 11:24:47 -04:00
|
|
|
auto& m = monitors.emplace_back(Monitor {name, {}, wl_unique_ptr<wl_output> {output}});
|
2021-10-26 06:59:41 -04:00
|
|
|
if (ready) {
|
|
|
|
setupMonitor(m);
|
|
|
|
}
|
|
|
|
return;
|
2021-10-24 13:04:29 -04:00
|
|
|
}
|
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)
|
|
|
|
{
|
2021-10-27 11:24:47 -04:00
|
|
|
monitors.remove_if([name](const Monitor &mon) { return mon.registryName == name; });
|
2021-10-26 06:59:41 -04:00
|
|
|
seats.remove_if([name](const Seat &seat) { return seat.name == name; });
|
2021-10-25 13:02:35 -04:00
|
|
|
}
|
|
|
|
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-27 12:36:57 -04:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "chv")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
printf("Usage: %s [-h] [-v] [-c command]\n", argv[0]);
|
|
|
|
printf(" -h: Show this help\n");
|
|
|
|
printf(" -v: Show somebar version\n");
|
|
|
|
printf(" -c: Sends a command to sombar. See README for details.\n");
|
|
|
|
printf("If any of these are specified, somebar exits after the action.\n");
|
|
|
|
printf("Otherwise, somebar will display itself.\n");
|
|
|
|
exit(0);
|
|
|
|
case 'v':
|
|
|
|
printf("somebar " SOMEBAR_VERSION "\n");
|
|
|
|
exit(0);
|
|
|
|
case 'c':
|
|
|
|
if (optind >= argc) {
|
|
|
|
die("Expected command");
|
|
|
|
}
|
|
|
|
auto path = std::string {getenv("XDG_RUNTIME_DIR")} + "/somebar-0";
|
|
|
|
int fd = open(path.c_str(), O_WRONLY | O_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "could not open %s: ", path.c_str());
|
|
|
|
perror("");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
auto str = std::string {};
|
|
|
|
for (auto i = optind; i<argc; i++) {
|
|
|
|
if (i > optind) str += " ";
|
|
|
|
str += argv[i];
|
|
|
|
}
|
|
|
|
str += "\n";
|
|
|
|
write(fd, str.c_str(), str.size());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
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-27 11:24:47 -04:00
|
|
|
epoll_event epollEv = {0};
|
|
|
|
std::array<epoll_event, 5> epollEvents;
|
|
|
|
epoll = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
if (epoll < 0) {
|
|
|
|
diesys("epoll_create1");
|
|
|
|
}
|
2021-10-22 10:39:19 -04:00
|
|
|
int sfd = signalfd(-1, &blockedsigs, SFD_CLOEXEC | SFD_NONBLOCK);
|
|
|
|
if (sfd < 0) {
|
2021-10-27 11:24:47 -04:00
|
|
|
diesys("signalfd");
|
|
|
|
}
|
|
|
|
epollEv.events = EPOLLIN;
|
|
|
|
epollEv.data.fd = sfd;
|
|
|
|
if (epoll_ctl(epoll, EPOLL_CTL_ADD, sfd, &epollEv) < 0) {
|
|
|
|
diesys("epoll_ctl add signalfd");
|
2021-10-22 10:39:19 -04:00
|
|
|
}
|
2021-10-22 10:32:53 -04:00
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
display = wl_display_connect(nullptr);
|
2021-10-19 16:46:46 -04:00
|
|
|
if (!display) {
|
2021-10-27 11:24:47 -04:00
|
|
|
die("Failed to connect to Wayland display");
|
2021-10-19 16:46:46 -04:00
|
|
|
}
|
2021-10-27 11:24:47 -04:00
|
|
|
displayFd = wl_display_get_fd(display);
|
2021-10-19 16:46:46 -04:00
|
|
|
|
|
|
|
auto registry = wl_display_get_registry(display);
|
|
|
|
wl_registry_add_listener(registry, ®istry_listener, nullptr);
|
2021-10-22 10:32:53 -04:00
|
|
|
wl_display_roundtrip(display);
|
|
|
|
onReady();
|
2021-10-19 16:46:46 -04:00
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
epollEv.events = EPOLLIN;
|
|
|
|
epollEv.data.fd = displayFd;
|
|
|
|
if (epoll_ctl(epoll, EPOLL_CTL_ADD, displayFd, &epollEv) < 0) {
|
|
|
|
diesys("epoll_ctl add wayland_display");
|
|
|
|
}
|
2021-10-19 16:46:46 -04:00
|
|
|
|
2021-10-22 10:32:53 -04:00
|
|
|
while (!quitting) {
|
2021-10-22 09:05:47 -04:00
|
|
|
waylandFlush();
|
2021-10-27 11:24:47 -04:00
|
|
|
auto res = epoll_wait(epoll, epollEvents.data(), epollEvents.size(), -1);
|
|
|
|
if (res < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
diesys("epoll_wait");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto i=0; i<res; i++) {
|
|
|
|
auto &ev = epollEvents[i];
|
|
|
|
if (ev.data.fd == displayFd) {
|
|
|
|
if (ev.events & EPOLLIN) {
|
|
|
|
if (wl_display_dispatch(display) < 0) {
|
|
|
|
die("wl_display_dispatch");
|
|
|
|
}
|
|
|
|
} if (ev.events & EPOLLOUT) {
|
|
|
|
epollEv.events = EPOLLIN;
|
|
|
|
epollEv.data.fd = displayFd;
|
|
|
|
if (epoll_ctl(epoll, EPOLL_CTL_MOD, displayFd, &epollEv) < 0) {
|
|
|
|
diesys("epoll_ctl");
|
|
|
|
}
|
|
|
|
waylandFlush();
|
|
|
|
}
|
|
|
|
} else if (ev.data.fd == statusFifoFd) {
|
|
|
|
onStatus();
|
|
|
|
} else if (ev.data.fd == sfd) {
|
|
|
|
quitting = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 09:05:47 -04:00
|
|
|
}
|
2021-10-22 10:32:53 -04:00
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
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-27 11:24:47 -04:00
|
|
|
cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-10-27 11:24:47 -04:00
|
|
|
epoll_event ev = {0};
|
|
|
|
ev.events = EPOLLIN | EPOLLOUT;
|
|
|
|
ev.data.fd = displayFd;
|
|
|
|
if (epoll_ctl(epoll, EPOLL_CTL_MOD, displayFd, &ev) < 0) {
|
|
|
|
diesys("epoll_ctl");
|
|
|
|
}
|
2021-10-19 16:46:46 -04:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 11:24:47 -04:00
|
|
|
|
2021-10-27 11:24:47 -04:00
|
|
|
void die(const char *why) {
|
2021-10-27 12:36:57 -04:00
|
|
|
fprintf(stderr, "%s\n", why);
|
2021-10-27 11:24:47 -04:00
|
|
|
cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void diesys(const char *why) {
|
|
|
|
perror(why);
|
2021-10-22 10:32:53 -04:00
|
|
|
cleanup();
|
2021-10-19 16:46:46 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-10-27 11:24:47 -04:00
|
|
|
|
|
|
|
void cleanup() {
|
|
|
|
if (!statusFifoName.empty()) {
|
|
|
|
unlink(statusFifoName.c_str());
|
|
|
|
}
|
|
|
|
}
|