Merge branch 'master' into stdin

This commit is contained in:
Raphael Robatsch 2021-10-30 11:44:40 +02:00
commit 82330ddf7d
2 changed files with 42 additions and 36 deletions

View file

@ -60,7 +60,7 @@ private:
_discardLine = true; _discardLine = true;
_consumedTo = _buffer.begin(); _consumedTo = _buffer.begin();
_bufferedTo = _buffer.begin(); _bufferedTo = _buffer.begin();
} else if (bytesRemaining > 0 && _consumedTo > _buffer.begin()) { } else {
// move the last partial message to the front of the buffer, so a full-sized // move the last partial message to the front of the buffer, so a full-sized
// message will fit // message will fit
std::copy(_consumedTo, _bufferedTo, _buffer.begin()); std::copy(_consumedTo, _bufferedTo, _buffer.begin());

View file

@ -48,14 +48,20 @@ struct Seat {
std::optional<SeatPointer> pointer; std::optional<SeatPointer> pointer;
}; };
static Bar* barFromSurface(const wl_surface* surface);
static void setupMonitor(Monitor& monitor);
static void updatemon(Monitor &mon); static void updatemon(Monitor &mon);
static void onReady();
static void setupStatusFifo(); static void setupStatusFifo();
static void onStatus(); static void onStatus();
static void onStdin(); static void onStdin();
static void handleStdin(const std::string& line); static void handleStdin(const std::string& line);
static void cleanup(); static void updateVisibility(const std::string& name, bool(*updater)(bool));
static void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version);
static void onGlobalRemove(void*, wl_registry* registry, uint32_t name);
static void requireGlobal(const void* p, const char* name); static void requireGlobal(const void* p, const char* name);
static void waylandFlush(); static void waylandFlush();
static void cleanup();
[[noreturn]] static void diesys(const char* why); [[noreturn]] static void diesys(const char* why);
wl_display* display; wl_display* display;
@ -108,7 +114,7 @@ static const struct zxdg_output_v1_listener xdgOutputListener = {
.description = [](void*, zxdg_output_v1*, const char*) { }, .description = [](void*, zxdg_output_v1*, const char*) { },
}; };
static Bar* barFromSurface(const wl_surface *surface) Bar* barFromSurface(const wl_surface* surface)
{ {
auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor& mon) { auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor& mon) {
return mon.bar && mon.bar->surface() == surface; return mon.bar && mon.bar->surface() == surface;
@ -179,12 +185,12 @@ static const struct wl_seat_listener seatListener = {
.name = [](void*, wl_seat*, const char *name) { } .name = [](void*, wl_seat*, const char *name) { }
}; };
static void setupMonitor(Monitor& monitor) { void setupMonitor(Monitor& monitor) {
monitor.bar.emplace(&monitor); monitor.bar.emplace(&monitor);
monitor.bar->setStatus(lastStatus); monitor.bar->setStatus(lastStatus);
} }
static void updatemon(Monitor& mon) void updatemon(Monitor& mon)
{ {
if (!mon.hasData) return; if (!mon.hasData) return;
if (mon.desiredVisibility) { if (mon.desiredVisibility) {
@ -199,7 +205,7 @@ static void updatemon(Monitor& mon)
} }
// called after we have received the initial batch of globals // called after we have received the initial batch of globals
static void onReady() void onReady()
{ {
requireGlobal(compositor, "wl_compositor"); requireGlobal(compositor, "wl_compositor");
requireGlobal(shm, "wl_shm"); requireGlobal(shm, "wl_shm");
@ -212,9 +218,10 @@ static void onReady()
for (auto& monitor : monitors) { for (auto& monitor : monitors) {
setupMonitor(monitor); setupMonitor(monitor);
} }
wl_display_roundtrip(display); // wait for xdg_output names before we read stdin
} }
static void setupStatusFifo() void setupStatusFifo()
{ {
for (auto i=0; i<100; i++) { for (auto i=0; i<100; i++) {
auto path = std::string{getenv("XDG_RUNTIME_DIR")} + "/somebar-" + std::to_string(i); auto path = std::string{getenv("XDG_RUNTIME_DIR")} + "/somebar-" + std::to_string(i);
@ -308,26 +315,8 @@ const std::string prefixToggle = "toggle ";
const std::string argAll = "all"; const std::string argAll = "all";
const std::string argSelected = "selected"; const std::string argSelected = "selected";
template<typename T>
static void updateVisibility(const std::string& name, T updater)
{
auto isCurrent = name == argSelected;
auto isAll = name == argAll;
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);
}
}
}
}
static LineBuffer<512> _statusBuffer; static LineBuffer<512> _statusBuffer;
static void onStatus() void onStatus()
{ {
_statusBuffer.readLines( _statusBuffer.readLines(
[](void* p, size_t size) { [](void* p, size_t size) {
@ -353,6 +342,23 @@ static void onStatus()
}); });
} }
void updateVisibility(const std::string& name, bool(*updater)(bool))
{
auto isCurrent = name == argSelected;
auto isAll = name == argAll;
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);
}
}
}
}
struct HandleGlobalHelper { struct HandleGlobalHelper {
wl_registry* registry; wl_registry* registry;
uint32_t name; uint32_t name;
@ -365,7 +371,7 @@ struct HandleGlobalHelper {
return true; return true;
} }
}; };
static void registryHandleGlobal(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version)
{ {
auto reg = HandleGlobalHelper { registry, name, interface }; auto reg = HandleGlobalHelper { registry, name, interface };
if (reg.handle(compositor, wl_compositor_interface, 4)) return; if (reg.handle(compositor, wl_compositor_interface, 4)) return;
@ -391,14 +397,14 @@ static void registryHandleGlobal(void*, wl_registry* registry, uint32_t name, co
return; return;
} }
} }
static void registryHandleRemove(void*, wl_registry* registry, uint32_t name) void onGlobalRemove(void*, wl_registry* registry, uint32_t name)
{ {
monitors.remove_if([name](const Monitor &mon) { return mon.registryName == name; }); monitors.remove_if([name](const Monitor &mon) { return mon.registryName == name; });
seats.remove_if([name](const Seat &seat) { return seat.name == name; }); seats.remove_if([name](const Seat &seat) { return seat.name == name; });
} }
static const struct wl_registry_listener registry_listener = { static const struct wl_registry_listener registry_listener = {
.global = registryHandleGlobal, .global = onGlobalAdd,
.global_remove = registryHandleRemove, .global_remove = onGlobalRemove,
}; };
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -541,6 +547,12 @@ void waylandFlush()
} }
} }
void cleanup() {
if (!statusFifoName.empty()) {
unlink(statusFifoName.c_str());
}
}
void die(const char* why) { void die(const char* why) {
fprintf(stderr, "%s\n", why); fprintf(stderr, "%s\n", why);
cleanup(); cleanup();
@ -552,9 +564,3 @@ void diesys(const char* why) {
cleanup(); cleanup();
exit(1); exit(1);
} }
void cleanup() {
if (!statusFifoName.empty()) {
unlink(statusFifoName.c_str());
}
}