-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (143 loc) · 5.24 KB
/
main.cpp
File metadata and controls
192 lines (143 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <cstring>
#include <syslog.h>
#include <pthread.h>
#include "crtc.h"
#include "hooks.h"
#include "libthinkpad.h"
#define VERSION "1.3.1"
using ThinkPad::PowerManagement::ACPI;
using ThinkPad::PowerManagement::ACPIEvent;
using ThinkPad::PowerManagement::ACPIEventHandler;
using ThinkPad::Utilities::Versioning;
using ThinkPad::Hardware::Dock;
class ACPIHandler : public ACPIEventHandler {
private:
CRTControllerManager manager;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Dock dock;
Hooks hooks;
public:
void handleEvent(ACPIEvent event);
};
void ACPIHandler::handleEvent(ACPIEvent event) {
switch(event) {
case ACPIEvent::DOCKED:
pthread_mutex_lock(&mutex);
manager.applyConfiguration(CRTControllerManager::DockState::DOCKED);
hooks.executeDockHook();
pthread_mutex_unlock(&mutex);
break;
case ACPIEvent::UNDOCKED:
pthread_mutex_lock(&mutex);
manager.applyConfiguration(CRTControllerManager::DockState::UNDOCKED);
hooks.executeUndockHook();
pthread_mutex_unlock(&mutex);
break;
case ACPIEvent::POWER_S3S4_EXIT:
if (!dock.probe()) {
syslog(LOG_INFO, "Dock is not sane, not running dynamic sleep handler\n");
return;
}
pthread_mutex_lock(&mutex);
if (dock.isDocked()) {
manager.applyConfiguration(CRTControllerManager::DockState::DOCKED);
} else {
manager.applyConfiguration(CRTControllerManager::DockState::UNDOCKED);
}
pthread_mutex_unlock(&mutex);
}
}
int startDaemon() {
openlog("dockd", LOG_NDELAY | LOG_PID, LOG_DAEMON);
ACPI acpi;
ACPIHandler handler;
acpi.addEventHandler(&handler);
acpi.start();
acpi.wait();
closelog();
return EXIT_SUCCESS;
}
int showHelp() {
printf("Usage: dockd [OPERAND] [?ARGUMENT]\n"
"\n"
" dockd --help - show this help dialog\n"
" dockd --config [docked|undocked] - write config files\n"
" dockd --set [docked|undocked] - set the saved config\n"
" dockd --daemon - start the dock daemon\n");
return EXIT_SUCCESS;
}
int writeConfig(const char *state) {
CRTControllerManager::DockState dockState = CRTControllerManager::DockState::INVALID;
if (strcmp(state, "docked") == 0) {
dockState = CRTControllerManager::DockState::DOCKED;
}
if (strcmp(state, "undocked") == 0) {
dockState = CRTControllerManager::DockState::UNDOCKED;
}
if (dockState == CRTControllerManager::DockState::INVALID) {
fprintf(stderr, "Invalid --config option: %s. See --help\n", state);
return EXIT_FAILURE;
}
CRTControllerManager manager;
if (manager.writeConfigToDisk(dockState)) {
printf("config file written to %s\n",
dockState == CRTControllerManager::DockState::DOCKED ? CONFIG_LOCATION_DOCKED : CONFIG_LOCATION_UNDOCKED);
}
return EXIT_SUCCESS;
}
int applyConfig(const char *state) {
CRTControllerManager::DockState dockState = CRTControllerManager::DockState::INVALID;
if (strcmp(state, "docked") == 0) {
dockState = CRTControllerManager::DockState::DOCKED;
}
if (strcmp(state, "undocked") == 0) {
dockState = CRTControllerManager::DockState::UNDOCKED;
}
if (dockState == CRTControllerManager::DockState::INVALID) {
fprintf(stderr, "Invalid --set option: %s. See --help\n", state);
return EXIT_FAILURE;
}
CRTControllerManager manager;
if (manager.applyConfiguration(dockState)) {
printf("config applied from %s\n",
dockState == CRTControllerManager::DockState::DOCKED ? CONFIG_LOCATION_DOCKED : CONFIG_LOCATION_UNDOCKED);
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
if (argc == 1) {
printf("dockd " VERSION " (libthinkpad %d.%d)\n"
"Copyright (C) 2017 The Thinkpads.org Team\n"
"License: FreeBSD License (BSD 2-Clause) <https://www.freebsd.org/copyright/freebsd-license.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n\n"
"Written by Ognjen Galic\n"
"See --help for more information\n", Versioning::getMajorVersion(), Versioning::getMinorVersion());
return EXIT_SUCCESS;
}
if (strcmp(argv[1], "--daemon") == 0) {
return startDaemon();
}
if (strcmp(argv[1], "--help") == 0) {
return showHelp();
}
if (strcmp(argv[1], "--config") == 0) {
if (argc < 3) {
fprintf(stderr, "--config requires an option. See --help.\n");
return EXIT_FAILURE;
}
return writeConfig(argv[2]);
}
if (strcmp(argv[1], "--set") == 0) {
if (argc < 3) {
fprintf(stderr, "--config requires an option. See --help.\n");
return EXIT_FAILURE;
}
return applyConfig(argv[2]);
}
fprintf(stderr, "Unknown option: %s. See --help\n", argv[1]);
return EXIT_FAILURE;
}