summaryrefslogtreecommitdiffstats
path: root/lib/Xconfig/resolution_and_depth.pm
blob: 05a61e52ab94feb4057f43dba8b24bdc9721beb6 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package Xconfig::resolution_and_depth; # $Id: resolution_and_depth.pm 250289 2008-12-08 09:59:02Z pixel $

use lib qw(/usr/lib/libDrakX); # for perl_checker
use diagnostics;
use strict;
# perl_checker: require MDK::Common
# perl_checker: require ugtk3
use common;


our %depth2text = (
      8 => N_("256 colors (8 bits)"),
     15 => N_("32 thousand colors (15 bits)"),
     16 => N_("65 thousand colors (16 bits)"),
     24 => N_("16 million colors (24 bits)"),
);
our @depths_available = ikeys(%depth2text);

my @bios_vga_modes = (
    { bios => 769, X =>  640, Y =>  480, Depth =>  8 },
    { bios => 771, X =>  800, Y =>  600, Depth =>  8 },
    { bios => 773, X => 1024, Y =>  768, Depth =>  8 },
    { bios => 775, X => 1280, Y => 1024, Depth =>  8 },
    { bios => 777, X => 1600, Y => 1200, Depth =>  8 }, 
    { bios => 784, X =>  640, Y =>  480, Depth => 15 },
    { bios => 787, X =>  800, Y =>  600, Depth => 15 },
    { bios => 790, X => 1024, Y =>  768, Depth => 15 },
    { bios => 793, X => 1280, Y => 1024, Depth => 15 },
    { bios => 796, X => 1600, Y => 1200, Depth => 15 }, 
    { bios => 785, X =>  640, Y =>  480, Depth => 16 },
    { bios => 788, X =>  800, Y =>  600, Depth => 16 },
    { bios => 791, X => 1024, Y =>  768, Depth => 16 },
    { bios => 794, X => 1280, Y => 1024, Depth => 16 },
    { bios => 797, X => 1600, Y => 1200, Depth => 16 }, 
);

sub from_bios {
    my ($bios) = @_;
    my $bios_int = $bios =~ /^0x(.*)/ ? hex($1) : $bios;
    find { $_->{bios} == $bios_int } @bios_vga_modes;
}

sub bios_vga_modes() { @bios_vga_modes }

sub XxY { &Xconfig::xfree::XxY }

sub to_string {
    my ($resolution) = @_;
    $resolution or return '';

    $resolution->{automatic}
      ? N("Automatic") . ($resolution->{Depth} ? sprintf(" (%dbpp)", $resolution->{Depth}) : '')
      : $resolution->{X} ? sprintf("%sx%s %dbpp", @$resolution{'X', 'Y', 'Depth'}) : 'frame-buffer';
}

sub allowed {
    my ($card) = @_;

    my ($prefered_depth, @resolution_and_depth);
    
    if ($card->{Driver} eq 'fbdev' && $card->{BoardName} !~ /Poulsbo/) {
	@resolution_and_depth = grep { $_->{Depth} == 16 } @bios_vga_modes;
    } else {
	my @depths;
	if (member($card->{Driver}, qw(fglrx qxl savage))) {
	    @depths = 24;
	} elsif ($card->{Driver} eq 'geode') {
	    @depths = qw(16);
	} elsif ($card->{BoardName} eq 'NVIDIA RIVA 128') { 
	    @depths = qw(8 15 24);
	} elsif ($card->{use_DRI_GLX}) {
	    $prefered_depth = 24;
	    @depths = (16, 24);
	} else {
	    @depths = our @depths_available;
	}
	my @resolutions = @Xconfig::xfree::resolutions;

	push @resolution_and_depth,
	  map {
	      my $Depth = $_;
	      map { m/(\d+)x(\d+)/ && { X => $1, Y => $2, Depth => $Depth } } @resolutions;
	  } @depths;
    }
    $prefered_depth, @resolution_and_depth;
}

