-
Notifications
You must be signed in to change notification settings - Fork 1
/
vconsole.c
1109 lines (961 loc) · 33.3 KB
/
vconsole.c
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "vconsole.h"
#include "libvirt-glib-event.h"
#define APPNAME "vconsole"
/* ------------------------------------------------------------------ */
int debug = 0;
GKeyFile *config;
/* ------------------------------------------------------------------ */
static char *config_file;
static void config_read(void)
{
char *home = getenv("HOME");
GError *err = NULL;
if (!home)
return;
config_file = g_strdup_printf("%s/.vconsole", home);
if (access(config_file, F_OK) != 0)
config_file = g_strdup_printf("%s/.config/vconsole.conf", home);
config = g_key_file_new();
g_key_file_load_from_file(config, config_file,
G_KEY_FILE_KEEP_COMMENTS, &err);
}
void config_write(void)
{
char *data;
gsize len;
GError *err = NULL;
int fd;
data = g_key_file_to_data(config, &len, &err);
fd = open(config_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (-1 == fd)
return;
write(fd, data, len);
fsync(fd);
close(fd);
g_free(data);
}
/* ------------------------------------------------------------------ */
static char *gtk_msg_type_name[] = {
[ GTK_MESSAGE_INFO ] = "INFO",
[ GTK_MESSAGE_WARNING ] = "WARNING",
[ GTK_MESSAGE_QUESTION ] = "QUESTION",
[ GTK_MESSAGE_ERROR ] = "ERROR",
};
void gtk_message(GtkWidget *parent, GtkWidget **dialog, GtkMessageType type,
char *fmt, ...)
{
GtkWidget *d = NULL;
va_list args;
char msgbuf[1024];
va_start(args, fmt);
vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
va_end(args);
if (debug)
fprintf(stderr, "%s: %s", gtk_msg_type_name[type], msgbuf);
if (dialog == NULL) {
dialog = &d;
}
if (*dialog == NULL) {
*dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
GTK_DIALOG_DESTROY_WITH_PARENT,
type, GTK_BUTTONS_CLOSE,
"%s", gtk_msg_type_name[type]);
if (dialog != NULL) {
g_signal_connect_swapped(*dialog, "response",
G_CALLBACK (gtk_widget_hide_on_delete),
*dialog);
} else {
g_signal_connect_swapped(*dialog, "response",
G_CALLBACK (gtk_widget_destroy),
*dialog);
}
}
if (gtk_widget_get_visible(*dialog)) {
char *text;
g_object_get(G_OBJECT(*dialog), "secondary-text", &text, NULL);
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(*dialog),
"%s%s", text, msgbuf);
} else {
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(*dialog),
"%s", msgbuf);
gtk_widget_show_all(*dialog);
}
}
static int gtk_getstring(GtkWidget *window, char *title, char *message,
char *dest, int dlen)
{
GtkWidget *dialog, *label, *entry, *vbox;
const char *txt;
int retval;
/* Create the widgets */
dialog = gtk_dialog_new_with_buttons(title,
GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
"_OK", GTK_RESPONSE_ACCEPT,
"_Cancel", GTK_RESPONSE_REJECT,
NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
label = gtk_label_new(message);
#if GTK_CHECK_VERSION(3,16,0)
gtk_label_set_xalign(GTK_LABEL(label), 0);
gtk_label_set_yalign(GTK_LABEL(label), 0.5);
#else
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
#endif
entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry), dest);
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_container_add(GTK_CONTAINER(vbox), label);
gtk_container_add(GTK_CONTAINER(vbox), entry);
gtk_box_set_spacing(GTK_BOX(vbox), 10);
/* show and wait for response */
gtk_widget_show_all(dialog);
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
case GTK_RESPONSE_ACCEPT:
txt = gtk_entry_get_text(GTK_ENTRY(entry));
snprintf(dest, dlen, "%s", txt);
retval = 0;
break;
default:
retval = -1;
break;
}
gtk_widget_destroy(dialog);
return retval;
}
static int gtk_yesno(GtkWidget *window, char *title, char *message)
{
GtkWidget *dialog, *label, *vbox;
bool retval;
/* Create the widgets */
dialog = gtk_dialog_new_with_buttons(title,
GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
"_Yes", GTK_RESPONSE_ACCEPT,
"_No", GTK_RESPONSE_REJECT,
NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_REJECT);
label = gtk_label_new(message);
#if GTK_CHECK_VERSION(3,16,0)
gtk_label_set_xalign(GTK_LABEL(label), 0);
gtk_label_set_yalign(GTK_LABEL(label), 0.5);
#else
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
#endif
vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_container_add(GTK_CONTAINER(vbox), label);
gtk_box_set_spacing(GTK_BOX(vbox), 10);
/* show and wait for response */
gtk_widget_show_all(dialog);
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
case GTK_RESPONSE_ACCEPT:
retval = true;
break;
default:
retval = false;
break;
}
gtk_widget_destroy(dialog);
return retval;
}
/* ------------------------------------------------------------------ */
static void menu_cb_connect_ask(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
char uri[256] = "";
int rc;
rc = gtk_getstring(win->toplevel, "Connect to host", "libvirt uri",
uri, sizeof(uri));
if (rc == 0 && strlen(uri))
connect_init(win, uri);
}
static void menu_cb_connect_menu(GtkMenuItem *item,
gpointer userdata)
{
struct vconsole_window *win = userdata;
GError *err = NULL;
const char *name;
char *uri;
name = gtk_menu_item_get_label(item);
uri = g_key_file_get_string(config, "connections", name, &err);
fprintf(stderr, "%s: %s -> %s\n", __func__, name, uri);
if (uri) {
connect_init(win, uri);
g_free(uri);
}
}
static void menu_cb_close_tab(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
domain_close_current_tab(win);
}
static void menu_cb_close_app(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
gtk_widget_destroy(win->toplevel);
}
static gboolean terminal_font_filter(const PangoFontFamily *family,
const PangoFontFace *face,
gpointer data)
{
return pango_font_family_is_monospace((PangoFontFamily *) family);
}
static void menu_cb_config_font(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
GtkWidget *dialog;
dialog = gtk_font_chooser_dialog_new("Terminal font",
GTK_WINDOW(win->toplevel));
if (win->tty_font)
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(dialog), win->tty_font);
gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog),
terminal_font_filter, NULL, NULL);
gtk_widget_show_all(dialog);
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
case GTK_RESPONSE_OK:
win->tty_font = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
g_key_file_set_string(config, "tty", "font", win->tty_font);
config_write();
domain_configure_all_vtes(win);
break;
}
gtk_widget_destroy(dialog);
}
static int pickcolor(GtkWindow *parent, char *title,
char *group, char *key, char *current)
{
GtkWidget *dialog;
GdkRGBA color = { 0, 0, 0, 0 };
char *name;
int rc = -1;
gdk_rgba_parse(&color, current);
dialog = gtk_color_chooser_dialog_new(title, parent);
gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(dialog), false);
gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(dialog), &color);
gtk_widget_show_all(dialog);
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
case GTK_RESPONSE_OK:
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dialog), &color);
name = gdk_rgba_to_string(&color);
g_key_file_set_string(config, group, key, name);
config_write();
rc = 0;
}
gtk_widget_destroy(dialog);
return rc;
}
static void menu_cb_config_fg(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
GError *err = NULL;
if (0 != pickcolor(GTK_WINDOW(win->toplevel), "Terminal text color",
"tty", "foreground", win->tty_fg))
return;
win->tty_fg = g_key_file_get_string(config, "tty", "foreground", &err);
domain_configure_all_vtes(win);
}
static void menu_cb_config_bg(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
GError *err = NULL;
if (0 != pickcolor(GTK_WINDOW(win->toplevel), "Terminal background",
"tty", "background", win->tty_bg))
return;
win->tty_bg = g_key_file_get_string(config, "tty", "background", &err);
domain_configure_all_vtes(win);
}
static struct vconsole_domain *find_guest(struct vconsole_window *win)
{
struct vconsole_domain *dom = NULL;
GtkTreeSelection *select;
GtkTreeIter iter;
GtkTreeModel *model;
if (gtk_notebook_get_current_page(GTK_NOTEBOOK(win->notebook)) == 0) {
select = gtk_tree_view_get_selection(GTK_TREE_VIEW(win->tree));
if (gtk_tree_selection_get_selected(select, &model, &iter)) {
gtk_tree_model_get(model, &iter, DPTR_COL, &dom, -1);
}
} else {
dom = domain_find_current_tab(win);
}
return dom;
}
static bool is_installed(const char *name)
{
char *path = strdup(getenv("PATH"));
char *elem, *binary, *ptr;
bool retval = false;
int rc;
for (elem = strtok_r(path, ":", &ptr);
elem != NULL;
elem = strtok_r(NULL, ":", &ptr)) {
binary = g_strdup_printf("%s/%s", elem, name);
rc = access(binary, X_OK);
g_free(binary);
if (rc == 0) {
retval = true;
break;
}
}
free(path);
return retval;
}
static void prepare_exec(void)
{
int i;
for (i = 3; i < 1024; i++)
close(i);
}
static void run_virt_viewer(struct vconsole_domain *dom,
bool reconnect)
{
char *argv[32];
int argc = 0;
char *uri;
uri = virConnectGetURI(dom->conn->ptr);
argv[argc++] = "virt-viewer";
argv[argc++] = "--wait";
argv[argc++] = "--attach";
if (reconnect)
argv[argc++] = "--reconnect";
argv[argc++] = "-c";
argv[argc++] = uri;
argv[argc++] = dom->uuid;
argv[argc++] = NULL;
assert(argc < sizeof(argv)/sizeof(argv[0]));
if (fork() <= 0) {
/* parent */
free(uri);
return;
} else {
/* child */
prepare_exec();
setenv("GDK_BACKEND", "x11", true);
execvp("virt-viewer", argv);
perror("execvp");
exit(1);
}
}
static void run_virsh_edit(struct vconsole_domain *dom)
{
char *argv[32];
int argc = 0;
char *app = "xterm";
char *uri;
uri = virConnectGetURI(dom->conn->ptr);
if (is_installed("gterm")) {
app = "gterm";
}
argv[argc++] = app;
if (dom->name) {
argv[argc++] = "-title";
argv[argc++] = dom->name;
}
argv[argc++] = "-e";
argv[argc++] = "virsh";
argv[argc++] = "-c";
argv[argc++] = uri;
argv[argc++] = "edit";
argv[argc++] = dom->uuid;
argv[argc++] = NULL;
assert(argc < sizeof(argv)/sizeof(argv[0]));
if (fork() <= 0) {
/* parent */
free(uri);
return;
} else {
/* child */
prepare_exec();
execvp(app, argv);
perror("execvp");
exit(1);
}
}
static void menu_cb_untabify(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_untabify(dom);
}
static void menu_cb_vm_edit(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
run_virsh_edit(dom);
}
static void menu_cb_vm_undefine(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
char *msg;
bool undefine;
if (dom) {
msg = g_strdup_printf("Really delete domain\n%s?",
dom->name);
undefine = gtk_yesno(win->toplevel, "Please confirm", msg);
g_free(msg);
if (undefine)
domain_undefine(dom);
}
}
static void menu_cb_vm_gfx(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
run_virt_viewer(dom, true);
}
static void menu_cb_vm_run(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_start(dom, false);
}
static void menu_cb_vm_run_reset_nvram(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_start(dom, true);
}
static void menu_cb_vm_run_gfx(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom) {
run_virt_viewer(dom, false);
domain_start(dom, false);
}
}
static void menu_cb_vm_pause(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_pause(dom);
}
static void menu_cb_vm_save(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_save(dom);
}
static void menu_cb_vm_reboot(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_reboot(dom);
}
static void menu_cb_vm_shutdown(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_shutdown(dom);
}
static void menu_cb_vm_reset(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_reset(dom);
}
static void menu_cb_vm_kill(GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
struct vconsole_window *win = data;
struct vconsole_domain *dom = find_guest(win);
if (dom)
domain_kill(dom);
}
static void menu_cb_about(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
static char *comments = "virtual machine console";
static char *copyright = "(c) 2012 Gerd Hoffmann";
static char *authors[] = { "Gerd Hoffmann <[email protected]>", NULL };
struct vconsole_window *win = userdata;
gtk_show_about_dialog(GTK_WINDOW(win->toplevel),
"authors", authors,
"comments", comments,
"copyright", copyright,
"version", VERSION,
NULL);
}
static void menu_cb_manual(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
if (fork() <= 0) {
/* parent */
return;
} else {
/* child */
prepare_exec();
execlp("xdg-open", "xdg-open", "man:vconsole(1)", NULL);
perror("execlp");
exit(1);
}
}
/* ------------------------------------------------------------------ */
static void menu_cb_blink_cursor(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
win->tty_blink = gtk_check_menu_item_get_active(win->blinking);
domain_configure_all_vtes(win);
g_key_file_set_boolean(config, "tty", "blink", win->tty_blink);
config_write();
}
static void menu_cb_vm_logging(GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
struct vconsole_window *win = userdata;
win->vm_logging = gtk_check_menu_item_get_active(win->guestlog);
domain_configure_all_logging(win);
g_key_file_set_boolean(config, "vm", "logging", win->vm_logging);
config_write();
}
/* ------------------------------------------------------------------ */
static const GActionEntry entries[] = {
{
/* --- file menu --- */
.name = "ConnectAsk",
.activate = menu_cb_connect_ask,
},{
.name = "CloseTab",
.activate = menu_cb_close_tab,
},{
.name = "CloseApp",
.activate = menu_cb_close_app,
},{
/* --- view menu --- */
.name = "TerminalFont",
.activate = menu_cb_config_font,
},{
.name = "TerminalForeground",
.activate = menu_cb_config_fg,
},{
.name = "TerminalBackground",
.activate = menu_cb_config_bg,
},{
.name = "TerminalBlink",
.activate = menu_cb_blink_cursor,
},{
.name = "Untabify",
.activate = menu_cb_untabify,
},{
/* --- guest menu --- */
.name = "GuestLogging",
.activate = menu_cb_vm_logging,
},{
.name = "GuestEdit",
.activate = menu_cb_vm_edit,
},{
.name = "GuestUndefine",
.activate = menu_cb_vm_undefine,
},{
.name = "GuestGfx",
.activate = menu_cb_vm_gfx,
},{
.name = "GuestRun",
.activate = menu_cb_vm_run,
},{
.name = "GuestRunResetNvram",
.activate = menu_cb_vm_run_reset_nvram,
},{
.name = "GuestRunGfx",
.activate = menu_cb_vm_run_gfx,
},{
.name = "GuestPause",
.activate = menu_cb_vm_pause,
},{
.name = "GuestSave",
.activate = menu_cb_vm_save,
},{
.name = "GuestReboot",
.activate = menu_cb_vm_reboot,
},{
.name = "GuestShutdown",
.activate = menu_cb_vm_shutdown,
},{
.name = "GuestReset",
.activate = menu_cb_vm_reset,
},{
.name = "GuestKill",
.activate = menu_cb_vm_kill,
},{
/* --- help menu --- */
.name = "About",
.activate = menu_cb_about,
},{
.name = "Manual",
.activate = menu_cb_manual,
},
};
/* ------------------------------------------------------------------ */
static char main_ui[] =
#include "main-ui.h"
"";
/* ------------------------------------------------------------------ */
static void window_destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
static void vconsole_build_recent(struct vconsole_window *win)
{
GError *err = NULL;
GtkWidget *item;
gchar **keys;
gsize i, nkeys = 0;
/* add entries */
keys = g_key_file_get_keys(config, "connections", &nkeys, &err);
for (i = 0; i < nkeys; i++) {
fprintf(stderr, "%s: %s\n", __func__, keys[i]);
item = gtk_menu_item_new_with_label(keys[i]);
gtk_menu_shell_append(GTK_MENU_SHELL(win->recent), item);
g_signal_connect(item, "activate",
G_CALLBACK(menu_cb_connect_menu), win);
}
g_strfreev(keys);
gtk_widget_show_all(win->recent);
}
static struct vconsole_window *vconsole_toplevel_create(void)
{
struct vconsole_window *win;
GSimpleActionGroup *ag;
GtkBuilder *builder;
GError *err;
win = g_new0(struct vconsole_window, 1);
builder = gtk_builder_new_from_string(main_ui, -1);
win->toplevel = GTK_WIDGET(gtk_builder_get_object(builder, "toplevel"));
win->notebook = GTK_WIDGET(gtk_builder_get_object(builder, "notebook"));
win->recent = GTK_WIDGET(gtk_builder_get_object(builder, "recent"));
win->guestlog = GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "guestlog"));
win->blinking = GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "blinking"));
/* signals */
gtk_builder_add_callback_symbols
(builder,
"window-destroy", G_CALLBACK(window_destroy),
NULL);
gtk_builder_connect_signals(builder, win);
/* actions */
ag = g_simple_action_group_new();
g_action_map_add_action_entries(G_ACTION_MAP(ag),
entries, G_N_ELEMENTS(entries),
win);
gtk_widget_insert_action_group(win->toplevel, "main", G_ACTION_GROUP(ag));
g_object_unref(builder);
/* read config */
err = NULL;
win->tty_font = g_key_file_get_string(config, "tty", "font", &err);
err = NULL;
win->tty_fg = g_key_file_get_string(config, "tty", "foreground", &err);
err = NULL;
win->tty_bg = g_key_file_get_string(config, "tty", "background", &err);
err = NULL;
win->tty_blink = g_key_file_get_boolean(config, "tty", "blink", &err);
err = NULL;
win->vm_logging = g_key_file_get_boolean(config, "vm", "logging", &err);
/* config defaults */
if (!win->tty_font)
win->tty_font = "Monospace 12";
if (!win->tty_fg)
win->tty_fg = "white";
if (!win->tty_bg)
win->tty_bg = "black";
/* apply config */
gtk_check_menu_item_set_active(win->blinking, win->tty_blink);
gtk_check_menu_item_set_active(win->guestlog, win->vm_logging);
return win;
}
static void vconsole_tab_list_activate(GtkTreeView *tree_view,
GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer user_data)
{
struct vconsole_window *win = user_data;
GtkTreeModel *model = GTK_TREE_MODEL(win->store);
GtkTreeIter parent, iter;
gboolean is_host;
char *name;
struct vconsole_domain *dom;
if (!gtk_tree_model_get_iter(model, &iter, path))
return;
gtk_tree_model_get(model, &iter, NAME_COL, &name, -1);
is_host = !gtk_tree_model_iter_parent(model, &parent, &iter);
if (is_host) {
if (debug)
fprintf(stderr, "%s: host %s\n", __func__, name);
if (gtk_tree_view_row_expanded(tree_view, path)) {
gtk_tree_view_collapse_row(tree_view, path);
} else {
gtk_tree_view_expand_row(tree_view, path, FALSE);
}
} else {
if (debug)
fprintf(stderr, "%s: guest %s\n", __func__, name);
gtk_tree_model_get(model, &iter, DPTR_COL, &dom, -1);
domain_activate(dom);
}
g_free(name);
}
static gint gtk_sort_iter_compare_str(GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer userdata)
{
gint sortcol = GPOINTER_TO_INT(userdata);
char *aa,*bb;
int ret;
gtk_tree_model_get(model, a, sortcol, &aa, -1);
gtk_tree_model_get(model, b, sortcol, &bb, -1);
if (NULL == aa && NULL == bb) {
ret = 0;
} else if (NULL == aa) {
ret = 1;
} else if (NULL == bb) {
ret = -1;
} else {
ret = strcmp(aa,bb);
}
g_free(aa);
g_free(bb);
return ret;
}
static void vconsole_tab_list_create(struct vconsole_window *win)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkWidget *label, *scroll;
GtkTreeSortable *sortable;
/* store & view */
win->store = gtk_tree_store_new(N_COLUMNS,
G_TYPE_STRING, // NAME_COL
G_TYPE_POINTER, // CPTR_COL
G_TYPE_STRING, // TYPE_COL
G_TYPE_STRING, // URI_COL
G_TYPE_POINTER, // DPTR_COL
G_TYPE_INT, // ID_COL
G_TYPE_STRING, // STATE_COL
G_TYPE_INT, // NR_CPUS_COL
G_TYPE_STRING, // LOAD_STR_COL
G_TYPE_INT, // LOAD_INT_COL
G_TYPE_STRING, // MEMORY_COL
G_TYPE_BOOLEAN, // IS_RUNNING_COL
G_TYPE_BOOLEAN, // HAS_MEMCPU_COL
G_TYPE_STRING, // FOREGROUND_COL
G_TYPE_INT); // WEIGHT_COL
sortable = GTK_TREE_SORTABLE(win->store);
win->tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(win->store));
g_signal_connect(G_OBJECT(win->tree), "row-activated",
G_CALLBACK(vconsole_tab_list_activate),
win);
/* name */
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Name",
renderer,
"text", NAME_COL,
"weight", WEIGHT_COL,
"foreground", FOREGROUND_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
gtk_tree_sortable_set_sort_func(sortable, NAME_COL,
gtk_sort_iter_compare_str,
GINT_TO_POINTER(NAME_COL), NULL);
gtk_tree_view_column_set_sort_column_id(column, NAME_COL);
/* id */
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer, "xalign", 0.5, NULL);
g_object_set(renderer, "height", 1, NULL);
column = gtk_tree_view_column_new_with_attributes("ID",
renderer,
"text", ID_COL,
"visible", IS_RUNNING_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* state */
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("State",
renderer,
"text", STATE_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* memory */
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer, "xalign", 1.0, NULL);
column = gtk_tree_view_column_new_with_attributes("memory",
renderer,
"text", MEMORY_COL,
"visible", HAS_MEMCPU_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* cpu count */
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer, "xalign", 0.5, NULL);
column = gtk_tree_view_column_new_with_attributes("vcpus",
renderer,
"text", NR_CPUS_COL,
"visible", HAS_MEMCPU_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* cpu load */
renderer = gtk_cell_renderer_progress_new();
g_object_set(renderer, "width", 100, NULL);
column = gtk_tree_view_column_new_with_attributes("Load",
renderer,
"text", LOAD_STR_COL,
"value", LOAD_INT_COL,
"visible", IS_RUNNING_COL,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* padding */
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer, "visible", FALSE, NULL);
column = gtk_tree_view_column_new_with_attributes("", renderer, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(win->tree), column);
/* sort store */
gtk_tree_sortable_set_sort_column_id(sortable, NAME_COL,
GTK_SORT_ASCENDING);
/* add tab */
label = gtk_label_new("Guests");
scroll = gtk_scrolled_window_new(NULL, NULL);