-
Notifications
You must be signed in to change notification settings - Fork 12
/
drm_hook.cpp
211 lines (159 loc) · 5.37 KB
/
drm_hook.cpp
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
/**************************************************************
drm_hook.cpp - Linux DRM/KMS library hook
---------------------------------------------------------
Switchres Modeline generation engine for emulation
License GPL-2.0+
Copyright 2010-2022 Chris Kennedy, Antonio Giner,
Alexandre Wodarczyk, Gil Delescluse
**************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>
#include <cstring>
// DRM headers
#include <xf86drm.h>
#include <xf86drmMode.h>
#define MAX_CONNECTORS 10
bool hook_connector(drmModeConnectorPtr conn);
drmModeConnectorPtr get_connector(uint32_t connector_id);
typedef struct connector_hook
{
drmModeConnector conn;
drmModeModeInfo modes[128];
uint32_t props[128];
uint64_t prop_values[128];
uint32_t encoders[128];
} connector_hook;
connector_hook connector[MAX_CONNECTORS];
int m_num_connectors = 0;
//============================================================
// drmModeGetConnector
//============================================================
drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
{
static void* (*my_get_connector)(int, uint32_t) = NULL;
if (!my_get_connector)
my_get_connector = (void*(*)(int, uint32_t))dlsym(RTLD_NEXT, "drmModeGetConnector");
// Allow hook detection (original func would return NULL)
if (fd == -1)
return &connector[0].conn;
drmModeConnectorPtr conn = get_connector(connector_id);
if (conn != NULL)
// already hooked
return conn;
else
{
// attempt connector hook
conn = (drmModeConnectorPtr)my_get_connector(fd, connector_id);
if (!conn) return NULL;
if (hook_connector(conn))
{
conn = get_connector(connector_id);
printf("Switchres: returning hooked connector %d\n", conn->connector_id);
}
}
return conn;
}
//============================================================
// drmModeGetConnectorCurrent
//============================================================
drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
{
static void* (*my_get_connector_current)(int, uint32_t) = NULL;
if (!my_get_connector_current)
my_get_connector_current = (void*(*)(int, uint32_t))dlsym(RTLD_NEXT, "drmModeGetConnectorCurrent");
// Allow hook detection (original func would return NULL)
if (fd == -1)
return &connector[0].conn;
drmModeConnectorPtr conn = get_connector(connector_id);
if (conn != NULL)
// already hooked
return conn;
else
{
// attempt connector hook
conn = (drmModeConnectorPtr)my_get_connector_current(fd, connector_id);
if (!conn) return NULL;
if (hook_connector(conn))
{
conn = get_connector(connector_id);
printf("Switchres: returning hooked connector %d\n", conn->connector_id);
}
}
return conn;
}
//============================================================
// drmModeFreeConnector
//============================================================
void drmModeFreeConnector(drmModeConnectorPtr ptr)
{
static void (*my_free_connector)(drmModeConnectorPtr) = NULL;
if (!my_free_connector)
my_free_connector = (void (*)(drmModeConnectorPtr)) dlsym(RTLD_NEXT, "drmModeFreeConnector");
// Skip our hooked connector
for (int i = 0; i < m_num_connectors; i++)
if (ptr == &connector[i].conn)
return;
my_free_connector(ptr);
}
//============================================================
// hook_connector
//============================================================
bool hook_connector(drmModeConnectorPtr conn)
{
if (conn == NULL)
return false;
if (m_num_connectors >= MAX_CONNECTORS)
return false;
if (conn->count_modes == 0)
return false;
connector_hook *conn_hook = &connector[m_num_connectors++];
drmModeConnectorPtr my_conn = &conn_hook->conn;
printf("Switchres: hooking connector %d\n", conn->connector_id);
*my_conn = *conn;
drmModeModeInfo *my_modes = conn_hook->modes;
memcpy(my_modes, conn->modes, sizeof(drmModeModeInfo) * conn->count_modes);
my_conn->modes = my_modes;
uint32_t *my_encoders = conn_hook->encoders;
memcpy(my_encoders, conn->encoders, sizeof(uint32_t) * conn->count_encoders);
my_conn->encoders = my_encoders;
uint32_t *my_props = conn_hook->props;
memcpy(my_props, conn->props, sizeof(uint32_t) * conn->count_props);
my_conn->props = my_props;
uint64_t *my_prop_values = conn_hook->prop_values;
memcpy(my_prop_values, conn->prop_values, sizeof(uint64_t) * conn->count_props);
my_conn->prop_values = my_prop_values;
drmModeFreeConnector(conn);
bool found = false;
drmModeModeInfo *mode = NULL;
for (int i = 0; i < my_conn->count_modes; i++)
{
mode = &my_modes[i];
if (mode->type & DRM_MODE_TYPE_PREFERRED)
{
found = true;
break;
}
}
// If preferred mode not found, default to first entry
if (!found)
mode = &my_modes[0];
// Add dummy mode to mode list (preferred mode with hdisplay +1, vfresh +1)
drmModeModeInfo *dummy_mode = &my_modes[my_conn->count_modes];
*dummy_mode = *mode;
dummy_mode->vrefresh++;
dummy_mode->hdisplay++;
dummy_mode->type |= (1<<7);
my_conn->count_modes++;
return true;
}
//============================================================
// get_connector
//============================================================
drmModeConnectorPtr get_connector(uint32_t connector_id)
{
for (int i = 0; i < m_num_connectors; i++)
if (connector[i].conn.connector_id == connector_id)
return &connector[i].conn;
return NULL;
}