# ($card->{VideoRam} || ($card->{server} eq 'FBDev' ? 2048 : 32768))
sub filter_using_VideoRam {
    my ($VideoRam, @resolutions) = @_;
    my $mem = 1024 * $VideoRam;
    grep { $_->{X} * $_->{Y} * $_->{Depth}/8 <= $mem } @resolutions;
    
}
sub filter_using_HorizSync_VertRefresh {
    my ($HorizSync, $VertRefresh, @resolutions) = @_;
    my $max_hsync = 1000 * max(split(/[,-]/, $HorizSync));
    my ($min_vsync, $max_vsync) = (min(split(/[,-]/, $VertRefresh)), max(split(/[,-]/, $VertRefresh)));

    #- enforce at least 60Hz, if max_vsync > 100 (ie don't do it on LCDs which are ok with low vsync)
    $min_vsync = max(60, $min_vsync) if $max_vsync > 100;

    #- computing with {Y} which is active sync instead of total sync, but that's ok
    grep { $max_hsync / $_->{Y} > $min_vsync } @resolutions;
}

sub choose {
    my ($in, $default_resolution, @resolutions) = @_;

    my @Depths = uniq(reverse map { $_->{Depth} } @resolutions);
    my $resolution = $default_resolution || {};
    $in->ask_from(N("Resolutions"), "",
		  [ {
		     val => \$resolution, type => 'list', sort => 0,
		     list => [ (sort { $a->{X} <=> $b->{X} } @resolutions), 
			       (map { { automatic => 1, Depth => $_ } } @Depths) ],
		     format => \&to_string,
		    } ]) or return;
    $resolution;
}


sub choices {
    my ($_raw_X, $resolution_wanted, $card, $monitors) = @_;
    $resolution_wanted ||= {};

    my ($prefered_depth, @resolutions) = allowed($card);

    @resolutions = filter_using_HorizSync_VertRefresh($monitors->[0]{HorizSync}, $monitors->[0]{VertRefresh}, @resolutions) if $monitors->[0]{HorizSync};
    @resolutions = filter_using_VideoRam($card->{VideoRam}, @resolutions) if $card->{VideoRam};

    #- sort it, so we can take the first one when we want the "best"
    @resolutions = sort { $b->{X} <=> $a->{X} || $b->{Y} <=> $a->{Y} || $b->{Depth} <=> $a->{Depth} } @resolutions;

    $_->{ratio} ||= Xconfig::xfree::resolution2ratio($_) foreach @resolutions;

    if ($resolution_wanted->{automatic} || !$resolution_wanted->{X} && !$monitors->[0]{HorizSync}) {
	return { automatic => 1, Depth => $resolution_wanted->{Depth} }, @resolutions;
    }

    if ($resolution_wanted->{X} && !$resolution_wanted->{Y}) {
	#- assuming ratio 4/3
	$resolution_wanted->{Y} = round($resolution_wanted->{X} * 3 / 4);
    } elsif (!$resolution_wanted->{X}) {
	if ($monitors->[0]{preferred_resolution}) {
	    put_in_hash($resolution_wanted, $monitors->[0]{preferred_resolution});
	} elsif ($monitors->[0]{ModelName} =~ /^Flat Panel (\d+)x(\d+)$/) {
	    put_in_hash($resolution_wanted, { X => $1, Y => $2 });
	} else {
	    return { automatic => 1, Depth => $resolution_wanted->{Depth} }, @resolutions;
	}
    }
    my @matching = grep { $_->{X} eq $resolution_wanted->{X} && $_->{Y} eq $resolution_wanted->{Y} } @resolutions;
    if (!@matching) {
	#- hard choice :-(
	#- first trying the greater resolution with same ratio
	my $ratio = $resolution_wanted->{X} / $resolution_wanted->{Y};
	@matching = grep { abs($ratio - $_->{X} / $_->{Y}) < 0.01 } @resolutions;
    }
    if (!@matching) {
	#- really hard choice :'-(
	#- take the first available resolution <= the wanted resolution
	@matching = grep { $_->{X} < $resolution_wanted->{X} } @resolutions;
    }
    if (!@matching) {
	@matching = @resolutions;
    }

    my $default_resolution;
    foreach my $Depth ($resolution_wanted->{Depth}, $prefered_depth) {
	$Depth and $default_resolution ||= find { $_->{Depth} eq $Depth } @matching;
    }
    $default_resolution ||= $matching[0];

    $default_resolution, @resolutions;
}

