X11/Protocol/XSetRoot.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 239 CC 26 SCAR 51.9
CC
26
Cov
0%
set_background
380.0
_tog_cup_pixel_is_reserved
12.0
_kill_current
12.0
CRAP 179.2
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2017 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
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # X11-Protocol-Other is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License 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 <http://www.gnu.org/licenses/>.
17  
18  
19 # Window Manager Notes:
20 #
21 # ctwm does it's workspaces by moving windows or unmapping or something.
22 # There's no virtual root but it does draw the root window to a
23 # per-workspace colour on each change which overwrites anything put there in
24 # xsetroot style.
25 #
26 # evilwm desktop workspaces done by hiding/mapping windows.
27 #
28 # tvtwm comes with an ssetroot which looks at __SWM_VROOT.
29 #
30 # awesome does desktops by hiding/mapping and a task bar across the top.
31  
32  
33 # Maybe:
34  
35 # Maybe read /usr/include/X11/bitmaps/gray like xsetroot -grey.
36 # Or /usr/include/X11/bitmaps/root_weave which is the server default.
37 # Those files are bitmaps so foreground,background colours.
38 #
39 # bitmap_filename => '/blah...'
40 # bitmap_usr_include => 'gray'
41 # bitmap_include => 'gray'
42 # bitmap_type => 'gray','root_weave','default' builtins
43 # mod => x,y
44 # reverse_colors
45 # color =>
46 # background =>
47 # bitmap_foreground =>
48 # bitmap_background =>
49 #
50 #
51 # root=>
52 # virtual_root=> no look at root SWM_VROOT
53 #
54  
55  
56  
57 BEGIN { require 5 }
58 package X11::Protocol::XSetRoot;
59 use strict;
60 use Carp;
61 use X11::AtomConstants;
62 use X11::Protocol::Other;
63 use X11::Protocol::WM;
64  
65 use vars '$VERSION';
66 $VERSION = 31;
67  
68 # uncomment this to run the ### lines
69 # use Smart::Comments;
70  
71  
72 sub set_background {
73 my ($class, %option) = @_;
74 ### XSetRoot set_background(): do { my %o = %option; delete $o{'X'}; %o }
75  
76 my $display;
77 my $X = $option{'X'};
78 if (! $X) {
79 $display = $option{'display'};
80 ### display: $display
81 require X11::Protocol;
82 $X = X11::Protocol->new (defined $display ? ($display) : ());
83 $display ||= ''; # so not undef
84 }
85 ### X: "$X"
86  
87 my $root = $option{'root'};
88 my $screen_number = $option{'screen'};
89  
90 if (! defined $root) {
91 if (defined $screen_number) {
92 $root = $X->{'screens'}->[$screen_number]->{'root'};
93 } else {
94 $root = $X->{'root'};
95 }
96 }
97 if (! defined $screen_number) {
98 $screen_number = X11::Protocol::Other::root_to_screen($X,$root);
99  
100 # Secret undocumented allowance for root=>$xid being something not an
101 # actual root window. Maybe a window_to_screen() checking among the
102 # roots and then QueryTree().
103 #
104 if (! defined $screen_number) {
105 my ($actual_root) = $X->QueryTree ($root);
106 $screen_number = X11::Protocol::Other::root_to_screen($X,$actual_root);
107 }
108 }
109 ### $root
110  
111 my $visual = X11::Protocol::Other::window_visual($X,$root);
112 my $visual_is_dynamic = X11::Protocol::Other::visual_is_dynamic($X,$visual);
113 my $allocated;
114  
115 my @window_attributes;
116 my $pixmap = $option{'pixmap'};
117  
118 if (! defined $pixmap) {
119 my $screen_info = $X->{'screens'}->[$screen_number];
120 my $pixel;
121 if (defined ($pixel = $option{'pixel'})) {
122 ### pixel: $pixel
123  
124 } elsif (defined (my $color = $option{'color'})) {
125 ($pixel) = _alloc_named_or_hex_color($X,
126 $screen_info->{'default_colormap'},
127 $color);
128 # when we allocate the pixel here we don't need a specified $X
129 # connection, one opened here is enough
130 $option{'X'} = 1;
131 } else {
132 croak "No color, pixel or pixmap for background";
133 }
134  
135 $allocated = $visual_is_dynamic
136 && ! ($pixel == $screen_info->{'black_pixel'}
137 || $pixel == $screen_info->{'white_pixel'}
138 || _tog_cup_pixel_is_reserved($X,$screen_number,$pixel));
139 ### $allocated
140  
141 if ($option{'use_esetroot'}) {
142 ### create Esetroot 1x1 pixmap of pixel ...
143 $pixmap = $X->new_rsrc;
144 $X->CreatePixmap ($pixmap,
145 $root, # drawable
146 _window_depth($X,$root), # depth
147 1,1); # width,height
148 my $gc = $X->new_rsrc;
149 $X->CreateGC ($gc, $pixmap, foreground => $pixel);
150 $X->PolyPoint ($pixmap, $gc, 'Origin', 0,0);
151 $X->FreeGC($gc);
152  
153 } else {
154 @window_attributes = (background_pixel => $pixel);
155 }
156 } else {
157 $allocated = $option{'pixmap_allocated_colors'};
158 }
159 if (defined $pixmap) {
160 ### pixmap: sprintf '%#X', $pixmap
161 $pixmap = _num_none($pixmap);
162 @window_attributes = (background_pixmap => $pixmap);
163 }
164 ### @window_attributes
165  
166 if ($allocated) {
167 if ($visual_is_dynamic) {
168 unless ($option{'X'}) {
169 croak 'Need X connection to set given pixmap or allocated pixel';
170 }
171 } else {
172 ### static visual, so no pixel allocation ...
173 $allocated = 0;
174 }
175 }
176  
177 # follow any __SWM_VROOT
178 $root = (X11::Protocol::WM::root_to_virtual_root($X,$root) || $root);
179  
180 # GrabServer() so atomic get/set of _XSETROOT_ID.
181 #
182 # After GetProperty() delete+kill old _XSETROOT_ID don't want another
183 # client to be able to slip in a new value which ChangeProperty() here
184 # would overwrite (and so leak resources).
185 #
186 # The QueryPointer() sync is under the grab too so that no-one else can
187 # KillClient() on our new _XSETROOT_ID, until after QueryPointer().
188 #
189 require X11::Protocol::GrabServer;
190 my $grab = X11::Protocol::GrabServer->new ($X);
191  
192 _kill_current ($X, $root);
193  
194 $X->ChangeWindowAttributes ($root, @window_attributes);
195 $X->ClearArea ($root, 0,0,0,0); # whole window
196  
197 if ($option{'use_esetroot'}) {
198 ### set _XROOTPMAP_ID, ESETROOT_PMAP_ID: sprintf '%#X', $pixmap
199 my $data = pack ('L', $pixmap);
200 $X->ChangeProperty($root,
201 $X->atom('ESETROOT_PMAP_ID'),
202 X11::AtomConstants::PIXMAP(),
203 32, # format
204 'Replace',
205 $data);
206 $X->ChangeProperty($root,
207 $X->atom('_XROOTPMAP_ID'),
208 X11::AtomConstants::PIXMAP(),
209 32, # format
210 'Replace',
211 $data);
212 $X->SetCloseDownMode('RetainPermanent');
213  
214 } else {
215 ### xsetroot free the pixmap ...
216 # if given, and if not $pixmap==0 meaning "None"
217 if ($pixmap) {
218 ### FreePixmap: $pixmap
219 $X->FreePixmap($pixmap);
220 }
221  
222 if ($allocated) {
223 my $id_pixmap = $X->new_rsrc;
224 ### save id_pixmap: sprintf('%#X', $id_pixmap)
225 $X->CreatePixmap ($id_pixmap, # 1x1 bitmap
226 $root, # drawable, for screen
227 1, # depth
228 1,1); # width,height
229 $X->ChangeProperty($root,
230 $X->atom('_XSETROOT_ID'),
231 X11::AtomConstants::PIXMAP(),
232 32, # format
233 'Replace',
234 pack ('L', $id_pixmap));
235 $X->SetCloseDownMode('RetainPermanent');
236 }
237 }
238  
239 # Check for errors with a QueryPointer round trip, either if allocated
240 # pixels because the application will do nothing more, or if $display
241 # opened here.
242 if ($allocated || defined $display) {
243 ### sync with QueryPointer ...
244 $X->QueryPointer($root);
245 }
246 }
247  
248 # Return true if $pixel is a TOG-CUP reserved colormap entry in the root
249 # colormap of $screen_number. If it's not, or if no TOG-CUP available, then
250 # return false.
251 #
252 sub _tog_cup_pixel_is_reserved {
253 my ($X, $screen_number, $pixel) = @_;
254 ### _tog_cup_pixel_is_reserved(): $pixel
255  
256 if ($X->{'ext'}->{'TOG_CUP'}
257 || $X->init_extension('TOG-CUP')) {
258 my $c;
259 foreach $c ($X->CupGetReservedColormapEntries($screen_number)) {
260 if ($c->[0] == $pixel) {
261 return 1;
262 }
263 }
264 }
265 return 0;
266 }
267  
268 # Similar to window_size().
269 # Return the depth of $window.
270 # If $window is one of the root windows then the root_depth from the screen
271 # info in $X is returned, otherwise the depth is obtained from GetGeometry().
272 sub _window_depth {
273 my ($X, $window) = @_;
274 ### _window_depth(): $window
275 my $screen_info = X11::Protocol::Other::root_to_screen_info($X,$window);
276 if ($screen_info) {
277 return $screen_info->{'root_depth'};
278 }
279 my %geom = $X->GetGeometry($window);
280 return $geom{'depth'};
281 }
282  
283  
284 # Thought about making this a public method, but doubt it would find much
285 # use except another setroot. After killing the saved resource properties
286 # the actual root window background ought to be set to something and a
287 # ClearArea() to draw.
288 #
289 # =item C<X11::Protocol::XSetRoot-E<gt>kill_current ($X)>
290 #
291 # =item C<X11::Protocol::XSetRoot-E<gt>kill_current ($X, $root)>
292 #
293 # Kill any existing C<_XSETROOT_ID> on the given C<$root> XID. If C<$root>
294 # is C<undef> or omitted then the C<$X-E<gt>root> default is used.
295 #
296 # This is normally only used when changing or replacing the background in
297 # the way C<set_background()> above does.
298 #
299 sub _kill_current {
300 my ($X, $root) = @_;
301 ### XSetRoot kill_current() ...
302 # $root ||= $X->{'root'};
303  
304 # Delete _XROOTPMAP_ID.
305 # Do this before KillClient(ESETROOT_PMAP_ID) so that _XROOTPMAP_ID is not
306 # left momentarily as a killed non-existent XID. Though anyone using
307 # _XROOTPMAP_ID must be prepared for the XID to be destroyed at any time
308 # since it belongs to another client. We're under a GrabServer() here
309 # anyway, so no normal clients get in between the deletes and replacement.
310 #
311 $X->DeleteProperty($root, $X->atom('_XROOTPMAP_ID'));
312  
313 # Delete and kill _XSETROOT_ID and ESETROOT_PMAP_ID.
314 #
315 foreach my $atom_name ('ESETROOT_PMAP_ID', '_XSETROOT_ID') {
316 ### atom: $X->atom($atom_name)
317 ### $atom_name
318 my ($value, $type, $format, $bytes_after)
319 = $X->GetProperty($root,
320 $X->atom($atom_name),
321 0, # AnyPropertyType
322 0, # offset
323 1, # length
324 1); # delete
325 ### $type
326 if ($type == X11::AtomConstants::PIXMAP() && $format == 32) {
327 my $xid = unpack 'L', $value;
328 ### kill _XSETROOT_ID: sprintf('%#X', $xid)
329  
330 # For safety check $xid!=0, since KillClient(0) would mean kill all
331 # temporary clients (ie. all normal clients).
332 if ($xid) {
333 $X->KillClient($xid);
334 }
335 }
336 }
337 ### _kill_current() done ...
338 }
339  
340 sub _alloc_named_or_hex_color {
341 my ($X, $colormap, $str) = @_;
342 ### _alloc_named_or_hex_color(): $str
343 {
344 my @exact;
345 if (@exact = X11::Protocol::Other::hexstr_to_rgb($str)) {
346 my ($pixel, @actual) = $X->AllocColor($colormap, @exact);
347 return ($pixel, @exact, @actual);
348 }
349 }
350 return $X->AllocNamedColor($colormap, $str);
351 }
352  
353 # or maybe $X->num('IDorNone',$xid)
354 sub _num_none {
355 my ($xid) = @_;
356 if ($xid eq 'None') {
357 return 0;
358 } else {
359 return $xid;
360 }
361 }
362  
363 1;
364 __END__