File: vswitch.cpp

package info (click to toggle)
wayfire 0.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,732 kB
  • sloc: cpp: 49,708; xml: 2,783; ansic: 699; makefile: 161
file content (304 lines) | stat: -rw-r--r-- 8,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "plugins/ipc/ipc-helpers.hpp"
#include "wayfire/core.hpp"
#include <wayfire/plugins/vswitch.hpp>
#include <wayfire/per-output-plugin.hpp>
#include <linux/input.h>
#include <wayfire/util/log.hpp>
#include <wayfire/seat.hpp>
#include "plugins/ipc/ipc-method-repository.hpp"
#include "wayfire/plugins/common/shared-core-data.hpp"

class vswitch : public wf::per_output_plugin_instance_t
{
  private:

    /**
     * Adapter around the general algorithm, so that our own stop function is
     * called.
     */
    class vswitch_basic_plugin : public wf::vswitch::workspace_switch_t
    {
      public:
        vswitch_basic_plugin(wf::output_t *output,
            std::function<void()> on_done) : workspace_switch_t(output)
        {
            this->on_done = on_done;
        }

        void stop_switch(bool normal_exit) override
        {
            workspace_switch_t::stop_switch(normal_exit);
            on_done();
        }

      private:
        std::function<void()> on_done;
    };

    std::unique_ptr<vswitch_basic_plugin> algorithm;
    std::unique_ptr<wf::vswitch::control_bindings_t> bindings;

    // Capabilities which are always required for vswitch, for now wall needs
    // a custom renderer.
    static constexpr uint32_t base_caps = wf::CAPABILITY_CUSTOM_RENDERER;

    wf::plugin_activation_data_t grab_interface = {
        .name   = "vswitch",
        .cancel = [=] () { algorithm->stop_switch(false); },
    };

  public:
    void init()
    {
        output->connect(&on_set_workspace_request);
        output->connect(&on_grabbed_view_disappear);

        algorithm = std::make_unique<vswitch_basic_plugin>(output,
            [=] () { output->deactivate_plugin(&grab_interface); });

        bindings = std::make_unique<wf::vswitch::control_bindings_t>(output);
        bindings->setup([this] (wf::point_t delta, wayfire_toplevel_view view, bool only_view)
        {
            // Do not switch workspace with sticky view, they are on all
            // workspaces anyway
            if (view && view->sticky)
            {
                view = nullptr;
            }

            if (this->set_capabilities(wf::CAPABILITY_MANAGE_DESKTOP))
            {
                if (delta == wf::point_t{0, 0})
                {
                    // Consume input event
                    return true;
                }

                if (only_view && view)
                {
                    auto size = output->get_screen_size();

                    for (auto& v : view->enumerate_views(false))
                    {
                        auto origin = wf::origin(v->get_pending_geometry());
                        v->move(origin.x + delta.x * size.width, origin.y + delta.y * size.height);
                    }

                    wf::view_change_workspace_signal data;
                    data.view = view;
                    data.from = output->wset()->get_current_workspace();
                    data.to   = data.from + delta;
                    output->emit(&data);
                    wf::get_core().seat->refocus();

                    return true;
                }

                return add_direction(delta, view);
            } else
            {
                return false;
            }
        });
    }

    inline bool is_active()
    {
        return output->is_plugin_active(grab_interface.name);
    }

    inline bool can_activate()
    {
        return is_active() || output->can_activate_plugin(&grab_interface);
    }

    /**
     * Check if we can switch the plugin capabilities.
     * This makes sense only if the plugin is already active, otherwise,
     * the operation can succeed.
     *
     * @param caps The additional capabilities required, aside from the
     *   base caps.
     */
    bool set_capabilities(uint32_t caps)
    {
        uint32_t total_caps = caps | base_caps;
        if (!is_active())
        {
            this->grab_interface.capabilities = total_caps;

            return true;
        }

        // already have everything needed
        if ((grab_interface.capabilities & total_caps) == total_caps)
        {
            // note: do not downgrade, in case total_caps are a subset of
            // current_caps
            return true;
        }

        // check for only the additional caps
        if (output->can_activate_plugin(caps))
        {
            grab_interface.capabilities = total_caps;

            return true;
        } else
        {
            return false;
        }
    }

