Skip to content

Commit

Permalink
Introduce FCITX_ADDON_FACTORY_V2 and FCITX_IMPORT_ADDON_FACTORY (#1185)
Browse files Browse the repository at this point in the history
FCITX_ADDON_FACTORY_V2 will generate an entry based on addon name, so it
will be unique and can be referenced more easily with static addon
loader.
  • Loading branch information
wengxt authored Nov 18, 2024
1 parent 5e48134 commit a8ac930
Show file tree
Hide file tree
Showing 53 changed files with 170 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/frontend/dbusfrontend/dbusfrontend.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Category=Frontend
Version=@PROJECT_VERSION@

[Addon/Dependencies]
0=dbus

0=core:@PROJECT_VERSION@
1=dbus
2 changes: 1 addition & 1 deletion src/frontend/dbusfrontend/dbusfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,4 +610,4 @@ class DBusFrontendModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::DBusFrontendModuleFactory);
FCITX_ADDON_FACTORY_V2(dbusfrontend, fcitx::DBusFrontendModuleFactory);
3 changes: 2 additions & 1 deletion src/frontend/fcitx4frontend/fcitx4frontend.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Category=Frontend
Version=@PROJECT_VERSION@

[Addon/Dependencies]
0=dbus
0=core:@PROJECT_VERSION@
1=dbus

[Addon/OptionalDependencies]
0=xcb
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/fcitx4frontend/fcitx4frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,4 @@ class Fcitx4FrontendModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::Fcitx4FrontendModuleFactory);
FCITX_ADDON_FACTORY_V2(fcitx4frontend, fcitx::Fcitx4FrontendModuleFactory);
3 changes: 2 additions & 1 deletion src/frontend/ibusfrontend/ibusfrontend.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Category=Frontend
Version=@PROJECT_VERSION@

[Addon/Dependencies]
0=dbus
0=core:@PROJECT_VERSION@
1=dbus

2 changes: 1 addition & 1 deletion src/frontend/ibusfrontend/ibusfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,4 +1010,4 @@ class IBusFrontendModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::IBusFrontendModuleFactory);
FCITX_ADDON_FACTORY_V2(ibusfrontend, fcitx::IBusFrontendModuleFactory);
3 changes: 2 additions & 1 deletion src/frontend/waylandim/waylandim.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Version=@PROJECT_VERSION@
Configurable=True

[Addon/Dependencies]
0=wayland:@PROJECT_VERSION@
0=core:@PROJECT_VERSION@
1=wayland:@PROJECT_VERSION@

2 changes: 1 addition & 1 deletion src/frontend/waylandim/waylandim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ class WaylandIMModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::WaylandIMModuleFactory);
FCITX_ADDON_FACTORY_V2(waylandim, fcitx::WaylandIMModuleFactory);
3 changes: 2 additions & 1 deletion src/frontend/xim/xim.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Version=@PROJECT_VERSION@
Configurable=True

[Addon/Dependencies]
0=xcb
0=core:@PROJECT_VERSION@
1=xcb

# This intends to load xim after dbus & ibusfrontend, so xim is released before dbus.
# This helps new fcitx server to become xim server properly when replacing.
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/xim/xim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,4 @@ class XIMModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::XIMModuleFactory);
FCITX_ADDON_FACTORY_V2(xim, fcitx::XIMModuleFactory);
1 change: 0 additions & 1 deletion src/im/keyboard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ endif()
if (TARGET Fcitx5::Module::Emoji)
target_link_libraries(keyboard Fcitx5::Module::Emoji)
endif()
target_include_directories(keyboard PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")
configure_file(keyboard.conf.in.in keyboard.conf.in @ONLY)
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/keyboard.conf.in keyboard.conf)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/keyboard.conf" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/addon"
Expand Down
3 changes: 3 additions & 0 deletions src/im/keyboard/keyboard.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Category=InputMethod
Version=@PROJECT_VERSION@
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@

[Addon/OptionalDependencies]
0=xcb
1=spell
Expand Down
3 changes: 3 additions & 0 deletions src/im/keyboard/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "fcitx-utils/stringutils.h"
#include "fcitx-utils/textformatflags.h"
#include "fcitx-utils/utf8.h"
#include "fcitx/addoninstance.h"
#include "fcitx/candidatelist.h"
#include "fcitx/event.h"
#include "fcitx/inputcontext.h"
Expand Down Expand Up @@ -940,3 +941,5 @@ bool KeyboardEngineState::handleBackspace(const InputMethodEntry &entry) {
}

} // namespace fcitx

FCITX_ADDON_FACTORY_V2(keyboard, fcitx::KeyboardEngineFactory);
27 changes: 27 additions & 0 deletions src/lib/fcitx/addoninstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,33 @@ class FCITXCORE_EXPORT AddonInstance {
} \
}

