polybar-dwm/include/modules/xworkspaces.hpp
Matthias Viehweger 52f0623315 feat(xworkspaces): Support occupied workspaces (#882)
A workspace is occupied if it is not active and there is at least one window managed by the WM (`_NET_CLIENT_LIST`) that has set `_NET_WM_DESKTOP` to that workspace.

The behavior when `_NET_WM_DESKTOP` is not set is not yet clear but this is unlikely to happen since most WMs will position windows on some desktop.

Closes #874
Fixes #1444
Fixes #1033 

* Set Desktop OCCUPIED if a window moves there

This covers more of an edge-case. I did this first by accident, it might
vanish later on.

* Replace tracking change of WS with currently used WS

* Untrack occupied workspaces

* Track windows and their desktops in pairs

* Match type of occupied_desktops with current_desktop

Because the index needs to be matched later on, type mismatches would be non-ideal.

* Recreate the occupied desktops everytime and remove duplicates

* Readd support for moving windows to other desktops

* Use less characters to empty the vector

* Rename variable storing the desktops

* Recount windows on every occasion

This alone simplifies the management and the lookup for occupation of a
workspace

* Keep track of number of windows in every workspace

* Add debugging output that shall be removed before merging

* Remove obsolete TODO

* m_client_list should always be diff'd, since the desktop may change

Therefore we update the desktop-count tally every time the client_list
changes. It may just be a desktop-change without a change of
clients.size()...

* Add more logging-spam to understand window/desktop lifecycle

* Lock event-handler to serialize handling of events

* Fix occupied workspace counting and change to bool array

Also, performance improvements when diffing new and old client lists

* Fix crash when all clients are removed

* Conform to linter and styleguide

* Shorten conditional as it is standard enough

Since this only guards against 0-divisions, it can be shortened
without risking too much confusion down the road.

* Guard against multiple threads accessing and modifying data

Fixes #1444

Modification of internal data happens through the handle-method, while
the build-method tries to access the data structures for display. Since
some modifications clear e.g. the m_viewports, references may become
invalid between looping over them an accessing them.

The mutex should guard against this simultanuous access.

* Do not 'adopt_lock', because calls come from very different threads

To my understanding, adopt_lock has some dependency on the mutex-ownership. Since
the lock is once called from the inside (in handle) and once from the outside (in
build), there might be a problem. After brief testing, the segfaults happened fewer
times.

See #1444

* Also listen to _NET_WM_DESKTOP

In order to move a window from one desktop to another, it is sufficient
to set the desktop-property of that window. xmonad fires a lot of events
in the case of moving a window, herbstluftwm only updates the
_NET_WM_DESKTOP-atom of the window.

This change reloads the clientlist in order to correctly set the
desktop state "occupied".

* Describe need and use of mutex

It might be possible to relieve the guard in xworkspaces_module::handle,
but I am unsure about this. Since xmonad emits a lot of events on almost
every minor change, I would let the guard keep its post, avoiding
race-conditions in event-handling.

* Give temporary variables better names

* Clarify purpose of loop

About 80% of this comment are taken from
https://github.com/jaagr/polybar/pull/882#discussion_r255317363

* Remove merge-remainder

* Use a simpler method to list occupied desktops.

Co-authored-by: Jérôme Boulmier <jerome.boulmier@outlook.fr>

* Document m_clients field
2019-10-21 10:00:38 +02:00

122 lines
3.1 KiB
C++

#pragma once
#include <bitset>
#include <mutex>
#include <set>
#include "components/config.hpp"
#include "components/types.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"
#include "x11/ewmh.hpp"
#include "x11/icccm.hpp"
#include "x11/window.hpp"
POLYBAR_NS
class connection;
namespace modules {
enum class desktop_state {
NONE,
ACTIVE,
URGENT,
EMPTY,
OCCUPIED,
};
enum class viewport_state {
NONE,
FOCUSED,
UNFOCUSED,
};
struct desktop {
explicit desktop(unsigned int index, unsigned int offset, desktop_state state, label_t&& label)
: index(index), offset(offset), state(state), label(label) {}
unsigned int index;
unsigned int offset;
desktop_state state;
label_t label;
};
struct viewport {
position pos;
string name;
vector<unique_ptr<desktop>> desktops;
viewport_state state;
label_t label;
};
/**
* Module used to display EWMH desktops
*/
class xworkspaces_module : public static_module<xworkspaces_module>,
public event_handler<evt::property_notify>,
public input_handler {
public:
explicit xworkspaces_module(const bar_settings& bar, string name_);
void update();
string get_output();
bool build(builder* builder, const string& tag) const;
protected:
void handle(const evt::property_notify& evt);
void rebuild_clientlist();
void rebuild_desktops();
void rebuild_desktop_states();
void set_desktop_urgent(xcb_window_t window);
bool input(string&& cmd);
private:
static vector<string> get_desktop_names();
static constexpr const char* DEFAULT_ICON{"icon-default"};
static constexpr const char* DEFAULT_LABEL_STATE{"%icon% %name%"};
static constexpr const char* DEFAULT_LABEL_MONITOR{"%name%"};
static constexpr const char* TAG_LABEL_MONITOR{"<label-monitor>"};
static constexpr const char* TAG_LABEL_STATE{"<label-state>"};
static constexpr const char* EVENT_PREFIX{"xworkspaces-"};
static constexpr const char* EVENT_CLICK{"focus="};
static constexpr const char* EVENT_SCROLL_UP{"next"};
static constexpr const char* EVENT_SCROLL_DOWN{"prev"};
connection& m_connection;
ewmh_connection_t m_ewmh;
vector<monitor_t> m_monitors;
bool m_monitorsupport{true};
vector<string> m_desktop_names;
unsigned int m_current_desktop;
string m_current_desktop_name;
/**
* Maps an xcb window to its desktop number
*/
map<xcb_window_t, unsigned int> m_clients;
vector<unique_ptr<viewport>> m_viewports;
map<desktop_state, label_t> m_labels;
label_t m_monitorlabel;
iconset_t m_icons;
bool m_pinworkspaces{false};
bool m_click{true};
bool m_scroll{true};
size_t m_index{0};
// The following mutex is here to protect the data of this modules.
// This can't be achieved using m_buildlock since we "CRTP override" get_output().
mutable mutex m_workspace_mutex;
event_timer m_timer{0L, 25L};
};
} // namespace modules
POLYBAR_NS_END