/[soft]/drakx-kbd-mouse-x11/trunk/lib/Xconfig/resolution_and_depth.pm
ViewVC logotype

Contents of /drakx-kbd-mouse-x11/trunk/lib/Xconfig/resolution_and_depth.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 391 - (show annotations) (download)
Thu Feb 3 13:54:58 2011 UTC (13 years, 2 months ago) by dmorgan
File size: 16658 byte(s)
Import cleaned drakx-kbd-mouse-x11
1 package Xconfig::resolution_and_depth; # $Id: resolution_and_depth.pm 250289 2008-12-08 09:59:02Z pixel $
2
3 use diagnostics;
4 use strict;
5
6 use common;
7
8
9 our %depth2text = (
10 8 => N_("256 colors (8 bits)"),
11 15 => N_("32 thousand colors (15 bits)"),
12 16 => N_("65 thousand colors (16 bits)"),
13 24 => N_("16 million colors (24 bits)"),
14 );
15 our @depths_available = ikeys(%depth2text);
16
17 my @bios_vga_modes = (
18 { bios => 769, X => 640, Y => 480, Depth => 8 },
19 { bios => 771, X => 800, Y => 600, Depth => 8 },
20 { bios => 773, X => 1024, Y => 768, Depth => 8 },
21 { bios => 775, X => 1280, Y => 1024, Depth => 8 },
22 { bios => 777, X => 1600, Y => 1200, Depth => 8 },
23 { bios => 784, X => 640, Y => 480, Depth => 15 },
24 { bios => 787, X => 800, Y => 600, Depth => 15 },
25 { bios => 790, X => 1024, Y => 768, Depth => 15 },
26 { bios => 793, X => 1280, Y => 1024, Depth => 15 },
27 { bios => 796, X => 1600, Y => 1200, Depth => 15 },
28 { bios => 785, X => 640, Y => 480, Depth => 16 },
29 { bios => 788, X => 800, Y => 600, Depth => 16 },
30 { bios => 791, X => 1024, Y => 768, Depth => 16 },
31 { bios => 794, X => 1280, Y => 1024, Depth => 16 },
32 { bios => 797, X => 1600, Y => 1200, Depth => 16 },
33 );
34
35 sub from_bios {
36 my ($bios) = @_;
37 my $bios_int = $bios =~ /^0x(.*)/ ? hex($1) : $bios;
38 find { $_->{bios} == $bios_int } @bios_vga_modes;
39 }
40
41 sub bios_vga_modes() { @bios_vga_modes }
42
43 sub size2default_resolution {
44 my ($size) = @_; #- size in inch
45
46 require detect_devices;
47 if (arch() =~ /ppc/) {
48 return "1024x768" if detect_devices::get_mac_model() =~ /^PowerBook|^iMac/;
49 } elsif (detect_devices::is_xbox()) {
50 return "640x480";
51 }
52
53 my %monitorSize2resolution = (
54 13 => "640x480",
55 14 => "800x600",
56 15 => "800x600",
57 16 => "1024x768",
58 17 => "1024x768",
59 18 => "1024x768",
60 19 => "1280x1024",
61 20 => "1280x1024",
62 21 => "1600x1200",
63 22 => "1600x1200",
64 );
65 $monitorSize2resolution{round($size)} || ($size < 13 ? "640x480" : "1600x1200");
66 }
67
68 sub XxY { &Xconfig::xfree::XxY }
69
70 sub to_string {
71 my ($resolution) = @_;
72 $resolution or return '';
73
74 $resolution->{automatic}
75 ? N("Automatic") . ($resolution->{Depth} ? sprintf(" (%dbpp)", $resolution->{Depth}) : '')
76 : $resolution->{X} ? sprintf("%sx%s %dbpp", @$resolution{'X', 'Y', 'Depth'}) : 'frame-buffer';
77 }
78
79 sub allowed {
80 my ($card) = @_;
81
82 my ($prefered_depth, @resolution_and_depth);
83
84 if ($card->{Driver} eq 'fbdev') {
85 @resolution_and_depth = grep { $_->{Depth} == 16 } @bios_vga_modes;
86 } else {
87 my @depths;
88 if ($card->{Driver} eq 'fglrx' || $card->{Driver} eq 'savage') {
89 @depths = 24;
90 } elsif ($card->{BoardName} eq 'NVIDIA RIVA 128') {
91 @depths = qw(8 15 24);
92 } elsif ($card->{use_DRI_GLX}) {
93 $prefered_depth = 24;
94 @depths = (16, 24);
95 } else {
96 @depths = our @depths_available;
97 }
98 my @resolutions = @Xconfig::xfree::resolutions;
99
100 push @resolution_and_depth,
101 map {
102 my $Depth = $_;
103 map { m/(\d+)x(\d+)/ && { X => $1, Y => $2, Depth => $Depth } } @resolutions;
104 } @depths;
105 }
106 $prefered_depth, @resolution_and_depth;
107 }
108
109 # ($card->{VideoRam} || ($card->{server} eq 'FBDev' ? 2048 : 32768))
110 sub filter_using_VideoRam {
111 my ($VideoRam, @resolutions) = @_;
112 my $mem = 1024 * $VideoRam;
113 grep { $_->{X} * $_->{Y} * $_->{Depth}/8 <= $mem } @resolutions;
114
115 }
116 sub filter_using_HorizSync_VertRefresh {
117 my ($HorizSync, $VertRefresh, @resolutions) = @_;
118 my $max_hsync = 1000 * max(split(/[,-]/, $HorizSync));
119 my ($min_vsync, $max_vsync) = (min(split(/[,-]/, $VertRefresh)), max(split(/[,-]/, $VertRefresh)));
120
121 #- enforce at least 60Hz, if max_vsync > 100 (ie don't do it on LCDs which are ok with low vsync)
122 $min_vsync = max(60, $min_vsync) if $max_vsync > 100;
123
124 #- computing with {Y} which is active sync instead of total sync, but that's ok
125 grep { $max_hsync / $_->{Y} > $min_vsync } @resolutions;
126 }
127
128 sub choose {
129 my ($in, $default_resolution, @resolutions) = @_;
130
131 my @Depths = uniq(reverse map { $_->{Depth} } @resolutions);
132 my $resolution = $default_resolution || {};
133 $in->ask_from(N("Resolutions"), "",
134 [ {
135 val => \$resolution, type => 'list', sort => 0,
136 list => [ (sort { $a->{X} <=> $b->{X} } @resolutions),
137 (map { { automatic => 1, Depth => $_ } } @Depths) ],
138 format => \&to_string,
139 } ]) or return;
140 $resolution;
141 }
142
143
144 sub choices {
145 my ($_raw_X, $resolution_wanted, $card, $monitors) = @_;
146 $resolution_wanted ||= {};
147
148 my ($prefered_depth, @resolutions) = allowed($card);
149
150 @resolutions = filter_using_HorizSync_VertRefresh($monitors->[0]{HorizSync}, $monitors->[0]{VertRefresh}, @resolutions) if $monitors->[0]{HorizSync};
151 @resolutions = filter_using_VideoRam($card->{VideoRam}, @resolutions) if $card->{VideoRam};
152
153 #- sort it, so we can take the first one when we want the "best"
154 @resolutions = sort { $b->{X} <=> $a->{X} || $b->{Y} <=> $a->{Y} || $b->{Depth} <=> $a->{Depth} } @resolutions;
155
156 $_->{ratio} ||= Xconfig::xfree::resolution2ratio($_) foreach @resolutions;
157
158 if ($resolution_wanted->{automatic} || !$resolution_wanted->{X} && !$monitors->[0]{HorizSync}) {
159 return { automatic => 1, Depth => $resolution_wanted->{Depth} }, @resolutions;
160 }
161
162 if ($resolution_wanted->{X} && !$resolution_wanted->{Y}) {
163 #- assuming ratio 4/3
164 $resolution_wanted->{Y} = round($resolution_wanted->{X} * 3 / 4);
165 } elsif (!$resolution_wanted->{X}) {
166 if ($monitors->[0]{preferred_resolution}) {
167 put_in_hash($resolution_wanted, $monitors->[0]{preferred_resolution});
168 } elsif ($monitors->[0]{ModelName} =~ /^Flat Panel (\d+)x(\d+)$/) {
169 put_in_hash($resolution_wanted, { X => $1, Y => $2 });
170 } elsif ($monitors->[0]{diagonal_size}) {
171 my ($X, $Y) = split('x', size2default_resolution($monitors->[0]{diagonal_size} * 1.08));
172 put_in_hash($resolution_wanted, { X => $X, Y => $Y });
173 } else {
174 put_in_hash($resolution_wanted, { X => 1024, Y => 768 });
175 }
176 }
177 my @matching = grep { $_->{X} eq $resolution_wanted->{X} && $_->{Y} eq $resolution_wanted->{Y} } @resolutions;
178 if (!@matching) {
179 #- hard choice :-(
180 #- first trying the greater resolution with same ratio
181 my $ratio = $resolution_wanted->{X} / $resolution_wanted->{Y};
182 @matching = grep { abs($ratio - $_->{X} / $_->{Y}) < 0.01 } @resolutions;
183 }
184 if (!@matching) {
185 #- really hard choice :'-(
186 #- take the first available resolution <= the wanted resolution
187 @matching = grep { $_->{X} < $resolution_wanted->{X} } @resolutions;
188 }
189 if (!@matching) {
190 @matching = @resolutions;
191 }
192
193 my $default_resolution;
194 foreach my $Depth ($resolution_wanted->{Depth}, $prefered_depth) {
195 $Depth and $default_resolution ||= find { $_->{Depth} eq $Depth } @matching;
196 }
197 $default_resolution ||= $matching[0];
198
199 $default_resolution, @resolutions;
200 }
201
202 sub configure {
203 my ($in, $raw_X, $card, $monitors, $b_auto, $o_resolution) = @_;
204
205 my ($default_resolution, @resolutions) = choices($raw_X, $o_resolution || $raw_X->get_resolution, $card, $monitors);
206
207 my $resolution;
208 if ($b_auto) {
209 #- use $default_resolution
210 if ($card->{Driver} eq 'fglrx' && !$default_resolution->{automatic}) {
211 $resolution = first(find { $default_resolution->{Y} eq $_->{Y} && $_->{Depth} == 24 }
212 $default_resolution, @resolutions);
213 $resolution ||= first(find { $_->{Depth} == 24 } $resolution, @resolutions);
214 }
215 } elsif ($in->isa('interactive::gtk')) {
216 $resolution = choose_gtk($in, $card, $default_resolution, @resolutions) or return;
217 } else {
218 $resolution = choose($in, $default_resolution, @resolutions) or return;
219 }
220 set_resolution_($raw_X, $card, $monitors, $default_resolution, $resolution, @resolutions);
221 }
222
223 sub configure_auto_install {
224 my ($raw_X, $card, $monitors, $old_X) = @_;
225
226 my ($default_resolution, @resolutions);
227 if ($old_X->{resolution_wanted} eq 'automatic') {
228 $default_resolution = { automatic => 1 };
229 } else {
230 my $resolution_wanted = $old_X->{resolution_wanted} && do {
231 my ($X, $Y) = split('x', $old_X->{resolution_wanted});
232 { X => $X, Y => $Y, Depth => $old_X->{default_depth} };
233 };
234
235 ($default_resolution, @resolutions) = choices($raw_X, $resolution_wanted, $card, $monitors);
236 $default_resolution or die "you selected an unusable depth";
237 }
238 set_resolution($raw_X, $card, $monitors, $default_resolution, @resolutions);
239 }
240
241 sub set_resolution {
242 my ($raw_X, $card, $monitors, $resolution, @other) = @_;
243 set_resolution_($raw_X, $card, $monitors, $resolution, $resolution, @other);
244 }
245 sub set_resolution_ {
246 my ($raw_X, $card, $monitors, $default_resolution, $resolution, @other) = @_;
247
248 my $PreferredMode;
249 if (!$resolution->{automatic}) {
250 my $ratio = Xconfig::xfree::resolution2ratio($resolution, 'non-strict');
251 @other = uniq_ { XxY($_) } @other;
252 @other = grep { $_->{X} < $resolution->{X} } @other;
253 @other = filter_on_ratio($ratio, @other);
254
255 set_915resolution($resolution) if is_915resolution_configured();
256
257 if (XxY($default_resolution) ne XxY($resolution)) {
258 log::l("setting PreferredMode since wanted resolution (" . XxY($resolution) . ") differs from the default one (" . XxY($default_resolution) . ")");
259 $PreferredMode = XxY($resolution);
260 }
261 }
262 if ($monitors->[0]{PreferredMode} ne $PreferredMode) {
263 if ($PreferredMode) {
264 $monitors->[0]{PreferredMode} = $PreferredMode;
265 } else {
266 delete $monitors->[0]{PreferredMode};
267 }
268 $raw_X->set_monitors(@$monitors);
269 }
270 if ($card->{Driver} eq 'geode') {
271 $card->{Options}{PanelGeometry} = XxY($resolution);
272 Xconfig::card::to_raw_X($card, $raw_X);
273 }
274
275 set_default_background($resolution);
276 my $resolutions = [ $resolution, @other ];
277 $raw_X->set_resolutions($resolutions);
278 $resolutions;
279 }
280 sub set_default_background {
281 my ($resolution) = @_;
282
283 $resolution->{X} && $resolution->{Y} or do {
284 $resolution = { X => 1024, Y => 768 };
285 log::l("defaulting background resolution to $resolution->{X}x$resolution->{Y}");
286 };
287
288 my $ratio = $resolution->{X} / $resolution->{Y};
289 my $dir = "$::prefix/usr/share/mdk/backgrounds";
290 my %theme = getVarsFromSh("$::prefix/etc/sysconfig/bootsplash");
291
292 my @l = map {
293 if (my ($X, $Y, undef, $hour) = /^\Q$theme{THEME}\E-(\d+)x(\d+)(-(.*))?.jpg$/) {
294 { file => $_, X => $X, Y => $Y, hour => $hour };
295 } else { () }
296 } all($dir);
297
298 my ($best, $_other) =
299 sort {
300 $a->[2] <=> $b->[2] || $b->[3] <=> $a->[3] || $a->[4] <=> $b->[4];
301 } map {
302 [
303 $_->{X}, $_->{Y},
304 int(abs($ratio - $_->{X} / $_->{Y}) * 100), #- we want the nearest ratio (precision .01)
305 $_->{X} >= $resolution->{X}, #- then we don't want a resolution smaller
306 abs($_->{X} - $resolution->{X}), #- the nearest resolution
307 ];
308 } @l;
309
310 my @wanted = grep { $best->[0] == $_->{X} && $best->[1] == $_->{Y} } @l;
311
312 if (-e "$dir/$theme{THEME}.xml") {
313 symlinkf "$theme{THEME}.xml", "$dir/Mageia.xml";
314 }
315
316 foreach (@wanted) {
317 if ($_->{hour}) {
318 symlinkf $_->{file}, "$dir/$theme{THEME}-$_->{hour}.jpg";
319 } else {
320 symlinkf $_->{file}, "$dir/default.jpg";
321 }
322 }
323 }
324 sub is_915resolution_configured() {
325 my $f = "$::prefix/etc/sysconfig/915resolution";
326 -e $f && { getVarsFromSh($f) }->{XRESO};
327 }
328 sub set_915resolution {
329 my ($resolution) = @_;
330
331 my $f = "$::prefix/etc/sysconfig/915resolution";
332 setVarsInSh($f, {
333 MODE => 'best',
334 XRESO => $resolution->{X},
335 YRESO => $resolution->{Y},
336 });
337 run_program::rooted($::prefix, 'service', '915resolution', 'start');
338 }
339
340 sub filter_on_ratio {
341 my ($ratio, @l) = @_;
342 grep {
343 !$ratio
344 || $_->{ratio} eq $ratio
345 || $ratio eq '4/3' && "$_->{X}x$_->{Y}" eq '1280x1024';
346 } @l;
347 }
348
349 sub choose_gtk {
350 my ($in, $card, $default_resolution, @resolutions) = @_;
351
352 my $chosen_Depth = $default_resolution->{Depth};
353 my $chosen_res = { X => $default_resolution->{X} || 1024, Y => $default_resolution->{Y} };
354 my $chosen_ratio = Xconfig::xfree::resolution2ratio($chosen_res, 'non-strict') || '4/3';
355
356 my $filter_on_Depth = sub {
357 grep { $_->{Depth} == $chosen_Depth } @_;
358 };
359 my $filter_on_res = sub {
360 grep { $_->{X} == $chosen_res->{X} && $_->{Y} == $chosen_res->{Y} } @_;
361 };
362 my $automatic_resolution = { automatic => 1, text => N_("Automatic") };
363 $chosen_res =
364 $default_resolution->{automatic} ? $automatic_resolution :
365 #- $chosen_res must be one of @resolutions, so that it has a correct {ratio} field
366 first($filter_on_res->(@resolutions)) || $resolutions[0];
367
368 require ugtk2;
369 mygtk2->import;
370 ugtk2->import(qw(:create :helpers :wrappers));
371 my $W = ugtk2->new(N("Resolution"), modal => 1);
372
373 my %monitor_images_x_res = do {
374 my @l = qw(640 800 1024 1152 1280 1400 1600 1920 2048);
375 my %h = map { $_ => ugtk2::_find_imgfile("monitor-$_.png") } @l;
376
377 #- for the other, use the biggest smaller
378 foreach my $x_res (uniq map { $_->{X} } @resolutions) {
379 my $x_res_ = max(grep { $_ <= $x_res } @l);
380 $h{$x_res} ||= $h{$x_res_} || $h{640};
381 }
382 %h;
383 };
384
385 my $res2text = sub { "$_[0]{X}x$_[0]{Y}" . ($chosen_ratio || $_[0]{ratio} =~ /other/ ? '' : " ($_[0]{ratio})") };
386 my @matching_ratio;
387 my $proposed_resolutions = [];
388 my $set_proposed_resolutions = sub {
389 my ($suggested_res) = @_;
390 @matching_ratio = filter_on_ratio($chosen_ratio, @resolutions);
391 gtkval_modify(\$proposed_resolutions, [
392 (reverse uniq_ { $res2text->($_) } @matching_ratio),
393 $automatic_resolution,
394 if_($chosen_ratio, { text => N_("Other") }),
395 ]);
396 if ($suggested_res->{automatic}) {
397 gtkval_modify(\$chosen_res, $automatic_resolution);
398 } elsif (!$filter_on_res->(@matching_ratio)) {
399 my $res = $suggested_res || find { $_->{X} == $chosen_res->{X} } @matching_ratio;
400 gtkval_modify(\$chosen_res, $res || $matching_ratio[0]);
401 }
402 };
403 $set_proposed_resolutions->($chosen_res);
404
405 my $depth_combo = gtknew('ComboBox', width => 220,
406 text_ref => \$chosen_Depth,
407 format => sub { translate($depth2text{$_[0]}) },
408 list => [ uniq(reverse map { $_->{Depth} } @resolutions) ],
409 changed => sub {
410 my @matching_Depth = $filter_on_Depth->(@matching_ratio);
411 if (!$filter_on_res->(@matching_Depth) && !$chosen_res->{automatic}) {
412 gtkval_modify(\$chosen_res, $matching_Depth[0]);
413 }
414 });
415 my $pix_colors = gtknew('Image',
416 file_ref => \$chosen_Depth,
417 format => sub {
418 $_[0] >= 24 ? "colors.png" : $_[0] >= 15 ? "colors16.png" : "colors8.png";
419 });
420 my $previous_res = $chosen_res;
421 my $res_combo = gtknew('ComboBox',
422 text_ref => \$chosen_res,
423 format => sub { $_[0]{text} ? translate($_[0]{text}) : &$res2text },
424 list_ref => \$proposed_resolutions,
425 changed => sub {
426 if ($chosen_res->{text} eq 'Other') {
427 undef $chosen_ratio;
428 $set_proposed_resolutions->($previous_res);
429 } elsif (!$chosen_res->{automatic}) {
430 my @matching_res = $filter_on_res->(@matching_ratio);
431 if (!$filter_on_Depth->(@matching_res)) {
432 gtkval_modify(\$chosen_Depth, $matching_res[0]{Depth});
433 }
434 }
435 $previous_res = $chosen_res;
436 });
437 my $pixmap_mo = gtknew('Image',
438 file_ref => \$chosen_res,
439 format => sub {
440 my $X = $_[0]{X} || '1024';
441 $monitor_images_x_res{$X} or internal_error("no image for resolution $X");
442 });
443
444 my $help_sub = $in->interactive_help_sub_display_id('configureX_resolution');
445 gtkadd($W->{window},
446 gtkpack_($W->create_box_with_title(N("Choose the resolution and the color depth"),
447 if_($card->{BoardName}, "(" . N("Graphics card: %s", $card->{BoardName}) . ")"),
448 ),
449 1, '',
450 0, $pixmap_mo,
451 0, gtknew('HBox', children => [
452 1, '',
453 0, gtknew('Table', col_spacings => 5, row_spacings => 5,
454 children => [
455 [ $res_combo, gtknew('Label', text => "") ],
456 [ $depth_combo, gtknew('Frame', shadow_type => 'etched_out', child => $pix_colors) ],
457 ]),
458 1, '',
459 ]),
460 1, '',
461 0, gtkadd($W->create_okcancel(N("Ok"), N("Cancel"), '', if_($help_sub, [ N("Help"), $help_sub, 1 ]))),
462 ));
463 $W->{ok}->grab_focus;
464
465 $W->main or return;
466
467 if ($chosen_res->{automatic}) {
468 $chosen_res->{Depth} = $chosen_Depth;
469 $chosen_res;
470 } else {
471 find { $_->{X} == $chosen_res->{X} &&
472 $_->{Y} == $chosen_res->{Y} &&
473 $_->{Depth} == $chosen_Depth } @resolutions;
474 }
475 }
476
477 1;

  ViewVC Help
Powered by ViewVC 1.1.30