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 as
6
* 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 mg = mir::graphics;
39
namespace mi = mir::input;
40
namespace geom = mir::geometry;
44
class XCursorImage : public mg::CursorImage
47
XCursorImage(_XcursorImage *image, std::shared_ptr<_XcursorImages> const& save_resource)
49
save_resource(save_resource)
57
void const* as_argb_8888() const override
61
geom::Size size() const override
63
return {image->width, image->height};
65
geom::Displacement hotspot() const override
67
return {image->xhot, image->yhot};
72
std::shared_ptr<_XcursorImages> const save_resource;
76
xcursor_name_for_mir_cursor(std::string const& mir_cursor_name)
78
if (mir_cursor_name == mir_default_cursor_name)
82
else if (mir_cursor_name == mir_arrow_cursor_name)
86
else if (mir_cursor_name == mir_busy_cursor_name)
90
else if (mir_cursor_name == mir_caret_cursor_name)
92
return "xterm"; // Yep
94
else if (mir_cursor_name == mir_pointing_hand_cursor_name)
98
else if (mir_cursor_name == mir_open_hand_cursor_name)
102
else if (mir_cursor_name == mir_closed_hand_cursor_name)
106
else if (mir_cursor_name == mir_horizontal_resize_cursor_name)
108
return "h_double_arrow";
110
else if (mir_cursor_name == mir_vertical_resize_cursor_name)
112
return "v_double_arrow";
114
else if (mir_cursor_name == mir_diagonal_resize_bottom_to_top_cursor_name)
116
return "top_right_corner";
118
else if (mir_cursor_name == mir_diagonal_resize_top_to_bottom_cursor_name)
120
return "bottom_right_corner";
122
else if (mir_cursor_name == mir_omnidirectional_resize_cursor_name)
126
else if (mir_cursor_name == mir_vsplit_resize_cursor_name)
128
return "v_double_arrow";
130
else if (mir_cursor_name == mir_hsplit_resize_cursor_name)
132
return "h_double_arrow";
134
else if (mir_cursor_name == mir_crosshair_cursor_name)
140
return mir_cursor_name;
145
miral::XCursorLoader::XCursorLoader()
147
load_cursor_theme("default");
150
miral::XCursorLoader::XCursorLoader(std::string const& theme)
152
load_cursor_theme(theme);
155
// Each XcursorImages represents images for the different sizes of a given symbolic cursor.
156
void miral::XCursorLoader::load_appropriately_sized_image(_XcursorImages *images)
158
// We would rather take this lock in load_cursor_theme but the Xcursor lib style
159
// makes it difficult to use our standard 'pass the lg around to _locked members' pattern
160
std::lock_guard<std::mutex> lg(guard);
162
// We have to save all the images as XCursor expects us to free them.
163
// This contains the actual image data though, so we need to ensure they stay alive
164
// with the lifetime of the mg::CursorImage instance which refers to them.
165
auto saved_xcursor_library_resource = std::shared_ptr<_XcursorImages>(images, [](_XcursorImages *images)
167
XcursorImagesDestroy(images);
170
for (int i = 0; i < images->nimage; i++)
172
_XcursorImage *candidate = images->images[i];
173
if (candidate->width == mi::default_cursor_size.width.as_uint32_t() &&
174
candidate->height == mi::default_cursor_size.height.as_uint32_t())
176
loaded_images[std::string(images->name)] = std::make_shared<XCursorImage>(candidate, saved_xcursor_library_resource);
181
loaded_images[std::string(images->name)] = std::make_shared<XCursorImage>(images->images[0], saved_xcursor_library_resource);
184
void miral::XCursorLoader::load_cursor_theme(std::string const& theme_name)
186
// Cursors are named by their square dimension...called the nominal size in XCursor terminology, so we just look up by width.
187
// Later we verify the actual size.
188
xcursor_load_theme(theme_name.c_str(), mi::default_cursor_size.width.as_uint32_t(),
189
[](XcursorImages* images, void *this_ptr) -> void
191
// Can't use lambda capture as this lambda is thunked to a C function ptr
192
auto p = static_cast<miral::XCursorLoader*>(this_ptr);
193
p->load_appropriately_sized_image(images);
197
std::shared_ptr<mg::CursorImage> miral::XCursorLoader::image(
198
std::string const& cursor_name,
199
geom::Size const& /*size*/)
201
auto xcursor_name = xcursor_name_for_mir_cursor(cursor_name);
203
std::lock_guard<std::mutex> lg(guard);
205
auto it = loaded_images.find(xcursor_name);
206
if (it != loaded_images.end())
210
it = loaded_images.find("arrow");
211
if (it != loaded_images.end())