Skip to content

Commit

Permalink
client/mpdclient: convert callback functions to abstract handler class
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 10, 2024
1 parent 41f7ece commit 276a0aa
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 106 deletions.
3 changes: 2 additions & 1 deletion src/Instance.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Instance::Instance()
options.host.empty() ? nullptr : options.host.c_str(),
options.port,
options.timeout_ms,
options.password.empty() ? nullptr : options.password.c_str()),
options.password.empty() ? nullptr : options.password.c_str(),
*this),
seek(event_loop, client),
reconnect_timer(event_loop, BIND_THIS_METHOD(OnReconnectTimer)),
update_timer(event_loop, BIND_THIS_METHOD(OnUpdateTimer)),
Expand Down
13 changes: 11 additions & 2 deletions src/Instance.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* A singleton holding global instance variables.
*/
class Instance final : UserInputHandler {
class Instance final : MpdClientHandler, UserInputHandler {
EventLoop event_loop;

struct mpdclient client;
Expand Down Expand Up @@ -118,7 +118,16 @@ private:
void OnCheckKeyBindings() noexcept;
#endif

// virtual methods from AsyncUserInputHandler
// virtual methods from Mpdclient
void OnMpdConnected() noexcept override;
void OnMpdConnectFailed() noexcept override;
void OnMpdConnectionLost() noexcept override;
void OnMpdError(std::string_view message) noexcept override;
void OnMpdError(std::exception_ptr e) noexcept override;
bool OnMpdAuth() noexcept override;
void OnMpdIdle(unsigned events) noexcept override;

// virtual methods from UserInputHandler
bool OnRawKey(int key) noexcept override;
bool OnCommand(Command cmd) noexcept override;
#ifdef HAVE_GETMOUSE
Expand Down
36 changes: 13 additions & 23 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "config.h"
#include "Instance.hxx"
#include "ncmpc.hxx"
#include "callbacks.hxx"
#include "charset.hxx"
#include "Options.hxx"
#include "Command.hxx"
Expand Down Expand Up @@ -114,11 +113,10 @@ Instance::OnReconnectTimer() noexcept
}

void
mpdclient_connected_callback() noexcept
Instance::OnMpdConnected() noexcept
{
#ifndef NCMPC_MINI
/* quit if mpd is pre 0.14 - song id not supported by mpd */
auto &client = global_instance->GetClient();
auto *connection = client.GetConnection();
if (mpd_connection_cmp_server_version(connection, 0, 21, 0) < 0) {
const unsigned *version =
Expand All @@ -130,51 +128,43 @@ mpdclient_connected_callback() noexcept
doupdate();

/* try again after 30 seconds */
global_instance->ScheduleReconnect(std::chrono::seconds(30));
ScheduleReconnect(std::chrono::seconds{30});
return;
}
#endif

screen->status_bar.ClearMessage();
screen_manager.status_bar.ClearMessage();
doupdate();

global_instance->UpdateClient();
UpdateClient();

auto_update_timer(*global_instance);
auto_update_timer(*this);
}

void
mpdclient_failed_callback() noexcept
Instance::OnMpdConnectFailed() noexcept
{
/* try again in 5 seconds */
global_instance->ScheduleReconnect(std::chrono::seconds(5));
ScheduleReconnect(std::chrono::seconds(5));
}

void
mpdclient_lost_callback() noexcept
Instance::OnMpdConnectionLost() noexcept
{
screen->Update(global_instance->GetClient(),
global_instance->GetSeek());

global_instance->ScheduleReconnect(std::chrono::seconds(1));
screen_manager.Update(client, seek);
ScheduleReconnect(std::chrono::seconds{1});
}

/**
* This function is called by the gidle.c library when MPD sends us an
* idle event (or when the connection dies).
*/
void
mpdclient_idle_callback([[maybe_unused]] unsigned events) noexcept
Instance::OnMpdIdle([[maybe_unused]] unsigned events) noexcept
{
auto &client = global_instance->GetClient();

#ifndef NCMPC_MINI
if (options.enable_xterm_title)
update_xterm_title(client);
#endif

screen->Update(client, global_instance->GetSeek());
auto_update_timer(*global_instance);
screen_manager.Update(client, seek);
auto_update_timer(*this);
}

void
Expand Down
15 changes: 6 additions & 9 deletions src/callbacks.cxx
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "callbacks.hxx"
#include "screen.hxx"
#include "Instance.hxx"
#include "screen_status.hxx"
#include "ncmpc.hxx"
#include "ui/Bell.hxx"
#include "client/mpdclient.hxx"

#include <curses.h>

bool
mpdclient_auth_callback(struct mpdclient *c) noexcept
Instance::OnMpdAuth() noexcept
{
auto *connection = c->GetConnection();
auto *connection = client.GetConnection();
if (connection == nullptr)
return false;

if (!mpd_connection_clear_error(connection))
return false;

screen->QueryPassword(*c);
screen_manager.QueryPassword(client);
return false;
}

void
mpdclient_error_callback(std::string_view message) noexcept
Instance::OnMpdError(std::string_view message) noexcept
{
screen_status_message(message);
Bell();
doupdate();
}

void
mpdclient_error_callback(std::exception_ptr e) noexcept
Instance::OnMpdError(std::exception_ptr e) noexcept
{
screen_status_error(std::move(e));
Bell();
Expand Down
47 changes: 0 additions & 47 deletions src/callbacks.hxx

This file was deleted.

48 changes: 25 additions & 23 deletions src/client/mpdclient.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright The Music Player Daemon Project

#include "mpdclient.hxx"
#include "callbacks.hxx"
#include "config.h"
#include "gidle.hxx"
#include "charset.hxx"
Expand All @@ -24,14 +23,15 @@ mpdclient::OnEnterIdleTimer() noexcept
}

static void
mpdclient_invoke_error_callback(enum mpd_error error,
mpdclient_invoke_error_callback(MpdClientHandler &handler,
enum mpd_error error,
const char *message) noexcept
{
if (error == MPD_ERROR_SERVER)
/* server errors are UTF-8, the others are locale */
mpdclient_error_callback(Utf8ToLocale{message});
handler.OnMpdError(Utf8ToLocale{message});
else
mpdclient_error_callback(message);
handler.OnMpdError(message);
}

void
Expand All @@ -42,7 +42,7 @@ mpdclient::InvokeErrorCallback() noexcept
enum mpd_error error = mpd_connection_get_error(connection);
assert(error != MPD_ERROR_SUCCESS);

mpdclient_invoke_error_callback(error,
mpdclient_invoke_error_callback(handler, error,
mpd_connection_get_error_message(connection));
}

Expand All @@ -56,7 +56,7 @@ mpdclient::OnIdle(unsigned _events) noexcept
events |= _events;
Update();

mpdclient_idle_callback(events);
handler.OnMpdIdle(events);
events = 0;

if (source != nullptr)
Expand All @@ -72,9 +72,9 @@ mpdclient::OnIdleError(enum mpd_error error,

idle = false;

mpdclient_invoke_error_callback(error, message);
mpdclient_invoke_error_callback(handler, error, message);
Disconnect();
mpdclient_lost_callback();
handler.OnMpdConnectionLost();
}

/****************************************************************************/
Expand All @@ -89,14 +89,14 @@ mpdclient::HandleError() noexcept

if (error == MPD_ERROR_SERVER &&
mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_PERMISSION)
return mpdclient_auth_callback(this);
return handler.OnMpdAuth();

mpdclient_invoke_error_callback(error,
mpdclient_invoke_error_callback(handler, error,
mpd_connection_get_error_message(connection));

if (!mpd_connection_clear_error(connection)) {
Disconnect();
mpdclient_lost_callback();
handler.OnMpdConnectionLost();
}

return false;
Expand All @@ -108,12 +108,12 @@ mpdclient::HandleAuthError() noexcept
enum mpd_error error = mpd_connection_get_error(connection);
assert(error != MPD_ERROR_SUCCESS);

mpdclient_invoke_error_callback(error,
mpdclient_invoke_error_callback(handler, error,
mpd_connection_get_error_message(connection));

if (!mpd_connection_clear_error(connection)) {
Disconnect();
mpdclient_lost_callback();
handler.OnMpdConnectionLost();
}
}

Expand All @@ -138,8 +138,10 @@ settings_is_local_socket(const struct mpd_settings *settings) noexcept

mpdclient::mpdclient(EventLoop &event_loop,
const char *_host, unsigned _port,
unsigned _timeout_ms, const char *_password)
:timeout_ms(_timeout_ms), password(_password),
unsigned _timeout_ms, const char *_password,
MpdClientHandler &_handler)
:handler(_handler),
timeout_ms(_timeout_ms), password(_password),
enter_idle_timer(event_loop, BIND_THIS_METHOD(OnEnterIdleTimer))
{
#ifdef ENABLE_ASYNC_CONNECT
Expand Down Expand Up @@ -340,7 +342,7 @@ mpdclient::OnConnected(struct mpd_connection *_connection) noexcept
if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
InvokeErrorCallback();
Disconnect();
mpdclient_failed_callback();
handler.OnMpdConnectFailed();
return false;
}

Expand All @@ -354,7 +356,7 @@ mpdclient::OnConnected(struct mpd_connection *_connection) noexcept
!mpd_run_password(connection, password)) {
InvokeErrorCallback();
Disconnect();
mpdclient_failed_callback();
handler.OnMpdConnectFailed();
return false;
}

Expand All @@ -364,7 +366,7 @@ mpdclient::OnConnected(struct mpd_connection *_connection) noexcept

if (!mpd_connection_clear_error(connection)) {
Disconnect();
mpdclient_failed_callback();
handler.OnMpdConnectFailed();
return false;
}
}
Expand All @@ -378,7 +380,7 @@ mpdclient::OnConnected(struct mpd_connection *_connection) noexcept
established */
events = (enum mpd_idle)MPD_IDLE_ALL;

mpdclient_connected_callback();
handler.OnMpdConnected();
return true;
}

Expand All @@ -393,9 +395,9 @@ mpdclient::OnAsyncMpdConnect(struct mpd_connection *_connection) noexcept
const char *password2 =
mpd_settings_get_password(&GetSettings());
if (password2 != nullptr && !mpd_run_password(_connection, password2)) {
mpdclient_error_callback(mpd_connection_get_error_message(_connection));
handler.OnMpdError(mpd_connection_get_error_message(_connection));
mpd_connection_free(_connection);
mpdclient_failed_callback();
handler.OnMpdConnectFailed();
return;
}

Expand All @@ -416,8 +418,8 @@ mpdclient::OnAsyncMpdConnectError(std::exception_ptr e) noexcept
}
#endif

mpdclient_error_callback(std::move(e));
mpdclient_failed_callback();
handler.OnMpdError(std::move(e));
handler.OnMpdConnectFailed();
}

void
Expand Down
Loading

0 comments on commit 276a0aa

Please sign in to comment.