korgwm/Common.pm

filter: Statementstmt 40.9% 45 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 18 totaltotal 27.4% 53 / 193 CC 24 SCAR 21.5
CC
24
Cov
31%
init_extension
20.0
focus_prev_push
20.0
focus_prev_remove
12.0
CRAP 8.6
« korgwm/Panel.pm ↑ Summary korgwm/Xkb.pm »
1 #!/usr/bin/perl
2 # made by: KorG
3 # vim: cc=119 et sw=4 ts=4 :
4 package X11::korgwm::Common;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw( any first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT = qw(
15 carp croak S_DEBUG DEBUG_API $X $cfg $focus $windows %screens %xcb_events %xcb_events_ignore @screens %atoms
16 add_event_cb add_event_ignore hexnum init_extension replace_event_cb screen_by_xy pointer %marked_windows
17 $visible_min_x $visible_min_y $visible_max_x $visible_max_y $prevent_focus_in $prevent_enter_notify $cpu_saver
18 focus_prev_push focus_prev_remove focus_prev_get prevent_focus_in prevent_enter_notify $cached_classes atom
19 gtk_init pinned_list pinned_add pinned_remove pinned_check
20 );
21  
22 # NOTE all the debug functions are defined in Config.pm
23 1 push @EXPORT, "DEBUG$_" for 1..9;
24  
25 1 our $X;
26 1 our %atoms;
27 1 our $cfg;
28 1 our $cpu_saver = 0.1; # number of seconds to sleep before events processing (100ms by default)
29 1 our $focus;
30 1 our $windows = {};
31 1 our $cached_classes = {};
32 1 our %screens;
33 1 our %xcb_events;
34 1 our %xcb_events_ignore;
35 1 our @screens;
36 1 our ($visible_min_x, $visible_min_y, $visible_max_x, $visible_max_y);
37 1 our %marked_windows;
38  
39 # Sometimes we want to ignore FocusIn (see Mouse/ENTER_NOTIFY and Executor/tag_select)
40 1 our $prevent_focus_in;
41 1 our $prevent_enter_notify;
42  
43 # focus_prev() is now implemented as a functional interface and allows switch between more than two windows
44 1 my $focus_prev_global = [];
45 1 my $focus_prev_size = 5;
46  
47 # Some windows could be always on top of the stack; to avoid confusion with "always_on [tag]" I name them "pinned"
48 1 my %pinned_windows;
49  
50 # Helpers for extensions
51 0 ▶sub add_event_cb($id, $sub) {
Subroutine add_event_cb: not called CC=2 SCAR=17.9
Pod undocumented
52 0 ▶ croak "Redefined event handler for $id" if defined $xcb_events{$id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $X11::korgwm::Common::xcb_events{$id} 0 0 0
53 0 $xcb_events{$id} = $sub;
54 }
55  
56 0 ▶sub add_event_ignore($id) {
Subroutine add_event_ignore: not called CC=2 SCAR=17.9
Pod undocumented
57 0 ▶ croak "Redefined event ignore for $id" if defined $xcb_events_ignore{$id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $X11::korgwm::Common::xcb_events_ignore{$id} 0 0 0
58 0 $xcb_events_ignore{$id} = undef;
59 }
60  
61 0 ▶sub replace_event_cb($id, $sub) {
Subroutine replace_event_cb: not called CC=2 SCAR=17.9
Pod undocumented
62 0 ▶ croak "Event handler for $id is not defined" unless defined $xcb_events{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $X11::korgwm::Common::xcb_events{$id} 0 0 0
63 0 $xcb_events{$id} = $sub;
64 }
65  
66 0 ▶sub init_extension($name, $first_event) {
Subroutine init_extension: not called CC=4 SCAR=30.0
Pod undocumented
67 0 my $ext = $X->query_extension_reply($X->query_extension(length($name), $name)->{sequence});
68 0 ▶ croak "$name extension not available" unless $ext->{present};
Branch: 1 branch
BranchTrueFalseTotal
unless $ext->{"present"} 0 0 0
69  
70 # We can skip this part unless we're interested getting event
71 0 ▶ return unless defined $first_event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_event 0 0 0
72 0 ▶ croak "Could not get $name first_event" unless $$first_event = $ext->{first_event};
Branch: 1 branch
BranchTrueFalseTotal
unless $$first_event = $ext->{"first_event"} 0 0 0
73 }
74  
75 # focus_prev helpers
76 0 ▶sub focus_prev_push($win, $storage = $focus_prev_global) {
Subroutine focus_prev_push: not called CC=4 SCAR=30.0
Pod undocumented
77 0 ▶ return unless defined $win;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $win 0 0 0
78 0 focus_prev_remove($win, $storage);
79 0 push @{ $storage }, $win;
80 0 ▶ shift @{ $storage } if @{ $storage } > $focus_prev_size;
Branch: 1 branch
BranchTrueFalseTotal
if @{$storage;} > $focus_prev_size 0 0 0
81 }
82  
83 0 ▶sub focus_prev_remove($win, $storage = $focus_prev_global) {
Subroutine focus_prev_remove: not called CC=3 SCAR=24.8
Pod undocumented
84 0 ▶ return unless defined $win;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $win 0 0 0
85 0 @{ $storage } = grep { $_ != $win } @{ $storage };
86 }
87  
88 0 ▶sub focus_prev_get($storage = $focus_prev_global) {
Subroutine focus_prev_get: not called CC=2 SCAR=17.9
Pod undocumented
89 0 (grep { $_ != $focus->{window} } @{ $storage })[-1];
90 }
91  
92 # pinned_windows helpers
93 0 ▶sub pinned_add($win) {
Subroutine pinned_add: not called CC=1 SCAR=6.9
Pod undocumented
94 0 $pinned_windows{ $win->{id} } = $win;
95 }
96  
97 0 ▶sub pinned_remove($win) {
Subroutine pinned_remove: not called CC=1 SCAR=6.9
Pod undocumented
98 0 delete $pinned_windows{ $win->{id} };
99 }
100  
101 0 ▶sub pinned_list() {
Subroutine pinned_list: not called CC=1 SCAR=6.9
Pod undocumented
102 0 values %pinned_windows;
103 }
104  
105 0 ▶sub pinned_check($win) {
Subroutine pinned_check: not called CC=1 SCAR=6.9
Pod undocumented
106 0 exists $pinned_windows{ $win->{id} };
107 }
108  
109 # Preventor functions to avoid code copy-pasting
110 0 ▶sub prevent_focus_in($timeout = 0.11) {
Subroutine prevent_focus_in: not called CC=2 SCAR=17.9
Pod undocumented
111 0 ▶ $prevent_focus_in = AE::timer $timeout, 0, sub { $prevent_focus_in = undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 }
113  
114 0 ▶sub prevent_enter_notify($timeout = 0.11) {
Subroutine prevent_enter_notify: not called CC=2 SCAR=17.9
Pod undocumented
115 0 ▶ $prevent_enter_notify = AE::timer $timeout, 0, sub { $prevent_enter_notify = undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 }
117  
118 # Other helpers
119 0 ▶sub screen_by_xy($x, $y) {
Subroutine screen_by_xy: not called CC=3 SCAR=24.8
Pod undocumented
120 0 ▶ return unless defined $x and defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 0 0 0
Condition0%0/3
defined $x and defined $y
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x and defined $y
defined $x defined $y
Truth table0%0/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
121 0 ▶ first { $_->contains_xy($x, $y) } @screens;
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 }
123  
124 0 ▶sub hexnum($str = $_) {
Subroutine hexnum: not called CC=3 SCAR=24.8
Pod undocumented
125 0 ▶ looks_like_number $str ? $str : hex($str);
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($str) ? : 0 0 0
126 }
127  
128 0 ▶sub pointer($wid = $X->root->id) {
Subroutine pointer: not called CC=3 SCAR=24.8
Pod undocumented
129 0 ▶ $X->query_pointer_reply($X->query_pointer($wid)->{sequence}) // {};
Condition0%0/2
$X11::korgwm::Common::X->query_pointer_reply($X11::korgwm::Common::X->query_pointer($wid)->{"sequence"}) // {}
l!l
00
MC/DC0%0/1
$X11::korgwm::Common::X->query_pointer_reply($X11::korgwm::Common::X->query_pointer($wid)->{"sequence"}) // {}
$X11::korgwm::Common::X->query_pointer_reply($X11::korgwm::Common::X->query_pointer($wid)->{"sequence"})
Truth table0%0/2
A
A= $X11::korgwm::Common::X->query_pointer_reply($X11::korgwm::Common::X->query_pointer($wid)->{"sequence"})
Aresult
11
00
130 }
131  
132 # Caching function to resolve and create atoms
133 0 ▶sub atom($name) {
Subroutine atom: not called CC=2 SCAR=17.9
Pod undocumented
134 0 ▶ return $atoms{$name} if defined $atoms{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $X11::korgwm::Common::atoms{$name} 0 0 0
135 0 $atoms{$name} = $X->intern_atom_reply($X->intern_atom(0, length($name), $name)->{sequence})->{atom};
136 }
137  
138 # GTK initialization (called either from Panel or Expose)
139 0 ▶sub gtk_init() {
Subroutine gtk_init: not called CC=3 SCAR=24.8
Pod undocumented
140 0 ▶ return if $X11::korgwm::gtk_init;
Branch: 1 branch
BranchTrueFalseTotal
if $X11::korgwm::gtk_init 0 0 0
141 0 Gtk3::disable_setlocale();
142 0 Gtk3::init();
143  
144 0 my $color_bg = sprintf "#%06x", $cfg->{color_bg};
145 0 my $color_fg = sprintf "#%06x", $cfg->{color_fg};
146 0 my $color_append_bg = sprintf "#%06x", $cfg->{color_append_bg};
147 0 my $color_append_fg = sprintf "#%06x", $cfg->{color_append_fg};
148 0 my $color_battery_low = sprintf '#%06x', $cfg->{color_battery_low};
149 0 my $color_border = sprintf "#%06x", $cfg->{color_border};
150 0 my $color_expose = sprintf "#%06x", $cfg->{color_expose};
151 0 my $color_urgent_bg = sprintf "#%06x", $cfg->{color_urgent_bg};
152 0 my $color_urgent_fg = sprintf "#%06x", $cfg->{color_urgent_fg};
153 0 ▶ my ($font_name, $font_size) = $cfg->{font} =~ /(.+)\s+(\d+)$/ or die "Font: $cfg->{font} has invalid format";
Branch: 1 branch
BranchTrueFalseTotal
() = $X11::korgwm::Common::cfg->{"font"} =~ /(.+)\s+(\d+)$/ or die qq[Font: $X11::korgwm::Common::cfg->{"font"} has invalid format] 0 0 0
154  
155 0 my $css_provider = Gtk3::CssProvider->new();
156 0 my $css = qq[
157 * {
158 background-color: $color_bg;
159 border-radius: 0;
160 color: $color_fg;
161 font-family: $font_name;
162 font-size: ${font_size}pt;
163 }
164  
165 .expose, .expose > * {
166 background-color: $color_expose;
167 }
168  
169 .append {
170 background-color: $color_append_bg;
171 color: $color_append_fg;
172 }
173  
174 .urgent {
175 background-color: $color_urgent_bg;
176 color: $color_urgent_fg;
177 }
178  
179 .battery-low {
180 color: $color_battery_low;
181 }
182  
183 calendar {
184 border-color: $color_border;
185 border: $cfg->{border_width}px solid;
186 }
187  
188 calendar > * {
189 padding: 0 10px;
190 }
191  
192 calendar.header {
193 border-bottom: 0px;
194 }
195  
196 calendar:indeterminate {
197 color: alpha(currentColor, 0.3);
198 }
199  
200 .active, calendar:selected {
201 background-color: $color_fg;
202 color: $color_bg;
203 }
204 ];
205  
206 0 $css_provider->load_from_data($css);
207 0 Gtk3::StyleContext::add_provider_for_screen(
208 Gtk3::Gdk::Screen::get_default(),
209 $css_provider,
210 Gtk3::STYLE_PROVIDER_PRIORITY_APPLICATION()
211 );
212 0 $X11::korgwm::gtk_init = 1;
213 }
214  
215 1 1;
« korgwm/Panel.pm ↑ Summary korgwm/Xkb.pm »