-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodes.c
More file actions
270 lines (223 loc) · 6.09 KB
/
Copy pathmodes.c
File metadata and controls
270 lines (223 loc) · 6.09 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Automatic Framerate Daemon for AMLogic S905/S912-based boxes.
* Copyright (C) 2017-2019 Andrey Zabolotnyi <zapparello@ya.ru>
*
* For copying conditions, see file COPYING.txt.
*
* Video mode handling & switching
*/
#include "afrd.h"
#include "colorspace.h"
#include <unistd.h>
display_mode_t *g_modes = NULL;
int g_modes_n = 0;
display_mode_t g_current_mode;
bool g_blackened = false;
static bool mode_parse (char *desc, display_mode_t *mode)
{
memset (mode, 0, sizeof (display_mode_t));
if (!desc)
return false;
strncpy (mode->name, desc, sizeof (mode->name));
if (strncmp (desc, "smpte", 5) == 0) {
mode->width = 4096;
mode->height = 2160;
desc += 5;
} else {
int v = parse_int (&desc);
char c = *desc;
if (c == 'x') {
mode->width = v;
mode->height = parse_int (&desc);
} else {
switch ((mode->height = v))
{
case 480: mode->width = 640; break;
case 576: mode->width = 720; break;
case 720: mode->width = 1280; break;
case 1080: mode->width = 1920; break;
case 2160: mode->width = 3840; break;
default: mode->name [0] = 0; return false;
}
}
c = *desc++;
// according to kernel sources, 'fp' means same as 'p'
if (c == 'f')
c = *desc++;
if (c == 'i')
mode->interlaced = true;
else if (c == 'p')
mode->interlaced = false;
else {
mode->name [0] = 0;
return false;
}
}
mode->framerate = parse_int (&desc);
// here follows 'hz' optionally followed by color space like '420'.
// we ignore them.
return true;
}
void display_mode_add (display_mode_t *mode)
{
/* keep only non-fractional modes in list */
display_mode_t new_mode = *mode;
new_mode.fractional = false;
/* check if mode is already in list */
for (int i = 0; i < g_modes_n; i++)
if (display_mode_equal (&new_mode, &g_modes [i]))
return;
g_modes_n++;
g_modes = (display_mode_t *)realloc (g_modes, sizeof (display_mode_t) * g_modes_n);
g_modes [g_modes_n - 1] = new_mode;
trace (2, "\t+ "DISPMODE_FMT"\n", DISPMODE_ARGS (new_mode, display_mode_hz (&new_mode)));
}
int display_modes_init ()
{
display_modes_fini ();
char *modes = sysfs_get_str (g_hdmi_dev, "disp_cap");
if (!modes)
return -1;
trace (2, "Parsing supported video modes\n");
// parse the list of video modes supported by display
char *cur = modes;
while (cur && *cur) {
cur += strspn (cur, spaces);
int mode_len = strcspn (cur, spaces);
if (!mode_len)
break;
if (cur [mode_len - 1] == '*')
mode_len--;
cur [mode_len] = 0;
display_mode_t mode;
if (mode_parse (cur, &mode))
display_mode_add (&mode);
else
trace (2, "\t%s: unrecognized mode\n", cur);
cur += mode_len + 1;
}
free (modes);
display_mode_get_current ();
// on some weird configs current video mode may not be listed in disp_cap
if (g_current_mode.name [0])
display_mode_add (&g_current_mode);
// add extra user-specified modes from config
strlist_t xmodes;
if (strlist_load (&xmodes, "mode.extra", "extra video modes")) {
for (int i = 0; i < xmodes.size; i++) {
display_mode_t mode;
if (mode_parse (xmodes.data [i], &mode))
display_mode_add (&mode);
}
strlist_free (&xmodes);
}
return 0;
}
void display_mode_get_current ()
{
// parse the current video mode
char *mode = sysfs_get_str (g_mode_path, NULL);
if (!mode || !strcmp (mode, "null")) {
trace (1, "Current video mode is null!\n");
return;
}
if (!mode_parse (mode, &g_current_mode)) {
trace (1, "Failed to recognize current video mode '%s'\n", mode);
free (mode);
return;
}
g_current_mode.fractional = false;
char *frac_rate = sysfs_get_str (g_hdmi_dev, "frac_rate_policy");
if (!frac_rate)
trace (1, "failed to read frac_rate_policy!\n");
else
{
g_current_mode.fractional = strtol (frac_rate, NULL, 10) != 0;
free (frac_rate);
}
}
void display_modes_fini ()
{
free (g_modes);
g_modes = NULL;
g_modes_n = 0;
}
bool display_mode_equal (display_mode_t *mode1, display_mode_t *mode2)
{
if ((mode1->width != mode2->width) ||
(mode1->height != mode2->height) ||
(mode1->interlaced != mode2->interlaced))
return false;
int hz1 = display_mode_hz (mode1);
int hz2 = display_mode_hz (mode2);
return (hz1 == hz2);
}
int display_mode_hz (display_mode_t *mode)
{
if (mode->fractional)
switch (mode->framerate) {
case 24: return ( 2997 * 256 + 62) / 125;
case 30: return ( 2997 * 256 + 50) / 100;
case 60: return ( 5994 * 256 + 50) / 100;
case 120: return (11988 * 256 + 50) / 100;
case 240: return (23976 * 256 + 50) / 100;
}
return mode->framerate * 256;
}
void display_mode_set_hz (display_mode_t *mode, int hz)
{
mode->fractional = true;
int hz_frac = display_mode_hz (mode);
int hz_int = mode->framerate * 256;
if (hz_frac == hz_int) {
// this mode has no fractional variant
mode->fractional = false;
return;
}
// find multiple of hz closest to hz_int
int hz_n = 1;
int best_hz = hz;
int best_diff = abs (hz - hz_int);
for (;;) {
hz_n++;
int multiple_hz = hz * hz_n;
int multiple_diff = abs (multiple_hz - hz_int);
if (multiple_diff > best_diff)
break;
best_hz = multiple_hz;
best_diff = multiple_diff;
}
if (abs (hz_int - best_hz) < abs (hz_frac - best_hz))
mode->fractional = false;
}
void display_mode_switch (display_mode_t *mode, bool force)
{
if (!g_blackened && !force &&
display_mode_equal (mode, &g_current_mode)) {
trace (1, "Display mode is already "DISPMODE_FMT"\n",
DISPMODE_ARGS (*mode, display_mode_hz (mode)));
return;
}
char frac [2] = { mode->fractional ? '1' : '0', 0 };
sysfs_set_str (g_hdmi_dev, "frac_rate_policy", frac);
colorspace_apply (mode->name);
// fractional mode transition via special null mode
if (force ||
((strcmp (mode->name, g_current_mode.name) == 0) &&
(mode->fractional != g_current_mode.fractional)))
display_mode_null ();
trace (1, "Switching display mode to "DISPMODE_FMT"\n",
DISPMODE_ARGS (*mode, display_mode_hz (mode)));
sysfs_write (g_mode_path, mode->name);
g_current_mode = *mode;
g_blackened = false;
hdcp_restore (false);
}
void display_mode_null ()
{
if (g_blackened)
return;
trace (2, "Blackout screen\n");
sysfs_write (g_mode_path, "null");
g_blackened = true;
}