summaryrefslogtreecommitdiff
path: root/gui-apps/oledsaver/files/oledsaver-cursor.cpp
blob: ee8f0e65e75ae9ef5a9517a39883888d8c3550e3 (plain)
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
#include <cmath>
#include <string>

#include <wayfire/core.hpp>
#include <wayfire/output-layout.hpp>
#include <wayfire/output.hpp>
#include <wayfire/plugin.hpp>
#include <wayfire/plugins/common/shared-core-data.hpp>
#include <wayfire/plugins/ipc/ipc-method-repository.hpp>

namespace wf
{
class oledsaver_cursor_plugin_t : public wf::plugin_interface_t
{
  public:
    void init() override
    {
        repo->register_method("oledsaver/cursor_status",
            [=] (wf::json_t data) { return this->cursor_status(data); });
        repo->register_method("oledsaver/hide_cursor",
            [=] (wf::json_t data) { return this->hide_cursor(data); });
        repo->register_method("oledsaver/unhide_cursor",
            [=] (wf::json_t data) { return this->unhide_cursor(data); });
    }

    void fini() override
    {
        repo->unregister_method("oledsaver/cursor_status");
        repo->unregister_method("oledsaver/hide_cursor");
        repo->unregister_method("oledsaver/unhide_cursor");
        set_cursor_hidden(false);
    }

    bool is_unloadable() override
    {
        return false;
    }

  private:
    wf::shared_data::ref_ptr_t<wf::ipc::method_repository_t> repo;
    bool cursor_hidden = false;

    wf::output_t *get_output_at(wf::pointf_t position)
    {
        for (auto output : wf::get_core().output_layout->get_outputs())
        {
            if (output->get_layout_geometry() & position)
            {
                return output;
            }
        }

        return nullptr;
    }

    void set_cursor_hidden(bool hidden)
    {
        if (hidden == cursor_hidden)
        {
            return;
        }

        if (hidden)
        {
            wf::get_core().hide_cursor();
        } else
        {
            wf::get_core().unhide_cursor();
        }

        cursor_hidden = hidden;
    }

    wf::json_t cursor_status(wf::json_t)
    {
        wf::json_t response;
        auto pos = wf::get_core().get_cursor_position();
        bool valid = std::isfinite(pos.x) && std::isfinite(pos.y);

        response["valid"] = valid;
        response["hidden"] = cursor_hidden;
        response["x"] = valid ? pos.x : 0.0;
        response["y"] = valid ? pos.y : 0.0;

        auto output = valid ? get_output_at(pos) : nullptr;
        response["output"] = (output && output->handle) ? output->handle->name : "";
        return response;
    }

    wf::json_t hide_cursor(wf::json_t)
    {
        set_cursor_hidden(true);
        return wf::ipc::json_ok();
    }

    wf::json_t unhide_cursor(wf::json_t)
    {
        set_cursor_hidden(false);
        return wf::ipc::json_ok();
    }
};
}

DECLARE_WAYFIRE_PLUGIN(wf::oledsaver_cursor_plugin_t);