2
* Copyright © 2014 Canonical Ltd.
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.
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.
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/>.
16
* Authored by: Robert Carr <robert.carr@canonical.com>
19
#include "xcursor_loader.h"
21
#include "mir/graphics/cursor_image.h"
23
#include <boost/throw_exception.hpp>
28
#include <mir_toolkit/cursors.h>
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
38
namespace me = mir::examples;
39
namespace mg = mir::graphics;
40
namespace mi = mir::input;
41
namespace geom = mir::geometry;
45
class XCursorImage : public mg::CursorImage
48
XCursorImage(_XcursorImage *image, std::shared_ptr<_XcursorImages> const& save_resource)
50
save_resource(save_resource)
52
if (image->width != mi::default_cursor_size.width.as_uint32_t() ||
53
image->height != mi::default_cursor_size.height.as_uint32_t())
55
BOOST_THROW_EXCEPTION(
56
std::runtime_error("Somehow we got a cursor not of the default size (currently only 24x24 supported)"));
64
void const* as_argb_8888() const override
68
geom::Size size() const override
70
return mi::default_cursor_size;
72
geom::Displacement hotspot() const override
74
return {image->xhot, image->yhot};
79
std::shared_ptr<_XcursorImages> const save_resource;
83
xcursor_name_for_mir_cursor(std::string const& mir_cursor_name)
85
if (mir_cursor_name == mir_default_cursor_name)
89
else if (mir_cursor_name == mir_arrow_cursor_name)
93
else if (mir_cursor_name == mir_busy_cursor_name)
97
else if (mir_cursor_name == mir_caret_cursor_name)
99
return "xterm"; // Yep
101
else if (mir_cursor_name == mir_pointing_hand_cursor_name)
105
else if (mir_cursor_name == mir_open_hand_cursor_name)
109
else if (mir_cursor_name == mir_closed_hand_cursor_name)
113
else if (mir_cursor_name == mir_horizontal_resize_cursor_name)
115
return "h_double_arrow";
117
else if (mir_cursor_name == mir_vertical_resize_cursor_name)
119
return "v_double_arrow";
121
else if (mir_cursor_name == mir_diagonal_resize_bottom_to_top_cursor_name)
123
return "top_right_corner";
125
else if (mir_cursor_name == mir_diagonal_resize_top_to_bottom_cursor_name)
127
return "bottom_right_corner";
129
else if (mir_cursor_name == mir_omnidirectional_resize_cursor_name)
133
else if (mir_cursor_name == mir_vsplit_resize_cursor_name)
135
return "v_double_arrow";
137
else if (mir_cursor_name == mir_hsplit_resize_cursor_name)
139
return "h_double_arrow";
141
else if (mir_cursor_name == mir_crosshair_cursor_name)
147
return mir_cursor_name;
152
me::XCursorLoader::XCursorLoader()
154
load_cursor_theme("default");
157
me::XCursorLoader::XCursorLoader(std::string const& theme)
159
load_cursor_theme(theme);
162
// Each XcursorImages represents images for the different sizes of a given symbolic cursor.
163
void me::XCursorLoader::load_appropriately_sized_image(_XcursorImages *images)
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);
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)
174
XcursorImagesDestroy(images);
177
_XcursorImage *image_of_correct_size = nullptr;
178
for (int i = 0; i < images->nimage; i++)
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())
184
image_of_correct_size = candidate;
188
if (!image_of_correct_size)
190
loaded_images[std::string(images->name)] = std::make_shared<XCursorImage>(image_of_correct_size, saved_xcursor_library_resource);
193
void me::XCursorLoader::load_cursor_theme(std::string const& theme_name)
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
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);
206
std::shared_ptr<mg::CursorImage> me::XCursorLoader::image(
207
std::string const& cursor_name,
208
geom::Size const& size)
210
auto xcursor_name = xcursor_name_for_mir_cursor(cursor_name);
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)"));
216
std::lock_guard<std::mutex> lg(guard);
218
auto it = loaded_images.find(xcursor_name);
219
if (it != loaded_images.end())
223
it = loaded_images.find("arrow");
224
if (it != loaded_images.end())