Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/common/mir/constexpr_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 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 2 or 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/>.
*/


#ifndef MIR_CONSTEXPR_UTILS_H_
#define MIR_CONSTEXPR_UTILS_H_

#include <string_view>

namespace mir
{
inline constexpr std::size_t strlen_c(std::string_view str)
{
return str.size();
}
}

#endif
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_library(mircommon SHARED
${PROJECT_SOURCE_DIR}/include/common/mir/input/mir_touchscreen_config.h
${PROJECT_SOURCE_DIR}/include/common/mir/input/mir_keyboard_config.h
${PROJECT_SOURCE_DIR}/include/common/mir/input/keyboard_leds.h
${PROJECT_SOURCE_DIR}/include/common/mir/constexpr_utils.h
${MIR_COMMON_SOURCES}
)

Expand Down
7 changes: 4 additions & 3 deletions src/miral/display_configuration_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <miral/display_configuration_option.h>
#include "static_display_config.h"

#include <mir/constexpr_utils.h>
#include <mir/graphics/default_display_configuration_policy.h>
#include <mir/graphics/display_configuration.h>
#include <mir/options/option.h>
Expand Down Expand Up @@ -226,13 +227,13 @@ void miral::display_configuration_options(mir::Server& server)
{
layout_selector = std::make_shared<mg::SingleDisplayConfigurationPolicy>();
}
else if (display_layout.compare(0, strlen(static_opt_val), static_opt_val) == 0)
else if (display_layout.compare(0, mir::strlen_c(static_opt_val), static_opt_val) == 0)
{
if (scale != 1.0)
{
mir::fatal_error("Display scale option can't be used with static display configuration");
}
auto sdc = std::make_shared<StaticDisplayConfig>(display_layout.substr(strlen(static_opt_val)));
auto sdc = std::make_shared<StaticDisplayConfig>(display_layout.substr(mir::strlen_c(static_opt_val)));
server.add_init_callback([sdc, &server]{ sdc->init_auto_reload(server); });
layout_selector = std::move(sdc);
}
Expand All @@ -249,7 +250,7 @@ void miral::display_configuration_options(mir::Server& server)
mir::fatal_error("Display scale option can't be used with autoscale");
}

if (display_layout.compare(0, strlen(static_opt_val), static_opt_val) == 0)
if (display_layout.compare(0, mir::strlen_c(static_opt_val), static_opt_val) == 0)
{
mir::fatal_error("Display autoscale option can't be used with static display configuration");
}
Expand Down
9 changes: 5 additions & 4 deletions src/platform/options/default_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

#include <mir/options/default_configuration.h>

#include <mir/abnormal_exit.h>
#include <mir/constexpr_utils.h>
#include <mir/graphics/platform.h>
#include <mir/logging/null_shared_library_prober_report.h>
#include <mir/options/program_option.h>
#include <mir/shared_library.h>
#include <mir/graphics/platform.h>
#include <mir/abnormal_exit.h>
#include <mir/shared_library_prober.h>
#include <mir/logging/null_shared_library_prober_report.h>

