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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* input-switch.c: switches display input via DDC/CI
*
* Copyright (c) 2017 - 2022, Přemysl Eric Janouch <p@janouch.name>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
// This makes openat() available even though I set _POSIX_C_SOURCE and
// _XOPEN_SOURCE to a version of POSIX older than 2008
#define _GNU_SOURCE
#include "config.h"
#undef PROGRAM_NAME
#define PROGRAM_NAME "input-switch"
#include "liberty/liberty.c"
#include "ddc-ci.c"
#include <dirent.h>
// This list is from the MCCS 2.2a specification
struct
{
int code; ///< Input code
const char *name; ///< Input name
int index; ///< Input index
}
g_inputs[] =
{
{ 0x01, "VGA", 1, }, // Analog video (R/G/B) 1
{ 0x02, "VGA", 2, }, // Analog video (R/G/B) 2
{ 0x03, "DVI", 1, }, // Digital video (TMDS) 1 DVI 1
{ 0x04, "DVI", 2, }, // Digital video (TMDS) 2 DVI 2
{ 0x05, "composite", 1, }, // Composite video 1
{ 0x06, "composite", 2, }, // Composite video 2
{ 0x07, "S-Video", 1, }, // S-video 1
{ 0x08, "S-Video", 2, }, // S-video 2
{ 0x09, "tuner", 1, }, // Tuner 1
{ 0x0A, "tuner", 2, }, // Tuner 2
{ 0x0B, "tuner", 3, }, // Tuner 3
{ 0x0C, "component", 1, }, // Component video (YPbPr/YCbCr) 1
{ 0x0D, "component", 2, }, // Component video (YPbPr/YCbCr) 2
{ 0x0E, "component", 3, }, // Component video (YPbPr/YCbCr) 3
{ 0x0F, "DP", 1, }, // DisplayPort 1
{ 0x10, "DP", 2, }, // DisplayPort 2
{ 0x11, "HDMI", 1, }, // Digital Video (TMDS) 3 HDMI 1
{ 0x12, "HDMI", 2, }, // Digital Video (TMDS) 4 HDMI 2
{ 0x15, "bnq-tb", 1, }, // Thunderbolt on BenQ PD3220U (no spec)
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
typedef bool (*ActionFunc) (int fd, int param, struct error **);
static bool
get_input_source (int fd, int input, struct error **e)
{
struct vcp_feature_readout readout = {};
if (!vcp_get_feature (fd, VCP_INPUT_SOURCE, &readout, e))
return false;
(void) input;
for (size_t i = 0; i < N_ELEMENTS (g_inputs); i++)
if (g_inputs[i].code == readout.cur)
{
printf ("input is %s %d\n", g_inputs[i].name, g_inputs[i].index);
return true;
}
printf ("input is %d\n", readout.cur);
return true;
}
static bool
set_input_source (int fd, int input, struct error **e)
{
struct vcp_feature_readout readout = {};
if (!vcp_get_feature (fd, VCP_INPUT_SOURCE, &readout, e))
return false;
if (input < 0 || input > readout.max)
return error_set (e, "input index out of range");
uint8_t set_req[] = { VCP_INPUT_SOURCE, input >> 8, input };
if (!ddc_send (fd, DDC_SET_VCP_FEATURE, set_req, sizeof set_req, e))
return false;
wait_ms (50);
printf ("input set from %d to %d of %d\n", readout.cur, input, readout.max);
return true;
}
static bool
set_bnq_kvm (int fd, int kvm, struct error **e)
{
// This function does a leap of faith, should check the actual manufacturer
enum { VCP_BNQ_KVM = 0xE4 };
struct vcp_feature_readout readout = {};
if (!vcp_get_feature (fd, VCP_BNQ_KVM, &readout, e))
return false;
if (kvm < 0 || kvm > readout.max)
return error_set (e, "KVM index out of range");
uint8_t set_req[] = { VCP_BNQ_KVM, kvm >> 8, kvm };
if (!ddc_send (fd, DDC_SET_VCP_FEATURE, set_req, sizeof set_req, e))
return false;
wait_ms (50);
printf ("KVM set from %d to %d of %d\n", readout.cur, kvm, readout.max);
return true;
}
static void
i2c (ActionFunc action, int param)
{
DIR *dev = opendir ("/dev");
if (!dev)
{
print_error ("cannot access %s: %s: %s",
"/dev", "opendir", strerror (errno));
return;
}
struct dirent *entry;
while ((entry = readdir (dev)))
{
if (strncmp (entry->d_name, "i2c-", 4))
continue;
printf ("Trying %s... ", entry->d_name);
int fd = openat (dirfd (dev), entry->d_name, O_RDONLY);
if (fd < 0)
{
print_error ("%s: %s", "openat", strerror (errno));
continue;
}
struct error *e = NULL;
if (!is_a_display (fd, &e)
|| !action (fd, param, &e))
{
printf ("%s\n", e->message);
error_free (e);
}
close (fd);
}
closedir (dev);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int
main (int argc, char *argv[])
{
g_log_message_real = log_message_custom;
if (argc <= 1)
{
printf ("Usage: %s {? | INPUT [INDEX]}\n", argv[0]);
exit (EXIT_FAILURE);
}
if (!strcmp (argv[1], "?"))
{
i2c (get_input_source, -1);
exit (EXIT_SUCCESS);
}
unsigned long input_source = 0;
if (xstrtoul (&input_source, argv[1], 10))
{
i2c (set_input_source, input_source);
exit (EXIT_SUCCESS);
}
unsigned long index = 1;
if (argc > 2 && !xstrtoul (&index, argv[2], 10))
exit_fatal ("given index is not a number: %s", argv[2]);
// Manufacturer-specific, argument currently necessary, but we could rotate
if (argc > 2 && !strcasecmp (argv[1], "bnq-kvm"))
{
i2c (set_bnq_kvm, index);
exit (EXIT_SUCCESS);
}
for (size_t i = 0; i < N_ELEMENTS (g_inputs); i++)
if (!strcasecmp_ascii (g_inputs[i].name, argv[1])
&& g_inputs[i].index == (int) index)
input_source = g_inputs[i].code;
if (!input_source)
exit_fatal ("unknown input source: %s %lu", argv[1], index);
i2c (set_input_source, input_source);
return 0;
}
|