2016-11-26 00:14:58 -05:00
|
|
|
#pragma once
|
|
|
|
|
2016-12-22 23:08:19 -05:00
|
|
|
#include "modules/meta/event_handler.hpp"
|
2016-11-26 00:14:58 -05:00
|
|
|
#include "modules/meta/static_module.hpp"
|
2023-05-08 13:36:12 -04:00
|
|
|
#include "modules/meta/types.hpp"
|
2016-11-26 00:14:58 -05:00
|
|
|
#include "x11/ewmh.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
class connection;
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
enum class desktop_state {
|
|
|
|
NONE,
|
|
|
|
ACTIVE,
|
|
|
|
URGENT,
|
|
|
|
EMPTY,
|
|
|
|
OCCUPIED,
|
|
|
|
};
|
|
|
|
|
2016-11-26 03:38:55 -05:00
|
|
|
enum class viewport_state {
|
|
|
|
NONE,
|
|
|
|
FOCUSED,
|
|
|
|
UNFOCUSED,
|
|
|
|
};
|
|
|
|
|
2016-12-03 07:45:22 -05:00
|
|
|
struct desktop {
|
2019-11-02 18:16:30 -04:00
|
|
|
explicit desktop(unsigned int index, desktop_state state, label_t&& label)
|
|
|
|
: index(index), state(state), label(label) {}
|
2017-01-24 21:29:11 -05:00
|
|
|
unsigned int index;
|
2016-12-03 07:45:22 -05:00
|
|
|
desktop_state state;
|
|
|
|
label_t label;
|
|
|
|
};
|
|
|
|
|
2016-11-26 03:38:55 -05:00
|
|
|
struct viewport {
|
|
|
|
position pos;
|
2016-11-26 00:14:58 -05:00
|
|
|
string name;
|
2016-12-03 07:45:22 -05:00
|
|
|
vector<unique_ptr<desktop>> desktops;
|
2016-11-26 03:38:55 -05:00
|
|
|
viewport_state state;
|
2016-11-26 00:14:58 -05:00
|
|
|
label_t label;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module used to display EWMH desktops
|
|
|
|
*/
|
2020-05-15 13:59:08 -04:00
|
|
|
class xworkspaces_module : public static_module<xworkspaces_module>, public event_handler<evt::property_notify> {
|
2016-11-26 00:14:58 -05:00
|
|
|
public:
|
2023-05-01 08:58:52 -04:00
|
|
|
explicit xworkspaces_module(const bar_settings& bar, string name_, const config&);
|
2016-11-26 00:14:58 -05:00
|
|
|
|
|
|
|
void update();
|
|
|
|
string get_output();
|
|
|
|
bool build(builder* builder, const string& tag) const;
|
|
|
|
|
2023-05-08 13:36:12 -04:00
|
|
|
static constexpr auto TYPE = XWORKSPACES_TYPE;
|
2020-05-15 13:59:08 -04:00
|
|
|
|
2020-05-24 12:35:12 -04:00
|
|
|
static constexpr auto EVENT_FOCUS = "focus";
|
|
|
|
static constexpr auto EVENT_NEXT = "next";
|
|
|
|
static constexpr auto EVENT_PREV = "prev";
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
protected:
|
2021-01-04 04:38:43 -05:00
|
|
|
void handle(const evt::property_notify& evt) override;
|
2017-01-24 21:29:11 -05:00
|
|
|
|
|
|
|
void rebuild_clientlist();
|
2020-12-31 09:49:39 -05:00
|
|
|
void rebuild_urgent_hints();
|
2016-11-26 00:14:58 -05:00
|
|
|
void rebuild_desktops();
|
2017-01-24 21:29:11 -05:00
|
|
|
void rebuild_desktop_states();
|
2021-03-03 15:55:43 -05:00
|
|
|
void update_current_desktop();
|
2017-01-24 21:29:11 -05:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void action_focus(const string& data);
|
|
|
|
void action_next();
|
|
|
|
void action_prev();
|
|
|
|
|
|
|
|
void focus_direction(bool next);
|
|
|
|
void focus_desktop(unsigned new_desktop);
|
2016-11-26 00:14:58 -05:00
|
|
|
|
|
|
|
private:
|
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 04:00:38 -04:00
|
|
|
static vector<string> get_desktop_names();
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
static constexpr const char* DEFAULT_ICON{"icon-default"};
|
2016-11-26 03:38:55 -05:00
|
|
|
static constexpr const char* DEFAULT_LABEL_STATE{"%icon% %name%"};
|
|
|
|
static constexpr const char* DEFAULT_LABEL_MONITOR{"%name%"};
|
2016-11-26 00:14:58 -05:00
|
|
|
|
2016-11-26 03:38:55 -05:00
|
|
|
static constexpr const char* TAG_LABEL_MONITOR{"<label-monitor>"};
|
|
|
|
static constexpr const char* TAG_LABEL_STATE{"<label-state>"};
|
2016-11-26 00:14:58 -05:00
|
|
|
|
|
|
|
connection& m_connection;
|
2022-03-06 10:40:42 -05:00
|
|
|
ewmh_util::ewmh_connection& m_ewmh;
|
2017-01-24 21:29:11 -05:00
|
|
|
|
2016-11-26 03:38:55 -05:00
|
|
|
vector<monitor_t> m_monitors;
|
2016-11-26 00:14:58 -05:00
|
|
|
|
2017-01-24 21:29:11 -05:00
|
|
|
vector<string> m_desktop_names;
|
2020-12-31 09:49:39 -05:00
|
|
|
vector<bool> m_urgent_desktops;
|
2017-01-24 21:29:11 -05:00
|
|
|
unsigned int m_current_desktop;
|
|
|
|
|
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 04:00:38 -04:00
|
|
|
/**
|
|
|
|
* Maps an xcb window to its desktop number
|
|
|
|
*/
|
|
|
|
map<xcb_window_t, unsigned int> m_clients;
|
2021-01-04 04:17:27 -05:00
|
|
|
map<unsigned int, unsigned int> m_windows;
|
2016-11-26 03:38:55 -05:00
|
|
|
vector<unique_ptr<viewport>> m_viewports;
|
2016-11-26 00:14:58 -05:00
|
|
|
map<desktop_state, label_t> m_labels;
|
2016-11-26 03:38:55 -05:00
|
|
|
label_t m_monitorlabel;
|
2016-11-26 00:14:58 -05:00
|
|
|
iconset_t m_icons;
|
2016-11-26 03:38:55 -05:00
|
|
|
bool m_pinworkspaces{false};
|
2016-11-26 04:33:32 -05:00
|
|
|
bool m_click{true};
|
|
|
|
bool m_scroll{true};
|
2021-01-28 04:11:33 -05:00
|
|
|
bool m_revscroll{false};
|
2016-11-26 00:14:58 -05:00
|
|
|
size_t m_index{0};
|
|
|
|
};
|
2022-03-06 10:40:42 -05:00
|
|
|
} // namespace modules
|
2016-11-26 00:14:58 -05:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|