aboutsummaryrefslogtreecommitdiff
path: root/modules/computer/display.c
blob: 61eac4eb1b5cbe74c05ba1bb0a64a094dcb1d1fb (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
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2007 Leandro A. F. Pereira <leandro@hardinfo.org>
 *
 *    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.
 *
 *    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 <string.h>

#include "hardinfo.h"
#include "computer.h"

static void
get_glx_info(DisplayInfo *di)
{
    gchar *output;
    if (g_spawn_command_line_sync("glxinfo", &output, NULL, NULL, NULL)) {
        gchar **output_lines;
        gint i = 0;

        for (output_lines = g_strsplit(output, "\n", 0);
             output_lines && output_lines[i];
             i++) {
            if (strstr(output_lines[i], "OpenGL")) {
                gchar **tmp = g_strsplit(output_lines[i], ":", 0);

                tmp[1] = g_strchug(tmp[1]);

                get_str("OpenGL vendor str", di->ogl_vendor);
                get_str("OpenGL renderer str", di->ogl_renderer);
                get_str("OpenGL version str", di->ogl_version);

                g_strfreev(tmp);
            } else if (strstr(output_lines[i], "direct rendering: Yes")) {
                di->dri = TRUE;
            }
        }

        g_free(output);
        g_strfreev(output_lines);

        if (!di->ogl_vendor)
            di->ogl_vendor = _("(Unknown)");
        if (!di->ogl_renderer)
            di->ogl_renderer = _("(Unknown)");
        if (!di->ogl_version)
            di->ogl_version = _("(Unknown)");

    } else {
        di->ogl_vendor = di->ogl_renderer = di->ogl_version = _("(Unknown)");
    }

}

static void
get_x11_info(DisplayInfo *di)
{
    gchar *output;

    if (g_spawn_command_line_sync("xdpyinfo", &output, NULL, NULL, NULL)) {
	gchar **output_lines, **old;

	output_lines = g_strsplit(output, "\n", 0);
	g_free(output);

	old = output_lines;
	while (*output_lines) {
            gchar **tmp = g_strsplit(*output_lines, ":", 2);
            output_lines++;

            if (tmp[1] && tmp[0]) {
              tmp[1] = g_strchug(tmp[1]);

              get_str("vendor string", di->vendor);
              get_str("X.Org version", di->version);
              get_str("XFree86 version", di->version);
              get_str("name of display", di->display_name);

              if (g_str_has_prefix(tmp[0], "number of extensions")) {
                int n;

                di->extensions = g_strdup("");

                for (n = atoi(tmp[1]); n; n--) {
                  di->extensions = h_strconcat(di->extensions,
                                               g_strstrip(*(++output_lines)),
                                               "=\n",
                                               NULL);
                }
                g_strfreev(tmp);

                break;
              }
            }

            g_strfreev(tmp);
	}

	g_strfreev(old);
    }

    GdkScreen *screen = gdk_screen_get_default();

    if (screen && GDK_IS_SCREEN(screen)) {
        gint n_monitors = gdk_screen_get_n_monitors(screen);
        gint i;

        di->monitors = NULL;
        for (i = 0; i < n_monitors; i++) {
            GdkRectangle rect;

            gdk_screen_get_monitor_geometry(screen, i, &rect);

            di->monitors = h_strdup_cprintf(_("Monitor %d=%dx%d pixels\n"),
                                            di->monitors, i, rect.width, rect.height);
        }
      } else {
          di->monitors = "";
      }
}

DisplayInfo *
computer_get_display(void)
{
    DisplayInfo *di = g_new0(DisplayInfo, 1);

    GdkScreen *screen = gdk_screen_get_default();

    if (screen && GDK_IS_SCREEN(screen)) {
        di->width = gdk_screen_get_width(screen);
        di->height = gdk_screen_get_height(screen);
    } else {
        di->width = di->height = 0;
    }

    get_glx_info(di);
    get_x11_info(di);

    return di;
}