aboutsummaryrefslogtreecommitdiff
path: root/hardinfo2/x_util.c
blob: 2a7febb469ef11f463dc270c57424ba8dea3e563 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2017 L. A. F. Pereira <l@tia.mat.br>
 *    This file
 *    Copyright (C) 2018 Burt P. <pburt0@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, version 2 or later.
 *
 *    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, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "hardinfo.h"
#include "x_util.h"
#include <X11/Xlib.h>

/* wayland stuff lives here for now */

wl_info *get_walyand_info() {
    wl_info *s = malloc(sizeof(wl_info));
    memset(s, 0, sizeof(wl_info));
    s->xdg_session_type = g_strdup( getenv("XDG_SESSION_TYPE") );
    if (s->xdg_session_type == NULL)
        s->xdg_session_type = strdup("x11");
    s->display_name = g_strdup( getenv("WAYLAND_DISPLAY") );
    return s;
}

void wl_free(wl_info *s) {
    if (s) {
        free(s->xdg_session_type);
        free(s->display_name);
        free(s);
    }
}

/* get x information from xrandr, xdpyinfo, and glxinfo */

static char *simple_line_value(char *line, const char *prefix) {
    if (g_str_has_prefix(g_strstrip(line), prefix)) {
        line += strlen(prefix) + 1;
        return g_strstrip(line);
    } else
        return NULL;
}

gboolean fill_glx_info(glx_info *glx) {
    gboolean spawned;
    gchar *out, *err, *p, *l, *next_nl;
    gchar *glx_cmd = g_strdup("glxinfo");

#define GLX_MATCH_LINE(prefix_str, struct_member) \
    if (l = simple_line_value(p, prefix_str)) { glx->struct_member = g_strdup(l); goto glx_next_line; }

    spawned = hardinfo_spawn_command_line_sync(glx_cmd,
            &out, &err, NULL, NULL);
    g_free(glx_cmd);
    if (spawned) {
        p = out;
        while(next_nl = strchr(p, '\n')) {
            strend(p, '\n');
            g_strstrip(p);
            GLX_MATCH_LINE("GLX version", glx_version);
            GLX_MATCH_LINE("OpenGL vendor string", ogl_vendor);
            GLX_MATCH_LINE("OpenGL renderer string", ogl_renderer);
            GLX_MATCH_LINE("OpenGL renderer string", ogl_renderer);
            GLX_MATCH_LINE("OpenGL core profile version string", ogl_core_version);
            GLX_MATCH_LINE("OpenGL core profile shading language version string", ogl_core_sl_version);
            GLX_MATCH_LINE("OpenGL version string", ogl_version);
            GLX_MATCH_LINE("OpenGL shading language version string", ogl_sl_version);
            GLX_MATCH_LINE("OpenGL ES profile version string", ogles_version);
            GLX_MATCH_LINE("OpenGL ES profile shading language version string", ogles_sl_version);

            if (l = simple_line_value(p, "direct rendering")) {
                if (strstr(p, "Yes"))
                    glx->direct_rendering = 1;
                goto glx_next_line;
            }

            glx_next_line:
                p = next_nl + 1;
        }
        g_free(out);
        g_free(err);
        return TRUE;
    }
    return FALSE;
}

glx_info *glx_create() {
    glx_info *s = malloc(sizeof(glx_info));
    memset(s, 0, sizeof(glx_info));
    return s;
}

void glx_free(glx_info *s) {
    if (s) {
        free(s->glx_version);
        free(s->ogl_vendor);
        free(s->ogl_renderer);
        free(s->ogl_core_version);
        free(s->ogl_core_sl_version);
        free(s->ogl_version);
        free(s->ogl_sl_version);
        free(s->ogles_version);
        free(s->ogles_sl_version);
        free(s);
    }
}

gboolean fill_xinfo(xinfo *xi) {
    gboolean spawned;
    gchar *out, *err, *p, *l, *next_nl;
    gchar *xi_cmd = g_strdup("xdpyinfo");

#define XI_MATCH_LINE(prefix_str, struct_member) \
    if (l = simple_line_value(p, prefix_str)) { xi->struct_member = g_strdup(l); goto xi_next_line; }

    spawned = hardinfo_spawn_command_line_sync(xi_cmd,
            &out, &err, NULL, NULL);
    g_free(xi_cmd);
    if (spawned) {
        p = out;
        while(next_nl = strchr(p, '\n')) {
            strend(p, '\n');
            g_strstrip(p);
            XI_MATCH_LINE("name of display", display_name);
            XI_MATCH_LINE("vendor string", vendor);
            XI_MATCH_LINE("vendor release number", release_number);
            XI_MATCH_LINE("XFree86 version", version);
            XI_MATCH_LINE("X.Org version", version);

            xi_next_line:
                p = next_nl + 1;
        }
        g_free(out);
        g_free(err);
        return TRUE;
    }
    return FALSE;
}