#include <algorithm>
#include <format>
Expand Down Expand Up @@ -573,7 +574,7 @@ void mo::DefaultConfiguration::parse_environment(
[=, this](std::string const& from) -> std::string
{
auto const prefix = "MIR_SERVER_";
auto const sizeof_prefix = strlen(prefix);
auto const sizeof_prefix = strlen_c(prefix);

if (!from.starts_with(prefix))
{
Expand Down
7 changes: 4 additions & 3 deletions src/platforms/evdev/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
#include "libinput_ptr.h"
#include "fd_store.h"

#include <mir/udev/wrapper.h>
#include <mir/console_services.h>
#include <mir/constexpr_utils.h>
#include <mir/dispatch/dispatchable.h>
#include <mir/dispatch/readable_fd.h>
#include <mir/dispatch/multiplexing_dispatchable.h>
#include <mir/dispatch/action_queue.h>
#include <mir/console_services.h>
#include <mir/udev/wrapper.h>

#include <mir/input/input_device_registry.h>
#include <mir/input/input_report.h>
Expand Down Expand Up @@ -310,7 +311,7 @@ void mie::Platform::start()

std::string devnode{workaround_device->devnode()};
// Libinput filters out anything without “event” as its name
if (strncmp(workaround_device->sysname(), "event", strlen("event")) != 0)
if (strncmp(workaround_device->sysname(), "event", strlen_c("event")) != 0)
{
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/gbm-kms/server/kms/platform_symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <mir/assert_module_entry_point.h>
#include <mir/libname.h>
#include <mir/console_services.h>
#include <mir/constexpr_utils.h>
#include "one_shot_device_observer.h"
#include <mir/graphics/egl_error.h>
#include <mir/graphics/gl_config.h>
Expand Down Expand Up @@ -452,7 +453,7 @@ auto probe_rendering_platform(
if (strncmp(
"llvmpipe",
renderer_string,
strlen("llvmpipe")) == 0)
mir::strlen_c("llvmpipe")) == 0)
{
mir::log_info("Detected software renderer: %s", renderer_string);
// Leave the priority at ::unsupported; if we've got a software renderer then
Expand Down
12 changes: 7 additions & 5 deletions src/server/console/linux_virtual_terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/

#include "linux_virtual_terminal.h"
#include "ioctl_vt_switcher.h"

#include <mir/constexpr_utils.h>
#include <mir/emergency_cleanup_registry.h>
#include <mir/fd.h>
#include <mir/graphics/display_report.h>
#include <mir/graphics/event_handler_register.h>
#include <mir/fd.h>
#include <mir/emergency_cleanup_registry.h>
#include "ioctl_vt_switcher.h"
#include <mir/raii.h>
#include <mir/synchronised.h>

Expand Down Expand Up @@ -658,9 +660,9 @@ std::future<std::unique_ptr<mir::Device>> mir::LinuxVirtualTerminal::acquire_dev

while (uevent.getline(line_buffer, sizeof(line_buffer)))
{
if (strncmp(line_buffer, "DEVNAME=", strlen("DEVNAME=")) == 0)
if (strncmp(line_buffer, "DEVNAME=", strlen_c("DEVNAME=")) == 0)
{
return std::string{"/dev/"} + std::string{line_buffer + strlen("DEVNAME=")};
return std::string{"/dev/"} + std::string{line_buffer + strlen_c("DEVNAME=")};
}
}
BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to read DEVNAME"}));
Expand Down
7 changes: 4 additions & 3 deletions src/server/console/logind_console_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@

#include "logind-session.h"

#include <mir/constexpr_utils.h>
#include <mir/fd.h>
#include <mir/main_loop.h>
#include <mir/glib_main_loop.h>
#include <mir/main_loop.h>

#define MIR_LOG_COMPONTENT "logind"
#include <mir/log.h>
Expand Down Expand Up @@ -265,7 +266,7 @@ mir::LogindConsoleServices::LogindConsoleServices(
session_path.c_str())},
switch_away{[](){ return true; }},
switch_to{[](){ return true; }},
active{strncmp("active", logind_session_get_state(session_proxy.get()), strlen("active")) == 0}
active{strncmp("active", logind_session_get_state(session_proxy.get()), strlen_c("active")) == 0}
{
GErrorPtr error;

Expand Down Expand Up @@ -789,7 +790,7 @@ GDBusMessage* mir::LogindConsoleServices::resume_device_dbus_filter(
return message;

// …if it's a signal, but it's not ResumeDevice, we don't need to process it.
if (strncmp(g_dbus_message_get_member(message), "ResumeDevice", strlen("ResumeDevice")) != 0)
if (strncmp(g_dbus_message_get_member(message), "ResumeDevice", strlen_c("ResumeDevice")) != 0)
return message;

// We've definitely got a ResumeDevice signal! Now to extract the parameters, and
Expand Down
5 changes: 3 additions & 2 deletions src/server/console/minimal_console_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "minimal_console_services.h"

#include <mir/constexpr_utils.h>
#include <mir/log.h>

#include <boost/exception/errinfo_errno.hpp>
Expand Down Expand Up @@ -104,9 +105,9 @@ std::future<std::unique_ptr<mir::Device>> mir::MinimalConsoleServices::acquire_d

while (uevent.getline(line_buffer, sizeof(line_buffer)))
{
if (strncmp(line_buffer, "DEVNAME=", strlen("DEVNAME=")) == 0)
if (strncmp(line_buffer, "DEVNAME=", strlen_c("DEVNAME=")) == 0)
{
return std::string{"/dev/"} + std::string{line_buffer + strlen("DEVNAME=")};
return std::string{"/dev/"} + std::string{line_buffer + strlen_c("DEVNAME=")};
}
}
BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to read DEVNAME"}));
Expand Down
17 changes: 10 additions & 7 deletions src/server/frontend_wayland/foreign_toplevel_manager_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

#include "wayland_utils.h"
#include "desktop_file_manager.h"
#include <mir/wayland/weak.h>

#include <mir/constexpr_utils.h>
#include <mir/executor.h>
#include <mir/frontend/surface_stack.h>
#include <mir/shell/shell.h>
#include <mir/shell/surface_specification.h>
#include <mir/log.h>
#include <mir/main_loop.h>
#include <mir/scene/null_observer.h>
#include <mir/scene/null_surface_observer.h>
#include <mir/scene/surface.h>
#include <mir/scene/session.h>
#include <mir/log.h>
#include <mir/executor.h>
#include <mir/main_loop.h>
#include <mir/shell/surface_specification.h>
#include <mir/shell/shell.h>
#include <mir/wayland/weak.h>

#include <algorithm>
#include <mutex>
Expand Down Expand Up @@ -579,9 +581,10 @@ void mf::GDesktopFileCache::refresh_app_cache()

// It is likely that [id] ends in a .desktop suffix. If that's the case, we will strip
// it out since that isn't useful in this context.

const char* const desktop_suffix = ".desktop";
if (id.ends_with(desktop_suffix))
id.erase(id.length() - strlen(desktop_suffix));
id.erase(id.length() - strlen_c(desktop_suffix));

std::shared_ptr<DesktopFile> file = std::make_shared<DesktopFile>(id.c_str(), wm_class, exec);
if (g_app_info_should_show(app_info))
Expand Down
9 changes: 5 additions & 4 deletions tests/mir_test_doubles/mock_drm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <mir/test/doubles/mock_drm.h>
#include <mir_test_framework/mmap_wrapper.h>
#include <mir_test_framework/open_wrapper.h>
#include <mir/constexpr_utils.h>
#include <mir/geometry/size.h>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -235,7 +236,7 @@ mtd::MockDRM::MockDRM()
[this](char const* path, int flags, std::optional<mode_t>) -> std::optional<int>
{
char const* const drm_prefix = "/dev/dri/";
if (!strncmp(path, drm_prefix, strlen(drm_prefix)))
if (!strncmp(path, drm_prefix, strlen_c(drm_prefix)))
{
// I don't think we need to be able to distinguish based on mode. ppc64el (at least) *does*
// call the 3-parameter open()
Expand Down Expand Up @@ -373,11 +374,11 @@ mtd::MockDRM::MockDRM()
1,
2,
3,
static_cast<int>(strlen("mock_driver")),
static_cast<int>(strlen_c("mock_driver")),
const_cast<char*>("mock_driver"),
static_cast<int>(strlen("1 Jan 1970")),
static_cast<int>(strlen_c("1 Jan 1970")),
const_cast<char*>("1 Jan 1970"),
static_cast<int>(strlen("Not really a driver")),
static_cast<int>(strlen_c("Not really a driver")),
const_cast<char*>("Not really a driver")
};
ON_CALL(*this, drmGetVersion(_))
Expand Down
11 changes: 6 additions & 5 deletions tests/unit-tests/console/test_linux_virtual_terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#include "src/server/console/linux_virtual_terminal.h"
#include "src/server/report/null_report_factory.h"
#include <mir/graphics/event_handler_register.h>
#include <mir/anonymous_shm_file.h>
#include <mir/constexpr_utils.h>
#include <mir/emergency_cleanup_registry.h>
#include <mir/graphics/event_handler_register.h>

#include <mir/test/fake_shared.h>
#include <mir/test/doubles/mock_display_report.h>
Expand Down Expand Up @@ -791,15 +792,15 @@ std::string uevent_content_for_device(
{
std::stringstream content;

if (strncmp(device_name, "/dev/", strlen("/dev/")) != 0)
if (strncmp(device_name, "/dev/", mir::strlen_c("/dev/")) != 0)
{
throw std::logic_error{"device_name is expected to be the fully-qualified /dev/foo path"};
}

content
<< "MAJOR=" << major << "\n"
<< "MINOR=" << minor << "\n"
<< "DEVNAME=" << device_name + strlen ("/dev/") << "\n";
<< "DEVNAME=" << device_name + mir::strlen_c("/dev/") << "\n";

return content.str();
}
Expand Down Expand Up @@ -931,8 +932,8 @@ TEST_F(LinuxVirtualTerminalTest, throws_error_when_parsing_fails)
"MINOR=61\n"
"I_AINT_NO_DEVNAME=fb0";

mir::AnonymousShmFile uevent{strlen(uevent_content)};
::memcpy(uevent.base_ptr(), uevent_content, strlen(uevent_content));
mir::AnonymousShmFile uevent{mir::strlen_c(uevent_content)};
::memcpy(uevent.base_ptr(), uevent_content, mir::strlen_c(uevent_content));

EXPECT_CALL(*fops, open(StrEq(expected_filename), FlagsSet(O_RDONLY, O_CLOEXEC)))
.WillOnce(Return(uevent.fd()));
Expand Down
7 changes: 4 additions & 3 deletions tests/unit-tests/console/test_minimal_console_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <mir_test_framework/open_wrapper.h>
#include <mir/test/doubles/mock_drm.h>
#include <mir/test/doubles/simple_device_observer.h>
#include <mir/constexpr_utils.h>

#include <mir/anonymous_shm_file.h>

Expand All @@ -42,15 +43,15 @@ std::string uevent_content_for_device(
{
std::stringstream content;

if (strncmp(device_name, "/dev/", strlen("/dev/")) != 0)
if (strncmp(device_name, "/dev/", mir::strlen_c("/dev/")) != 0)
{
throw std::logic_error{"device_name is expected to be the fully-qualified /dev/foo path"};
}

content
<< "MAJOR=" << major << "\n"
<< "MINOR=" << minor << "\n"
<< "DEVNAME=" << device_name + strlen ("/dev/") << "\n";
<< "DEVNAME=" << device_name + mir::strlen_c("/dev/") << "\n";

return content.str();
}
Expand Down Expand Up @@ -276,7 +277,7 @@ TEST_F(MinimalConsoleServicesTest, failure_to_open_sys_file_results_in_immediate
auto error_on_device_open = mtf::add_open_handler(
[](char const* path, int, std::optional<mode_t>) -> std::optional<int>
{
if (!strncmp("/sys", path, strlen("/sys")))
if (!strncmp("/sys", path, mir::strlen_c("/sys")))
{
errno = EINVAL;
return {-1};
Expand Down