Skip to content

Commit

Permalink
Minor cleanups to token authority and xdg activation code, bump version,
Browse files Browse the repository at this point in the history
update symbols
  • Loading branch information
tarek-y-ismail committed Dec 20, 2024
1 parent fbffb60 commit 6994bc1
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 115 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set(MIR_VERSION_MAJOR 2)
set(MIR_VERSION_MINOR 19)
set(MIR_VERSION_PATCH 2)
set(MIR_VERSION_MINOR 20)
set(MIR_VERSION_PATCH 0)

add_compile_definitions(MIR_VERSION_MAJOR=${MIR_VERSION_MAJOR})
add_compile_definitions(MIR_VERSION_MINOR=${MIR_VERSION_MINOR})
Expand Down
75 changes: 12 additions & 63 deletions src/include/server/mir/shell/token_authority.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
#ifndef MIR_SHELL_TOKEN_AUTHORITY_H_
#define MIR_SHELL_TOKEN_AUTHORITY_H_

#include "mir/main_loop.h"
#include "mir/time/alarm.h"

#include <chrono>
#include <cmath>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <unistd.h>
#include <unordered_set>

namespace mir
{
class MainLoop;
namespace time { class Alarm; }
namespace shell
{
class TokenAuthority
Expand All @@ -44,73 +39,27 @@ class TokenAuthority
Token(
std::string token,
std::unique_ptr<time::Alarm> alarm,
std::optional<RevocationListener> revocation_listener) :
token{token},
alarm{std::move(alarm)},
revocation_listener{revocation_listener}
{
alarm->reschedule_in(timeout_ms);
}

explicit Token(std::string token) :
token{token},
alarm{nullptr}
{
}
std::optional<RevocationListener> revocation_listener);

operator std::string() const
{
return token;
}

bool operator==(Token const& token) const
{
return this->token == token.token;
}

std::size_t hash() const
{
return std::hash<std::string>{}(token);
}
bool operator==(Token const& token) const;
operator std::string() const;
std::size_t hash() const;

private:
friend TokenAuthority;

explicit Token(std::string const&);

std::string token;
std::shared_ptr<mir::time::Alarm> const alarm;
std::optional<RevocationListener> revocation_listener;
};

TokenAuthority(std::shared_ptr<MainLoop> main_loop) :
main_loop{main_loop}
{
}
TokenAuthority(std::shared_ptr<MainLoop> main_loop);

auto issue_token(std::optional<Token::RevocationListener> revocation_listener) -> Token
{
std::scoped_lock lock{mutex};
auto issue_token(std::optional<Token::RevocationListener> revocation_listener) -> Token;

auto const generated = generate_token();
auto alarm = main_loop->create_alarm(
[this, generated]()
{
revoke_token(generated);
});

auto const [iter, _] = issued_tokens.insert(Token{generated ,std::move(alarm), revocation_listener});
return *iter;
}

void revoke_token(std::string to_remove)
{
std::scoped_lock lock{mutex};
auto const iter = issued_tokens.find(Token{to_remove});
if(iter != issued_tokens.end())
{
if(auto cb = iter->revocation_listener)
(*cb)(*iter);
issued_tokens.erase(iter);
}
}
void revoke_token(std::string to_remove);

private:
static auto generate_token() -> std::string;
Expand Down
16 changes: 0 additions & 16 deletions src/miral/launch_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,6 @@ auto execute_with_environment(std::vector<std::string> const app, Environment& a
}
} // namespace

auto miral::launch_app_env(
std::vector<std::string> const& app,
mir::optional_value<std::string> const& wayland_display,
mir::optional_value<std::string> const& x11_display,
miral::AppEnvironment const& app_env) -> pid_t
{
Environment application_environment;

for (auto const& [key, value]: app_env)
assign_or_unset(application_environment, key, value);

assign_or_unset(application_environment, "WAYLAND_DISPLAY", wayland_display); // configure Wayland socket
assign_or_unset(application_environment, "DISPLAY", x11_display); // configure X11 socket

return execute_with_environment(app, application_environment);
}