gboolean fill_xrr_info(xrr_info *xrr) {
    gboolean spawned;
    gchar *out, *err, *p, *next_nl;
    gchar *xrr_cmd = g_strdup("xrandr --prop");
    int ec;

    x_screen ts;
    x_output to;
    char output_id[128];
    char status[128];
    char alist[512];

    memset(&ts, 0, sizeof(x_screen));
    memset(&to, 0, sizeof(x_output));
    memset(output_id, 0, sizeof(output_id));
    memset(status, 0, sizeof(status));
    memset(alist, 0, sizeof(alist));

    spawned = hardinfo_spawn_command_line_sync(xrr_cmd,
            &out, &err, NULL, NULL);
    g_free(xrr_cmd);
    if (spawned) {
        p = out;
        while(next_nl = strchr(p, '\n')) {
            strend(p, '\n');
            g_strstrip(p);

            ec = sscanf(p, "Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d",
                &ts.number,
                &ts.min_px_width, &ts.min_px_height,
                &ts.px_width, &ts.px_height,
                &ts.max_px_width, &ts.max_px_height);
            if (ec == 7) {
                /* new screen */
                xrr->screen_count++;
                if (xrr->screens == NULL)
                    xrr->screens = malloc(xrr->screen_count * sizeof(x_screen));
                else
                    xrr->screens = realloc(xrr->screens, xrr->screen_count * sizeof(x_screen));
                memcpy(&xrr->screens[xrr->screen_count-1], &ts, sizeof(x_screen));
                memset(&ts, 0, sizeof(x_screen)); /* clear the temp */
                goto xrr_next_line;
            }

            /* looking for:
             * <output_id> (connected|disconnected|unknown connection) (primary|?) <%dx%d+%d+%d> (attribute_list) mm x mm
             */
            ec = sscanf(p, "%127s %127[^(](%511[^)]", output_id, status, alist);
            if (ec == 3) {
                int is_output = 0, found_rect = 0, n = 0;
                gchar **ot = g_strsplit(status, " ", 0);

                /* find rect */
                while (ot[n] != NULL) {
                    ec = sscanf(ot[n], "%dx%d+%d+%d", &to.px_width, &to.px_height, &to.px_offset_x, &to.px_offset_y);
                    if (ec == 4) {
                        found_rect = 1;
                        break;
                    }
                    n++;
                }
                if (found_rect)
                    to.screen = xrr->screen_count - 1;
                else
                    to.screen = -1;

                if (strcmp("disconnected", ot[0]) == 0) {
                    is_output = 1;
                    to.connected = 0;
                } else
                if (strcmp("unknown", ot[0]) == 0 && strcmp("connection", ot[1]) == 0) {
                    is_output = 1;
                    to.connected = -1;
                } else
                if (strcmp("connected", ot[0]) == 0) {
                    is_output = 1;
                    to.connected = 1;
                }
                g_strfreev(ot);
                if (is_output) {
                    strncpy(to.name, output_id, 63);
                    xrr->output_count++;
                    if (xrr->outputs == NULL)
                        xrr->outputs = malloc(xrr->output_count * sizeof(x_output));
                    else
                        xrr->outputs = realloc(xrr->outputs, xrr->output_count * sizeof(x_output));
                    memcpy(&xrr->outputs[xrr->output_count-1], &to, sizeof(x_output));
                    memset(&to, 0, sizeof(x_output)); /* clear the temp */
                    goto xrr_next_line;
                }
            }

            xrr_next_line:
                p = next_nl + 1;
        }
        g_free(out);
        g_free(err);
        return TRUE;
    }
    return FALSE;
}

gboolean fill_basic_xlib(xinfo *xi) {
    Display *display;
    int s, w, h, rn;

    display = XOpenDisplay(NULL);
    if (display) {
        if (!xi->display_name)
            xi->display_name = XDisplayName(NULL);

        if (!xi->vendor)
            xi->vendor = XServerVendor(display);

        if (!xi->release_number) {
            rn = XVendorRelease(display);
            xi->release_number = g_strdup_printf("%d", rn);
        }

        if (!xi->xrr->screen_count) {
            s = DefaultScreen(display);
            w = XDisplayWidth(display, s);
            h = XDisplayHeight(display, s);

            /* create at least one screen */
            x_screen ts;
            memset(&ts, 0, sizeof(x_screen));
            ts.number = s;
            ts.px_width = w;
            ts.px_height = h;

            xi->xrr->screen_count++;
            if (xi->xrr->screens == NULL)
                xi->xrr->screens = malloc(xi->xrr->screen_count * sizeof(x_screen));
            else
                xi->xrr->screens = realloc(xi->xrr->screens, xi->xrr->screen_count * sizeof(x_screen));
            memcpy(&xi->xrr->screens[xi->xrr->screen_count-1], &ts, sizeof(x_screen));
        }
        return TRUE;
    }
    return FALSE;
}

xrr_info *xrr_create() {
    xrr_info *xrr = malloc(sizeof(xrr_info));
    memset(xrr, 0, sizeof(xrr_info));
    return xrr;
}

void xrr_free(xrr_info *xrr) {
    if (xrr) {
        free(xrr->screens);
        free(xrr->outputs);
        free(xrr->providers);
        free(xrr->monitors);
        free(xrr);
    }
}

xinfo *xinfo_get_info() {
    int fail = 0;
    xinfo *xi = malloc(sizeof(xinfo));
    memset(xi, 0, sizeof(xinfo));
    xi->glx = glx_create();
    xi->xrr = xrr_create();

    if ( !fill_xinfo(xi) )
        fail++;
    if ( !fill_xrr_info(xi->xrr) )
        fail++;
    if ( !fill_glx_info(xi->glx) )
        fail++;

    if (fail) {
        /* as fallback, try xlib directly */
        if ( !fill_basic_xlib(xi) )
            xi->nox = 1;
    }
    return xi;
}

void xinfo_free(xinfo *xi) {
    if (xi) {
        free(xi->display_name);
        free(xi->vendor);
        free(xi->version);
        free(xi->release_number);
        xrr_free(xi->xrr);
        glx_free(xi->glx);
        free(xi);
    }
}