korgwm/Executor.pm untested

filter: Statementstmt 0.0% 0 / 258 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 419 CC 2 SCAR 9.4
CC
2
Cov
0%
parse
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.6
1 #!/usr/bin/perl
2 # made by: KorG
3 # vim: cc=119 et sw=4 ts=4 :
4 package X11::korgwm::Executor;
5 use strict;
6 use warnings;
7 use feature 'signatures';
8  
9 use POSIX qw( setsid );
10 use List::Util qw( any );
11 use X11::XCB ':all';
12 use X11::korgwm::Common;
13  
14 # Implementation of all the commands (unless some module push here additional funcs)
15 our @parser = (
16 # nop
17 [qr/nop[wlq]?\((.*)\)/, sub ($arg) { return sub { 1 }}],
18  
19 # Exec command
20 [qr/exec\((.+)\)/, sub ($arg) { return sub {
21 my $pid = fork;
22 die "Cannot fork(2)" unless defined $pid;
23 return if $pid;
24 close $_ for *STDOUT, *STDERR, *STDIN;
25  
26 # Intentionally ignoring open(2) errors
27 open STDIN, "+<", "/dev/null";
28 open STDOUT, ">&STDIN";
29 open STDERR, ">&STDIN";
30  
31 # Should always succeed after fork(2)
32 setsid();
33 exec $arg;
34 die "Cannot execute $arg";
35 }}],
36  
37 # Switch to a marked window
38 [qr/mark_switch_window\(\)/, sub ($arg) { return sub {
39 &X11::korgwm::Hotkeys::grab_key(sub ($data) {
40 # $mask is ignored for now, but it is still available
41 my ($key, $mask) = @$data;
42 return unless defined $key;
43  
44 my $win = $marked_windows{$key};
45 return S_DEBUG(5, "Cannot find any window with mark " . chr($key)) unless defined $win;
46  
47 DEBUG5 and carp "Switching to a window $win with key " . chr($key);
48 $win->select();
49 });
50 }}],
51  
52 # Mark a focused window with some character
53 [qr/mark_window\(\)/, sub ($arg) { return sub {
54 &X11::korgwm::Hotkeys::grab_key(sub ($data) {
55 # $mask is ignored for now, but it is still available
56 my ($key, $mask) = @$data;
57 return unless defined $key and defined $focus->{window};
58  
59 DEBUG4 and carp "Marking window $focus->{window} with key " . chr($key);
60 $marked_windows{$key} = $focus->{window};
61 });
62 }}],
63  
64 # Set active tag
65 [qr/tag_select\((\d+)\)/, sub ($arg) { return sub {
66 # Prevent pointer events
67 prevent_focus_in();
68 prevent_enter_notify();
69  
70 $focus->{screen}->tag_set_active($arg - 1);
71 $focus->{screen}->refresh();
72 $X->flush();
73 }}],
74  
75 # Append windows from other tag
76 [qr/tag_append\((\d+)\)/, sub ($arg) { return sub {
77 return unless $arg > 0;
78 my $screen = $focus->{screen};
79 my $tag = $screen->current_tag();
80 my $other = $screen->{tags}->[ $arg - 1 ];
81 $tag->append($other);
82 $screen->refresh();
83 $X->flush();
84 }}],
85  
86 # Window close or toggle floating / maximize / always_on
87 [qr/win_(close|toggle_(?:floating|maximize|always_on|pinned))\(\)/, sub ($arg) { return sub {
88 my $win = $focus->{window};
89 return unless defined $win;
90  
91 # Call relevant function
92 $arg eq "close" ? $win->close() :
93 $arg eq "toggle_floating" ? $win->toggle_floating() :
94 $arg eq "toggle_maximize" ? $win->toggle_maximize(2) :
95 $arg eq "toggle_always_on" ? $win->toggle_always_on() :
96 $arg eq "toggle_pinned" ? $win->toggle_pinned() :
97 croak "Unknown win_toggle_$arg function called" ;
98  
99 $focus->{screen}->refresh();
100 $X->flush();
101 }}],
102  
103 # Window move to a particular tag
104 [qr/win_move_tag\((\d+)\)/, sub ($arg) { return sub {
105 my $win = $focus->{window};
106 return unless defined $win;
107  
108 return if $win->{always_on};
109  
110 my ($screen, @multiple) = $win->screens();
111 croak "win_move_tag() not implemented for windows on multiple screens" if @multiple;
112  
113 my $new_tag = $screen->{tags}->[$arg - 1] or return;
114 my $curr_tag = $screen->current_tag();
115 return if $new_tag == $curr_tag;
116  
117 $win->hide(); # always from visible tag to invisible
118 $new_tag->win_add($win);
119 $curr_tag->win_remove($win, 1);
120  
121 # Follow the window if required
122 if ($cfg->{move_follow}) {
123 # Prevent unwanted events
124 prevent_enter_notify();
125 prevent_focus_in();
126  
127 $screen->{focus} = $win;
128 $screen->tag_set_active($new_tag->{idx}, rotate => 0);
129 }
130  
131 $screen->refresh();
132 $X->flush();
133  
134 $win->warp_pointer() if $cfg->{move_follow};
135 }}],
136  
137 # Set active screen
138 [qr/screen_select\((\d+)\)/, sub ($arg) { return sub {
139 my $dst = $arg; # to avoid source sub corruption
140 while ($dst > 1) {
141 return $screens[$dst - 1]->set_active() if defined $screens[$dst - 1];
142 $dst--;
143 }
144 croak "No screens found" unless defined $screens[0];
145 $screens[0]->set_active();
146 }}],
147 [qr/iddqd|idkfa/i, sub { print "I love KorG!\n" }],
148  
149 # Window move to particular screen
150 [qr/win_move_screen\((\d+)\)/, sub ($arg) { return sub {
151 my $win = $focus->{window};
152 return unless defined $win;
153  
154 my $new_screen = $screens[$arg - 1] or return;
155 my ($old_screen, @multiple) = $win->screens();
156 croak "win_move_screen() not implemented for windows on multiple screens" if @multiple;
157 return if $new_screen == $old_screen;
158 return if $new_screen->current_tag->{max_window} and $win->{maximized};
159  
160 prevent_enter_notify();
161  
162 my $always_on = $win->{always_on};
163 $old_screen->win_remove($win, 1);
164 $new_screen->win_add($win, $always_on);
165  
166 # Follow focus
167 $new_screen->{focus} = $win;
168 $focus->{screen} = $new_screen;
169  
170 $win->floating_move_screen($old_screen, $new_screen);
171  
172 $old_screen->refresh();
173 $new_screen->set_active($win);
174 $X->flush();
175 }}],
176  
177 # Focus previous window (screen independent)
178 [qr/focus_prev\(\)/, sub ($arg) { return sub {
179 my $win = focus_prev_get();
180 return unless defined $win;
181  
182 $win->select();
183 }}],
184  
185 # Cycle focus
186 [qr/focus_cycle\((.+)\)/, sub ($arg) { return sub {
187 my $tag = $focus->{screen}->current_tag();
188 my $win = $tag->next_window($arg eq "backward");
189 return unless defined $win;
190 prevent_enter_notify();
191 $win->focus();
192 }}],
193  
194 # Focus move
195 [qr/focus_move\(([hjkl])\)/, sub ($arg) { return sub {
196 my $win = $focus->{window};
197 return unless defined $win;
198  
199 my $new = $win->win_by_direction({ h => "left", j => "down", k => "up", l => "right" }->{$arg});
200 return unless defined $new;
201 $new->focus();
202 $new->warp_pointer() if $cfg->{mouse_follow};
203 }}],
204  
205 # Focus swap
206 [qr/focus_swap\(([hjkl])\)/, sub ($arg) { return sub {
207 my $win = $focus->{window};
208 return unless defined $win;
209  
210 my $new = $win->win_by_direction({ h => "left", j => "down", k => "up", l => "right" }->{$arg});
211 return unless defined $new;
212 $win->swap($new);
213 }}],
214  
215 # Change the layout
216 [qr/layout_set\((\w+\s*(,\s*\d+)?)\)/, sub ($arg) { return sub {
217 my ($func, $reverse) = split /\s*,\s*/, $arg;
218 my $tag = $focus->{screen}->current_tag();
219 $tag->layout_set($func, $reverse) or return;
220 $tag->show();
221 }}],
222  
223 # Resize the layout
224 [qr/layout_resize\(([hjkl])\)/, sub ($arg) { return sub {
225 my $win = $focus->{window};
226 return unless defined $win and not $win->{floating};
227  
228 my ($delta_x, $delta_y) =
229 $arg eq "h" ? (-0.1, 0) :
230 $arg eq "j" ? (0, 0.1) :
231 $arg eq "k" ? (0, -0.1) :
232 $arg eq "l" ? (0.1, 0) : (0, 0);
233  
234 # Select proper tag; croak if this window belongs to multiple tags
235 my @visible_tags = $win->tags_visible();
236 croak "Resizing layout for windows on several visible tags is not implemented" if @visible_tags > 1;
237 return unless @visible_tags; # ignore requests for invisibe windows
238 my $tag = $visible_tags[0];
239  
240 # Call actual resize
241 $tag->{layout}->resize(0 + @{ $tag->{windows_tiled} }, @{ $win }{qw( real_i real_j )}, $delta_x, $delta_y);
242 $tag->show();
243 $win->warp_pointer();
244 }}],
245  
246 # Expose windows
247 [qr/expose\(\)/, sub ($arg) { return sub {
248 &X11::korgwm::Expose::expose();
249 }}],
250  
251 # Open the calendar
252 [qr/toggle_calendar\(\)/, sub ($arg) { return sub {
253 my $screen = $focus->{screen} // return carp "Focus screen undefined";
254 my $ebox = $screen->{panel}->{"_ebox:clock"} // return carp "_ebox:clock undefined";
255 my $win = $ebox->get_window() // return carp "Can't find relevant Panel window";
256 my $display = $win->get_display() // return carp "Can't find Panel display";
257 my $device_manager = $display->get_device_manager() // return carp "Can't find device manager";
258 my $device = $device_manager->get_client_pointer() // return carp "Can't find mouse pointer device";
259  
260 my $event = Gtk3::Gdk::Event->new('button-press');
261 $event->button(Gtk3::Gdk::BUTTON_PRIMARY());
262 $event->window($win);
263 $event->send_event(Gtk3::true());
264 $event->device($device);
265 Gtk3::main_do_event($event);
266 }}],
267  
268 # Make some window urgent by class
269 [qr/urgent_by_class\((.+)\)/, sub ($arg) { return sub {
270 &X11::korgwm::Window::urgent_by_class($arg);
271 }}],
272  
273 # Resize the layout from API
274 [qr/layout_resize\((\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*-?0\.\d+\s*,\s*-?0\.\d+)\)/, sub ($arg) { return sub {
275 my ($arg_screen, $arg_tag, $arg_i, $arg_j, $arg_delta_x, $arg_delta_y) = split /\s*,\s*/, $arg;
276 my $screen = $screens[$arg_screen] // return;
277 my $tag = $screen->{tags}->[$arg_tag] // return;
278 $tag->{layout}->resize(0 + @{ $tag->{windows_tiled} }, $arg_i, $arg_j, $arg_delta_x, $arg_delta_y);
279 $tag->show();
280 }}],
281  
282 # Exit from WM
283 [qr/exit\(\)/, sub ($arg) { return sub {
284 $X11::korgwm::exit_trigger = "I can see, friends, we're in for a fabulous evening's apocalypse!";
285 }}],
286 );
287  
288 # Define some debug internals
289 DEBUG_API and push @parser,
290 [qr/dump_windows\(\)/, sub ($arg) { return sub ($hdl) {
291 {
292 require Data::Dumper;
293 local $Data::Dumper::Sortkeys = 1;
294 local $Data::Dumper::Maxdepth = 3;
295 $hdl->push_write(Data::Dumper::Dumper($windows));
296 }
297 }}],
298 [qr/dump_screen\((\d+)\)/, sub ($arg) { return sub ($hdl) {
299 {
300 my $screen = $screens[$arg];
301 return $hdl->push_write("No such screen: $arg\n") unless $screen;
302 require Data::Dumper;
303 local $Data::Dumper::Sortkeys = 1;
304 $hdl->push_write(Data::Dumper::Dumper($screen));
305 }
306 }}],
307 [qr/dump_screens\(\)/, sub ($arg) { return sub ($hdl) {
308 {
309 require Data::Dumper;
310 local $Data::Dumper::Sortkeys = 1;
311 local $Data::Dumper::Maxdepth = 3;
312 $hdl->push_write(Data::Dumper::Dumper(\@screens));
313 }
314 }}],
315 [qr/dump_tag\((\d+\s*,\s*\d+)\)/, sub ($arg) { return sub ($hdl) {
316 {
317 my ($arg_screen, $arg_tag) = split /\s*,\s*/, $arg;
318 my $screen = $screens[$arg_screen];
319 return $hdl->push_write("No such screen: $arg_screen\n") unless $screen;
320 my $tag = $screen->{tags}->[$arg_tag];
321 return $hdl->push_write("No such tag: $arg_tag\n") unless $tag;
322 require Storable;
323 require Data::Dumper;
324 local $Data::Dumper::Sortkeys = 1;
325 my $ttag = Storable::dclone($tag);
326 $ttag->{screen} = "<truncated screen> " . $tag->{screen}->{id};
327 $hdl->push_write(Data::Dumper::Dumper($ttag));
328 }
329 }}],
330 ;
331  
332 # Parse $cmd and return corresponding \&sub
333 sub parse($cmd) {
334 for my $known (@parser) {
335 return $known->[1]->($1) if $cmd =~ m{^$known->[0]$}s;
336 }
337 croak "Don't know how to parse $cmd";
338 }
339  
340 1;