summaryrefslogtreecommitdiffstats
path: root/lib/Xconfig/glx.pm
blob: 847cc6605e33d3a9d5607958084adb988b526f5c (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
package Xconfig::glx;

use diagnostics;
use strict;

use common;
use any;

my $compositing_server_config = '/etc/sysconfig/compositing-server';
my $compositing_wm_config = '/etc/sysconfig/compositing-wm';

our @gl_compositing_types = (
    {
        type => "none",
        name => N_("No 3D desktop effects"),
    },
    {
        type => "metisse",
        name => N_("Metisse"),
        methods => [ qw(Xmetisse) ],
        wms => [ qw(metisse-start-fvwm) ],
    },
    {
        type => "cube",
        name => N_("3D desktop (cube workspace)"),
        methods => [ qw(native Xgl) ],
        wms => [ qw(compiz-fusion) ],
    },
);

our @gl_compositing_servers = (
    {
        method => "native",
        name => N_("native support"),
        packages => [],
        description => N_("Some drivers provide native support for OpenGL compositing (using AIGLX for example). If your system supports it, it is the preferred solution."), 
    },
    {
        method => "Xgl",
        name => "Xgl",
        packages => [ 'x11-server-xgl' ],
        description => N_("Xgl is an additional graphical server that adds 3D desktop support."),
    },
    {
        method => "Xmetisse",
        name => "Metisse (experimental)",
        packages => [ 'x11-server-xmetisse' ],
        description => N_("Metisse is an experimental X-based window system."),
    },
);

our @gl_compositing_wms = (
    {
        wm => 'compiz-fusion',
        configuration_tool => 'ccsm',
        packages => [ 'compiz-fusion', 'compiz-decorator-gtk' ],
        desktop_packages => {
            KDE => [ 'compiz-decorator-kde' ],
        },
        name => 'Compiz Fusion',
        description => N_("Compiz Fusion is a 3D compositing window manager"),
    },
    {
        wm => 'metisse-start-fvwm',
        configuration_tool => '',
        packages => [ 'metisse' ],
        name => 'Metisse FVWM',
        description => N_("Metisse FVWM is a FVWM window manager modified for Metisse."),
    },
);

sub parse_glxinfo {
    my (%options) = @_;

    my $glx_info = {};
    my $extensions_section;

    my @glxinfo = $options{file} ? cat_($options{file}) : do {
        require run_program;
        my $lib = arch() =~ /x86_64/ ? "lib64" : "lib";
        local $ENV{LD_LIBRARY_PATH} = $options{Mesa} && "/usr/$lib/mesa";
        run_program::rooted_get_stdout($::prefix, '/usr/bin/glxinfo', '2>', '/dev/null');
    };
    foreach (@glxinfo) {
        chomp;
        if (/^direct rendering: Yes/) {
            $glx_info->{capabilities}{direct_rendering} = 1;
        } elsif (my ($type, $value) = /^(.*) string: (.*)$/) {
            $type =~ s/\s/_/g;
            $glx_info->{strings}{$type} = $value;
        } elsif (/^(.*) extensions:/) {
            $extensions_section = $1;
            $extensions_section =~ s/\s/_/g;
        } elsif (/^\s/) {
            if ($extensions_section) {
                push @{$glx_info->{extensions}{$extensions_section}}, grep { $_ } split(/,?\s+/);
            }
        } else {
            undef $extensions_section;
        }
    }
    $glx_info;
}

my @glx_blacklist = qw(nv sis tdfx vmware);

sub is_in_metisse() {
    my $display = $ENV{DISPLAY};
    $display =~ s/\..*//;
    $display && any {
	member($display, split("\0", cat_("/proc/" . $_ . "/cmdline")));
    } fuzzy_pidofs("Xmetisse");
}

sub get_glx_status {
    my ($o_mesa_file, $o_system_file) = @_;

    my ($driver, $resolution);
    require Xconfig::xfree;
    eval {
        my $raw_X = Xconfig::xfree->read;
        $driver = $raw_X->get_Driver;
        $resolution = $raw_X->get_resolution;
    };
    $resolution ||= {};
    member($driver, @glx_blacklist) and return {};

    my ($mesa_glx_info, $system_glx_info);
    if ($o_mesa_file) {
        $mesa_glx_info = parse_glxinfo(file => $o_mesa_file);
        $system_glx_info = $o_system_file ? parse_glxinfo(file => $o_system_file) : $mesa_glx_info;
    } else {
        $system_glx_info = parse_glxinfo();
        $mesa_glx_info = parse_glxinfo(Mesa => 1);
    }

    my $glx_status = {};

    $glx_status->{context}{Mesa} = $system_glx_info->{strings}{OpenGL_renderer} =~ /Mesa/;
    $glx_status->{context}{MesaDRI} = $system_glx_info->{strings}{OpenGL_renderer} =~ /Mesa DRI/;
    $glx_status->{context}{Xgl} =
      $system_glx_info->{strings}{server_glx_vendor} eq 'SGI' &&
      member('GLX_EXT_texture_from_pixmap', @{$mesa_glx_info->{extensions}{GLX}}) && (
          #- avoid nv, but detect i810 and fglrx
          member('GL_ARB_fragment_program', @{$mesa_glx_info->{extensions}{OpenGL}}) ||
            !$glx_status->{context}{Mesa} && member('GLX_SGIX_fbconfig', @{$system_glx_info->{extensions}{GLX}})
      );
    $glx_status->{context}{Xmetisse} = is_in_metisse();

    $glx_status->{capabilities}{direct_rendering} =
      $system_glx_info->{capabilities}{direct_rendering} ||
      $glx_status->{context}{Xgl} ||
      $glx_status->{context}{Xmetisse};

    $glx_status->{capabilities}{Xgl} =
      $glx_status->{context}{Xgl} ||
        $glx_status->{capabilities}{direct_rendering} && (
            !$glx_status->{context}{MesaDRI} ||
            member('GLX_MESA_allocate_memory', @{$system_glx_info->{extensions}{GLX}})
          ) && (
            #- Xgl turns black with i810 and 16 bits
            $driver ne 'i810' || $resolution->{Depth} eq '24'
          );

    $glx_status->{capabilities}{native} =
      $glx_status->{capabilities}{direct_rendering} &&
      member('GLX_EXT_texture_from_pixmap', @{$system_glx_info->{extensions}{client_glx}}) &&
      member('GLX_EXT_texture_from_pixmap', @{$system_glx_info->{extensions}{server_glx}});

    $glx_status->{capabilities}{Xmetisse} = $glx_status->{capabilities}{direct_rendering};

    #- no support in configuration tools yet
    # $glx_status->{capabilities}{'3d-desktop'} = $glx_status->{capabilities}{direct_rendering};

    $glx_status;
}

sub detect() {
    my $compositing_server = +{ getVarsFromSh($::prefix . $compositing_server_config) };
    my $compositing_wm = +{ getVarsFromSh($::prefix . $compositing_wm_config) };
    my $glx = { capabilities => get_glx_status()->{capabilities} };
    $glx->{supported} = $glx->{capabilities}{direct_rendering};
    $glx->{compositing} = $compositing_wm->{COMPOSITING_WM_START} eq 'yes' || $compositing_wm->{COMPOSITING_WM_START} eq 'auto' && $glx->{supported};
    $glx->{wm} = $compositing_wm->{COMPOSITING_WM};
    $glx->{method} =
      $glx->{compositing} ? (
        $glx->{capabilities}{native} && member($compositing_server->{COMPOSITING_SERVER_START}, qw(auto no)) && !$compositing_server->{COMPOSITING_SERVER} ?
          'native' :
        $glx->{capabilities}{Xgl} && member($compositing_server->{COMPOSITING_SERVER_START}, qw(auto yes)) && $compositing_server->{COMPOSITING_SERVER} eq 'Xgl' ?
          'Xgl' :
        $glx->{capabilities}{Xmetisse} && member($compositing_server->{COMPOSITING_SERVER_START}, qw(auto yes)) && $compositing_server->{COMPOSITING_SERVER} eq 'Xmetisse' ?
          'Xmetisse' :
          ''
      ) : '';
    $glx;
}

sub detect_may_install {
    my ($in) = @_;
    $in->do_pkgs->ensure_is_installed('mesa-demos', '/usr/bin/glxinfo') or return;
    Xconfig::glx::detect();
}

sub detect_types {
    my ($glx) = @_;
    my @available_types = (
        grep {
            !$_->{methods} ||
              any { $glx->{capabilities}{$_} } @{$_->{methods}};
        } @gl_compositing_types
    );
    my $type = find { member($glx->{method}, @{$_->{methods}}) } @available_types;
    \@available_types, $type;
}

sub choose_interactive {
    my ($in, $glx) = @_;

    my ($available, $type) = detect_types($glx);

    $in->ask_from_({ title => N("3D Desktop effects") }, [
	{ label => N("3D Desktop effects"), title => 1 },
        if_(!$glx->{supported},
            { label => N("Your system does not support 3D desktop effects.") }),
        { type => 'list', val => \$type, allow_empty_list => 1,
          list => $available, format => sub { translate($_[0]{name}) } },
    ]) or return;

    $glx->{method} = find { $glx->{capabilities}{$_} } @{$type->{methods}};
    $glx->{wm} = first(@{$type->{wms}});

    1;
}

sub install {
    my ($in, $glx) = @_;
    my $server = find { $_->{method} eq $glx->{method} } @gl_compositing_servers;
    my $wm = find { $_->{wm} eq $glx->{wm} } @gl_compositing_wms;
    my @sessions = any::sessions();
    my @packages = map {
        my $entry = $_;
        @{$entry->{packages}},
        map { @{$entry->{desktop_packages}{$_} || []} } @sessions;
    } grep { $_ } $server, $wm;
    $in->do_pkgs->ensure_are_installed(\@packages);
}

sub write {
    my ($glx) = @_;
    my $server_start = member($glx->{method}, 'Xgl', 'Xmetisse');
    addVarsInSh($::prefix . $compositing_server_config, {
        COMPOSITING_SERVER_START => bool2yesno($server_start),
        COMPOSITING_SERVER => $server_start && $glx->{method},
    });
    addVarsInSh($::prefix . $compositing_wm_config, {
        COMPOSITING_WM_START =>  bool2yesno(member($glx->{method}, qw(native Xgl Xmetisse))),
        COMPOSITING_WM => $glx->{wm},
    });
}

1;