-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockmonitortool.c
145 lines (109 loc) · 3.84 KB
/
dockmonitortool.c
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
/*
dockmonitortool.c
See README about what it does.
See LICENSE for licensing details.
Per Weijnitz <per.weijnitz@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include "config.h"
//// CONSTANTS
// The file in sysfs corresponding to the dock. It is a file
// that contains int 0 when no dock is present, and not 0 when
// a dock is present. This may vary between different hardware, so
// you need to find the file that is right for your system.
#define DOCK "/sys/devices/platform/hp-wmi/dock"
// Define the two commands which are your two perfekt display setups,
// one for when the computer is docked, and one for when it's not.
#define CMD_NODOCK { "/usr/bin/xrandr", "--output", "DP3", "--off", "--output", "DP2", "--off", "--output", "DP1", "--off", "--output", "HDMI3", "--off", "--output", "HDMI2", "--off", "--output", "HDMI1", "--off", "--output", "LVDS1", "--mode", "1366x768", "--pos", "0x0", "--rotate", "normal", "--output", "VGA1", "--off", NULL }
#define CMD_DOCK { "/usr/bin/xrandr", "--output", "DP3", "--off", "--output", "DP2", "--off", "--output", "DP1", "--mode", "1920x1200", "--pos", "0x0", "--rotate", "normal", "--output", "HDMI3", "--off", "--output", "HDMI2", "--off", "--output", "HDMI1", "--off", "--output", "LVDS1", "--off", "--output", "VGA1", "--off", NULL }
// some macros
#define LOG(...) do{ fprintf(stderr, __VA_ARGS__); } while(0)
#define DIE(...) do{ fprintf(stderr, __VA_ARGS__); \
exit(EXIT_FAILURE); } while(0)
#define DIE_PERROR(...) do{ fprintf(stderr, __VA_ARGS__); perror(0); \
exit(EXIT_FAILURE); } while(0)
// prototypes
int read_int (const char* file_name);
void execcmd(const char* cmd[]);
/*
* Function: read_int
* -----------------------
* Reads a single integer from the given file.
*
* file_name: name of the file to read from.
*
* returns: an integer.
*
* todo: switch to strtol, add error checks
*/
int read_int (const char* file_name) {
FILE* file = fopen (file_name, "r");
int i = 0;
fscanf (file, "%d", &i);
fclose (file);
return i;
}
/*
* Function: execcmd
* -----------------------
* Execute a command, and block while it is executing.
*
* cmd: the command to execute, including its arguments.
*
*/
void execcmd(const char* cmd[]) {
pid_t pid = -1;
pid = fork();
if (pid == -1) {
perror(0);
_exit(EXIT_FAILURE);
} else if (pid > 0) {
// parent
int status;
waitpid(pid, &status, 0);
} else {
// child
setsid();
execv(cmd[0], (char * const*)cmd);
perror("execv");
_exit(EXIT_FAILURE); // exec never returns
}
}
int main(int argc, char *argv[]) {
int dockstatus = -1;
int nextval = -1;
const char *cmd_dock[] = CMD_DOCK;
const char *cmd_nodock[] = CMD_NODOCK;
while(1) {
nextval = read_int(DOCK);
if(nextval != dockstatus) {
dockstatus = nextval;
LOG("DOCK IS %d\n", dockstatus);
sleep(1);
if(dockstatus == 0) {
execcmd(cmd_nodock);
} else {
execcmd(cmd_dock);
}
}
sleep(2);
}
return 0;
}
/* eof */