sub configure {
    my ($in, $raw_X, $card, $monitors, $b_auto, $o_resolution) = @_;

    my ($default_resolution, @resolutions) = choices($raw_X, $o_resolution || $raw_X->get_resolution, $card, $monitors);

    my $resolution;
    if ($b_auto) {
	#- use $default_resolution
	if ($card->{Driver} eq 'fglrx' && !$default_resolution->{automatic}) {
	    $resolution = first(find { $default_resolution->{Y} eq $_->{Y} && $_->{Depth} == 24 }
					$default_resolution, @resolutions);
	    $resolution ||= first(find { $_->{Depth} == 24 } $resolution, @resolutions);
	}
    } elsif ($in->isa('interactive::gtk')) {
	$resolution = choose_gtk($in, $card, $default_resolution, @resolutions) or return;
    } else {
	$resolution = choose($in, $default_resolution, @resolutions) or return;
    }
    set_resolution_($raw_X, $card, $monitors, $default_resolution, $resolution, @resolutions);
}

sub configure_auto_install {
    my ($raw_X, $card, $monitors, $old_X) = @_;

    my ($default_resolution, @resolutions);
    if ($old_X->{resolution_wanted} eq 'automatic') {
	$default_resolution = { automatic => 1 };
    } else {
	my $resolution_wanted = $old_X->{resolution_wanted} && do {
	    my ($X, $Y) = split('x', $old_X->{resolution_wanted});
	    { X => $X, Y => $Y, Depth => $old_X->{default_depth} };
	};

	($default_resolution, @resolutions) = choices($raw_X, $resolution_wanted, $card, $monitors);
	$default_resolution or die "you selected an unusable depth";
    }
    set_resolution($raw_X, $card, $monitors, $default_resolution, @resolutions);
}

sub set_resolution {
    my ($raw_X, $card, $monitors, $resolution, @other) = @_; 
    set_resolution_($raw_X, $card, $monitors, $resolution, $resolution, @other);
}
sub set_resolution_ {
    my ($raw_X, $card, $monitors, $default_resolution, $resolution, @other) = @_; 

    my $PreferredMode;
    if (!$resolution->{automatic}) {
	my $ratio = Xconfig::xfree::resolution2ratio($resolution, 'non-strict');
	@other = uniq_ { XxY($_) } @other;
	@other = grep { $_->{X} < $resolution->{X} } @other;
	@other = filter_on_ratio($ratio, @other);

	set_915resolution($resolution) if is_915resolution_configured();

	if (XxY($default_resolution) ne XxY($resolution)) {
	    log::l("setting PreferredMode since wanted resolution (" . XxY($resolution) . ") differs from the default one (" . XxY($default_resolution) . ")");
	    $PreferredMode = XxY($resolution);
	}
    }
    if ($monitors->[0]{PreferredMode} ne $PreferredMode) {
	if ($PreferredMode) {
	    $monitors->[0]{PreferredMode} = $PreferredMode;
	} else {
	    delete $monitors->[0]{PreferredMode};
	}
	$raw_X->set_monitors(@$monitors);
    }
    if ($card->{Driver} eq 'geode') {
	$card->{Options}{PanelGeometry} = XxY($resolution);
	Xconfig::card::to_raw_X($card, $raw_X);	
    }

    set_default_background($resolution);
    my $resolutions = [ $resolution, @other ];
    $raw_X->set_resolutions($resolutions);
    $resolutions;
}
sub set_default_background {
    my ($resolution) = @_;
    
    $resolution->{X} && $resolution->{Y} or do {
	$resolution = { X => 1600, Y => 1200 };
	log::l("defaulting background resolution to $resolution->{X}x$resolution->{Y}");
    };

    my $ratio = $resolution->{X} / $resolution->{Y};
    my $dir = "$::prefix/usr/share/mga/backgrounds";
    my %theme = getVarsFromSh("$::prefix/etc/sysconfig/bootsplash");

    my @l = map {
	if (my ($X, $Y, undef, $hour) = /^\Q$theme{THEME}\E-(\d+)x(\d+)(-(.*))?.png$/) {
	    { file => $_, X => $X, Y => $Y, hour => $hour };
	} else { () }
    } all($dir);

    my ($best, $_other) = 
      sort {
	  $a->[2] <=> $b->[2] || $b->[3] <=> $a->[3] || $a->[4] <=> $b->[4];
      } map {
	  [
	      $_->{X}, $_->{Y},
	      int(abs($ratio - $_->{X} / $_->{Y}) * 100), #- we want the nearest ratio (precision .01)
	      $_->{X} >= $resolution->{X}, #- then we don't want a resolution smaller
	      abs($_->{X} - $resolution->{X}), #- the nearest resolution
	  ];
      } @l;

    my @wanted = grep { $best->[0] == $_->{X} && $best->[1] == $_->{Y} } @l;

    if (-e "$dir/$theme{THEME}.xml") {
	symlinkf "$theme{THEME}.xml", "$dir/Mageia.xml";
    }
    
    foreach (@wanted) {
	if ($_->{hour}) {
	    symlinkf $_->{file}, "$dir/$theme{THEME}-$_->{hour}.png";
	} else {
	    symlinkf $_->{file}, "$dir/default.png";
	}
    }
}
sub is_915resolution_configured() {
    my $f = "$::prefix/etc/sysconfig/915resolution";    
    -e $f && { getVarsFromSh($f) }->{XRESO};
}
sub set_915resolution {
    my ($resolution) = @_;

    my $f = "$::prefix/etc/sysconfig/915resolution";    
    setVarsInSh($f, { 
	MODE => 'best', 
	XRESO => $resolution->{X}, 
	YRESO => $resolution->{Y},
    });
    run_program::rooted($::prefix, 'service', '915resolution', 'start');
}