    bool add_direction(wf::point_t delta, wayfire_view view = nullptr)
    {
        if (!is_active() && !start_switch())
        {
            return false;
        }

        if (view && ((view->role != wf::VIEW_ROLE_TOPLEVEL) || !view->is_mapped()))
        {
            view = nullptr;
        }

        algorithm->set_overlay_view(toplevel_cast(view));
        algorithm->set_target_workspace(
            output->wset()->get_current_workspace() + delta);

        return true;
    }

    wf::signal::connection_t<wf::view_disappeared_signal> on_grabbed_view_disappear =
        [=] (wf::view_disappeared_signal *ev)
    {
        if (ev->view == algorithm->get_overlay_view())
        {
            algorithm->set_overlay_view(nullptr);
        }
    };

    wf::signal::connection_t<wf::workspace_change_request_signal> on_set_workspace_request =
        [=] (wf::workspace_change_request_signal *ev)
    {
        if (ev->old_viewport == ev->new_viewport)
        {
            // nothing to do
            ev->carried_out = true;

            return;
        }

        if (is_active())
        {
            ev->carried_out = add_direction(ev->new_viewport - ev->old_viewport);
        } else
        {
            if (this->set_capabilities(0))
            {
                if (ev->fixed_views.size() > 2)
                {
                    LOGE("NOT IMPLEMENTED: ",
                        "changing workspace with more than 1 fixed view");
                }

                ev->carried_out = add_direction(ev->new_viewport - ev->old_viewport,
                    ev->fixed_views.empty() ? nullptr : ev->fixed_views[0]);
            }
        }
    };

    bool start_switch()
    {
        if (!output->activate_plugin(&grab_interface))
        {
            return false;
        }

        algorithm->start_switch();

        return true;
    }

    void fini()
    {
        if (is_active())
        {
            algorithm->stop_switch(false);
        }

        bindings->tear_down();
    }
};

class wf_vswitch_global_plugin_t : public wf::per_output_plugin_t<vswitch>
{
    wf::shared_data::ref_ptr_t<wf::ipc::method_repository_t> ipc_repo;

  public:
    void init() override
    {
        per_output_plugin_t::init();
        ipc_repo->register_method("vswitch/set-workspace", request_workspace);
    }

    void fini() override
    {
        per_output_plugin_t::fini();
        ipc_repo->unregister_method("vswitch/set-workspace");
    }

    wf::ipc::method_callback request_workspace = [=] (const nlohmann::json& data)
    {
        WFJSON_EXPECT_FIELD(data, "x", number_unsigned);
        WFJSON_EXPECT_FIELD(data, "y", number_unsigned);
        WFJSON_EXPECT_FIELD(data, "output-id", number_unsigned);
        WFJSON_OPTIONAL_FIELD(data, "view-id", number_unsigned);

        auto wo = wf::ipc::find_output_by_id(data["output-id"]);
        if (!wo)
        {
            return wf::ipc::json_error("Invalid output!");
        }

        auto grid_size = wo->wset()->get_workspace_grid_size();
        if ((data["x"] >= grid_size.width) || (data["y"] >= grid_size.height))
        {
            return wf::ipc::json_error("Workspace coordinates are too big!");
        }

        wayfire_toplevel_view switch_with_view;
        if (data.contains("view-id"))
        {
            auto view = toplevel_cast(wf::ipc::find_view_by_id(data["view-id"]));
            if (!view)
            {
                return wf::ipc::json_error("Invalid view or view not toplevel!");
            }

            if (!view->is_mapped())
            {
                return wf::ipc::json_error("Cannot grab unmapped view!");
            }

            if (view->get_output() != wo)
            {
                return wf::ipc::json_error("Cannot grab view on a different output!");
            }

            switch_with_view = view;
        }

        if (output_instance[wo]->set_capabilities(wf::CAPABILITY_MANAGE_COMPOSITOR))
        {
            wf::point_t new_viewport = {data["x"], data["y"]};
            wf::point_t cur_viewport = wo->wset()->get_current_workspace();
            wf::point_t delta = new_viewport - cur_viewport;
            output_instance[wo]->add_direction(delta, switch_with_view);
        }

        return wf::ipc::json_ok();
    };
};

DECLARE_WAYFIRE_PLUGIN(wf_vswitch_global_plugin_t);