| 1 | # $Source: /home/keck/lib/perl/X11/RCS/Screens.pm,v $ | ||
| 2 | # $Revision: 4.18 $$Date: 2007/07/06 17:00:30 $ | ||
| 3 | # Contents | ||
| 4 | # 1 standard 7 current 13 font 19 Screen::Client | ||
| 5 | # 2 new 8 Screen 14 managers 20 geometry | ||
| 6 | # 3 screen 9 current 15 translations 21 fonts | ||
| 7 | # 4 names 10 name 16 sunfun 22 notes | ||
| 8 | # 5 match_loose 11 width etc 17 x 23 pod | ||
| 9 | # 6 match_tight 12 clients 18 unicode | ||
| 10 | |||
| 11 | # ---------------------------------------------------------------------- | ||
| 12 | |||
| 13 | #1# standard | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | package X11::Screens; | ||
| 19 | |||
| 20 | use Carp; | ||
| 21 | use Data::Dumper; | ||
| 22 | |||
| 23 | our $VERSION = 0.2; | ||
| 24 | |||
| 25 | # ---------------------------------------------------------------------- | ||
| 26 | |||
| 27 | #2# new | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $screens = (require "$ENV{HOME}/.xscreens"); | ||
| 31 | croak("failed to read $ENV{HOME}/.xscreens") unless $screens; | ||
| 32 | for my $name (keys %$screens) { | ||
| 33 | my $screen = $screens->{$name}; | ||
| 34 | bless $screen, 'X11::Screen'; | ||
| 35 | $screen->{name} = $name; | ||
| 36 | my $clients = $screen->{clients}; | ||
| 37 | for my $client (values %$clients) { | ||
| 38 | $client = { geometry => $client } unless ref $client eq 'HASH'; | ||
| 39 | my $geometry = $client->{geometry}; | ||
| 40 | if (ref $geometry) { | ||
| 41 | my ($w0, $h0) = $geometry->[0] =~ /(\d+)x(\d+)/; | ||
| 42 | croak("bad geometry '$geometry->[0]'") unless defined $h0; | ||
| 43 | my ($w1, $h1) = $geometry->[1] =~ /(\d+)x(\d+)/; | ||
| 44 | croak("bad geometry '$geometry->[1]'") unless defined $h1; | ||
| 45 | croak("bizarre geometries") if | ||
| 46 | ($w0 > $w1 && $h0 < $h1) || ($w0 < $w1 && $h0 > $h1); | ||
| 47 | if ($w0 < $w1 || $h0 < $h1) { | ||
| 48 | $client->{geometry} = | ||
| 49 | { chars => $geometry->[0], pixels => $geometry->[1] }; | ||
| 50 | } else { | ||
| 51 | $client->{geometry} = | ||
| 52 | { chars => $geometry->[1], pixels => $geometry->[0] }; | ||
| 53 | } | ||
| 54 | } else { | ||
| 55 | $client->{geometry} = { chars => undef, pixels => $geometry }; | ||
| 56 | } | ||
| 57 | $client->{screens} = $screens; | ||
| 58 | bless $client, 'X11::Screen::Client'; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | bless $screens; | ||
| 62 | } | ||
| 63 | |||
| 64 | # ---------------------------------------------------------------------- | ||
| 65 | |||
| 66 | #3# screen | ||
| 67 | |||
| 68 | sub screen { | ||
| 69 | my ($screens, $name) = @_; | ||
| 70 | $screens->{name} | ||
| 71 | } | ||
| 72 | |||
| 73 | # ---------------------------------------------------------------------- | ||
| 74 | |||
| 75 | #4# names | ||
| 76 | |||
| 77 | sub names { | ||
| 78 | my $screens = shift; | ||
| 79 | keys %$screens; | ||
| 80 | } | ||
| 81 | |||
| 82 | # ---------------------------------------------------------------------- | ||
| 83 | |||
| 84 | #5# match_loose | ||
| 85 | |||
| 86 | sub match_loose { | ||
| 87 | my $screens = shift; | ||
| 88 | my %given = @_; | ||
| 89 | my @return; | ||
| 90 | for my $screen (values %$screens) { | ||
| 91 | my $match = 1; | ||
| 92 | my $hashref = $screen; | ||
| 93 | DOTTED: for my $dotted (keys %given) { | ||
| 94 | my $got; | ||
| 95 | my @keys = split /\./, $dotted; | ||
| 96 | for my $i (0 .. $#keys) { | ||
| 97 | my $key = $keys[$i]; | ||
| 98 | next DOTTED unless ref $hashref; | ||
| 99 | $got = $hashref->{$key}, last if $i == $#keys; | ||
| 100 | my $hashref = $hashref->{$key}; | ||
| 101 | } | ||
| 102 | next unless defined $got; | ||
| 103 | next if ref $got; | ||
| 104 | $match = 0 unless $got eq $given{$dotted}; | ||
| 105 | } | ||
| 106 | push(@return, $screen) if $match; | ||
| 107 | } | ||
| 108 | @return; | ||
| 109 | } | ||
| 110 | |||
| 111 | # ---------------------------------------------------------------------- | ||
| 112 | |||
| 113 | #6# match_tight | ||
| 114 | |||
| 115 | sub match_tight { | ||
| 116 | my $screens = shift; | ||
| 117 | my %given = @_; | ||
| 118 | my @return; | ||
| 119 | SCREEN: for my $screen (values %$screens) { | ||
| 120 | my $hashref = $screen; | ||
| 121 | DOTTED: for my $dotted (keys %given) { | ||
| 122 | my $got; | ||
| 123 | my @keys = split /\./, $dotted; | ||
| 124 | for my $i (0 .. $#keys) { | ||
| 125 | my $key = $keys[$i]; | ||
| 126 | next SCREEN unless ref $hashref; | ||
| 127 | $got = $hashref->{$key}, last if $i == $#keys; | ||
| 128 | my $hashref = $hashref->{$key}; | ||
| 129 | } | ||
| 130 | next SCREEN unless defined $got; | ||
| 131 | next SCREEN if ref $got; | ||
| 132 | next SCREEN unless $got eq $given{$dotted}; | ||
| 133 | } | ||
| 134 | push @return, $screen; | ||
| 135 | } | ||
| 136 | @return; | ||
| 137 | } | ||
| 138 | |||
| 139 | # ---------------------------------------------------------------------- | ||
| 140 | |||
| 141 | #7# current | ||
| 142 | |||
| 143 | # X11::Screens->current | ||
| 144 | # $screens->current | ||
| 145 | # X11::Screens->current($tkmain) | ||
| 146 | # $screens->current($tkmain) | ||
| 147 | |||
| 148 | sub current { | ||
| 149 | my $screens = shift; | ||
| 150 | $screens = X11::Screens->new unless ref $screens; | ||
| 151 | my $tkmain = shift; | ||
| 152 | my $current = X11::Screen->current($tkmain); | ||
| 153 | my $screen; | ||
| 154 | for my $name (keys %$screens) { | ||
| 155 | my $s = $screens->{$name}; | ||
| 156 | next if defined $s->{width} && | ||
| 157 | $s->{width} != $current->{width}; | ||
| 158 | next if defined $s->{height} && | ||
| 159 | $s->{height} != $current->{height}; | ||
| 160 | next if defined $s->{depth} && | ||
| 161 | $s->{depth} != $current->{depth}; | ||
| 162 | next if defined $s->{resolution} && | ||
| 163 | $s->{resolution} != $current->{resolution}; | ||
| 164 | $screen = $s; | ||
| 165 | } | ||
| 166 | croak("no entry in $ENV{HOME}/.xscreens matching current screen") | ||
| 167 | unless $screen; | ||
| 168 | $screen->{$_} = $current->{$_} for | ||
| 169 | qw(tkmain width height depth resolution); | ||
| 170 | $screen; | ||
| 171 | } | ||
| 172 | |||
| 173 | # ---------------------------------------------------------------------- | ||
| 174 | |||
| 175 | #8# Screen | ||
| 176 | |||
| 177 | package X11::Screen; | ||
| 178 | |||
| 179 | use Carp; | ||
| 180 | |||
| 181 | # ---------------------------------------------------------------------- | ||
| 182 | |||
| 183 | #9# current | ||
| 184 | |||
| 185 | sub current { | ||
| 186 | my $tkmain = shift; | ||
| 187 | $tkmain = shift unless ref $tkmain; | ||
| 188 | if (defined $tkmain) { | ||
| 189 | croak("argument not a Tk::MainWindow reference") | ||
| 190 | unless ref $tkmain eq 'Tk::MainWindow'; | ||
| 191 | } else { | ||
| 192 | require Tk; | ||
| 193 | $tkmain = Tk::MainWindow->new; | ||
| 194 | croak("Tk::MainWindow->new failed") unless $tkmain; | ||
| 195 | } | ||
| 196 | my $screen = { | ||
| 197 | tkmain => $tkmain, | ||
| 198 | width => $tkmain->screenwidth, | ||
| 199 | height => $tkmain->screenheight, | ||
| 200 | depth => $tkmain->screendepth, | ||
| 201 | resolution => $tkmain->pixels('1i'), | ||
| 202 | }; | ||
| 203 | bless $screen; | ||
| 204 | } | ||
| 205 | |||
| 206 | # ---------------------------------------------------------------------- | ||
| 207 | |||
| 208 | #10# name | ||
| 209 | |||
| 210 | sub name { | ||
| 211 | my $screen = shift; | ||
| 212 | $screen->{name}; | ||
| 213 | } | ||
| 214 | |||
| 215 | # ---------------------------------------------------------------------- | ||
| 216 | |||
| 217 | #11# width etc | ||
| 218 | |||
| 219 | sub width { | ||
| 220 | my $screen = shift; | ||
| 221 | $screen->{width} | ||
| 222 | } | ||
| 223 | |||
| 224 | sub height { | ||
| 225 | my $screen = shift; | ||
| 226 | $screen->{height} | ||
| 227 | } | ||
| 228 | |||
| 229 | sub depth { | ||
| 230 | my $screen = shift; | ||
| 231 | $screen->{depth} | ||
| 232 | } | ||
| 233 | |||
| 234 | sub resolution { | ||
| 235 | my $screen = shift; | ||
| 236 | $screen->{resolution} | ||
| 237 | } | ||
| 238 | |||
| 239 | # ---------------------------------------------------------------------- | ||
| 240 | |||
| 241 | #12# clients | ||
| 242 | |||
| 243 | # keys added by X11::Screens->new | ||
| 244 | |||
| 245 | sub clients { | ||
| 246 | my $screen = shift; | ||
| 247 | $screen->{clients} | ||
| 248 | } | ||
| 249 | |||
| 250 | # ---------------------------------------------------------------------- | ||
| 251 | |||
| 252 | #13# font | ||
| 253 | |||
| 254 | sub font { | ||
| 255 | my $screen = shift; | ||
| 256 | $screen->{font} | ||
| 257 | } | ||
| 258 | |||
| 259 | # ---------------------------------------------------------------------- | ||
| 260 | |||
| 261 | #14# managers | ||
| 262 | |||
| 263 | sub managers { | ||
| 264 | my $screen = shift; | ||
| 265 | $screen->{managers} | ||
| 266 | } | ||
| 267 | |||
| 268 | # ---------------------------------------------------------------------- | ||
| 269 | |||
| 270 | #15# translations | ||
| 271 | |||
| 272 | sub translations { | ||
| 273 | my $screen = shift; | ||
| 274 | $screen->{translations} | ||
| 275 | } | ||
| 276 | |||
| 277 | # ---------------------------------------------------------------------- | ||
| 278 | |||
| 279 | #16# sunfun | ||
| 280 | |||
| 281 | sub sunfun { | ||
| 282 | my $screen = shift; | ||
| 283 | $screen->{sunfun} | ||
| 284 | } | ||
| 285 | |||
| 286 | # ---------------------------------------------------------------------- | ||
| 287 | |||
| 288 | #17# x | ||
| 289 | |||
| 290 | sub x { | ||
| 291 | my $screen = shift; | ||
| 292 | $screen->{x} | ||
| 293 | } | ||
| 294 | |||
| 295 | # ---------------------------------------------------------------------- | ||
| 296 | |||
| 297 | #18# unicode | ||
| 298 | |||
| 299 | sub unicode { | ||
| 300 | my $screen = shift; | ||
| 301 | $screen->{unicode} | ||
| 302 | } | ||
| 303 | |||
| 304 | # ---------------------------------------------------------------------- | ||
| 305 | |||
| 306 | #19# X11::Screen::Client | ||
| 307 | |||
| 308 | package X11::Screen::Client; | ||
| 309 | |||
| 310 | # ---------------------------------------------------------------------- | ||
| 311 | |||
| 312 | #20# geometry | ||
| 313 | |||
| 314 | # keys added by X11::Screens->new | ||
| 315 | |||
| 316 | sub geometry { | ||
| 317 | my ($client, $units) = @_; | ||
| 318 | $client->{geometry}{$units}; | ||
| 319 | } | ||
| 320 | |||
| 321 | # ---------------------------------------------------------------------- | ||
| 322 | |||
| 323 | #21# fonts | ||
| 324 | |||
| 325 | sub normal { my $client = shift; $client->{normal}; } | ||
| 326 | sub bold { my $client = shift; $client->{bold}; } | ||
| 327 | |||
| 328 | sub font { | ||
| 329 | my $client = shift; | ||
| 330 | my $weight = shift || 'normal'; | ||
| 331 | $client->{$weight}; | ||
| 332 | } | ||
| 333 | |||
| 334 | # ---------------------------------------------------------------------- | ||
| 335 | |||
| 336 | 1; | ||
| 337 | __END__ |