File Coverage

blib/lib/X11/Protocol/Other.pm
Criterion Covered Total %
statement 17 67 25.3
branch 0 14 0.0
condition 0 7 0.0
subroutine 6 17 35.2
pod 11 11 100.0
total 34 116 29.3


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014, 2017, 2019 Kevin Ryde
2              
3             # This file is part of X11-Protocol-Other.
4             #
5             # X11-Protocol-Other is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # X11-Protocol-Other is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with X11-Protocol-Other. If not, see .
17              
18             package X11::Protocol::Other;
19 7     7   10638 use 5.004;
  7         41  
20 7     7   38 use strict;
  7         12  
  7         135  
21 7     7   38 use Carp;
  7         14  
  7         527  
22 7     7   1447 use X11::AtomConstants;
  7         22  
  7         305  
23              
24 7     7   40 use vars '$VERSION', '@ISA', '@EXPORT_OK';
  7         13  
  7         456  
25             $VERSION = 31;
26              
27 7     7   42 use Exporter;
  7         12  
  7         6586  
28             @ISA = ('Exporter');
29             @EXPORT_OK = qw(root_to_screen
30             root_to_screen_info
31             default_colormap_to_screen
32             default_colormap_to_screen_info
33             visual_is_dynamic
34             visual_class_is_dynamic
35             window_size
36             window_visual
37             get_property_atoms
38             hexstr_to_rgb
39             );
40              
41             # uncomment this to run the ### lines
42             #use Smart::Comments;
43              
44             sub window_size {
45 0     0 1   my ($X, $window) = @_;
46             ### Other window_size(): "$X $window"
47 0           my $screen_info;
48 0 0         if ($screen_info = root_to_screen_info($X,$window)) {
49             return ($screen_info->{'width_in_pixels'},
50 0           $screen_info->{'height_in_pixels'});
51             }
52 0           my %geom = $X->GetGeometry ($window);
53 0           return ($geom{'width'}, $geom{'height'});
54             }
55             sub window_visual {
56 0     0 1   my ($X, $window) = @_;
57             ### Other window_visual(): "$X $window"
58 0           my $screen_info;
59 0 0         if ($screen_info = root_to_screen_info($X,$window)) {
60 0           return $screen_info->{'root_visual'};
61             }
62 0           my %attr = $X->GetWindowAttributes ($window);
63 0           return $attr{'visual'};
64             }
65              
66             #------------------------------------------------------------------------------
67              
68             sub root_to_screen {
69 0     0 1   my ($X, $root) = @_;
70             ### Other root_to_screen(): $root
71             return ($X->{__PACKAGE__.'.root_to_screen_number'}
72 0           ||= { map {($X->{'screens'}->[$_]->{'root'} => $_)}
73 0           0 .. $#{$X->{'screens'}} })
74 0   0       ->{$root};
75             }
76             sub root_to_screen_info {
77 0     0 1   my ($X, $root) = @_;
78             ### Other root_to_screen_info(): $root
79 0           my $ret;
80 0 0         if (defined ($ret = root_to_screen($X,$root))) {
81 0           $ret = $X->{'screens'}->[$ret];
82             }
83 0           return $ret;
84              
85             # return ($X->{__PACKAGE__.'.root_to_screen_info'}
86             # ||= { map {($_->{'root'} => $_)} @{$X->{'screens'}} })->{$root}
87             }
88              
89             #------------------------------------------------------------------------------
90              
91             sub default_colormap_to_screen {
92 0     0 1   my ($X, $colormap) = @_;
93             ### default_colormap_to_screen(): $colormap
94             return ($X->{__PACKAGE__.'.default_colormap_to_screen_number'}
95 0           ||= { map {($X->{'screens'}->[$_]->{'default_colormap'} => $_)}
96 0           0 .. $#{$X->{'screens'}} })
97 0   0       ->{$colormap};
98             }
99             sub default_colormap_to_screen_info {
100 0     0 1   my ($X, $colormap) = @_;
101             ### Other colormap_to_screen_info(): $colormap
102 0           my $ret;
103 0 0         if (defined ($ret = default_colormap_to_screen($X,$colormap))) {
104 0           $ret = $X->{'screens'}->[$ret];
105             }
106 0           return $ret;
107             }
108              
109             # # return true if $colormap is one of the screen default colormaps
110             # sub colormap_is_default {
111             # my ($X, $colormap) = @_;
112             # return defined (default_colormap_to_screen($X,$colormap));
113             # }
114              
115              
116             #------------------------------------------------------------------------------
117             # my %visual_class_is_dynamic = (StaticGray => 0, 0 => 0,
118             # GrayScale => 1, 1 => 1,
119             # StaticColor => 0, 2 => 0,
120             # PseudoColor => 1, 3 => 1,
121             # TrueColor => 0, 4 => 0,
122             # DirectColor => 1, 5 => 1,
123             # );
124             sub visual_class_is_dynamic {
125 0     0 1   my ($X, $visual_class) = @_;
126 0           return $X->num('VisualClass',$visual_class) & 1;
127             }
128             sub visual_is_dynamic {
129 0     0 1   my ($X, $visual_id) = @_;
130 0   0       my $visual_info = $X->{'visuals'}->{$visual_id}
131             || croak 'Unknown visual ',$visual_id;
132 0           return visual_class_is_dynamic ($X, $visual_info->{'class'});
133             }
134              
135             #------------------------------------------------------------------------------
136              
137             # cf XcmsLRGB_RGB_ParseString() in XcmsLRGB.c
138              
139             sub hexstr_to_rgb {
140 0     0 1   my ($str) = @_;
141             ### hexstr_to_rgb(): $str
142             # Crib: [:xdigit:] is new in 5.6, so only 0-9A-F
143 0 0         $str =~ /^#(([0-9A-F]{3}){1,4})$/i or return;
144 0           my $len = length($1)/3; # of each group, so 1,2,3 or 4
145 0           return (map {hex(substr($_ x 4, 0, 4))} # first 4 chars of replicated
  0            
146             substr ($str, 1, $len), # full groups
147             substr ($str, 1+$len, $len),
148             substr ($str, -$len));
149             }
150              
151             # my %hex_factor = (1 => 0x1111,
152             # 2 => 0x101,
153             # 3 => 0x10 + 1/0x100,
154             # 4 => 1);
155             # my $factor = $hex_factor{$len} || return;
156             # ### $len
157             # ### $factor
158              
159              
160             #------------------------------------------------------------------------------
161              
162             sub get_property_atoms {
163 0     0 1   my ($X, $window, $property) = @_;
164 0           (my $value,
165             undef, # type
166             my $format,
167             my $bytes_after)
168             = $X->GetProperty ($window, $property,
169             X11::AtomConstants::ATOM(), # type
170             0, # offset
171             0x7FFFFFFF, # long-length: CARD32, unlimited
172             0); # delete
173             ### $value
174             ### $format
175 0 0         $format == 32 or return; # not atoms
176 0 0         if ($bytes_after) {
177 0           croak "oops, extremely long property, has $bytes_after more";
178             }
179 0           return unpack('L*', $value);
180             }
181              
182             sub set_property_atoms {
183 0     0 1   my $X = shift;
184 0           my $window = shift;
185 0           my $property = shift;
186 0           $X->ChangeProperty($window,
187             $property, # property
188             X11::AtomConstants::ATOM(), # type
189             32, # format
190             'Replace',
191             pack('L*',@_));
192             }
193              
194             # sub set_property_atom_names {
195             # my ($X, $window, $property, @atoms) = @_;
196             # # ENHANCE-ME: might like to intern all atoms in one round-trip, or perhaps
197             # # that's better left to a single big pre-fill of atoms in mainline code
198             # set_property_atoms($X,$window,$property,
199             # map {$X->atom($_)} @atoms);
200             # }
201              
202              
203             #------------------------------------------------------------------------------
204              
205             # # return true if $pixel is black or white in the default root window colormap
206             # sub pixel_is_black_or_white {
207             # my ($X, $pixel) = @_;
208             # return ($pixel == $X->{'black_pixel'} || $pixel == $X->{'white_pixel'});
209             # }
210             #
211              
212             1;
213             __END__