#define FCITX_ADDON_FACTORY_V2(AddonName, ClassName) \
extern "C" { \
FCITXCORE_EXPORT \
::fcitx::AddonFactory *fcitx_addon_factory_instance_##AddonName() { \
static ClassName factory; \
return &factory; \
} \
}

#define FCITX_ADDON_FACTORY_V2_BACKWARDS(AddonName, ClassName) \
FCITX_ADDON_FACTORY_V2(AddonName, ClassName) \
FCITX_ADDON_FACTORY(ClassName)

#define FCITX_IMPORT_ADDON_FACTORY(StaticRegistry, AddonName) \
extern "C" { \
::fcitx::AddonFactory *fcitx_addon_factory_instance_##AddonName(); \
} \
class StaticAddonRegistrar_##AddonName { \
public: \
StaticAddonRegistrar_##AddonName() { \
(StaticRegistry) \
.emplace(FCITX_STRINGIFY(AddonName), \
fcitx_addon_factory_instance_##AddonName()); \
} \
}; \
StaticAddonRegistrar_##AddonName staticAddonRegistrar_##AddonName

/// A convenient macro to obtain the addon pointer of another addon.
#define FCITX_ADDON_DEPENDENCY_LOADER(NAME, ADDONMANAGER) \
auto NAME() { \
Expand Down
16 changes: 13 additions & 3 deletions src/lib/fcitx/addonloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
*
*/

#include "fcitx/addonloader.h"
#include <exception>
#include <memory>
#include <string>
#include <utility>
#include "fcitx-utils/flags.h"
#include "fcitx-utils/library.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/standardpath.h"
#include "fcitx-utils/stringutils.h"
#include "fcitx/addoninfo.h"
#include "fcitx/addoninstance.h"
#include "addonloader_p.h"
#include "config.h"

Expand Down Expand Up @@ -41,9 +51,9 @@ AddonInstance *SharedLibraryLoader::load(const AddonInfo &info,
continue;
}
try {
registry_.emplace(
info.uniqueName(),
std::make_unique<SharedLibraryFactory>(std::move(lib)));
registry_.emplace(info.uniqueName(),
std::make_unique<SharedLibraryFactory>(
info, std::move(lib)));
} catch (const std::exception &e) {
FCITX_ERROR() << "Failed to initialize addon factory for addon "
<< info.uniqueName() << ". Error: " << e.what();
Expand Down
8 changes: 4 additions & 4 deletions src/lib/fcitx/addonloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#ifndef _FCITX_ADDONRESOLVER_H_
#define _FCITX_ADDONRESOLVER_H_
#ifndef _FCITX_ADDONLOADER_H_
#define _FCITX_ADDONLOADER_H_

#include <string>
#include <unordered_map>
Expand All @@ -18,7 +18,7 @@ namespace fcitx {
class AddonFactory;
class AddonManager;

typedef std::unordered_map<std::string, AddonFactory *> StaticAddonRegistry;
using StaticAddonRegistry = std::unordered_map<std::string, AddonFactory *>;

class FCITXCORE_EXPORT AddonLoader {
public:
Expand All @@ -29,4 +29,4 @@ class FCITXCORE_EXPORT AddonLoader {
};
} // namespace fcitx

#endif // _FCITX_ADDONRESOLVER_H_
#endif // _FCITX_ADDONLOADER_H_
20 changes: 18 additions & 2 deletions src/lib/fcitx/addonloader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,36 @@
#ifndef _FCITX_ADDONLOADER_P_H_
#define _FCITX_ADDONLOADER_P_H_

#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include "fcitx-utils/library.h"
#include "fcitx-utils/standardpath.h"
#include "fcitx-utils/stringutils.h"
#include "addonfactory.h"
#include "addoninfo.h"
#include "addoninstance.h"
#include "addonloader.h"

namespace fcitx {

namespace {
constexpr char FCITX_ADDON_FACTORY_ENTRY[] = "fcitx_addon_factory_instance";
}

class SharedLibraryFactory {
public:
SharedLibraryFactory(Library lib) : library_(std::move(lib)) {
auto *funcPtr = library_.resolve("fcitx_addon_factory_instance");
SharedLibraryFactory(const AddonInfo &info, Library lib)
: library_(std::move(lib)) {
std::string v2Name = stringutils::concat(FCITX_ADDON_FACTORY_ENTRY, "_",
info.uniqueName());
auto *funcPtr = library_.resolve(v2Name.data());
if (!funcPtr) {
funcPtr = library_.resolve(FCITX_ADDON_FACTORY_ENTRY);
}
if (!funcPtr) {
throw std::runtime_error(library_.error());
}
Expand Down
5 changes: 3 additions & 2 deletions src/modules/clipboard/clipboard.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ OnDemand=False
Configurable=True

[Addon/OptionalDependencies]
0=xcb:@PROJECT_VERSION@
1=wayland:@PROJECT_VERSION@
0=core:@PROJECT_VERSION@
1=xcb:@PROJECT_VERSION@
2=wayland:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/clipboard/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,4 @@ class ClipboardModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::ClipboardModuleFactory);
FCITX_ADDON_FACTORY_V2(clipboard, fcitx::ClipboardModuleFactory);
3 changes: 2 additions & 1 deletion src/modules/dbus/dbus.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Category=Module
Version=@PROJECT_VERSION@

[Addon/Dependencies]
0=keyboard
0=core:@PROJECT_VERSION@
1=keyboard

[Addon/OptionalDependencies]
0=xcb
Expand Down
2 changes: 1 addition & 1 deletion src/modules/dbus/dbusmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,4 @@ class DBusModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::DBusModuleFactory)
FCITX_ADDON_FACTORY_V2(dbus, fcitx::DBusModuleFactory)
3 changes: 3 additions & 0 deletions src/modules/emoji/emoji.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Library=libemoji
Category=Module
Version=@PROJECT_VERSION@
OnDemand=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/emoji/emoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ class EmojiModuleFactory : public AddonFactory {

} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::EmojiModuleFactory);
FCITX_ADDON_FACTORY_V2(emoji, fcitx::EmojiModuleFactory);
3 changes: 3 additions & 0 deletions src/modules/imselector/imselector.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Library=libimselector
Type=SharedLibrary
OnDemand=False
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/imselector/imselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,4 @@ class IMSelectorFactory : public AddonFactory {

} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::IMSelectorFactory);
FCITX_ADDON_FACTORY_V2(imselector, fcitx::IMSelectorFactory);
4 changes: 2 additions & 2 deletions src/modules/notificationitem/notificationitem.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Version=@PROJECT_VERSION@
OnDemand=True

[Addon/Dependencies]
0=dbus

0=core:@PROJECT_VERSION@
1=dbus
3 changes: 2 additions & 1 deletion src/modules/notificationitem/notificationitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "fcitx-utils/endian_p.h"
#include "fcitx-utils/i18n.h"
#include "fcitx/addonfactory.h"
#include "fcitx/addoninstance.h"
#include "fcitx/addonmanager.h"
#include "fcitx/misc_p.h"
#include "classicui_public.h"
Expand Down Expand Up @@ -423,4 +424,4 @@ class NotificationItemFactory : public AddonFactory {

} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::NotificationItemFactory)
FCITX_ADDON_FACTORY_V2(notificationitem, fcitx::NotificationItemFactory)
3 changes: 2 additions & 1 deletion src/modules/notifications/notifications.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ OnDemand=True
Configurable=True

[Addon/Dependencies]
0=dbus
0=core:@PROJECT_VERSION@
1=dbus
2 changes: 1 addition & 1 deletion src/modules/notifications/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ class NotificationsModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::NotificationsModuleFactory)
FCITX_ADDON_FACTORY_V2(notifications, fcitx::NotificationsModuleFactory)
2 changes: 1 addition & 1 deletion src/modules/quickphrase/quickphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,4 @@ class QuickPhraseModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::QuickPhraseModuleFactory)
FCITX_ADDON_FACTORY_V2(quickphrase, fcitx::QuickPhraseModuleFactory)
3 changes: 3 additions & 0 deletions src/modules/spell/spell.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Category=Module
Version=@PROJECT_VERSION@
OnDemand=True
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/spell/spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ Spell::hintForDisplay(const std::string &language, SpellProvider provider,
}
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::SpellModuleFactory)
FCITX_ADDON_FACTORY_V2(spell, fcitx::SpellModuleFactory)
3 changes: 3 additions & 0 deletions src/modules/unicode/unicode.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ Version=@PROJECT_VERSION@
OnDemand=False
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@

[Addon/OptionalDependencies]
0=clipboard
2 changes: 1 addition & 1 deletion src/modules/unicode/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,4 @@ class UnicodeModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::UnicodeModuleFactory);
FCITX_ADDON_FACTORY_V2(unicode, fcitx::UnicodeModuleFactory);
3 changes: 3 additions & 0 deletions src/modules/wayland/wayland.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Library=libwayland
Category=Module
Version=@PROJECT_VERSION@
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/wayland/waylandmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,4 @@ class WaylandModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::WaylandModuleFactory);
FCITX_ADDON_FACTORY_V2(wayland, fcitx::WaylandModuleFactory);
3 changes: 3 additions & 0 deletions src/modules/xcb/xcb.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Library=libxcb
Category=Module
Version=@PROJECT_VERSION@
Configurable=True

[Addon/Dependencies]
0=core:@PROJECT_VERSION@
2 changes: 1 addition & 1 deletion src/modules/xcb/xcbmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ class XCBModuleFactory : public AddonFactory {
};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::XCBModuleFactory);
FCITX_ADDON_FACTORY_V2(xcb, fcitx::XCBModuleFactory);
Loading

0 comments on commit a8ac930

Please sign in to comment.