File: data_device.h

package info (click to toggle)
wlcs 1.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,528 kB
  • sloc: cpp: 14,211; xml: 7,744; ansic: 952; sh: 164; makefile: 33
file content (195 lines) | stat: -rw-r--r-- 5,340 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright © 2018 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 */

#ifndef WLCS_DATA_DEVICE_H
#define WLCS_DATA_DEVICE_H

#include "active_listeners.h"
#include "wl_interface_descriptor.h"
#include "wl_handle.h"

#include <wayland-client.h>

#include <memory>

namespace wlcs
{
WLCS_CREATE_INTERFACE_DESCRIPTOR(wl_data_device_manager)

class DataSource
{
public:
    DataSource() = default;

    explicit DataSource(struct wl_data_source* ds) : self{ds, deleter} {}

    operator struct wl_data_source*() const { return self.get(); }

    void reset() { self.reset(); }

    void reset(struct wl_data_source* ds) { self.reset(ds, deleter); }

    friend void wl_data_source_destroy(DataSource const&) = delete;

private:
    static void deleter(struct wl_data_source* ds) { wl_data_source_destroy(ds); }

    std::shared_ptr<struct wl_data_source> self;
};

struct DataDeviceListener
{
    DataDeviceListener(struct wl_data_device* data_device)
    {
        active_listeners.add(this);
        wl_data_device_add_listener(data_device, &thunks, this);
    }

    virtual ~DataDeviceListener() { active_listeners.del(this); }

    DataDeviceListener(DataDeviceListener const&) = delete;
    DataDeviceListener& operator=(DataDeviceListener const&) = delete;

    virtual void data_offer(
        struct wl_data_device* wl_data_device,
        struct wl_data_offer* id);

    virtual void enter(
        struct wl_data_device* wl_data_device,
        uint32_t serial,
        struct wl_surface* surface,
        wl_fixed_t x,
        wl_fixed_t y,
        struct wl_data_offer* id);

    virtual void leave(struct wl_data_device* wl_data_device);

    virtual void motion(
        struct wl_data_device* wl_data_device,
        uint32_t time,
        wl_fixed_t x,
        wl_fixed_t y);

    virtual void drop(struct wl_data_device* wl_data_device);

    virtual void selection(
        struct wl_data_device* wl_data_device,
        struct wl_data_offer* id);

private:
    static void data_offer(
        void* data,
        struct wl_data_device* wl_data_device,
        struct wl_data_offer* id);

    static void enter(
        void* data,
        struct wl_data_device* wl_data_device,
        uint32_t serial,
        struct wl_surface* surface,
        wl_fixed_t x,
        wl_fixed_t y,
        struct wl_data_offer* id);

    static void leave(void* data, struct wl_data_device* wl_data_device);

    static void motion(
        void* data,
        struct wl_data_device* wl_data_device,
        uint32_t time,
        wl_fixed_t x,
        wl_fixed_t y);

    static void drop(void* data, struct wl_data_device* wl_data_device);

    static void selection(
        void* data,
        struct wl_data_device* wl_data_device,
        struct wl_data_offer* id);


    static ActiveListeners active_listeners;
    constexpr static wl_data_device_listener thunks =
        {
            &data_offer,
            &enter,
            &leave,
            &motion,
            &drop,
            &selection
        };
};

struct DataOfferListener
{
    DataOfferListener() { active_listeners.add(this); }
    virtual ~DataOfferListener() { active_listeners.del(this); }

    DataOfferListener(DataOfferListener const&) = delete;
    DataOfferListener& operator=(DataOfferListener const&) = delete;

    void listen_to(struct wl_data_offer* data_offer)
    {
        wl_data_offer_add_listener(data_offer, &thunks, this);
    }

    virtual void offer(struct wl_data_offer* data_offer, char const* mime_type);

    virtual void source_actions(struct wl_data_offer* data_offer, uint32_t dnd_actions);

    virtual void action(struct wl_data_offer* data_offer, uint32_t dnd_action);

private:
    static void offer(void* data, struct wl_data_offer* data_offer, char const* mime_type);

    static void source_actions(void* data, struct wl_data_offer* data_offer, uint32_t dnd_actions);

    static void action(void* data, struct wl_data_offer* data_offer, uint32_t dnd_action);

    static ActiveListeners active_listeners;
    constexpr static wl_data_offer_listener thunks =
        {
            &offer,
            &source_actions,
            &action
        };
};

class DataDevice
{
public:
    DataDevice() = default;

    explicit DataDevice(struct wl_data_device* dd) : self{dd, deleter} {}

    operator struct wl_data_device*() const { return self.get(); }

    void reset() { self.reset(); }

    void reset(struct wl_data_device* dd) { self.reset(dd, deleter); }

    friend void wl_data_device_destroy(DataDevice const&) = delete;

private:
    static void deleter(struct wl_data_device* dd) { wl_data_device_destroy(dd); }

    std::shared_ptr<struct wl_data_device> self;
};
}

#endif //WLCS_DATA_DEVICE_H