auto miral::launch_app_env(
std::vector<std::string> const& app,
Expand Down
5 changes: 0 additions & 5 deletions src/miral/launch_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ namespace miral
{
using AppEnvironment = std::map<std::string, std::optional<std::string>>;

auto launch_app_env(std::vector<std::string> const& app,
mir::optional_value<std::string> const& wayland_display,
mir::optional_value<std::string> const& x11_display,
AppEnvironment const& app_env) -> pid_t;

auto launch_app_env(std::vector<std::string> const& app,
mir::optional_value<std::string> const& wayland_display,
mir::optional_value<std::string> const& x11_display,
Expand Down
64 changes: 39 additions & 25 deletions src/server/frontend_wayland/xdg_activation_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "mir/events/keyboard_event.h"
#include "mir/optional_value.h"
#include "mir/shell/token_authority.h"
#include "xdg_activation_v1.h"
#include "mir/input/keyboard_observer.h"
Expand All @@ -35,13 +36,15 @@
#include <chrono>
#include <memory>
#include <ranges>
#include <string>
#include <vector>

namespace mf = mir::frontend;
namespace mw = mir::wayland;
namespace ms = mir::scene;
namespace msh = mir::shell;
namespace mi = mir::input;
namespace sr = std::ranges;


namespace mir
Expand All @@ -58,6 +61,11 @@ struct XdgActivationTokenData
{
}

bool operator==(std::string const& token) const
{
return this->token == token;
}

std::string const token;
std::weak_ptr<ms::Session> session;

Expand Down Expand Up @@ -128,12 +136,34 @@ mf::XdgActivationV1::~XdgActivationV1()
session_coordinator->remove_listener(session_listener);
}

auto mf::XdgActivationV1::find_token_unlocked(std::string const& token) const
{
return sr::find_if(
pending_tokens,
[&token](auto const& activation_token)
{
return *activation_token == token;
});
}

std::shared_ptr<mf::XdgActivationTokenData> mf::XdgActivationV1::get_token_data(std::string const& token)
{
std::lock_guard guard(pending_tokens_mutex);

if (auto iter = find_token_unlocked(token); iter != pending_tokens.end())
{
return *iter;
}

return nullptr;
}

std::shared_ptr<mf::XdgActivationTokenData> const& mf::XdgActivationV1::create_token(std::shared_ptr<ms::Session> const& session)
{
auto generated = token_authority->issue_token(
[this](auto const& token)
{
this->try_consume_token(token);
this->remove_token(token);
});

auto token = std::make_shared<XdgActivationTokenData>(generated, session);
Expand All @@ -145,28 +175,14 @@ std::shared_ptr<mf::XdgActivationTokenData> const& mf::XdgActivationV1::create_t
}
}

std::shared_ptr<mf::XdgActivationTokenData> mf::XdgActivationV1::try_consume_token(std::string const& token)
void mf::XdgActivationV1::remove_token(std::string const& token)
{
{
std::lock_guard guard(pending_tokens_mutex);

auto const iter = std::find_if(
pending_tokens.begin(),
pending_tokens.end(),
[&token](auto const& activation_token)
{
return activation_token->token == token;
});
std::lock_guard guard(pending_tokens_mutex);

if (iter != pending_tokens.end())
{
auto result = *iter;
pending_tokens.erase(iter);
return result;
}
if (auto iter = find_token_unlocked(token); iter != pending_tokens.end())
{
pending_tokens.erase(iter);
}

return nullptr;
}

void mf::XdgActivationV1::invalidate_all()
Expand All @@ -179,14 +195,13 @@ void mf::XdgActivationV1::invalidate_all()

void mf::XdgActivationV1::invalidate_if_not_from_session(std::shared_ptr<ms::Session> const& session)
{
namespace srv = std::ranges::views;
auto const is_invalid = [&session](auto const& activation_token)
{
return activation_token->session.expired() || activation_token->session.lock() != session;
};

std::lock_guard guard(pending_tokens_mutex);
for (auto const& expired_token : pending_tokens | srv::filter(is_invalid))
for (auto const& expired_token : pending_tokens | sr::views::filter(is_invalid))
token_authority->revoke_token(expired_token->token);
}

Expand Down Expand Up @@ -261,15 +276,14 @@ void mf::XdgActivationV1::Instance::activate(std::string const& token, struct wl
// 3. A key was pressed down between the issuing of the token and the activation
// of the surface with that token
//
auto xdg_token = xdg_activation_v1->try_consume_token(token);
auto xdg_token = xdg_activation_v1->get_token_data(token);
if (!xdg_token)
{
mir::log_error("XdgActivationV1::activate invalid token: %s", token.c_str());
return;
}

// try_consume_token removes the token from our list of tokens, need to
// inform the token authority as well.
// Calls `remove_token` since we set that up at token creation time
xdg_activation_v1->token_authority->revoke_token(token);

main_loop->enqueue(this, [
Expand Down
6 changes: 5 additions & 1 deletion src/server/frontend_wayland/xdg_activation_v1.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "mir/scene/null_session_listener.h"
#include "mir/scene/session_listener.h"
#include "mir/server.h"
#include "mir/shell/token_authority.h"
#include "xdg-activation-v1_wrapper.h"
#include "mir/observer_registrar.h"

Expand Down Expand Up @@ -71,8 +72,8 @@ class XdgActivationV1 : public wayland::XdgActivationV1::Global
std::shared_ptr<shell::TokenAuthority> token_authority);
~XdgActivationV1();

std::shared_ptr<XdgActivationTokenData> get_token_data(std::string const& token);
std::shared_ptr<XdgActivationTokenData> const& create_token(std::shared_ptr<mir::scene::Session> const& session);
std::shared_ptr<XdgActivationTokenData> try_consume_token(std::string const& token);
void invalidate_all();
void invalidate_if_not_from_session(std::shared_ptr<mir::scene::Session> const&);

Expand Down Expand Up @@ -120,6 +121,9 @@ class XdgActivationV1 : public wayland::XdgActivationV1::Global
};

void bind(wl_resource* resource) override;
auto find_token_unlocked(std::string const& token) const;
void remove_token(std::string const& token);


std::shared_ptr<mir::shell::Shell> shell;
std::shared_ptr<mir::scene::SessionCoordinator> const session_coordinator;
Expand Down
1 change: 0 additions & 1 deletion src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "mir/server.h"

#include "frontend_wayland/xdg_activation_v1.h"
#include "mir/emergency_cleanup.h"
#include "mir/fd.h"
#include "mir/frontend/connector.h"
Expand Down
Loading

0 comments on commit 6994bc1

Please sign in to comment.