Repository navigation
Expand file tree
/
Copy pathpreviews_daemon.vala
More file actions
727 lines (682 loc) · 25.7 KB
/
Copy pathpreviews_daemon.vala
File metadata and controls
727 lines (682 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
using Gtk;
using Cairo;
using Gdk;
/*
Budgie WindowPreviews
Author: Jacob Vlijm
Copyright © 2017 Ubuntu Budgie Developers
Website=https://ubuntubudgie.org
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see <https://www.gnu.org/licenses/>.
*/
// valac --pkg gtk+-3.0 --pkg gio-2.0 --pkg cairo --pkg gdk-x11-3.0 --pkg libwnck-3.0 -X "-D WNCK_I_KNOW_THIS_IS_UNSTABLE"
namespace NewPreviews {
int currtilindex;
bool no_windows;
int currws;
int maxcol;
bool allworkspaces;
bool showtooltips;
bool allapps;
Gtk.Button[] currbuttons;
string user;
File triggerdir;
File nexttrigger;
File allappstrigger;
File allappstriggerhotc;
File currapptriggerhotc;
bool allappshotc_trigger;
bool currapphotc_trigger;
bool prev_winexists;
File previoustrigger;
File triggercurrent;
bool ignore;
string filepath;
Gtk.Window previews_window;
string[] num_ids_fromdir;
FileMonitor monitor;
unowned Wnck.Screen wnck_scr;
unowned GLib.List<Wnck.Window> z_list;
Gdk.X11.Window timestamp_window;
string previewspath;
private uint get_now() {
// time stamp needs it
return Gdk.X11.get_server_time(timestamp_window);
}
private int get_stringindex (string[] arr, string lookfor) {
// get index of string in list
for (int i=0; i < arr.length; i++) {
if(lookfor == arr[i]) return i;
}
return -1;
}
public class PreviewsWindow : Gtk.Window {
Grid maingrid;
Grid currlast_startspacer;
Grid[] subgrids;
string[] win_workspaces;
string newpv_css = """
.windowbutton {
border-width: 2px;
border-color: #5A5A5A;
background-color: transparent;
padding: 4px;
border-radius: 1px;
-gtk-icon-effect: none;
border-style: solid;
transition: 0.1s linear;
}
.windowbutton:hover {
border-color: #E6E6E6;
background-color: transparent;
border-width: 1px;
padding: 6px;
border-radius: 1px;
border-style: solid;
}
.windowbutton:focus {
border-color: white;
background-color: transparent;
border-width: 2px;
padding: 3px;
}
.label {
color: white;
padding-bottom: 0px;
}
""";
private string[] get_devices() {
// get the fixed section of the function
string output = "";
try {
GLib.Process.spawn_command_line_sync(
Config.PACKAGE_BINDIR + "/xinput --list", out output
);
}
catch (Error e) {
//simply pass, nothing bad will happen on a single failure
}
string[] keyboard_data = output.split("\n");
string[] valid = {};
foreach (string s in keyboard_data) {
if (
!s.contains("Button") &&
!s.contains("Virtual") &&
!s.contains("pointer") &&
s.contains("id")) {
//device ids to check
string tocheck = s.split("=")[1].split("\t")[0];
valid += tocheck;
}
}
return valid;
}
private Grid create_hspacer(int extend = 0) {
// last row needs to be positioned, add to all boxes,
// only set width > 0 on the last
var spacegrid = new Gtk.Grid();
spacegrid.attach(new Gtk.Grid(), 0, 0, 1, 1);
spacegrid.attach(new Gtk.Grid(), 1, 0, 1, 1);
spacegrid.set_column_spacing(extend);
return spacegrid;
}
private void remove_button (Button button) {
// remove a button from the array of buttons
// to prevent browse errors
Button[] newbuttons = {};
foreach (Button b in currbuttons) {
if (b != button) {
newbuttons += b;
}
}
currbuttons = newbuttons;
}
private void set_closebuttonimg(Button button, string icon_name) {
// we don't like repeating
var newimage = new Gtk.Image.from_icon_name(
"budgie_wpreviews_" + icon_name,
Gtk.IconSize.BUTTON);
button.set_image(newimage);
}
private Grid makebuttongrid(
string imgpath, Image appicon, string windowname, string some_xid
) {
var subgrid = new Gtk.Grid();
subgrid.set_row_spacing(0);
// window image button
var button = new Gtk.Button();
if (showtooltips) {
button.set_tooltip_text(windowname);
}
button.set_size_request(280, 180);
var image = new Gtk.Image.from_file (imgpath);
button.set_image(image);
var st_ct = button.get_style_context();
st_ct.add_class("windowbutton");
st_ct.remove_class("image-button");
button.set_relief(Gtk.ReliefStyle.NONE);
button.clicked.connect (() => {
//raise_win(s)
activate_window(some_xid);
previews_window.destroy();
});
currbuttons += button;
subgrid.attach(button, 0, 1, 1, 1);
// box
Gtk.Box actionbar = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
subgrid.attach(actionbar, 0, 0, 1, 1);
// app icon
actionbar.pack_start(appicon, false, false, 0);
// window name
Label wname = new Label(windowname);
wname.set_ellipsize(Pango.EllipsizeMode.END);
wname.set_max_width_chars(22);
var label_ct = wname.get_style_context();
label_ct.add_class("label");
actionbar.pack_start(wname, false, false, 10);
// close X button and its behavior
var closebutton = new Gtk.Button();
set_closebuttonimg(closebutton, "grey_x");
closebutton.set_relief(Gtk.ReliefStyle.NONE);
closebutton.set_can_focus(false);
closebutton.enter_notify_event.connect (() => {
set_closebuttonimg(closebutton,
"white2_x"
);
return false;
});
closebutton.leave_notify_event.connect (() => {
set_closebuttonimg(closebutton,
"grey_x"
);
return false;
});
button.enter_notify_event.connect (() => {
set_closebuttonimg(closebutton,
"white_x"
);
return false;
});
button.leave_notify_event.connect (() => {
set_closebuttonimg(closebutton,
"grey_x"
);
return false;
});
actionbar.pack_end(closebutton, false, false, 0);
closebutton.clicked.connect (() => {
if (currbuttons.length == 1) {
this.destroy();
}
else {
remove_button(button);
subgrid.set_sensitive(false);
currtilindex = 0;
this.resize(100, 100);
}
close_window(some_xid);
});
return subgrid;
}
// new functions, looking up window from most recent Wnck stack
private Wnck.Window? get_wnckwindow_fromxid (string xid_str) {
z_list = wnck_scr.get_windows_stacked();
foreach (Wnck.Window w in z_list) {
string compare = w.get_xid().to_string();
if (compare == xid_str) {
return w;
}
}
return null;
}
private void close_window (string xid_str) {
Wnck.Window found_match = get_wnckwindow_fromxid (xid_str);
if (found_match != null) {
uint now = get_now();
found_match.close(now);
}
}
private void activate_window (string xid_str) {
Wnck.Window found_match = get_wnckwindow_fromxid (xid_str);
if (found_match != null) {
uint now = get_now();
found_match.activate(now);
}
}
private bool filter_wmclass (
Wnck.Window w, string? wm_class
) {
// if set, only allow current wm_class
if (allapps || wm_class == null) {
return true;
}
else {
string group = w.get_class_group_name();
if (group == wm_class) {
return true;
}
return false;
}
}
private bool filter_workspace (int windowspace, int currspace) {
// check windows on workspace if set in gsettings
if (allworkspaces) {
return true;
}
else {
if (windowspace == currspace) {
return true;
}
return false;
}
}
public void actonbrowsetrigger () {
// browse through tiles -only works if prv window exists-
if (nexttrigger.query_exists()) {
currtilindex += 1;
if (currtilindex == currbuttons.length) {
currtilindex = 0;
}
delete_file(nexttrigger);
if (currtilindex <= currbuttons.length &&
currtilindex >= 0) {
currbuttons[currtilindex].grab_focus();
}
}
else if (previoustrigger.query_exists()) {
currtilindex -= 1;
if (currtilindex < 0) {
currtilindex = currbuttons.length - 1;
}
delete_file(previoustrigger);
if (currtilindex <= currbuttons.length &&
currtilindex >= 0) {
currbuttons[currtilindex].grab_focus();
}
}
}
public PreviewsWindow () {
/*
safety" procedure to make sure the window doesn't stick on
reversed release Alt-Tab
*/
wnck_scr.active_window_changed.connect(check_ifactive);
if (!allappshotc_trigger && !currapphotc_trigger) {
string[] devices = get_devices();
GLib.Timeout.add (100, () => {
bool pressed = false;
foreach (string s in devices) {
string output = "";
try {
GLib.Process.spawn_command_line_sync(
Config.PACKAGE_BINDIR + "/xinput --query-state " + s, out output
);
}
catch (Error e) {
// just pass
}
pressed = output.contains("down");
if (pressed) {
break;
}
}
if (!pressed) {
if (currbuttons.length != 0) {
currbuttons[currtilindex].clicked();
}
return false;
}
return true;
});
}
// if nothing to show
no_windows = true;
this.set_default_size(200, 150);
this.set_decorated(false);
this.set_keep_above(true);
this.set_skip_taskbar_hint(true);
monitor.changed.connect(actonbrowsetrigger);
currbuttons = {};
currtilindex = 0;
// set initial numbers cols/rows etc.
int row = 1;
int col = 0;
// whole bunch of styling
var screen = this.get_screen();
this.set_app_paintable(true);
var visual = screen.get_rgba_visual();
this.set_visual(visual);
this.draw.connect(on_draw);
Gtk.CssProvider css_provider = new Gtk.CssProvider();
try {
css_provider.load_from_data(newpv_css);
Gtk.StyleContext.add_provider_for_screen(
screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER
);
}
catch (Error e) {
}
// create maingrid
maingrid = new Gtk.Grid();
maingrid.attach(new Label(""), 0, 0, 1, 1);
maingrid.attach(new Label("\n"), 100, 100, 1, 1);
maingrid.set_column_spacing(20);
maingrid.set_row_spacing(20);
// create arrays from dirlist ->
// window_id arr, path arr (which is dirlist), workspace arr
string[] currpreviews = previews(previewspath);
num_ids_fromdir = {};
foreach (string s in currpreviews) {
string[] fname = s.split("/");
string[] last_section = fname[fname.length - 1].split(".");
string win_workspace = last_section[1];
win_workspaces += win_workspace;
string found_xid = last_section[0];
num_ids_fromdir += found_xid;
}
z_list = wnck_scr.get_windows_stacked();
// watch out! window can be null -> wm_class can be null
string? wm_class = null;
Wnck.Window? curr_active = wnck_scr.get_active_window();
if (curr_active != null) {
wm_class = curr_active.get_class_group_name();
}
foreach (Wnck.Window w in z_list) {
string z_intid = w.get_xid().to_string(); // store in array
int dirlistindex = get_stringindex(num_ids_fromdir, z_intid);
if (
w.get_window_type() == Wnck.WindowType.NORMAL &&
dirlistindex != -1
) {
int window_on_workspace = int.parse(
win_workspaces[dirlistindex]
);
if (
filter_workspace(window_on_workspace, currws) &&
filter_wmclass(w, wm_class)
) {
no_windows = false;
string img_path = currpreviews[dirlistindex];
Pixbuf icon = w.get_mini_icon();
Image img = new Gtk.Image.from_pixbuf(icon);
string wname = w.get_name();
string some_xid = w.get_xid().to_string();
Grid newtile = makebuttongrid(img_path, img, wname, some_xid);
subgrids += newtile;
}
}
}
// reverse buttons
Button[] reversed_buttons = {};
int n_buttons = currbuttons.length;
while (n_buttons > 0) {
reversed_buttons += currbuttons[n_buttons - 1];
n_buttons -= 1;
}
currbuttons = reversed_buttons;
// reverse order of tiles
Grid[] reversed_tiles = {};
int n_tiles = subgrids.length;
while (n_tiles > 0) {
reversed_tiles += subgrids[n_tiles-1];
n_tiles -= 1;
}
// firstbox / row
var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 20);
// start spacer
box.pack_start(create_hspacer(), false, false, 0);
currlast_startspacer = create_hspacer();
foreach (Grid g in reversed_tiles) {
box.pack_start(g, false, false, 0);
col += 1;
if (col == maxcol) {
// end spacer previous one
box.pack_start(create_hspacer(), false, false, 0);
maingrid.attach(box, 1, row, 1);
box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 20);
currlast_startspacer = create_hspacer();
// start spacer new one
box.pack_start(currlast_startspacer, false, false, 0);
row += 1;
col = 0;
}
}
// add last box, align (tile width = 300px)
box.pack_start(create_hspacer(), false, false, 0);
maingrid.attach(box, 1, row, 1);
if (col != 0) {
int tofix = maxcol - col;
int add = tofix * 300 / 2;
currlast_startspacer.set_column_spacing (add);
}
if (reversed_buttons.length > 1) {
currtilindex = 1;
reversed_buttons[1].grab_focus();
}
this.title = "PreviewsWindow";
prev_winexists = true;
this.add(maingrid);
// lazy patch, but for a rare situation, should do
if (reversed_buttons.length == 0) {
this.destroy();
cleanup();
}
}
private void check_ifactive () {
Wnck.Window curr = wnck_scr.get_active_window();
if (curr.get_name() != "PreviewsWindow") {
this.destroy();
}
}
private bool on_draw (Widget da, Context ctx) {
// needs to be connected to transparency settings change
ctx.set_source_rgba(0.15, 0.15, 0.15, 0.85);
ctx.set_operator(Cairo.Operator.SOURCE);
ctx.paint();
ctx.set_operator(Cairo.Operator.OVER);
return false;
}
private string[] previews (string directory) {
// list the created preview images
string[] somestrings = {};
try {
var dr = Dir.open(directory);
string ? filename = null;
while ((filename = dr.read_name()) != null) {
string addpic = GLib.Path.build_filename(
directory, filename
);
somestrings += addpic;
}
}
catch (FileError err) {
stderr.printf(err.message);
}
return somestrings;
}
}
private void delete_file (File file) {
try {
file.delete();
}
catch (Error e) {
}
}
private void cleanup () {
// remove trigger files
delete_file(allappstrigger);
delete_file(allappstriggerhotc);
delete_file(triggercurrent);
delete_file(currapptriggerhotc);
ignore = false;
prev_winexists = false;
}
private bool close_onrelease(Gdk.EventKey k) {
// on releasing Alt_L, destroy previews, virtually click current
// button (connect is gone with destroying previews window)
string key = Gdk.keyval_name(k.keyval);
if (key == "Escape") {
previews_window.destroy();
}
if (key == "Alt_L" || key == "Meta_L") {
if (!no_windows) {
currbuttons[currtilindex].clicked();
}
else {
previews_window.destroy();
}
}
return true;
}
private void raise_previewswin(Wnck.Window newwin) {
// make sure new previews window is activated on creation
if (newwin.get_name() == "PreviewsWindow") {
uint timestamp = get_now();
newwin.activate(timestamp);
}
}
private void get_n_cols () {
// set number of columns, depending on screen width
Gdk.Monitor prim = Gdk.Display.get_default().get_primary_monitor();
var geo = prim.get_geometry();
int width = geo.width;
maxcol = width / 360;
}
private void update_currws () {
// keep track of current workspace
wnck_scr.force_update();
var currspace = wnck_scr.get_active_workspace();
unowned GLib.List<Wnck.Workspace> currspaces = wnck_scr.get_workspaces();
int n = 0;
foreach (Wnck.Workspace ws in currspaces) {
if (ws == currspace) {
currws = n;
break;
}
n += 1;
}
}
private void actonfile(File file, File? otherfile, FileMonitorEvent event) {
if (event == FileMonitorEvent.CREATED) {
bool allapps_trigger = allappstrigger.query_exists();
bool onlycurrent_trigger = triggercurrent.query_exists();
allappshotc_trigger = allappstriggerhotc.query_exists();
currapphotc_trigger = currapptriggerhotc.query_exists();
bool fromhotcorner = allappshotc_trigger || currapphotc_trigger;
if (fromhotcorner && prev_winexists) {
previews_window.destroy();
cleanup();
}
else if (
allapps_trigger || onlycurrent_trigger || allappshotc_trigger || currapphotc_trigger
) {
if (allappshotc_trigger || currapphotc_trigger) {
cleanup();
}
if (!ignore) {
if (allapps_trigger || allappshotc_trigger) {
allapps = true;
}
else {
allapps = false;
}
previews_window = new PreviewsWindow();
previews_window.destroy.connect(cleanup);
previews_window.key_release_event.connect(close_onrelease);
previews_window.set_position(Gtk.WindowPosition.CENTER_ALWAYS);
previews_window.show_all();
}
ignore = true;
}
else {
previews_window.destroy();
ignore = false;
}
}
}
private string get_filepath (string arg) {
// get path of current (executable) file
string[] steps = arg.split("/");
string[] trim_filename = steps[0:steps.length-1];
return string.joinv("/", trim_filename);
}
private void windowdaemon(string[] args) {
filepath = get_filepath (args[0]);
GLib.Settings previews_settings = new GLib.Settings(
"org.ubuntubudgie.budgie-wpreviews"
);
allworkspaces = previews_settings.get_boolean("allworkspaces");
showtooltips = previews_settings.get_boolean("showtooltips");
previews_settings.changed.connect (() => {
allworkspaces = previews_settings.get_boolean("allworkspaces");
showtooltips = previews_settings.get_boolean("showtooltips");
});
var tmp = Environment.get_tmp_dir() + "/";
triggerdir = File.new_for_path(tmp);
allappstrigger = File.new_for_path(
tmp.concat(user, "_prvtrigger_all")
);
allappstriggerhotc = File.new_for_path(
tmp.concat(user, "_prvtrigger_all_hotcorner")
);
currapptriggerhotc = File.new_for_path(
tmp.concat(user, "_prvtrigger_curr_hotcorner")
);
nexttrigger = File.new_for_path(
tmp.concat(user, "_nexttrigger")
);
previoustrigger = File.new_for_path(
tmp.concat(user, "_previoustrigger")
);
triggercurrent = File.new_for_path(
tmp.concat(user, "_prvtrigger_current")
);
// start with a clean plate please
cleanup();
// start the loop
// X11 stuff, non-dynamic part
unowned X.Window xwindow = Gdk.X11.get_default_root_xwindow();
unowned X.Display xdisplay = Gdk.X11.get_default_xdisplay();
Gdk.X11.Display display = Gdk.X11.Display.lookup_for_xdisplay(xdisplay);
timestamp_window = new Gdk.X11.Window.foreign_for_display(display, xwindow);
// monitoring files / dirs
try {
monitor = triggerdir.monitor(FileMonitorFlags.NONE, null);
monitor.changed.connect(actonfile);
}
catch (Error e) {
}
// monitoring wnck_screen & Display for n_columns
var gdk_screen = Gdk.Screen.get_default();
gdk_screen.monitors_changed.connect(get_n_cols);
get_n_cols();
// miscellaneous
wnck_scr = Wnck.Screen.get_default();
wnck_scr.active_workspace_changed.connect(update_currws);
update_currws();
wnck_scr.window_opened.connect(raise_previewswin);
// prevent cold start (not sure why, but it works)
previews_window = new PreviewsWindow();
previews_window.destroy();
}
public static void main (string[] args) {
user = Environment.get_user_name();
var tmp = Environment.get_tmp_dir() + "/";
previewspath = tmp.concat(user, "_window-previews");
try {
File file = File.new_for_commandline_arg (previewspath);
file.make_directory ();
} catch (Error e) {
// directory exists, nothing to do
}
Gtk.init(ref args);
NewPreviews.windowdaemon(args);
Gtk.main();
}
}