Skip to content

Commit 9878d64

Browse files
authored
feat: macOS auto start (#1604)
* feat: macOS auto start * refactor: reword cmake auto start option name * fix: typo
1 parent b0b907e commit 9878d64

6 files changed

Lines changed: 60 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ option(ENABLE_SANITIZERS "Enable ASan + UBSan for debug builds" OFF)
5353
option(ENABLE_CLANG_TIDY "Run clang-tidy during compilation" OFF)
5454
option(INSTALL_MODULES_LOAD_CONFIG "Install config files to auto load required kernel modules" ON)
5555
option(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

5758
set(VICINAE_BUNDLE_ID "com.vicinaehq.Vicinae" CACHE STRING "macOS app bundle identifier")
5859

nix/vicinae.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ in
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;

src/server/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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()
667678
else()
668679
list(APPEND SRCS
669680
src/services/app-runtime/linux/linux-app-runtime.hpp

src/server/src/server.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
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
103107
class UrlSchemeOpenFilter : public QObject {
104108
public:
@@ -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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace vicinae::macos {
4+
5+
void registerLoginItemOnce();
6+
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

0 commit comments

Comments
 (0)