~alan-griffiths/mir/migrate-tests-to-miral

« back to all changes in this revision

Viewing changes to examples/xcursor_loader.cpp

  • Committer: Christopher James Halse Rogers
  • Date: 2017-09-07 05:58:13 UTC
  • mfrom: (4242 development-branch)
  • mto: (4243.1.1 mir3)
  • mto: This revision was merged to the branch mainline in revision 4244.
  • Revision ID: christopher.halse.rogers@canonical.com-20170907055813-4qsg25nirybc8jj3
Merge trunk, resolving conflict

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2014 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 2 or 3,
6
 
 * as published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Robert Carr <robert.carr@canonical.com>
17
 
 */
18
 
 
19
 
#include "xcursor_loader.h"
20
 
 
21
 
#include "mir/graphics/cursor_image.h"
22
 
 
23
 
#include <boost/throw_exception.hpp>
24
 
#include <stdexcept>
25
 
 
26
 
#include <string.h>
27
 
 
28
 
#include <mir_toolkit/cursors.h>
29
 
 
30
 
// Unfortunately this can not be compiled as C++...so we can not namespace
31
 
// these symbols. In order to differentiate from internal symbols
32
 
//  we refer to them via their _ prefixed version, i.e. _XcursorImage
33
 
extern "C"
34
 
{
35
 
#include "xcursor.h"
36
 
}
37
 
 
38
 
namespace me = mir::examples;
39
 
namespace mg = mir::graphics;
40
 
namespace mi = mir::input;
41
 
namespace geom = mir::geometry;
42
 
 
43
 
namespace
44
 
{
45
 
class XCursorImage : public mg::CursorImage
46
 
{
47
 
public:
48
 
    XCursorImage(_XcursorImage *image, std::shared_ptr<_XcursorImages> const& save_resource)
49
 
        : image(image),
50
 
          save_resource(save_resource)
51
 
    {
52
 
        if (image->width != mi::default_cursor_size.width.as_uint32_t() ||
53
 
            image->height != mi::default_cursor_size.height.as_uint32_t())
54
 
        {
55
 
            BOOST_THROW_EXCEPTION(
56
 
                std::runtime_error("Somehow we got a cursor not of the default size (currently only 24x24 supported)"));
57
 
        }
58
 
    }
59
 
 
60
 
    ~XCursorImage()
61
 
    {
62
 
    }
63
 
 
64
 
    void const* as_argb_8888() const override
65
 
    {
66
 
        return image->pixels;
67
 
    }
68
 
    geom::Size size() const override
69
 
    {
70
 
        return mi::default_cursor_size;
71
 
    }
72
 
    geom::Displacement hotspot() const override
73
 
    {
74
 
        return {image->xhot, image->yhot};
75
 
    }
76
 
 
77
 
private:
78
 
    _XcursorImage *image;
79
 
    std::shared_ptr<_XcursorImages> const save_resource;
80
 
};
81
 
 
82
 
std::string const
83
 
xcursor_name_for_mir_cursor(std::string const& mir_cursor_name)
84
 
{
85
 
    if (mir_cursor_name == mir_default_cursor_name)
86
 
    {
87
 
        return "arrow";
88
 
    }
89
 
    else if (mir_cursor_name == mir_arrow_cursor_name)
90
 
    {
91
 
        return "arrow";
92
 
    }
93
 
    else if (mir_cursor_name == mir_busy_cursor_name)
94
 
    {
95
 
        return "watch";
96
 
    }
97
 
    else if (mir_cursor_name == mir_caret_cursor_name)
98
 
    {
99
 
        return "xterm"; // Yep
100
 
    }
101
 
    else if (mir_cursor_name == mir_pointing_hand_cursor_name)
102
 
    {
103
 
        return "hand2";
104
 
    }
105
 
    else if (mir_cursor_name == mir_open_hand_cursor_name)
106
 
    {
107
 
        return "hand";
108
 
    }
109
 
    else if (mir_cursor_name == mir_closed_hand_cursor_name)
110
 
    {
111
 
        return "grabbing";
112
 
    }
113
 
    else if (mir_cursor_name == mir_horizontal_resize_cursor_name)
114
 
    {
115
 
        return "h_double_arrow";
116
 
    }
117
 
    else if (mir_cursor_name == mir_vertical_resize_cursor_name)
118
 
    {
119
 
        return "v_double_arrow";
120
 
    }
121
 
    else if (mir_cursor_name == mir_diagonal_resize_bottom_to_top_cursor_name)
122
 
    {
123
 
        return "top_right_corner";
124
 
    }
125
 
    else if (mir_cursor_name == mir_diagonal_resize_top_to_bottom_cursor_name)
126
 
    {
127
 
        return "bottom_right_corner";
128
 
    }
129
 
    else if (mir_cursor_name == mir_omnidirectional_resize_cursor_name)
130
 
    {
131
 
        return "fleur";
132
 
    }
133
 
    else if (mir_cursor_name == mir_vsplit_resize_cursor_name)
134
 
    {
135
 
        return "v_double_arrow";
136
 
    }
137
 
    else if (mir_cursor_name == mir_hsplit_resize_cursor_name)
138
 
    {
139
 
        return "h_double_arrow";
140
 
    }
141
 
    else if (mir_cursor_name == mir_crosshair_cursor_name)
142
 
    {
143
 
        return "crosshair";
144
 
    }
145
 
    else
146
 
    {
147
 
        return mir_cursor_name;
148
 
    }
149
 
}
150
 
}
151
 
 
152
 
