Skip to content

Commit f5359b0

Browse files
authored
Merge pull request #171 from Siborgium/collapse
Run nwggrid as daemon (#163)
2 parents eb60737 + 1f11731 commit f5359b0

30 files changed

+1596
-765
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
# Project files
3535
*.geany
36+
*.kdev4
3637

3738
subprojects/nlohmann_json
39+
subprojects/gtk-layer-shell-0
3840
builddir

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ latest release](https://github.com/nwg-piotr/nwg-launchers/releases/latest).
3838
- `meson` and `ninja` - build dependencies
3939
- `nlohmann-json` - will be downloaded as a subproject if not found on the system
4040
- `gtk-layer-shell` - optional, set to `auto` by default; will be downloaded as a subproject if explicitly enabled, but not found on the system
41+
- `librsvg` - optional, required to support SVG icons
4142

4243
### Building
4344

@@ -84,11 +85,27 @@ This only works with the `-p` argument:
8485

8586
[![Swappshot-Mon-Mar-23-205030-2020.th.png](https://scrot.cloud/images/2020/03/23/Swappshot-Mon-Mar-23-205030-2020.th.png)](https://scrot.cloud/image/jb3k) [![Swappshot-Mon-Mar-23-205157-2020.th.png](https://scrot.cloud/images/2020/03/23/Swappshot-Mon-Mar-23-205157-2020.th.png)](https://scrot.cloud/image/jOWg) [![Swappshot-Mon-Mar-23-205248-2020.th.png](https://scrot.cloud/images/2020/03/23/Swappshot-Mon-Mar-23-205248-2020.th.png)](https://scrot.cloud/image/joh5)
8687

88+
Starting with version 0.6.0 nwggrid can be run in server mode which drastically improves responsiveness.
89+
First, start a server with `nwggrid-server` command.
90+
When it's up and running, run `nwggrid -client` to show the grid.
91+
8792
### Usage
8893

8994
```
9095
$ nwggrid -h
91-
GTK application grid: nwggrid 0.4.1 (c) 2020 Piotr Miller, Sergey Smirnykh & Contributors
96+
GTK application grid: nwggrid 0.6.0 (c) 2021 Piotr Miller, Sergey Smirnykh & Contributors
97+
98+
Usage:
99+
nwggrid -client sends -SIGUSR1 to nwggrid-server, requires nwggrid-server running
100+
nwggrid [ARGS...] launches nwggrid-server -oneshot ARGS...
101+
102+
See also:
103+
nwggrid-server -h
104+
```
105+
106+
```
107+
$ nwggrid-server -h
108+
GTK application grid: nwggrid 0.6.0 (c) 2021 Piotr Miller, Sergey Smirnykh & Contributors
92109
93110
Options:
94111
-h show this help message and exit
@@ -102,6 +119,11 @@ Options:
102119
-c <name> css file name (default: style.css)
103120
-l <ln> force use of <ln> language
104121
-wm <wmname> window manager name (if can not be detected)
122+
-oneshot run in the foreground, exit when window is closed
123+
generally you should not use this option, use simply `nwggrid` instead
124+
[requires layer-shell]:
125+
-layer-shell-layer {BACKGROUND,BOTTOM,TOP,OVERLAY}, default: OVERLAY
126+
-layer-shell-exclusive-zone {auto, valid integer (usually -1 or 0)}, default: auto
105127
```
106128

107129
### Terminal applications
@@ -133,7 +155,7 @@ This command creates a horizontal or vertical button bar, out of a template file
133155

134156
```
135157
$ nwgbar -h
136-
GTK button bar: nwgbar 0.4.1 (c) Piotr Miller & Contributors 2020
158+
GTK button bar: nwgbar 0.6.0 (c) Piotr Miller & Contributors 2021
137159
138160
Options:
139161
-h show this help message and exit
@@ -146,6 +168,10 @@ Options:
146168
-b <background> background colour in RRGGBB or RRGGBBAA format (RRGGBBAA alpha overrides <opacity>)
147169
-s <size> button image size (default: 72)
148170
-wm <wmname> window manager name (if can not be detected)
171+
172+
[requires layer-shell]:
173+
-layer-shell-layer {BACKGROUND,BOTTOM,TOP,OVERLAY}, default: OVERLAY
174+
-layer-shell-exclusive-zone {auto, valid integer (usually -1 or 0)}, default: auto
149175
```
150176

151177
### Custom background
@@ -229,7 +255,7 @@ This program provides 2 commands:
229255

230256
```
231257
$ nwgdmenu -h
232-
GTK dynamic menu: nwgdmenu 0.4.1 (c) Piotr Miller & Contributors 2020
258+
GTK dynamic menu: nwgdmenu 0.6.0 (c) Piotr Miller & Contributors 2021
233259
234260
<input> | nwgdmenu - displays newline-separated stdin input as a GTK menu
235261
nwgdmenu - creates a GTK menu out of commands found in $PATH
@@ -244,7 +270,11 @@ Options:
244270
-o <opacity> background opacity (0.0 - 1.0, default 0.3)
245271
-b <background> background colour in RRGGBB or RRGGBBAA format (RRGGBBAA alpha overrides <opacity>)
246272
-wm <wmname> window manager name (if can not be detected)
247-
-run ignore stdin, always build from commands in $PATH(1)
273+
-run ignore stdin, always build from commands in $PATH
274+
275+
[requires layer-shell]:
276+
-layer-shell-layer {BACKGROUND,BOTTOM,TOP,OVERLAY}, default: OVERLAY
277+
-layer-shell-exclusive-zone {auto, valid integer (usually -1 or 0)}, default: auto
248278
249279
Hotkeys:
250280
Delete clear search box

bar/bar.cc

Lines changed: 101 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* GTK-based button bar
3-
* Copyright (c) 2020 Piotr Miller
3+
* Copyright (c) 2021 Piotr Miller
44
55
* Website: http://nwg.pl
66
* Project: https://github.com/nwg-piotr/nwg-launchers
@@ -14,7 +14,7 @@
1414
#include "bar.h"
1515

1616
const char* const HELP_MESSAGE =
17-
"GTK button bar: nwgbar " VERSION_STR " (c) Piotr Miller & Contributors 2020\n\n\
17+
"GTK button bar: nwgbar " VERSION_STR " (c) Piotr Miller & Contributors 2021\n\n\
1818
Options:\n\
1919
-h show this help message and exit\n\
2020
-v arrange buttons vertically\n\
@@ -31,114 +31,118 @@ Options:\n\
3131
-layer-shell-exclusive-zone {auto, valid integer (usually -1 or 0)}, default: auto\n";
3232

3333
int main(int argc, char *argv[]) {
34-
struct timeval tp;
35-
gettimeofday(&tp, NULL);
36-
long int start_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
37-
38-
create_pid_file_or_kill_pid("nwgbar");
39-
40-
InputParser input(argc, argv);
41-
if(input.cmdOptionExists("-h")){
42-
std::cout << HELP_MESSAGE;
43-
std::exit(0);
44-
}
34+
try {
35+
struct timeval tp;
36+
gettimeofday(&tp, NULL);
37+
long int start_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
38+
39+
InputParser input(argc, argv);
40+
if(input.cmdOptionExists("-h")){
41+
std::cout << HELP_MESSAGE;
42+
std::exit(0);
43+
}
4544

46-
auto background_color = input.get_background_color(0.9);
45+
auto background_color = input.get_background_color(0.9);
4746

48-
auto config_dir = get_config_dir("nwgbar");
49-
if (!fs::is_directory(config_dir)) {
50-
Log::info("Config dir not found, creating...");
51-
fs::create_directories(config_dir);
52-
}
47+
auto config_dir = get_config_dir("nwgbar");
48+
if (!fs::is_directory(config_dir)) {
49+
Log::info("Config dir not found, creating...");
50+
fs::create_directories(config_dir);
51+
}
5352

54-
auto app = Gtk::Application::create();
53+
auto app = Gtk::Application::create();
5554

56-
auto provider = Gtk::CssProvider::create();
57-
auto display = Gdk::Display::get_default();
58-
auto screen = display->get_default_screen();
59-
if (!provider || !display || !screen) {
60-
Log::error("Failed to initialize GTK");
61-
return EXIT_FAILURE;
62-
}
55+
auto provider = Gtk::CssProvider::create();
56+
auto display = Gdk::Display::get_default();
57+
auto screen = display->get_default_screen();
58+
if (!provider || !display || !screen) {
59+
Log::error("Failed to initialize GTK");
60+
return EXIT_FAILURE;
61+
}
6362

64-
BarConfig config {
65-
input,
66-
screen
67-
};
63+
BarConfig config {
64+
input,
65+
screen
66+
};
67+
68+
// default or custom template
69+
auto default_bar_file = config_dir / "bar.json";
70+
auto custom_bar_file = config_dir / config.definition_file;
71+
// copy default anyway if not found
72+
if (!fs::exists(default_bar_file)) {
73+
try {
74+
fs::copy_file(DATA_DIR_STR "/nwgbar/bar.json", default_bar_file, fs::copy_options::overwrite_existing);
75+
} catch (...) {
76+
Log::error("Failed copying default template");
77+
}
78+
}
6879

69-
// default or custom template
70-
auto default_bar_file = config_dir / "bar.json";
71-
auto custom_bar_file = config_dir / config.definition_file;
72-
// copy default anyway if not found
73-
if (!fs::exists(default_bar_file)) {
80+
ns::json bar_json;
7481
try {
75-
fs::copy_file(DATA_DIR_STR "/nwgbar/bar.json", default_bar_file, fs::copy_options::overwrite_existing);
76-
} catch (...) {
77-
Log::error("Failed copying default template");
82+
bar_json = json_from_file(custom_bar_file);
83+
} catch (...) {
84+
Log::error("Template file not found, using default");
85+
bar_json = json_from_file(default_bar_file);
7886
}
79-
}
87+
Log::info(bar_json.size(), " bar entries loaded");
8088

81-
ns::json bar_json;
82-
try {
83-
bar_json = json_from_file(custom_bar_file);
84-
} catch (...) {
85-
Log::error("Template file not found, using default");
86-
bar_json = json_from_file(default_bar_file);
87-
}
88-
Log::info(bar_json.size(), " bar entries loaded");
89-
90-
std::vector<BarEntry> bar_entries {};
91-
if (bar_json.size() > 0) {
92-
bar_entries = get_bar_entries(std::move(bar_json));
93-
}
94-
95-
Gtk::StyleContext::add_provider_for_screen(screen, provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
96-
{
97-
auto css_file = setup_css_file("nwgbar", config_dir, config.css_filename);
98-
provider->load_from_path(css_file);
99-
Log::info("Using css file \'", css_file, "\'");
100-
}
101-
auto icon_theme = Gtk::IconTheme::get_for_screen(screen);
102-
if (!icon_theme) {
103-
Log::error("Failed to load icon theme");
104-
return EXIT_FAILURE;
105-
}
106-
auto& icon_theme_ref = *icon_theme.get();
107-
auto icon_missing = Gdk::Pixbuf::create_from_file(DATA_DIR_STR "/nwgbar/icon-missing.svg");
108-
109-
BarWindow window{ config };
110-
window.set_background_color(background_color);
111-
112-
/* Create buttons */
113-
for (auto& entry : bar_entries) {
114-
Gtk::Image* image = app_image(icon_theme_ref, entry.icon, icon_missing, config.icon_size);
115-
auto& ab = window.boxes.emplace_back(std::move(entry.name),
116-
std::move(entry.exec),
117-
std::move(entry.icon));
118-
ab.set_image_position(Gtk::POS_TOP);
119-
ab.set_image(*image);
120-
}
89+
std::vector<BarEntry> bar_entries {};
90+
if (bar_json.size() > 0) {
91+
bar_entries = get_bar_entries(std::move(bar_json));
92+
}
12193

122-
int column = 0;
123-
int row = 0;
94+
Gtk::StyleContext::add_provider_for_screen(screen, provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
95+
{
96+
auto css_file = setup_css_file("nwgbar", config_dir, config.css_filename);
97+
provider->load_from_path(css_file);
98+
Log::info("Using css file \'", css_file, "\'");
99+
}
100+
IconProvider icon_provider {
101+
Gtk::IconTheme::get_for_screen(screen),
102+
config.icon_size
103+
};
104+
105+
BarWindow window{ config };
106+
window.set_background_color(background_color);
107+
108+
/* Create buttons */
109+
for (auto& entry : bar_entries) {
110+
auto image = Gtk::make_managed<Gtk::Image>(icon_provider.load_icon(entry.icon));
111+
auto& ab = window.boxes.emplace_back(std::move(entry.name),
112+
std::move(entry.exec),
113+
std::move(entry.icon));
114+
ab.set_image_position(Gtk::POS_TOP);
115+
ab.set_image(*image);
116+
}
124117

125-
window.grid.freeze_child_notify();
126-
for (auto& box : window.boxes) {
127-
window.grid.attach(box, column, row, 1, 1);
128-
if (config.orientation == Orientation::Vertical) {
129-
row++;
130-
} else {
131-
column++;
118+
int column = 0;
119+
int row = 0;
120+
121+
window.grid.freeze_child_notify();
122+
for (auto& box : window.boxes) {
123+
window.grid.attach(box, column, row, 1, 1);
124+
if (config.orientation == Orientation::Vertical) {
125+
row++;
126+
} else {
127+
column++;
128+
}
132129
}
133-
}
134-
window.grid.thaw_child_notify();
135-
window.show_all_children();
136-
window.show(hint::Fullscreen);
130+
window.grid.thaw_child_notify();
131+
Instance instance{ *app.get(), "nwgbar" };
132+
133+
window.show_all_children();
134+
window.show(hint::Fullscreen);
137135

138-
gettimeofday(&tp, NULL);
139-
long int end_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
136+
gettimeofday(&tp, NULL);
137+
long int end_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
140138

141-
Log::info("Time: ", end_ms - start_ms, "ms");
139+
Log::info("Time: ", end_ms - start_ms, "ms");
142140

143-
return app->run(window);
141+
return app->run(window);
142+
} catch (const Glib::FileError& error) {
143+
Log::error(error.what());
144+
} catch (const std::runtime_error& error) {
145+
Log::error(error.what());
146+
}
147+
return EXIT_FAILURE;
144148
}

bar/bar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* GTK-based button bar
2-
* Copyright (c) 2020 Piotr Miller
2+
* Copyright (c) 2021 Piotr Miller
33
44
* Website: http://nwg.pl
55
* Project: https://github.com/nwg-piotr/nwg-launchers

bar/bar_classes.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* GTK-based button bar
2-
* Copyright (c) 2020 Piotr Miller
2+
* Copyright (c) 2021 Piotr Miller
33
44
* Website: http://nwg.pl
55
* Project: https://github.com/nwg-piotr/nwg-launchers

bar/bar_tools.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* GTK-based application grid
2-
* Copyright (c) 2020 Piotr Miller
2+
* Copyright (c) 2021 Piotr Miller
33
44
* Website: http://nwg.pl
55
* Project: https://github.com/nwg-piotr/nwg-launchers

bar/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ executable(
1414
)
1515

1616
install_data(
17-
['style.css', 'bar.json', 'icon-missing.svg'],
17+
['style.css', 'bar.json'],
1818
install_dir: conf_data.get('datadir', '') / 'nwgbar'
1919
)

0 commit comments

Comments
 (0)