sub filter_on_ratio {
    my ($ratio, @l) = @_;
    grep {
	!$ratio
	      || $_->{ratio} eq $ratio 
	      || $ratio eq '4/3' && "$_->{X}x$_->{Y}" eq '1280x1024';
    } @l;
}

sub choose_gtk {
    my ($in, $card, $default_resolution, @resolutions) = @_;

    my $chosen_Depth = $default_resolution->{Depth};
    my $chosen_res = { X => $default_resolution->{X} || 1024, Y => $default_resolution->{Y} };
    my $chosen_ratio = Xconfig::xfree::resolution2ratio($chosen_res, 'non-strict') || '4/3';

    my $filter_on_Depth = sub {
	grep { $_->{Depth} == $chosen_Depth } @_;
    };
    my $filter_on_res = sub {
	grep { $_->{X} == $chosen_res->{X} && $_->{Y} == $chosen_res->{Y} } @_;
    };
    my $automatic_resolution = { automatic => 1, text => N_("Automatic") };
    $chosen_res = 
      $default_resolution->{automatic} ? $automatic_resolution : 
      #- $chosen_res must be one of @resolutions, so that it has a correct {ratio} field
      first($filter_on_res->(@resolutions)) || $resolutions[0];

    require ugtk3;
    mygtk3->import;
    ugtk3->import(qw(:create :helpers :wrappers));
    my $W = ugtk3->new(N("Resolution"), modal => 1);

    my %monitor_images_x_res = do {
	my @l = qw(640 800 1024 1152 1280 1400 1600 1920 2048);
	my %h = map { $_ => ugtk3::_find_imgfile("monitor-$_.png") } @l;

	#- for the other, use the biggest smaller
	foreach my $x_res (uniq map { $_->{X} } @resolutions) {
	    my $x_res_ = max(grep { $_ <= $x_res } @l);
	    $h{$x_res} ||= $h{$x_res_} || $h{640};
	}
	%h;
    };

    my $res2text = sub { "$_[0]{X}x$_[0]{Y}" . ($chosen_ratio || $_[0]{ratio} =~ /other/ ? '' : "  ($_[0]{ratio})") };
    my @matching_ratio;
    my $proposed_resolutions = [];
    my $set_proposed_resolutions = sub {
	my ($suggested_res) = @_;
	@matching_ratio = filter_on_ratio($chosen_ratio, @resolutions);
	gtkval_modify(\$proposed_resolutions, [ 
	    (reverse uniq_ { $res2text->($_) } @matching_ratio),
	    $automatic_resolution,
	    if_($chosen_ratio, { text => N_("Other") }),
	]);
	if ($suggested_res->{automatic}) {
	    gtkval_modify(\$chosen_res, $automatic_resolution);
	} elsif (!$filter_on_res->(@matching_ratio)) {
	    my $res = $suggested_res || find { $_->{X} == $chosen_res->{X} } @matching_ratio;
	    gtkval_modify(\$chosen_res, $res || $matching_ratio[0]);
	}
    };
    $set_proposed_resolutions->($chosen_res);

    my $depth_combo = gtknew('ComboBox', width => 220, 
			     text_ref => \$chosen_Depth,
			     format => sub { translate($depth2text{$_[0]}) },
			     list => [ uniq(reverse map { $_->{Depth} } @resolutions) ],
			     changed => sub {
				 my @matching_Depth = $filter_on_Depth->(@matching_ratio);
				 if (!$filter_on_res->(@matching_Depth) && !$chosen_res->{automatic}) {
				     gtkval_modify(\$chosen_res, $matching_Depth[0]);
				 }
			     });
    my $pix_colors = gtknew('Image', 
			    file_ref => \$chosen_Depth,
			    format => sub {
				$_[0] >= 24 ? "colors.png" : $_[0] >= 15 ? "colors16.png" : "colors8.png";
			    });
    my $previous_res = $chosen_res;
    my $res_combo = gtknew('ComboBox', 
			   text_ref => \$chosen_res,
			   format => sub { $_[0]{text} ? translate($_[0]{text}) : &$res2text },
			   list_ref => \$proposed_resolutions,
			   changed => sub {
			       if ($chosen_res->{text} eq 'Other') {
				   undef $chosen_ratio;
				   $set_proposed_resolutions->($previous_res);
			       } elsif (!$chosen_res->{automatic}) {
				   my @matching_res = $filter_on_res->(@matching_ratio);
				   if (!$filter_on_Depth->(@matching_res)) {
				       gtkval_modify(\$chosen_Depth, $matching_res[0]{Depth});
				   }
			       }
			       $previous_res = $chosen_res;
			   });
    my $pixmap_mo = gtknew('Image', 
			   file_ref => \$chosen_res,
			   format => sub {
			       my $X = $_[0]{X} || '1024';
			       $monitor_images_x_res{$X} or internal_error("no image for resolution $X");
			   });

    my $help_sub = $in->interactive_help_sub_display_id('configureX_resolution');
    gtkadd($W->{window},
	   gtkpack_($W->create_box_with_title(N("Choose the resolution and the color depth"),
					      if_($card->{BoardName}, "(" . N("Graphics card: %s", $card->{BoardName}) . ")"),
					     ),
		    1, '',
		    0, $pixmap_mo,
		    0, gtknew('HBox', children => [
			  1, '',
			  0, gtknew('Table', col_spacings => 5, row_spacings => 5, 
				    children => [
						 [ $res_combo, gtknew('Label', text => "") ],
						 [ $depth_combo, gtknew('Frame', shadow_type => 'etched_out', child => $pix_colors) ],
						]),
			  1, '',
		       ]),
	            1, '',
		    0, gtkadd($W->create_okcancel(N("Ok"), N("Cancel"), '', if_($help_sub, [ N("Help"), $help_sub, 1 ]))),
		    ));
    $W->{ok}->grab_focus;

    $W->main or return;

    if ($chosen_res->{automatic}) {
	$chosen_res->{Depth} = $chosen_Depth;
	$chosen_res;
    } else {
	find { $_->{X} == $chosen_res->{X} && 
	       $_->{Y} == $chosen_res->{Y} && 
	       $_->{Depth} == $chosen_Depth } @resolutions;
    }
}

1;