me::XCursorLoader::XCursorLoader()
153
 
{
154
 
    load_cursor_theme("default");
155
 
}
156
 
 
157
 
me::XCursorLoader::XCursorLoader(std::string const& theme)
158
 
{
159
 
    load_cursor_theme(theme);
160
 
}
161
 
 
162
 
// Each XcursorImages represents images for the different sizes of a given symbolic cursor.
163
 
void me::XCursorLoader::load_appropriately_sized_image(_XcursorImages *images)
164
 
{
165
 
    // We would rather take this lock in load_cursor_theme but the Xcursor lib style 
166
 
    // makes it difficult to use our standard 'pass the lg around to _locked members' pattern
167
 
    std::lock_guard<std::mutex> lg(guard);
168
 
 
169
 
    // We have to save all the images as XCursor expects us to free them.
170
 
    // This contains the actual image data though, so we need to ensure they stay alive 
171
 
    // with the lifetime of the mg::CursorImage instance which refers to them.
172
 
    auto saved_xcursor_library_resource = std::shared_ptr<_XcursorImages>(images, [](_XcursorImages *images)
173
 
        {
174
 
            XcursorImagesDestroy(images);
175
 
        });
176
 
 
177
 
    _XcursorImage *image_of_correct_size = nullptr;
178
 
    for (int i = 0; i < images->nimage; i++)
179
 
    {
180
 
        _XcursorImage *candidate = images->images[i];
181
 
        if (candidate->width == mi::default_cursor_size.width.as_uint32_t() &&
182
 
            candidate->height == mi::default_cursor_size.height.as_uint32_t())
183
 
        {
184
 
            image_of_correct_size = candidate;
185
 
            break;
186
 
        }
187
 
    }
188
 
    if (!image_of_correct_size)
189
 
        return;
190
 
    loaded_images[std::string(images->name)] = std::make_shared<XCursorImage>(image_of_correct_size, saved_xcursor_library_resource);
191
 
}
192
 
 
193
 
void me::XCursorLoader::load_cursor_theme(std::string const& theme_name)
194
 
{
195
 
    // Cursors are named by their square dimension...called the nominal size in XCursor terminology, so we just look up by width.
196
 
    // Later we verify the actual size.
197
 
    xcursor_load_theme(theme_name.c_str(), mi::default_cursor_size.width.as_uint32_t(), 
198
 
        [](XcursorImages* images, void *this_ptr)  -> void
199
 
        {
200
 
            // Can't use lambda capture as this lambda is thunked to a C function ptr
201
 
            auto p = static_cast<me::XCursorLoader*>(this_ptr);
202
 
            p->load_appropriately_sized_image(images);
203
 
        }, this);
204
 
}
205
 
 
206
 
std::shared_ptr<mg::CursorImage> me::XCursorLoader::image(
207
 
    std::string const& cursor_name,
208
 
    geom::Size const& size)
209
 
{
210
 
    auto xcursor_name = xcursor_name_for_mir_cursor(cursor_name);
211
 
 
212
 
    if (size != mi::default_cursor_size)
213
 
        BOOST_THROW_EXCEPTION(
214
 
            std::logic_error("Only the default cursor size is currently supported (mi::default_cursor_size)"));
215
 
    
216
 
    std::lock_guard<std::mutex> lg(guard);
217
 
 
218
 
    auto it = loaded_images.find(xcursor_name);
219
 
    if (it != loaded_images.end())
220
 
        return it->second;
221
 
 
222
 
    // Fall back
223
 
    it = loaded_images.find("arrow");
224
 
    if (it != loaded_images.end())
225
 
        return it->second;
226
 
    
227
 
    return nullptr;
228
 
}