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
|
#include "common.hh"
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <thread>
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_keysyms.h>
#include "animation.hh"
#include "animator.hh"
#include "args.hh"
#include "blissful_monitor.hh"
#include "cairo.hh"
#include "fake_monitor.hh"
#include "io.hh"
#include "monitor.hh"
#include "monmon.hh"
#include "pango.hh"
#include "poll_looper.hh"
#include "x.hh"
namespace {
class IceccMonMon : public MonMon, virtual Monitor::Observer {
public:
IceccMonMon(std::shared_ptr<PollLooper> const& looper, unsigned columns)
: MonMon(looper), connected_(false), max_jobs_(0), jobs_(0), requests_(0),
force_columns_(columns) {
}
void connect(Args const* args) {
std::unique_ptr<Monitor> monitor;
#if FAKE_MONITOR
monitor = FakeMonitor::create(looper_);
#else
monitor = Monitor::create(looper_);
#endif
monitor_ = BlissfulMonitor::create(looper_, std::move(monitor));
monitor_->add_observer(this);
monitor_->connect(args->arg("network", ""),
args->arg("scheduler", ""));
}
protected:
void width_changed() override {
job_pattern_.reset();
}
#if FAKE_MONITOR
void do_toggle_fakes() override {
monitor_->toggle_fakes();
}
#endif
private:
class MachineAnimation;
struct Machine : virtual Animator::AnimationObserver {
uint32_t id;
Monitor::Machine data;
unsigned jobs;
unsigned requests;
double x;
std::shared_ptr<MachineAnimation> animation;
explicit Machine(uint32_t id)
: id(id), jobs(0), requests(0), x(0.0) {
}
Machine(Machine const&) = delete;
Machine& operator=(Machine const&) = delete;
~Machine() override {
assert(!animation);
}
bool operator<(Machine const& machine) const {
if (data.name < machine.data.name) return true;
if (data.name > machine.data.name) return false;
return id < machine.id;
}
void ticked(Animator*, Animation*, double value) override {
x = value;
}
void stopped(Animator*, Animation*) override {
x = static_cast<double>(jobs) / data.max_jobs;
animation.reset();
}
};
class MachineAnimation : public Animation {
public:
explicit MachineAnimation(Machine* machine)
: machine_(machine), last_(0.0) {
}
bool tick(double duration, double* value) override {
auto target = static_cast<double>(machine_->jobs)
/ machine_->data.max_jobs;
if (duration == 0.0) {
last_ = duration;
*value = machine_->x;
return machine_->x != target;
} else {
double diff = target - machine_->x;
double max = (duration - last_) * .5;
last_ = duration;
if (fabs(diff) > max) {
diff = diff < 0.0 ? -max : max;
*value = machine_->x + diff;
return true;
} else {
*value = target;
return false;
}
}
}
private:
Machine* machine_;
double last_;
};
void state(Monitor*, Monitor::State state) override {
switch (state) {
case Monitor::SEARCHING:
connected_ = false;
stop_all_animations();
machines_.clear();
max_jobs_ = 0;
jobs_ = 0;
requests_ = 0;
if (!wnd_) return;
draw();
break;
case Monitor::CONNECTED:
connected_ = true;
draw();
break;
}
}
void stop_all_animations() override {
if (!animator_) return;
for (auto& machine : machines_) {
if (machine->animation) animator_->stop(machine->animation.get());
}
}
void internal_quit() override {
monitor_->disconnect();
}
void update(Machine* machine) {
auto old = machine->data.max_jobs;
machine->data = monitor_->machine(machine->id);
if (old < machine->data.max_jobs) {
max_jobs_ += machine->data.max_jobs - old;
if (machine->jobs) animate(machine);
} else if (old > machine->data.max_jobs) {
max_jobs_ -= old - machine->data.max_jobs;
if (machine->jobs) animate(machine);
}
}
void draw_content(cairo_t* cairo, PangoLayout* layout, uint16_t w, uint16_t h)
override {
auto const pad_x = std::min(9.0, w / 20.0), pad_y = pad_x;
auto const margin_x = std::min(7.5, w / 20.0), margin_y = margin_x;
auto y = pad_y;
auto const columns = force_columns_ ? force_columns_ : std::max(1, w / h);
auto const box_width = (w - pad_x) / columns - pad_x;
auto const box_height = box_height_ + margin_y * 2;
if (!job_pattern_) {
job_pattern_.reset(cairo_pattern_create_linear(0.0, 0.0, box_width, 0.0));
cairo_pattern_add_color_stop_rgb(job_pattern_.get(), 0.000000,
0.000000, 0.000000, 0.000000);
cairo_pattern_add_color_stop_rgb(job_pattern_.get(), 0.594324,
0.729412, 0.000000, 0.000000);
cairo_pattern_add_color_stop_rgb(job_pattern_.get(), 0.809683,
1.000000, 0.545098, 0.196078);
cairo_pattern_add_color_stop_rgb(job_pattern_.get(), 0.899833,
0.972549, 0.937255, 0.074510);
cairo_pattern_add_color_stop_rgb(job_pattern_.get(), 1.000000,
0.976471, 0.968627, 0.831373);
}
pango_layout_set_width(layout,
(box_width - margin_x * 2) * PANGO_SCALE);
unsigned col = 0;
for (auto const& machine : machines_) {
pango_layout_set_text(layout, machine->data.name.data(),
machine->data.name.size());
auto x = pad_x + col * (box_width + pad_x);
rounded_path(cairo, x, y, box_width, box_height);
cairo_set_source_rgba(cairo, 0.1, 0.1, 0.1, 0.7);
if (machine->x > 0.0) {
cairo_fill_preserve(cairo);
auto old = cairo::unique_path(cairo_copy_path(cairo));
cairo_new_path(cairo);
cairo_rectangle(cairo, x, y, machine->x * box_width, box_height);
cairo_clip(cairo);
cairo_append_path(cairo, old.get());
cairo_matrix_t matrix;
cairo_matrix_init_translate(&matrix, -x, 0);
cairo_pattern_set_matrix(job_pattern_.get(), &matrix);
cairo_set_source(cairo, job_pattern_.get());
cairo_fill(cairo);
cairo_reset_clip(cairo);
} else {
cairo_fill(cairo);
}
if (machine->requests > 0) {
auto radius = box_height / 10.0;
cairo_set_line_width(cairo, 1.5);
cairo_new_path(cairo);
cairo_move_to(cairo, x + box_width - radius, y);
cairo_rel_line_to(cairo,
-(machine->requests * (box_width - radius * 2))
/ requests_, 0);
cairo_set_source_rgb(cairo, 0, 0.6, 0);
cairo_stroke(cairo);
}
cairo_move_to(cairo, x + margin_x, y + margin_y);
pango_cairo_layout_path(cairo, layout);
cairo_set_source_rgb(cairo, 0.0, 0.0, 0.0);
cairo_set_line_cap(cairo, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width(cairo, 2.0);
cairo_stroke(cairo);
cairo_set_source_rgb(cairo, 1.0, 1.0, 1.0);
cairo_move_to(cairo, x + margin_x, y + margin_y);
pango_cairo_show_layout(cairo, layout);
if (++col == columns) {
col = 0;
y += box_height + pad_y * 2;
if (y >= h) break;
}
}
}
void added_machine(Monitor*, uint32_t id) override {
auto machine = std::make_unique<Machine>(id);
update(machine.get());
machines_.emplace(
std::lower_bound(machines_.begin(), machines_.end(), machine,
compare_machine),
std::move(machine));
draw();
}
static bool compare_machine(std::unique_ptr<Machine> const& m1,
std::unique_ptr<Machine> const& m2) {
return *m1 < *m2;
}
void updated_machine(Monitor*, uint32_t id) override {
for (auto& machine : machines_) {
if (machine->id == id) {
auto old = machine->data.name;
update(machine.get());
if (machine->data.name != old) {
// TODO: Perhaps remove and insert instead?
std::sort(machines_.begin(), machines_.end(), compare_machine);
}
draw();
return;
}
}
assert(false);
}
void removed_machine(Monitor*, uint32_t id) override {
for (auto it = machines_.begin(); it != machines_.end(); ++it) {
if ((*it)->id == id) {
max_jobs_ -= (*it)->data.max_jobs;
requests_ -= (*it)->requests;
jobs_ -= (*it)->jobs;
if (animator_) animator_->stop((*it)->animation.get());
machines_.erase(it);
draw();
return;
}
}
assert(false);
}
void added_job(Monitor*, uint32_t source, uint32_t target) override {
if (source == target) {
for (auto& machine : machines_) {
if (machine->id == source) {
++machine->jobs;
animate(machine.get());
}
}
} else {
for (auto& machine : machines_) {
if (machine->id == source) {
++machine->requests;
}
if (machine->id == target) {
++machine->jobs;
animate(machine.get());
}
}
++requests_;
}
++jobs_;
draw();
}
void removed_job(Monitor*, uint32_t source, uint32_t target) override {
if (source == target) {
for (auto& machine : machines_) {
if (machine->id == source) {
--machine->jobs;
animate(machine.get());
}
}
} else {
for (auto& machine : machines_) {
if (machine->id == source) {
--machine->requests;
}
if (machine->id == target) {
--machine->jobs;
animate(machine.get());
}
}
--requests_;
}
--jobs_;
draw();
}
void animate(Machine* machine) {
if (!animator_) return;
if (machine->animation) return;
machine->animation = std::make_unique<MachineAnimation>(machine);
animator_->start(machine->animation, machine);
}
std::unique_ptr<Monitor> monitor_;
bool connected_;
std::vector<std::unique_ptr<Machine>> machines_;
unsigned max_jobs_;
unsigned jobs_;
unsigned requests_;
cairo::unique_pattern job_pattern_;
unsigned force_columns_;
};
struct XcbKeySymbolsDelete {
void operator()(xcb_key_symbols_t* ptr) const {
if (ptr) xcb_key_symbols_free(ptr);
}
};
struct XcbGenericEventDelete {
void operator()(xcb_generic_event_t* ptr) const {
free(ptr);
}
};
void xcb_event_loop(x::shared_connection const& conn,
std::shared_ptr<x::Atoms> const& atoms,
std::shared_ptr<MonMon> const& monmon) {
std::unique_ptr<xcb_key_symbols_t, XcbKeySymbolsDelete> syms;
syms.reset(xcb_key_symbols_alloc(conn.get()));
std::unique_ptr<xcb_generic_event_t, XcbGenericEventDelete> event;
auto const quit = atoms->get("__MONMON_QUIT");
auto const wm_protocols = atoms->get("WM_PROTOCOLS");
auto const wm_delete_window = atoms->get("WM_DELETE_WINDOW");
while (true) {
event.reset(xcb_wait_for_event(conn.get()));
if (!event) {
std::cerr << "fatal error: "
<< xcb_connection_has_error(conn.get()) << std::endl;
monmon->quit_from_xcb();
return;
}
if (event->response_type == 0) {
auto e = reinterpret_cast<xcb_generic_error_t*>(event.get());
#ifndef NDEBUG
std::cerr << "error: " << xcb_event_get_error_label(e->error_code)
<< std::endl;
#endif
continue;
}
switch (XCB_EVENT_RESPONSE_TYPE(event)) {
case XCB_EXPOSE: {
auto e = reinterpret_cast<xcb_expose_event_t*>(event.get());
if (monmon->match(e->window)) {
monmon->expose(e->x, e->y, e->width, e->height);
}
if (e->count != 0) continue; // Only flush for e->count == 0
break;
}
case XCB_KEY_PRESS: {
auto e = reinterpret_cast<xcb_key_press_event_t*>(event.get());
auto sym = xcb_key_press_lookup_keysym(syms.get(), e, 0);
switch (sym) {
case 'Q':
case 'q':
case 0xff1b: { // escape
if (monmon->match(e->event)) {
monmon->quit_from_xcb();
return;
}
break;
}
#if FAKE_MONITOR
case ' ':
if (monmon->match(e->event)) {
monmon->toggle_fakes();
}
break;
#endif
}
break;
}
case XCB_DESTROY_NOTIFY: {
auto e = reinterpret_cast<xcb_destroy_notify_event_t*>(event.get());
if (monmon->match(e->window)) {
monmon->quit_from_xcb();
return;
}
break;
}
case XCB_CONFIGURE_NOTIFY: {
auto e = reinterpret_cast<xcb_configure_notify_event_t*>(event.get());
if (XCB_EVENT_SENT(e) && monmon->match(e->window)) {
monmon->configure(e->x, e->y, e->width, e->height);
}
break;
}
case XCB_CLIENT_MESSAGE: {
auto e = reinterpret_cast<xcb_client_message_event_t*>(event.get());
if (e->format == 32
&& e->type == wm_protocols
&& e->data.data32[0] == wm_delete_window) {
if (monmon->match(e->window)) {
monmon->quit_from_xcb();
return;
}
} else if (e->format == 32 && e->type == quit) {
// Do not call quit_from_xcb() here, only reason to end up
// here is if main already called quit_from_main()
return;
}
break;
}
case XCB_MAPPING_NOTIFY: {
auto e = reinterpret_cast<xcb_mapping_notify_event_t*>(event.get());
xcb_refresh_keyboard_mapping(syms.get(), e);
break;
}
case XCB_REPARENT_NOTIFY: {
auto e = reinterpret_cast<xcb_reparent_notify_event_t*>(event.get());
if (monmon->match(e->window)) {
monmon->update_desktop_window();
}
break;
}
case XCB_PROPERTY_NOTIFY: {
auto e = reinterpret_cast<xcb_property_notify_event_t*>(event.get());
monmon->update_desktop_window(e->window, e->atom);
break;
}
}
xcb_flush(conn.get());
}
}
} // namespace
int main(int argc, char** argv) {
std::unique_ptr<Args> args(Args::create());
args->add("network", "NETNAME",
"monitor NETNAME instead of default ICECREAM");
args->add("scheduler", "HOST",
"connect to scheduler at HOST instead of broadcasting");
args->add("display", "HOST:VS", "connect to X server at [HOST]:[VS]");
args->add("no-render", "do not use render extension even if available");
args->add("titlebar", "create a normal window instead of the default without"
" titlebar (or other WM addons)");
args->add("black", "use black background instead of background pixmap");
args->add("columns", "COLUMNS", "force columns to be COLUMNS instead of"
" calculated from window size");
args->add("help", "display this text and exit.");
if (!args->run(argc, argv)) {
std::cout << "Try `monmon --help` for usage." << std::endl;
return EXIT_FAILURE;
}
if (args->is_set("help")) {
std::cout << "Usage: `monmon [OPTIONS...]`\n"
<< "icecream (icecc) monitor\n"
<< "\n";
args->print_help(std::cout);
return EXIT_FAILURE;
}
unsigned columns = 0;
{
auto tmp = args->arg("columns", nullptr);
if (tmp) {
char* end = nullptr;
errno = 0;
columns = strtoul(tmp, &end, 10);
if (errno || columns == 0 || *end) {
std::cerr << "Invalid value for columns: '" << tmp << "'" << std::endl;
return EXIT_FAILURE;
}
}
}
int screen_index;
x::shared_connection conn(
xcb_connect(args->arg("display", nullptr), &screen_index));
if (xcb_connection_has_error(conn.get())) {
std::cerr << "Unable to connect to X server." << std::endl;
return EXIT_FAILURE;
}
x::prefetch_extensions(conn.get());
auto screen = x::get_screen(conn.get(), screen_index);
auto format = x::get_best_format(conn.get(), screen,
!args->is_set("no-render"));
if (!format.good()) {
std::cerr << "Unable to find a good X visual." << std::endl;
return EXIT_FAILURE;
}
std::shared_ptr<PollLooper> looper(PollLooper::create());
std::shared_ptr<x::Atoms> atoms(x::Atoms::create(conn));
atoms->preload("WM_DELETE_WINDOW");
atoms->preload("WM_PROTOCOLS");
atoms->preload("__MONMON_QUIT");
MonMon::preload(atoms.get());
std::shared_ptr<x::Ewmh> ewmh(x::Ewmh::create(conn, screen_index));
auto monmon = std::make_shared<IceccMonMon>(looper, columns);
monmon->init(conn, screen, format, atoms, ewmh, 400, 400,
args->is_set("titlebar"), args->is_set("black"));
monmon->connect(args.get());
std::thread xcb_thread(xcb_event_loop, conn, atoms, monmon);
conn.reset();
looper->run();
xcb_thread.join();
return EXIT_SUCCESS;
}
|