File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ option(ENABLE_SANITIZERS "Enable ASan + UBSan for debug builds" OFF)
5353option (ENABLE_CLANG_TIDY "Run clang-tidy during compilation" OFF )
5454option (INSTALL_MODULES_LOAD_CONFIG "Install config files to auto load required kernel modules" ON )
5555option (AUTO_INSTALL_BROWSER_MANIFESTS "Install per-user browser native messaging host manifests at server startup" ON )
56+ option (AUTO_ENABLE_AUTOSTART "Register the app to start at login on first launch (currently macOS only)" ON )
5657
5758set (VICINAE_BUNDLE_ID "com.vicinaehq.Vicinae" CACHE STRING "macOS app bundle identifier" )
5859
Original file line number Diff line number Diff line change 7575 "CMAKE_INSTALL_BINDIR" = "bin" ;
7676 "CMAKE_INSTALL_LIBDIR" = "lib" ;
7777 "AUTO_INSTALL_BROWSER_MANIFESTS" = "OFF" ;
78+ "AUTO_ENABLE_AUTOSTART" = "OFF" ;
7879 } ;
7980
8081 strictDeps = true ;
Original file line number Diff line number Diff line change @@ -664,6 +664,17 @@ if (APPLE)
664664 src/services/snippet/macos-snippet-server.mm
665665 src/services/update/macos-update-installer.mm
666666 PROPERTIES COMPILE_OPTIONS "-fobjc-arc" )
667+
668+ if (AUTO_ENABLE_AUTOSTART)
669+ add_compile_definitions (AUTO_ENABLE_AUTOSTART )
670+ list (APPEND SRCS
671+ src/services/autostart/macos-login-item.hpp
672+ src/services/autostart/macos-login-item.mm
673+ )
674+ set_source_files_properties (src/services/autostart/macos-login-item.mm
675+ PROPERTIES COMPILE_OPTIONS "-fobjc-arc" )
676+ list (APPEND LIBS "-framework ServiceManagement" )
677+ endif ()
667678else ()
668679 list (APPEND SRCS
669680 src/services/app-runtime/linux /linux -app-runtime.hpp
Original file line number Diff line number Diff line change 9999#include < QFileOpenEvent>
100100#endif
101101
102+ #ifdef AUTO_ENABLE_AUTOSTART
103+ #include " services/autostart/macos-login-item.hpp"
104+ #endif
105+
102106#ifdef Q_OS_MACOS
103107class UrlSchemeOpenFilter : public QObject {
104108public:
@@ -199,6 +203,10 @@ int startServer(const ServerLaunchOptions &launchOpts) {
199203
200204 Omnicast::ensureDirectories ();
201205
206+ #ifdef AUTO_ENABLE_AUTOSTART
207+ vicinae::macos::registerLoginItemOnce ();
208+ #endif
209+
202210 {
203211 auto registry = ServiceRegistry::instance ();
204212
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ namespace vicinae ::macos {
4+
5+ void registerLoginItemOnce ();
6+
7+ }
Original file line number Diff line number Diff line change 1+ #include " macos-login-item.hpp"
2+ #include " vicinae.hpp"
3+ #include < filesystem>
4+ #include < fstream>
5+ #include < qlogging.h>
6+ #import < Foundation/Foundation.h>
7+ #import < ServiceManagement/ServiceManagement.h>
8+
9+ namespace vicinae ::macos {
10+
11+ // we only set it once, otherwise we would end up overriding the user preference
12+ // every single time
13+ void registerLoginItemOnce () {
14+ if (!NSBundle .mainBundle .bundleIdentifier ) return ;
15+
16+ const auto marker = Omnicast::stateDir () / " login-item-registered" ;
17+ if (std::filesystem::exists (marker)) return ;
18+
19+ SMAppService *service = SMAppService.mainAppService ;
20+
21+ if (service.status != SMAppServiceStatusEnabled) {
22+ NSError *error = nil ;
23+ if (![service registerAndReturnError: &error]) {
24+ qWarning () << " Failed to register login item:" << error.localizedDescription .UTF8String ;
25+ return ;
26+ }
27+ }
28+
29+ std::ofstream{marker};
30+ }
31+
32+ } // namespace vicinae::macos
You can’t perform that action at this time.
0 commit comments