-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.php
136 lines (117 loc) · 4.03 KB
/
switch.php
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
#!/opt/homebrew/bin/php
<?php
/**************************************************************************************************
*
* Switch Monitors (mac)
*
* The script takes for granted there are 2 monitors connected to the mac. Then, it switches the
* position of the monitors.
*
* SatelliteWP
* 2024.09
*
* Tested with:
* PHP 8.3.12
* Displayplacer 1.4.0
* macOS 14.5 (Sonoma)
*
*************************************************************************************************/
class SwitchDisplay {
/**
* Start the script
*/
public function start() {
$displays = $this->getDisplays();
if (count($displays) == 2) {
$this->switch($displays);
}
}
/**
* Switch the position of the monitors by sending the command to displayplacer
* @param array $displays Monitors
*
* @return array
*/
public function switch($displays) {
$display1 = $displays[0];
$display2 = $displays[1];
$command = "/opt/homebrew/bin/displayplacer " . $this->getDisplayPlacerCommand($display2, $display1) . " " . $this->getDisplayPlacerCommand($display1, $display2) . "\n";
$output = null;
$result_code = null;
exec($command, $output, $result_code);
return [
'output' => $output,
'result_code' => $result_code
];
}
/**
* Get the displayplacer command to place a monitor
*
* @param array $display Monitor
* @param array $otherDisplay Other monitor
*
* @return string
*/
public function getDisplayPlacerCommand($display, $otherDisplay) {
$cmd = '"id:%s res:%s hz:%s color_depth:%s enabled:%s scaling:%s origin:%s degree:%s"';
$result = sprintf($cmd, $display['Persistent screen id'], $display['Resolution'], $display['Hertz'], $display['Color Depth'], $display['Enabled'], $display['Scaling'], $otherDisplay['Origin'], $display['Rotation']);
return $result;
}
/**
* Get the list of monitors
*
* @return array
*/
public function getDisplays() {
$output = [];
exec('/opt/homebrew/bin/displayplacer list', $output);
$monitors = [];
$monitor = [];
$key = null;
$value = null;
$previousKey = null;
foreach($output as $line) {
// Empty line means a new monitor
if ($line == '') {
$monitors[] = $monitor;
$monitor = [];
}
// When this text is found, we are at the end of the list
elseif (strpos($line, 'Execute the command') === 0) {
break;
}
else {
$parts = explode(': ', $line);
// If the line contains a colon, it's a key-value pair
if (strpos($line, ':' ) !== false) {
$key = $parts[0];
if (count($parts) == 2) {
$value = $parts[1];
$value_parts = explode(' - ', $value);
if (count($value_parts) > 1) {
$value = $value_parts[0];
}
// If the line starts with two spaces, it's a subkey
if (substr($line, 0, 2) == ' ') {
$subkey = trim($key, " ");
$monitor[$previousKey][$subkey] = $value;
}
// Otherwise, normal key-value pair
else {
$monitor[$key] = $value;
$previousKey = null;
}
}
// If there is only one colon, it's a key to an array
else {
$previousKey = trim($key, " :");
$monitor[$previousKey] = [];
}
}
}
}
return $monitors;
}
}
$sd = new SwitchDisplay();
$sd->start();