Change :block command on GtkTreeView for uniformity
[pipeglade.git] / pipeglade.c
blob509ab1929de8a1ad0cc47384f79e9ae6e7b9228f
1 /*
2 * Copyright (c) 2014-2016 Bert Burgemeister <trebbu@googlemail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <cairo-pdf.h>
25 #include <cairo-ps.h>
26 #include <cairo-svg.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkunixprint.h>
31 #include <gtk/gtkx.h>
32 #include <inttypes.h>
33 #include <libxml/xpath.h>
34 #include <locale.h>
35 #include <math.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/select.h>
43 #include <sys/stat.h>
44 #include <time.h>
45 #include <unistd.h>
47 #define VERSION "4.7.0"
48 #define BUFLEN 256
49 #define WHITESPACE " \t\n"
50 #define MAIN_WIN "main"
51 #define USAGE \
52 "usage: pipeglade [[-i in-fifo] " \
53 "[-o out-fifo] " \
54 "[-b] " \
55 "[-u glade-file.ui] " \
56 "[-e xid]\n" \
57 " [-l log-file] " \
58 "[-O err-file] " \
59 "[--display X-server]] | " \
60 "[-h |" \
61 "-G |" \
62 "-V]\n"
64 #define ABORT \
65 do { \
66 fprintf(stderr, \
67 "In %s (%s:%d): ", \
68 __func__, __FILE__, __LINE__); \
69 abort(); \
70 } while (0)
72 #define OOM_ABORT \
73 do { \
74 fprintf(stderr, \
75 "Out of memory in %s (%s:%d): ", \
76 __func__, __FILE__, __LINE__); \
77 abort(); \
78 } while (0)
82 * ============================================================
83 * Helper functions
84 * ============================================================
88 * Check if s1 and s2 are equal strings
90 static bool
91 eql(const char *s1, const char *s2)
93 return s1 != NULL && s2 != NULL && strcmp(s1, s2) == 0;
97 * Print a formatted message to stream s and give up with status
99 static void
100 bye(int status, FILE *s, const char *fmt, ...)
102 va_list ap;
104 va_start(ap, fmt);
105 vfprintf(s, fmt, ap);
106 va_end(ap);
107 exit(status);
110 static void
111 show_lib_versions(void)
113 bye(EXIT_SUCCESS, stdout,
114 "GTK+ v%d.%d.%d (running v%d.%d.%d)\n"
115 "cairo v%s (running v%s)\n",
116 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
117 gtk_get_major_version(), gtk_get_minor_version(),
118 gtk_get_micro_version(),
119 CAIRO_VERSION_STRING, cairo_version_string());
123 * XEmbed us if xid_s is given, or show a standalone window; give up
124 * on errors
126 static void
127 xembed_if(char *xid_s, GObject *main_window)
129 GtkWidget *plug, *body;
130 Window xid;
131 char xid_s2[BUFLEN];
133 if (xid_s == NULL) { /* standalone */
134 gtk_widget_show(GTK_WIDGET(main_window));
135 return;
137 /* We're being XEmbedded */
138 xid = strtoul(xid_s, NULL, 10);
139 snprintf(xid_s2, BUFLEN, "%lu", xid);
140 if (!eql(xid_s, xid_s2))
141 bye(EXIT_FAILURE, stderr,
142 "%s is not a valid XEmbed socket id\n", xid_s);
143 body = gtk_bin_get_child(GTK_BIN(main_window));
144 gtk_container_remove(GTK_CONTAINER(main_window), body);
145 plug = gtk_plug_new(xid);
146 if (!gtk_plug_get_embedded(GTK_PLUG(plug)))
147 bye(EXIT_FAILURE, stderr,
148 "unable to embed into XEmbed socket %s\n", xid_s);
149 gtk_container_add(GTK_CONTAINER(plug), body);
150 gtk_widget_show(plug);
154 * If requested, redirect stderr to file name
156 static void
157 redirect_stderr(const char *name)
159 if (name == NULL)
160 return;
161 if (freopen(name, "a", stderr) == NULL)
162 /* complaining on stdout since stderr is closed now */
163 bye(EXIT_FAILURE, stdout, "redirecting stderr to %s: %s\n",
164 name, strerror(errno));
165 if (fchmod(fileno(stderr), 0600) < 0)
166 bye(EXIT_FAILURE, stdout, "setting permissions of %s: %s\n",
167 name, strerror(errno));
168 setvbuf(stderr, NULL, _IOLBF, 0);
169 return;
173 * fork() if requested in bg; give up on errors
175 static void
176 go_bg_if(bool bg, FILE *in, FILE *out, char *err_file)
178 pid_t pid = 0;
180 if (!bg)
181 return;
182 if (in == stdin || out == stdout)
183 bye(EXIT_FAILURE, stderr,
184 "parameter -b requires both -i and -o\n");
185 pid = fork();
186 if (pid < 0)
187 bye(EXIT_FAILURE, stderr,
188 "going to background: %s\n", strerror(errno));
189 if (pid > 0)
190 bye(EXIT_SUCCESS, stdout, "%d\n", pid);
191 /* We're the child */
192 close(fileno(stdin)); /* making certain not-so-smart */
193 close(fileno(stdout)); /* system/run-shell commands happy */
194 if (err_file == NULL)
195 freopen("/dev/null", "w", stderr);
199 * Return the current locale and set it to "C". Should be free()d if
200 * done.
202 static char *
203 lc_numeric()
205 char *lc_orig;
206 char *lc = setlocale(LC_NUMERIC, NULL);
208 if ((lc_orig = malloc(strlen(lc) + 1)) == NULL)
209 OOM_ABORT;
210 strcpy(lc_orig, lc);
211 setlocale(LC_NUMERIC, "C");
212 return lc_orig;
216 * Set locale (back) to lc; free lc
218 static void
219 lc_numeric_free(char *lc)
221 setlocale(LC_NUMERIC, lc);
222 free(lc);
226 * Print a warning about a malformed command to stderr. Runs inside
227 * gtk_main().
229 static void
230 ign_cmd(GType type, const char *msg)
232 const char *name, *pad = " ";
234 if (type == G_TYPE_INVALID) {
235 name = "";
236 pad = "";
237 } else
238 name = g_type_name(type);
239 fprintf(stderr, "ignoring %s%scommand \"%s\"\n", name, pad, msg);
243 * Check if n is, or can be made, the name of a fifo, and put its
244 * struct stat into sb. Give up if n exists but is not a fifo.
246 static void
247 find_fifo(const char *n, struct stat *sb)
249 int fd;
251 if ((fd = open(n, O_RDONLY | O_NONBLOCK)) > -1) {
252 if (fstat(fd, sb) == 0 &&
253 S_ISFIFO(sb->st_mode) &&
254 fchmod(fd, 0600) == 0) {
255 fstat(fd, sb);
256 close(fd);
257 return;
259 bye(EXIT_FAILURE, stderr, "using pre-existing fifo %s: %s\n",
260 n, strerror(errno));
262 if (mkfifo(n, 0600) != 0)
263 bye(EXIT_FAILURE, stderr, "making fifo %s: %s\n",
264 n, strerror(errno));
265 find_fifo(n, sb);
268 static FILE *
269 open_fifo(const char *name, const char *fmode, FILE *fallback, int bmode)
271 FILE *s = NULL;
272 int fd;
273 struct stat sb1, sb2;
275 if (name == NULL)
276 s = fallback;
277 else {
278 find_fifo(name, &sb1);
279 /* TODO: O_RDWR on fifo is undefined in POSIX */
280 if (!((fd = open(name, O_RDWR)) > -1 &&
281 fstat(fd, &sb2) == 0 &&
282 sb1.st_mode == sb2.st_mode &&
283 sb1.st_ino == sb2.st_ino &&
284 sb1.st_dev == sb2.st_dev &&
285 (s = fdopen(fd, fmode)) != NULL))
286 bye(EXIT_FAILURE, stderr, "opening fifo %s (%s): %s\n",
287 name, fmode, strerror(errno));
289 setvbuf(s, NULL, bmode, 0);
290 return s;
294 * Create a log file if necessary, and open it. A name of "-"
295 * requests use of stderr.
297 static FILE *
298 open_log(const char *name)
300 FILE *s = NULL;
302 if (name == NULL)
303 return NULL;
304 if (eql(name, "-"))
305 return stderr;
306 if ((s = fopen(name, "a")) == NULL)
307 bye(EXIT_FAILURE, stderr, "opening log file %s: %s\n",
308 name, strerror(errno));
309 if (fchmod(fileno(s), 0600) < 0)
310 bye(EXIT_FAILURE, stderr, "setting permissions of %s: %s\n",
311 name, strerror(errno));
312 return s;
316 * Delete fifo fn if streams s and forbidden are distinct
318 static void
319 rm_unless(FILE *forbidden, FILE *s, char *fn)
321 if (s == forbidden)
322 return;
323 fclose(s);
324 remove(fn);
328 * Microseconds elapsed since start
330 static long int
331 usec_since(struct timespec *start)
333 struct timespec now;
335 clock_gettime(CLOCK_MONOTONIC, &now);
336 return (now.tv_sec - start->tv_sec) * 1e6 +
337 (now.tv_nsec - start->tv_nsec) / 1e3;
341 * Write string s to stream o, escaping newlines and backslashes
343 static void
344 fputs_escaped(const char *s, FILE *o)
346 size_t i = 0;
347 char c;
349 while ((c = s[i++]) != '\0')
350 switch (c) {
351 case '\\': fputs("\\\\", o); break;
352 case '\n': fputs("\\n", o); break;
353 default: putc(c, o); break;
358 * Write log file
360 static void
361 log_msg(FILE *l, char *msg)
363 static char *old_msg;
364 static struct timespec start;
366 if (l == NULL) /* no logging */
367 return;
368 if (msg == NULL && old_msg == NULL)
369 fprintf(l, "##########\t##### (New Pipeglade session) #####\n");
370 else if (msg == NULL && old_msg != NULL) {
371 /* command done; start idle */
372 fprintf(l, "%10ld\t", usec_since(&start));
373 fputs_escaped(old_msg, l);
374 putc('\n', l);
375 free(old_msg);
376 old_msg = NULL;
377 } else if (msg != NULL && old_msg == NULL) {
378 /* idle done; start command */
379 fprintf(l, "%10ld\t### (Idle) ###\n", usec_since(&start));
380 if ((old_msg = malloc(strlen(msg) + 1)) == NULL)
381 OOM_ABORT;
382 strcpy(old_msg, msg);
383 } else
384 ABORT;
385 clock_gettime(CLOCK_MONOTONIC, &start);
388 static bool
389 has_suffix(const char *s, const char *suffix)
391 int s_suf = strlen(s) - strlen(suffix);
393 if (s_suf < 0)
394 return false;
395 return eql(suffix, s + s_suf);
399 * Remove suffix from name; find the object named like this
401 static GObject *
402 obj_sans_suffix(GtkBuilder *builder, const char *suffix, const char *name)
404 char str[BUFLEN + 1] = {'\0'};
405 int str_l;
407 str_l = suffix - name;
408 strncpy(str, name, str_l < BUFLEN ? str_l : BUFLEN);
409 return gtk_builder_get_object(builder, str);
413 * Read UI definition from ui_file; give up on errors
415 static GtkBuilder *
416 builder_from_file(char *ui_file)
418 GError *error = NULL;
419 GtkBuilder *b;
421 b = gtk_builder_new();
422 if (gtk_builder_add_from_file(b, ui_file, &error) == 0)
423 bye(EXIT_FAILURE, stderr, "%s\n", error->message);
424 return b;
428 * Return the id attribute of widget
430 static const char *
431 widget_id(GtkBuildable *widget)
433 return gtk_buildable_get_name(widget);
437 * Get the main window; give up on errors
439 static GObject *
440 find_main_window(GtkBuilder *builder)
442 GObject *mw;
444 if (GTK_IS_WINDOW(mw = gtk_builder_get_object(builder, MAIN_WIN)))
445 return mw;
446 bye(EXIT_FAILURE, stderr, "no toplevel window with id \'" MAIN_WIN "\'\n");
447 return NULL; /* NOT REACHED */
451 * Store a line from stream s into buf, which should have been malloc'd
452 * to bufsize. Enlarge buf and bufsize if necessary.
454 static size_t
455 read_buf(FILE *s, char **buf, size_t *bufsize)
457 bool esc = false;
458 fd_set rfds;
459 int c;
460 int ifd = fileno(s);
461 size_t i = 0;
463 FD_ZERO(&rfds);
464 FD_SET(ifd, &rfds);
465 for (;;) {
466 select(ifd + 1, &rfds, NULL, NULL, NULL);
467 c = getc(s);
468 if (c == '\n' || feof(s))
469 break;
470 if (i >= *bufsize - 1)
471 if ((*buf = realloc(*buf, *bufsize *= 2)) == NULL)
472 OOM_ABORT;
473 if (esc) {
474 esc = false;
475 switch (c) {
476 case 'n': (*buf)[i++] = '\n'; break;
477 case 'r': (*buf)[i++] = '\r'; break;
478 default: (*buf)[i++] = c; break;
480 } else if (c == '\\')
481 esc = true;
482 else
483 (*buf)[i++] = c;
485 (*buf)[i] = '\0';
486 return i;
491 * ============================================================
492 * Receiving feedback from the GUI
493 * ============================================================
496 static void
497 send_msg_to(FILE* o, GtkBuildable *obj, const char *tag, va_list ap)
499 char *data;
500 const char *w_id = widget_id(obj);
501 fd_set wfds;
502 int ofd = fileno(o);
503 struct timeval timeout = {1, 0};
505 FD_ZERO(&wfds);
506 FD_SET(ofd, &wfds);
507 if (select(ofd + 1, NULL, &wfds, NULL, &timeout) == 1) {
508 fprintf(o, "%s:%s ", w_id, tag);
509 while ((data = va_arg(ap, char *)) != NULL)
510 fputs_escaped(data, o);
511 putc('\n', o);
512 } else
513 fprintf(stderr,
514 "send error; discarding feedback message %s:%s\n",
515 w_id, tag);
519 * Send GUI feedback to stream o. The message format is
520 * "<origin>:<tag> <data ...>". The variadic arguments are strings;
521 * last argument must be NULL.
523 static void
524 send_msg(FILE *o, GtkBuildable *obj, const char *tag, ...)
526 va_list ap;
528 va_start(ap, tag);
529 send_msg_to(o, obj, tag, ap);
530 va_end(ap);
534 * Send message from GUI to stream o. The message format is
535 * "<origin>:set <data ...>", which happens to be a legal command.
536 * The variadic arguments are strings; last argument must be NULL.
538 static void
539 send_msg_as_cmd(FILE *o, GtkBuildable *obj, const char *tag, ...)
541 va_list ap;
543 va_start(ap, tag);
544 send_msg_to(o, obj, "set", ap);
545 va_end(ap);
549 * Stuff to pass around
551 struct info {
552 FILE *fout; /* UI feedback messages */
553 FILE *fin; /* command input */
554 FILE *flog; /* logging output */
555 GtkBuilder *builder; /* to be read from .ui file */
556 GObject *obj;
557 GtkTreeModel *model;
558 char *txt;
562 * Data to be passed to and from the GTK main loop
564 struct ui_data {
565 void (*fn)(struct ui_data *);
566 GObject *obj;
567 char *action;
568 char *data;
569 char *cmd;
570 char *cmd_tokens;
571 GType type;
572 struct info *args;
576 * Return pointer to a newly allocated struct info
578 struct info *
579 info_new_full(FILE *stream, GObject *obj, GtkTreeModel *model, char *txt)
581 struct info *ar;
583 if ((ar = malloc(sizeof(struct info))) == NULL)
584 OOM_ABORT;
585 ar->fout = stream;
586 ar->fin = NULL;
587 ar->flog = NULL;
588 ar->builder = NULL;
589 ar->obj = obj;
590 ar->model = model;
591 ar->txt = txt;
592 return ar;
595 struct info *
596 info_txt_new(FILE *stream, char *txt)
598 return info_new_full(stream, NULL, NULL, txt);
601 struct info *
602 info_obj_new(FILE *stream, GObject *obj, GtkTreeModel *model)
604 return info_new_full(stream, obj, model, NULL);
608 * Use msg_sender() to send a message describing a particular cell
610 static void
611 send_tree_cell_msg_by(void msg_sender(FILE *, GtkBuildable *, const char *, ...),
612 const char *path_s,
613 GtkTreeIter *iter, int col, struct info *ar)
615 GtkBuildable *obj = GTK_BUILDABLE(ar->obj);
616 GtkTreeModel *model = ar->model;
617 GType col_type;
618 GValue value = G_VALUE_INIT;
619 char str[BUFLEN], *lc = lc_numeric();
621 gtk_tree_model_get_value(model, iter, col, &value);
622 col_type = gtk_tree_model_get_column_type(model, col);
623 switch (col_type) {
624 case G_TYPE_INT:
625 snprintf(str, BUFLEN, " %d %d", col, g_value_get_int(&value));
626 msg_sender(ar->fout, obj, "gint", path_s, str, NULL);
627 break;
628 case G_TYPE_LONG:
629 snprintf(str, BUFLEN, " %d %ld", col, g_value_get_long(&value));
630 msg_sender(ar->fout, obj, "glong", path_s, str, NULL);
631 break;
632 case G_TYPE_INT64:
633 snprintf(str, BUFLEN, " %d %" PRId64, col, g_value_get_int64(&value));
634 msg_sender(ar->fout, obj, "gint64", path_s, str, NULL);
635 break;
636 case G_TYPE_UINT:
637 snprintf(str, BUFLEN, " %d %u", col, g_value_get_uint(&value));
638 msg_sender(ar->fout, obj, "guint", path_s, str, NULL);
639 break;
640 case G_TYPE_ULONG:
641 snprintf(str, BUFLEN, " %d %lu", col, g_value_get_ulong(&value));
642 msg_sender(ar->fout, obj, "gulong", path_s, str, NULL);
643 break;
644 case G_TYPE_UINT64:
645 snprintf(str, BUFLEN, " %d %" PRIu64, col, g_value_get_uint64(&value));
646 msg_sender(ar->fout, obj, "guint64", path_s, str, NULL);
647 break;
648 case G_TYPE_BOOLEAN:
649 snprintf(str, BUFLEN, " %d %d", col, g_value_get_boolean(&value));
650 msg_sender(ar->fout, obj, "gboolean", path_s, str, NULL);
651 break;
652 case G_TYPE_FLOAT:
653 snprintf(str, BUFLEN, " %d %f", col, g_value_get_float(&value));
654 msg_sender(ar->fout, obj, "gfloat", path_s, str, NULL);
655 break;
656 case G_TYPE_DOUBLE:
657 snprintf(str, BUFLEN, " %d %f", col, g_value_get_double(&value));
658 msg_sender(ar->fout, obj, "gdouble", path_s, str, NULL);
659 break;
660 case G_TYPE_STRING:
661 snprintf(str, BUFLEN, " %d ", col);
662 msg_sender(ar->fout, obj, "gchararray", path_s, str, g_value_get_string(&value), NULL);
663 break;
664 default:
665 fprintf(stderr, "column %d not implemented: %s\n", col, G_VALUE_TYPE_NAME(&value));
666 break;
668 g_value_unset(&value);
669 lc_numeric_free(lc);
673 * Use msg_sender() to send one message per column for a single row
675 static void
676 send_tree_row_msg_by(void msg_sender(FILE *, GtkBuildable *, const char *, ...),
677 char *path_s, GtkTreeIter *iter, struct info *ar)
679 int col;
681 for (col = 0; col < gtk_tree_model_get_n_columns(ar->model); col++)
682 send_tree_cell_msg_by(msg_sender, path_s, iter, col, ar);
686 * send_tree_row_msg serves as an argument for
687 * gtk_tree_selection_selected_foreach()
689 static gboolean
690 send_tree_row_msg(GtkTreeModel *model,
691 GtkTreePath *path, GtkTreeIter *iter, struct info *ar)
693 char *path_s = gtk_tree_path_to_string(path);
695 ar->model = model;
696 send_tree_row_msg_by(send_msg, path_s, iter, ar);
697 g_free(path_s);
698 return FALSE;
702 * save_tree_row_msg serves as an argument for
703 * gtk_tree_model_foreach().
704 * Send message from GUI to global stream "save".
706 static gboolean
707 save_tree_row_msg(GtkTreeModel *model,
708 GtkTreePath *path, GtkTreeIter *iter, struct info *ar)
710 char *path_s = gtk_tree_path_to_string(path);
712 ar->model = model;
713 send_tree_row_msg_by(send_msg_as_cmd, path_s, iter, ar);
714 g_free(path_s);
715 return FALSE;
718 static void
719 cb_calendar(GtkBuildable *obj, struct info *ar)
721 char str[BUFLEN];
722 unsigned int year = 0, month = 0, day = 0;
724 gtk_calendar_get_date(GTK_CALENDAR(obj), &year, &month, &day);
725 snprintf(str, BUFLEN, "%04u-%02u-%02u", year, ++month, day);
726 send_msg(ar->fout, obj, ar->txt, str, NULL);
729 static void
730 cb_color_button(GtkBuildable *obj, struct info *ar)
732 GdkRGBA color;
734 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(obj), &color);
735 send_msg(ar->fout, obj, ar->txt, gdk_rgba_to_string(&color), NULL);
738 static void
739 cb_editable(GtkBuildable *obj, struct info *ar)
741 send_msg(ar->fout, obj, ar->txt, gtk_entry_get_text(GTK_ENTRY(obj)), NULL);
745 * Callback that sends a message about a pointer device button press
746 * in a GtkEventBox
748 static bool
749 cb_event_box_button(GtkBuildable *obj, GdkEvent *e, struct info *ar)
751 char data[BUFLEN], *lc = lc_numeric();
753 snprintf(data, BUFLEN, "%d %.1lf %.1lf",
754 e->button.button, e->button.x, e->button.y);
755 send_msg(ar->fout, obj, ar->txt, data, NULL);
756 lc_numeric_free(lc);
757 return true;
761 * Callback that sends in a message the name of the key pressed when
762 * a GtkEventBox is focused
764 static bool
765 cb_event_box_key(GtkBuildable *obj, GdkEvent *e, struct info *ar)
767 send_msg(ar->fout, obj, ar->txt, gdk_keyval_name(e->key.keyval), NULL);
768 return true;
772 * Callback that sends a message about pointer device motion in a
773 * GtkEventBox
775 static bool
776 cb_event_box_motion(GtkBuildable *obj, GdkEvent *e, struct info *ar)
778 char data[BUFLEN], *lc = lc_numeric();
780 snprintf(data, BUFLEN, "%.1lf %.1lf", e->button.x, e->button.y);
781 send_msg(ar->fout, obj, ar->txt, data, NULL);
782 lc_numeric_free(lc);
783 return true;
787 * Callback that only sends "name:tag" and returns false
789 static bool
790 cb_event_simple(GtkBuildable *obj, GdkEvent *e, struct info *ar)
792 (void) e;
793 send_msg(ar->fout, obj, ar->txt, NULL);
794 return false;
797 static void
798 cb_file_chooser_button(GtkBuildable *obj, struct info *ar)
800 send_msg(ar->fout, obj, ar->txt,
801 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(obj)), NULL);
804 static void
805 cb_font_button(GtkBuildable *obj, struct info *ar)
807 send_msg(ar->fout, obj, ar->txt,
808 gtk_font_button_get_font_name(GTK_FONT_BUTTON(obj)), NULL);
811 static void
812 cb_menu_item(GtkBuildable *obj, struct info *ar)
814 send_msg(ar->fout, obj, ar->txt,
815 gtk_menu_item_get_label(GTK_MENU_ITEM(obj)), NULL);
818 static void
819 cb_range(GtkBuildable *obj, struct info *ar)
821 char str[BUFLEN], *lc = lc_numeric();
823 snprintf(str, BUFLEN, "%f", gtk_range_get_value(GTK_RANGE(obj)));
824 send_msg(ar->fout, obj, ar->txt, str, NULL);
825 lc_numeric_free(lc);
829 * Callback that sends user's selection from a file dialog
831 static void
832 cb_send_file_chooser_dialog_selection(struct info *ar)
834 send_msg(ar->fout, GTK_BUILDABLE(ar->obj), "file",
835 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(ar->obj)),
836 NULL);
837 send_msg(ar->fout, GTK_BUILDABLE(ar->obj), "folder",
838 gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(ar->obj)),
839 NULL);
843 * Callback that sends in a message the content of the text buffer
844 * passed in user_data
846 static void
847 cb_send_text(GtkBuildable *obj, struct info *ar)
849 GtkTextIter a, b;
851 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(ar->obj), &a, &b);
852 send_msg(ar->fout, obj, "text",
853 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar->obj), &a, &b, TRUE),
854 NULL);
858 * Callback that sends in a message the highlighted text from the text
859 * buffer which was passed in user_data
861 static void
862 cb_send_text_selection(GtkBuildable *obj, struct info *ar)
864 GtkTextIter a, b;
866 gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(ar->obj), &a, &b);
867 send_msg(ar->fout, obj, "text",
868 gtk_text_buffer_get_text(GTK_TEXT_BUFFER(ar->obj), &a, &b, TRUE),
869 NULL);
873 * Callback that only sends "name:tag" and returns true
875 static bool
876 cb_simple(GtkBuildable *obj, struct info *ar)
878 send_msg(ar->fout, obj, ar->txt, NULL);
879 return true;
882 static void
883 cb_spin_button(GtkBuildable *obj, struct info *ar)
885 char str[BUFLEN], *lc = lc_numeric();
887 snprintf(str, BUFLEN, "%f", gtk_spin_button_get_value(GTK_SPIN_BUTTON(obj)));
888 send_msg(ar->fout, obj, ar->txt, str, NULL);
889 lc_numeric_free(lc);
892 static void
893 cb_switch(GtkBuildable *obj, void *pspec, struct info *ar)
895 (void) pspec;
896 send_msg(ar->fout, obj,
897 gtk_switch_get_active(GTK_SWITCH(obj)) ? "1" : "0",
898 NULL);
901 static void
902 cb_toggle_button(GtkBuildable *obj, struct info *ar)
904 send_msg(ar->fout, obj,
905 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(obj)) ? "1" : "0",
906 NULL);
909 static void
910 cb_tree_selection(GtkBuildable *obj, struct info *ar)
912 GtkTreeSelection *sel = GTK_TREE_SELECTION(obj);
913 GtkTreeView *view = gtk_tree_selection_get_tree_view(sel);
915 ar->obj = G_OBJECT(view);
916 send_msg(ar->fout, GTK_BUILDABLE(view), ar->txt, NULL);
917 gtk_tree_selection_selected_foreach(
918 sel, (GtkTreeSelectionForeachFunc) send_tree_row_msg, ar);
923 * ============================================================
924 * cb_draw() maintains a drawing on a GtkDrawingArea; it needs a few
925 * helper functions
926 * ============================================================
930 * The set of supported drawing operations
932 enum cairo_fn {
933 ARC,
934 ARC_NEGATIVE,
935 CLOSE_PATH,
936 CURVE_TO,
937 FILL,
938 FILL_PRESERVE,
939 LINE_TO,
940 MOVE_TO,
941 RECTANGLE,
942 REL_CURVE_TO,
943 REL_LINE_TO,
944 REL_MOVE_TO,
945 REL_MOVE_FOR,
946 RESET_CTM,
947 SET_DASH,
948 SET_FONT_FACE,
949 SET_FONT_SIZE,
950 SET_LINE_CAP,
951 SET_LINE_JOIN,
952 SET_LINE_WIDTH,
953 SET_SOURCE_RGBA,
954 SHOW_TEXT,
955 STROKE,
956 STROKE_PRESERVE,
957 TRANSFORM,
961 * Text placement mode for rel_move_for()
963 enum ref_point {
975 enum draw_op_policy {
976 APPEND,
977 BEFORE,
978 REPLACE,
982 * One single element of a drawing
984 struct draw_op {
985 struct draw_op *next;
986 struct draw_op *prev;
987 unsigned long long int id;
988 unsigned long long int before;
989 enum draw_op_policy policy;
990 enum cairo_fn op;
991 void *op_args;
995 * Argument sets for the various drawing operations
997 struct arc_args {
998 double x;
999 double y;
1000 double radius;
1001 double angle1;
1002 double angle2;
1005 struct curve_to_args {
1006 double x1;
1007 double y1;
1008 double x2;
1009 double y2;
1010 double x3;
1011 double y3;
1014 struct move_to_args {
1015 double x;
1016 double y;
1019 struct rectangle_args {
1020 double x;
1021 double y;
1022 double width;
1023 double height;
1026 struct rel_move_for_args {
1027 enum ref_point ref;
1028 int len;
1029 char text[];
1032 struct set_dash_args {
1033 int num_dashes;
1034 double dashes[];
1037 struct set_font_face_args {
1038 cairo_font_slant_t slant;
1039 cairo_font_weight_t weight;
1040 char family[];
1043 struct set_font_size_args {
1044 double size;
1047 struct set_line_cap_args {
1048 cairo_line_cap_t line_cap;
1051 struct set_line_join_args {
1052 cairo_line_join_t line_join;
1055 struct set_line_width_args {
1056 double width;
1059 struct set_source_rgba_args {
1060 GdkRGBA color;
1063 struct show_text_args {
1064 int len;
1065 char text[];
1068 struct transform_args {
1069 cairo_matrix_t matrix;
1072 static void
1073 draw(cairo_t *cr, enum cairo_fn op, void *op_args)
1075 switch (op) {
1076 case LINE_TO: {
1077 struct move_to_args *args = op_args;
1079 cairo_line_to(cr, args->x, args->y);
1080 break;
1082 case REL_LINE_TO: {
1083 struct move_to_args *args = op_args;
1085 cairo_rel_line_to(cr, args->x, args->y);
1086 break;
1088 case MOVE_TO: {
1089 struct move_to_args *args = op_args;
1091 cairo_move_to(cr, args->x, args->y);
1092 break;
1094 case REL_MOVE_TO: {
1095 struct move_to_args *args = op_args;
1097 cairo_rel_move_to(cr, args->x, args->y);
1098 break;
1100 case ARC: {
1101 struct arc_args *args = op_args;
1103 cairo_arc(cr, args->x, args->y, args->radius, args->angle1, args->angle2);
1104 break;
1106 case ARC_NEGATIVE: {
1107 struct arc_args *args = op_args;
1109 cairo_arc_negative(cr, args->x, args->y, args->radius, args->angle1, args->angle2);
1110 break;
1112 case CURVE_TO: {
1113 struct curve_to_args *args = op_args;
1115 cairo_curve_to(cr, args->x1, args->y1, args->x2, args->y2, args->x3, args->y3);
1116 break;
1118 case REL_CURVE_TO: {
1119 struct curve_to_args *args = op_args;
1121 cairo_curve_to(cr, args->x1, args->y1, args->x2, args->y2, args->x3, args->y3);
1122 break;
1124 case RECTANGLE: {
1125 struct rectangle_args *args = op_args;
1127 cairo_rectangle(cr, args->x, args->y, args->width, args->height);
1128 break;
1130 case CLOSE_PATH:
1131 cairo_close_path(cr);
1132 break;
1133 case SHOW_TEXT: {
1134 struct show_text_args *args = op_args;
1136 cairo_show_text(cr, args->text);
1137 break;
1139 case REL_MOVE_FOR: {
1140 cairo_text_extents_t e;
1141 double dx = 0.0, dy = 0.0;
1142 struct rel_move_for_args *args = op_args;
1144 cairo_text_extents(cr, args->text, &e);
1145 switch (args->ref) {
1146 case C: dx = -e.width / 2; dy = e.height / 2; break;
1147 case E: dx = -e.width; dy = e.height / 2; break;
1148 case N: dx = -e.width / 2; dy = e.height; break;
1149 case NE: dx = -e.width; dy = e.height; break;
1150 case NW: dy = e.height; break;
1151 case S: dx = -e.width / 2; break;
1152 case SE: dx = -e.width; break;
1153 case SW: break;
1154 case W: dy = e.height / 2; break;
1155 default: ABORT; break;
1157 cairo_rel_move_to(cr, dx, dy);
1158 break;
1160 case RESET_CTM:
1161 cairo_identity_matrix(cr);
1162 break;
1163 case STROKE:
1164 cairo_stroke(cr);
1165 break;
1166 case STROKE_PRESERVE:
1167 cairo_stroke_preserve(cr);
1168 break;
1169 case FILL:
1170 cairo_fill(cr);
1171 break;
1172 case FILL_PRESERVE:
1173 cairo_fill_preserve(cr);
1174 break;
1175 case SET_DASH: {
1176 struct set_dash_args *args = op_args;
1178 cairo_set_dash(cr, args->dashes, args->num_dashes, 0);
1179 break;
1181 case SET_FONT_FACE: {
1182 struct set_font_face_args *args = op_args;
1184 cairo_select_font_face(cr, args->family, args->slant, args->weight);
1185 break;
1187 case SET_FONT_SIZE: {
1188 struct set_font_size_args *args = op_args;
1190 cairo_set_font_size(cr, args->size);
1191 break;
1193 case SET_LINE_CAP: {
1194 struct set_line_cap_args *args = op_args;
1196 cairo_set_line_cap(cr, args->line_cap);
1197 break;
1199 case SET_LINE_JOIN: {
1200 struct set_line_join_args *args = op_args;
1202 cairo_set_line_join(cr, args->line_join);
1203 break;
1205 case SET_LINE_WIDTH: {
1206 struct set_line_width_args *args = op_args;
1208 cairo_set_line_width(cr, args->width);
1209 break;
1211 case SET_SOURCE_RGBA: {
1212 struct set_source_rgba_args *args = op_args;
1214 gdk_cairo_set_source_rgba(cr, &args->color);
1215 break;
1217 case TRANSFORM: {
1218 struct transform_args *args = op_args;
1220 cairo_transform(cr, &args->matrix);
1221 break;
1223 default:
1224 ABORT;
1225 break;
1230 * Callback that draws on a GtkDrawingArea
1232 static gboolean
1233 cb_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
1235 struct draw_op *op;
1237 (void) data;
1238 for (op = g_object_get_data(G_OBJECT(widget), "draw_ops");
1239 op != NULL;
1240 op = op->next)
1241 draw(cr, op->op, op->op_args);
1242 return FALSE;
1247 * ============================================================
1248 * Manipulating the GUI
1249 * ============================================================
1253 * Generic actions that are applicable to most widgets
1257 * Simulate user activity on various widgets. Runs inside gtk_main().
1259 static void
1260 fake_ui_activity(struct ui_data *ud)
1262 char dummy;
1264 if (!GTK_IS_WIDGET(ud->obj) || sscanf(ud->data, " %c", &dummy) > 0)
1265 ign_cmd(ud->type, ud->cmd);
1266 else if (GTK_IS_SPIN_BUTTON(ud->obj)) {
1267 ud->args->txt = "text";
1268 cb_spin_button(GTK_BUILDABLE(ud->obj), ud->args); /* TODO: rename to "value" */
1269 } else if (GTK_IS_SCALE(ud->obj)) {
1270 ud->args->txt = "value";
1271 cb_range(GTK_BUILDABLE(ud->obj), ud->args);
1272 } else if (GTK_IS_ENTRY(ud->obj)) {
1273 ud->args->txt = "text";
1274 cb_editable(GTK_BUILDABLE(ud->obj), ud->args);
1275 } else if (GTK_IS_CALENDAR(ud->obj)) {
1276 ud->args->txt = "clicked";
1277 cb_calendar(GTK_BUILDABLE(ud->obj), ud->args);
1278 } else if (GTK_IS_FILE_CHOOSER_BUTTON(ud->obj)) {
1279 ud->args->txt = "file";
1280 cb_file_chooser_button(GTK_BUILDABLE(ud->obj), ud->args);
1281 } else if (!gtk_widget_activate(GTK_WIDGET(ud->obj)))
1282 ign_cmd(ud->type, ud->cmd);
1285 static void
1286 update_focus(struct ui_data *ud){
1287 char dummy;
1289 if (GTK_IS_WIDGET(ud->obj) &&
1290 sscanf(ud->data, " %c", &dummy) < 1 &&
1291 gtk_widget_get_can_focus(GTK_WIDGET(ud->obj)))
1292 gtk_widget_grab_focus(GTK_WIDGET(ud->obj));
1293 else
1294 ign_cmd(ud->type, ud->cmd);
1298 * Have the widget say "ping". Runs inside gtk_main().
1300 static void
1301 ping(struct ui_data *ud)
1303 char dummy;
1305 if (!GTK_IS_WIDGET(ud->obj) || sscanf(ud->data, " %c", &dummy) > 0)
1306 ign_cmd(ud->type, ud->cmd);
1307 ud->args->txt = "ping";
1308 cb_simple(GTK_BUILDABLE(ud->obj), ud->args);
1312 * Write snapshot of widget in an appropriate format to file
1314 static void
1315 take_snapshot(struct ui_data *ud)
1317 cairo_surface_t *sur = NULL;
1318 cairo_t *cr = NULL;
1319 int height;
1320 int width;
1322 if (!GTK_IS_WIDGET(ud->obj) ||
1323 !gtk_widget_is_drawable(GTK_WIDGET(ud->obj))) {
1324 ign_cmd(ud->type, ud->cmd);
1325 return;
1327 height = gtk_widget_get_allocated_height(GTK_WIDGET(ud->obj));
1328 width = gtk_widget_get_allocated_width(GTK_WIDGET(ud->obj));
1329 if (has_suffix(ud->data, ".epsf") || has_suffix(ud->data, ".eps")) {
1330 sur = cairo_ps_surface_create(ud->data, width, height);
1331 cairo_ps_surface_set_eps(sur, TRUE);
1332 } else if (has_suffix(ud->data, ".pdf"))
1333 sur = cairo_pdf_surface_create(ud->data, width, height);
1334 else if (has_suffix(ud->data, ".ps"))
1335 sur = cairo_ps_surface_create(ud->data, width, height);
1336 else if (has_suffix(ud->data, ".svg"))
1337 sur = cairo_svg_surface_create(ud->data, width, height);
1338 else {
1339 ign_cmd(ud->type, ud->cmd);
1340 return;
1342 cr = cairo_create(sur);
1343 gtk_widget_draw(GTK_WIDGET(ud->obj), cr);
1344 cairo_destroy(cr);
1345 cairo_surface_destroy(sur);
1348 struct handler_id {
1349 unsigned int id; /* returned by g_signal_connect() and friends */
1350 bool blocked; /* we avoid multiple blocking/unblocking */
1351 struct handler_id *next;
1354 static void
1355 update_blocked(struct ui_data *ud)
1357 char dummy;
1358 struct handler_id *hid;
1359 unsigned long int val;
1361 if (sscanf(ud->data, "%lu %c", &val, &dummy) == 1 && val < 2) {
1362 for (hid = g_object_get_data(ud->obj, "signal-id");
1363 hid != NULL; hid = hid->next) {
1364 if (val == 0 && hid->blocked == true) {
1365 g_signal_handler_unblock(ud->obj, hid->id);
1366 hid->blocked = false;
1367 } else if (val == 1 && hid->blocked == false) {
1368 g_signal_handler_block(ud->obj, hid->id);
1369 hid->blocked = true;
1372 } else
1373 ign_cmd(ud->type, ud->cmd);
1376 static void
1377 update_sensitivity(struct ui_data *ud)
1379 char dummy;
1380 unsigned int val;
1382 if (GTK_IS_WIDGET(ud->obj) &&
1383 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
1384 gtk_widget_set_sensitive(GTK_WIDGET(ud->obj), val);
1385 else
1386 ign_cmd(ud->type, ud->cmd);
1389 static void
1390 update_size_request(struct ui_data *ud)
1392 char dummy;
1393 int x, y;
1395 if (GTK_IS_WIDGET(ud->obj) &&
1396 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1397 gtk_widget_set_size_request(GTK_WIDGET(ud->obj), x, y);
1398 else if (GTK_IS_WIDGET(ud->obj) &&
1399 sscanf(ud->data, " %c", &dummy) < 1)
1400 gtk_widget_set_size_request(GTK_WIDGET(ud->obj), -1, -1);
1401 else
1402 ign_cmd(ud->type, ud->cmd);
1405 static void
1406 update_tooltip_text(struct ui_data *ud)
1408 if (GTK_IS_WIDGET(ud->obj))
1409 gtk_widget_set_tooltip_text(GTK_WIDGET(ud->obj), ud->data);
1410 else
1411 ign_cmd(ud->type, ud->cmd);
1414 static void
1415 update_visibility(struct ui_data *ud)
1417 char dummy;
1418 unsigned int val;
1420 if (GTK_IS_WIDGET(ud->obj) &&
1421 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
1422 gtk_widget_set_visible(GTK_WIDGET(ud->obj), val);
1423 else
1424 ign_cmd(ud->type, ud->cmd);
1428 * Change the style of the widget passed. Runs inside gtk_main().
1430 static void
1431 update_widget_style(struct ui_data *ud)
1433 GtkStyleContext *context;
1434 GtkStyleProvider *style_provider;
1435 char *style_decl;
1436 const char *prefix = "* {", *suffix = "}";
1437 size_t sz;
1439 if (!GTK_IS_WIDGET(ud->obj)) {
1440 ign_cmd(ud->type, ud->cmd);
1441 return;
1443 style_provider = g_object_get_data(ud->obj, "style_provider");
1444 sz = strlen(prefix) + strlen(suffix) + strlen(ud->data) + 1;
1445 context = gtk_widget_get_style_context(GTK_WIDGET(ud->obj));
1446 gtk_style_context_remove_provider(context, style_provider);
1447 if ((style_decl = malloc(sz)) == NULL)
1448 OOM_ABORT;
1449 strcpy(style_decl, prefix);
1450 strcat(style_decl, ud->data);
1451 strcat(style_decl, suffix);
1452 gtk_style_context_add_provider(context, style_provider,
1453 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1454 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(style_provider),
1455 style_decl, -1, NULL);
1456 free(style_decl);
1460 * Check if one of the generic actions is requested; complain if none
1461 * of them is
1463 static void
1464 try_generic_cmds(struct ui_data *ud)
1466 if (eql(ud->action, "block"))
1467 update_blocked(ud);
1468 else if (eql(ud->action, "set_sensitive"))
1469 update_sensitivity(ud);
1470 else if (eql(ud->action, "set_visible"))
1471 update_visibility(ud);
1472 else if (eql(ud->action, "set_tooltip_text"))
1473 update_tooltip_text(ud);
1474 else if (eql(ud->action, "grab_focus"))
1475 update_focus(ud);
1476 else if (eql(ud->action, "set_size_request"))
1477 update_size_request(ud);
1478 else if (eql(ud->action, "style"))
1479 update_widget_style(ud);
1480 else if (eql(ud->action, "force"))
1481 fake_ui_activity(ud);
1482 /* next line intentionally mangled to exclude it from */
1483 /* auto-generated list of commands */
1484 else if (eql(ud->action, /* undocumented! */ "ping"))
1485 ping(ud);
1486 else if (eql(ud->action, "snapshot"))
1487 take_snapshot(ud);
1488 else
1489 ign_cmd(ud->type, ud->cmd);
1493 * Manipulation of specific widgets
1496 static void
1497 update_button(struct ui_data *ud)
1499 if (eql(ud->action, "set_label"))
1500 gtk_button_set_label(GTK_BUTTON(ud->obj), ud->data);
1501 else
1502 try_generic_cmds(ud);
1505 static void
1506 update_calendar(struct ui_data *ud)
1508 GtkCalendar *calendar = GTK_CALENDAR(ud->obj);
1509 char dummy;
1510 int year = 0, month = 0, day = 0;
1512 if (eql(ud->action, "select_date") &&
1513 sscanf(ud->data, "%d-%d-%d %c", &year, &month, &day, &dummy) == 3) {
1514 if (month > -1 && month <= 11 && day > 0 && day <= 31) {
1515 gtk_calendar_select_month(calendar, --month, year);
1516 gtk_calendar_select_day(calendar, day);
1517 } else
1518 ign_cmd(ud->type, ud->cmd);
1519 } else if (eql(ud->action, "mark_day") &&
1520 sscanf(ud->data, "%d %c", &day, &dummy) == 1) {
1521 if (day > 0 && day <= 31)
1522 gtk_calendar_mark_day(calendar, day);
1523 else
1524 ign_cmd(ud->type, ud->cmd);
1525 } else if (eql(ud->action, "clear_marks") && sscanf(ud->data, " %c", &dummy) < 1)
1526 gtk_calendar_clear_marks(calendar);
1527 else
1528 try_generic_cmds(ud);
1532 * Common actions for various kinds of window. Return false if
1533 * command is ignored. Runs inside gtk_main().
1535 static bool
1536 update_class_window(struct ui_data *ud)
1538 GtkWindow *window = GTK_WINDOW(ud->obj);
1539 char dummy;
1540 int x, y;
1542 if (eql(ud->action, "set_title"))
1543 gtk_window_set_title(window, ud->data);
1544 else if (eql(ud->action, "fullscreen") && sscanf(ud->data, " %c", &dummy) < 1)
1545 gtk_window_fullscreen(window);
1546 else if (eql(ud->action, "unfullscreen") && sscanf(ud->data, " %c", &dummy) < 1)
1547 gtk_window_unfullscreen(window);
1548 else if (eql(ud->action, "resize") &&
1549 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1550 gtk_window_resize(window, x, y);
1551 else if (eql(ud->action, "resize") && sscanf(ud->data, " %c", &dummy) < 1) {
1552 gtk_window_get_default_size(window, &x, &y);
1553 gtk_window_resize(window, x, y);
1554 } else if (eql(ud->action, "move") &&
1555 sscanf(ud->data, "%d %d %c", &x, &y, &dummy) == 2)
1556 gtk_window_move(window, x, y);
1557 else
1558 return false;
1559 return true;
1562 static void
1563 update_color_button(struct ui_data *ud)
1565 GdkRGBA color;
1567 if (eql(ud->action, "set_color")) {
1568 gdk_rgba_parse(&color, ud->data);
1569 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(ud->obj), &color);
1570 } else
1571 try_generic_cmds(ud);
1574 static void
1575 update_combo_box_text(struct ui_data *ud)
1577 GtkComboBoxText *combobox = GTK_COMBO_BOX_TEXT(ud->obj);
1578 char dummy;
1579 int txt0, pos;
1581 if (eql(ud->action, "prepend_text"))
1582 gtk_combo_box_text_prepend_text(combobox, ud->data);
1583 else if (eql(ud->action, "append_text"))
1584 gtk_combo_box_text_append_text(combobox, ud->data);
1585 else if (eql(ud->action, "remove") &&
1586 sscanf(ud->data, "%d %c", &pos, &dummy) == 1)
1587 gtk_combo_box_text_remove(combobox, pos);
1588 else if (eql(ud->action, "insert_text") &&
1589 sscanf(ud->data, "%d %n", &pos, &txt0) == 1)
1590 gtk_combo_box_text_insert_text(combobox, pos, ud->data + txt0);
1591 else
1592 try_generic_cmds(ud);
1596 * update_drawing_area(), which runs inside gtk_main(), maintains a
1597 * list of drawing operations. It needs a few helper functions. It
1598 * is the responsibility of cb_draw() to actually execute the list.
1601 enum draw_op_stat {
1602 FAILURE,
1603 SUCCESS,
1604 NEED_REDRAW,
1608 * Fill structure *op with the drawing operation according to action
1609 * and with the appropriate set of arguments
1611 static enum draw_op_stat
1612 set_draw_op(struct draw_op *op, const char *action, const char *data)
1614 char dummy;
1615 const char *raw_args = data;
1616 enum draw_op_stat result = SUCCESS;
1617 int args_start = 0;
1619 if (sscanf(data, "=%llu %n", &op->id, &args_start) == 1) {
1620 op->policy = REPLACE;
1621 result = NEED_REDRAW;
1622 } else if (sscanf(data, "%llu<%llu %n", &op->id, &op->before, &args_start) == 2) {
1623 op->policy = BEFORE;
1624 result = NEED_REDRAW;
1625 } else if (sscanf(data, "%llu %n", &op->id, &args_start) == 1)
1626 op->policy = APPEND;
1627 else
1628 return FAILURE;
1629 raw_args += args_start;
1630 if (eql(action, "line_to")) {
1631 struct move_to_args *args;
1633 if ((args = malloc(sizeof(*args))) == NULL)
1634 OOM_ABORT;
1635 op->op = LINE_TO;
1636 op->op_args = args;
1637 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1638 return FAILURE;
1639 } else if (eql(action, "rel_line_to")) {
1640 struct move_to_args *args;
1642 if ((args = malloc(sizeof(*args))) == NULL)
1643 OOM_ABORT;
1644 op->op = REL_LINE_TO;
1645 op->op_args = args;
1646 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1647 return FAILURE;
1648 } else if (eql(action, "move_to")) {
1649 struct move_to_args *args;
1651 if ((args = malloc(sizeof(*args))) == NULL)
1652 OOM_ABORT;
1653 op->op = MOVE_TO;
1654 op->op_args = args;
1655 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1656 return FAILURE;
1657 } else if (eql(action, "rel_move_to")) {
1658 struct move_to_args *args;
1660 if ((args = malloc(sizeof(*args))) == NULL)
1661 OOM_ABORT;
1662 op->op = REL_MOVE_TO;
1663 op->op_args = args;
1664 if (sscanf(raw_args, "%lf %lf %c", &args->x, &args->y, &dummy) != 2)
1665 return FAILURE;
1666 } else if (eql(action, "arc")) {
1667 struct arc_args *args;
1668 double deg1, deg2;
1670 if ((args = malloc(sizeof(*args))) == NULL)
1671 OOM_ABORT;
1672 op->op = ARC;
1673 op->op_args = args;
1674 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %c",
1675 &args->x, &args->y, &args->radius, &deg1, &deg2, &dummy) != 5)
1676 return FAILURE;
1677 args->angle1 = deg1 * (M_PI / 180.L);
1678 args->angle2 = deg2 * (M_PI / 180.L);
1679 } else if (eql(action, "arc_negative")) {
1680 double deg1, deg2;
1681 struct arc_args *args;
1683 if ((args = malloc(sizeof(*args))) == NULL)
1684 OOM_ABORT;
1685 op->op = ARC_NEGATIVE;
1686 op->op_args = args;
1687 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %c",
1688 &args->x, &args->y, &args->radius, &deg1, &deg2, &dummy) != 5)
1689 return FAILURE;
1690 args->angle1 = deg1 * (M_PI / 180.L);
1691 args->angle2 = deg2 * (M_PI / 180.L);
1692 } else if (eql(action, "curve_to")) {
1693 struct curve_to_args *args;
1695 if ((args = malloc(sizeof(*args))) == NULL)
1696 OOM_ABORT;
1697 op->op = CURVE_TO;
1698 op->op_args = args;
1699 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1700 &args->x1, &args->y1, &args->x2, &args->y2, &args->x3, &args->y3, &dummy) != 6)
1701 return FAILURE;
1702 } else if (eql(action, "rel_curve_to")) {
1703 struct curve_to_args *args;
1705 if ((args = malloc(sizeof(*args))) == NULL)
1706 OOM_ABORT;
1707 op->op = REL_CURVE_TO;
1708 op->op_args = args;
1709 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1710 &args->x1, &args->y1, &args->x2, &args->y2, &args->x3, &args->y3, &dummy) != 6)
1711 return FAILURE;
1712 } else if (eql(action, "rectangle")) {
1713 struct rectangle_args *args;
1715 if ((args = malloc(sizeof(*args))) == NULL)
1716 OOM_ABORT;
1717 op->op = RECTANGLE;
1718 op->op_args = args;
1719 if (sscanf(raw_args, "%lf %lf %lf %lf %c",
1720 &args->x, &args->y, &args->width, &args->height, &dummy) != 4)
1721 return FAILURE;
1722 } else if (eql(action, "close_path")) {
1723 op->op = CLOSE_PATH;
1724 if (sscanf(raw_args, " %c", &dummy) > 0)
1725 return FAILURE;
1726 op->op_args = NULL;
1727 } else if (eql(action, "show_text")) {
1728 struct show_text_args *args;
1729 int len;
1731 len = strlen(raw_args) + 1;
1732 if ((args = malloc(sizeof(*args) + len * sizeof(args->text[0]))) == NULL)
1733 OOM_ABORT;
1734 op->op = SHOW_TEXT;
1735 op->op_args = args;
1736 args->len = len; /* not used */
1737 strncpy(args->text, raw_args, len);
1738 result = NEED_REDRAW;
1739 } else if (eql(action, "rel_move_for")) {
1740 char ref_point[2 + 1];
1741 int start, len;
1742 struct rel_move_for_args *args;
1744 if (sscanf(raw_args, "%2s %n", ref_point, &start) < 1)
1745 return FAILURE;
1746 len = strlen(raw_args + start) + 1;
1747 if ((args = malloc(sizeof(*args) + len * sizeof(args->text[0]))) == NULL)
1748 OOM_ABORT;
1749 if (eql(ref_point, "c"))
1750 args->ref = C;
1751 else if (eql(ref_point, "e"))
1752 args->ref = E;
1753 else if (eql(ref_point, "n"))
1754 args->ref = N;
1755 else if (eql(ref_point, "ne"))
1756 args->ref = NE;
1757 else if (eql(ref_point, "nw"))
1758 args->ref = NW;
1759 else if (eql(ref_point, "s"))
1760 args->ref = S;
1761 else if (eql(ref_point, "se"))
1762 args->ref = SE;
1763 else if (eql(ref_point, "sw"))
1764 args->ref = SW;
1765 else if (eql(ref_point, "w"))
1766 args->ref = W;
1767 else
1768 return FAILURE;
1769 op->op = REL_MOVE_FOR;
1770 op->op_args = args;
1771 args->len = len; /* not used */
1772 strncpy(args->text, (raw_args + start), len);
1773 } else if (eql(action, "stroke")) {
1774 op->op = STROKE;
1775 if (sscanf(raw_args, " %c", &dummy) > 0)
1776 return FAILURE;
1777 op->op_args = NULL;
1778 result = NEED_REDRAW;
1779 } else if (eql(action, "stroke_preserve")) {
1780 op->op = STROKE_PRESERVE;
1781 if (sscanf(raw_args, " %c", &dummy) > 0)
1782 return FAILURE;
1783 op->op_args = NULL;
1784 result = NEED_REDRAW;
1785 } else if (eql(action, "fill")) {
1786 op->op = FILL;
1787 if (sscanf(raw_args, " %c", &dummy) > 0)
1788 return FAILURE;
1789 op->op_args = NULL;
1790 result = NEED_REDRAW;
1791 } else if (eql(action, "fill_preserve")) {
1792 op->op = FILL_PRESERVE;
1793 if (sscanf(raw_args, " %c", &dummy) > 0)
1794 return FAILURE;
1795 op->op_args = NULL;
1796 result = NEED_REDRAW;
1797 } else if (eql(action, "set_dash")) {
1798 char *next, *end;
1799 char data1[strlen(raw_args) + 1];
1800 int n, i;
1801 struct set_dash_args *args;
1803 strcpy(data1, raw_args);
1804 next = end = data1;
1805 n = -1;
1806 do {
1807 n++;
1808 next = end;
1809 strtod(next, &end);
1810 } while (next != end);
1811 if ((args = malloc(sizeof(*args) + n * sizeof(args->dashes[0]))) == NULL)
1812 OOM_ABORT;
1813 op->op = SET_DASH;
1814 op->op_args = args;
1815 args->num_dashes = n;
1816 for (i = 0, next = data1; i < n; i++, next = end) {
1817 args->dashes[i] = strtod(next, &end);
1819 } else if (eql(action, "set_font_face")) {
1820 char slant[7 + 1]; /* "oblique" */
1821 char weight[6 + 1]; /* "normal" */
1822 int family_start, family_len;
1823 struct set_font_face_args *args;
1825 if (sscanf(raw_args, "%7s %6s %n%*s", slant, weight, &family_start) != 2)
1826 return FAILURE;
1827 family_len = strlen(raw_args + family_start) + 1;
1828 if ((args = malloc(sizeof(*args) + family_len * sizeof(args->family[0]))) == NULL)
1829 OOM_ABORT;
1830 op->op = SET_FONT_FACE;
1831 op->op_args = args;
1832 strncpy(args->family, raw_args + family_start, family_len);
1833 if (eql(slant, "normal"))
1834 args->slant = CAIRO_FONT_SLANT_NORMAL;
1835 else if (eql(slant, "italic"))
1836 args->slant = CAIRO_FONT_SLANT_ITALIC;
1837 else if (eql(slant, "oblique"))
1838 args->slant = CAIRO_FONT_SLANT_OBLIQUE;
1839 else
1840 return FAILURE;
1841 if (eql(weight, "normal"))
1842 args->weight = CAIRO_FONT_WEIGHT_NORMAL;
1843 else if (eql(weight, "bold"))
1844 args->weight = CAIRO_FONT_WEIGHT_BOLD;
1845 else
1846 return FAILURE;
1847 } else if (eql(action, "set_font_size")) {
1848 struct set_font_size_args *args;
1850 if ((args = malloc(sizeof(*args))) == NULL)
1851 OOM_ABORT;
1852 op->op = SET_FONT_SIZE;
1853 op->op_args = args;
1854 if (sscanf(raw_args, "%lf %c", &args->size, &dummy) != 1)
1855 return FAILURE;
1856 } else if (eql(action, "set_line_cap")) {
1857 char str[6 + 1]; /* "square" */
1858 struct set_line_cap_args *args;
1860 if ((args = malloc(sizeof(*args))) == NULL)
1861 OOM_ABORT;
1862 op->op = SET_LINE_CAP;
1863 op->op_args = args;
1864 if (sscanf(raw_args, "%6s %c", str, &dummy) != 1)
1865 return FAILURE;
1866 if (eql(str, "butt"))
1867 args->line_cap = CAIRO_LINE_CAP_BUTT;
1868 else if (eql(str, "round"))
1869 args->line_cap = CAIRO_LINE_CAP_ROUND;
1870 else if (eql(str, "square"))
1871 args->line_cap = CAIRO_LINE_CAP_SQUARE;
1872 else
1873 return FAILURE;
1874 } else if (eql(action, "set_line_join")) {
1875 char str[5 + 1]; /* "miter" */
1876 struct set_line_join_args *args;
1878 if ((args = malloc(sizeof(*args))) == NULL)
1879 OOM_ABORT;
1880 op->op = SET_LINE_JOIN;
1881 op->op_args = args;
1882 if (sscanf(raw_args, "%5s %c", str, &dummy) != 1)
1883 return FAILURE;
1884 if (eql(str, "miter"))
1885 args->line_join = CAIRO_LINE_JOIN_MITER;
1886 else if (eql(str, "round"))
1887 args->line_join = CAIRO_LINE_JOIN_ROUND;
1888 else if (eql(str, "bevel"))
1889 args->line_join = CAIRO_LINE_JOIN_BEVEL;
1890 else
1891 return FAILURE;
1892 } else if (eql(action, "set_line_width")) {
1893 struct set_line_width_args *args;
1895 if ((args = malloc(sizeof(*args))) == NULL)
1896 OOM_ABORT;
1897 op->op = SET_LINE_WIDTH;
1898 op->op_args = args;
1899 if (sscanf(raw_args, "%lf %c", &args->width, &dummy) != 1)
1900 return FAILURE;
1901 } else if (eql(action, "set_source_rgba")) {
1902 struct set_source_rgba_args *args;
1904 if ((args = malloc(sizeof(*args))) == NULL)
1905 OOM_ABORT;
1906 op->op = SET_SOURCE_RGBA;
1907 op->op_args = args;
1908 gdk_rgba_parse(&args->color, raw_args);
1909 } else if (eql(action, "transform")) {
1910 char dummy;
1911 double xx, yx, xy, yy, x0, y0;
1913 if (sscanf(raw_args, "%lf %lf %lf %lf %lf %lf %c",
1914 &xx, &yx, &xy, &yy, &x0, &y0, &dummy) == 6) {
1915 struct transform_args *args;
1917 if ((args = malloc(sizeof(*args))) == NULL)
1918 OOM_ABORT;
1919 op->op_args = args;
1920 op->op = TRANSFORM;
1921 cairo_matrix_init(&args->matrix, xx, yx, xy, yy, x0, y0);
1922 } else if (sscanf(raw_args, " %c", &dummy) < 1) {
1923 op->op = RESET_CTM;
1924 op->op_args = NULL;
1925 } else
1926 return FAILURE;
1927 } else if (eql(action, "translate")) {
1928 double tx, ty;
1929 struct transform_args *args;
1931 if ((args = malloc(sizeof(*args))) == NULL)
1932 OOM_ABORT;
1933 op->op = TRANSFORM;
1934 op->op_args = args;
1935 if (sscanf(raw_args, "%lf %lf %c", &tx, &ty, &dummy) != 2)
1936 return FAILURE;
1937 cairo_matrix_init_translate(&args->matrix, tx, ty);
1938 } else if (eql(action, "scale")) {
1939 double sx, sy;
1940 struct transform_args *args;
1942 if ((args = malloc(sizeof(*args))) == NULL)
1943 OOM_ABORT;
1944 op->op = TRANSFORM;
1945 op->op_args = args;
1946 if (sscanf(raw_args, "%lf %lf %c", &sx, &sy, &dummy) != 2)
1947 return FAILURE;
1948 cairo_matrix_init_scale(&args->matrix, sx, sy);
1949 } else if (eql(action, "rotate")) {
1950 double angle;
1951 struct transform_args *args;
1953 if ((args = malloc(sizeof(*args))) == NULL)
1954 OOM_ABORT;
1955 op->op = TRANSFORM;
1956 op->op_args = args;
1957 if (sscanf(raw_args, "%lf %c", &angle, &dummy) != 1)
1958 return FAILURE;
1959 cairo_matrix_init_rotate(&args->matrix, angle * (M_PI / 180.L));
1960 } else
1961 return FAILURE;
1962 return result;
1966 * Add another element to widget's "draw_ops" list
1968 static enum draw_op_stat
1969 ins_draw_op(GObject *widget, const char *action, const char *data)
1971 enum draw_op_stat result;
1972 struct draw_op *new_op = NULL, *draw_ops = NULL, *prev_op = NULL;
1974 if ((new_op = malloc(sizeof(*new_op))) == NULL)
1975 OOM_ABORT;
1976 new_op->op_args = NULL;
1977 new_op->next = NULL;
1978 if ((result = set_draw_op(new_op, action, data)) == FAILURE) {
1979 free(new_op->op_args);
1980 free(new_op);
1981 return FAILURE;
1983 switch (new_op->policy) {
1984 case APPEND:
1985 if ((draw_ops = g_object_get_data(widget, "draw_ops")) == NULL)
1986 g_object_set_data(widget, "draw_ops", new_op);
1987 else {
1988 for (prev_op = draw_ops;
1989 prev_op->next != NULL;
1990 prev_op = prev_op->next);
1991 prev_op->next = new_op;
1993 break;
1994 case BEFORE:
1995 for (prev_op = NULL, draw_ops = g_object_get_data(widget, "draw_ops");
1996 draw_ops != NULL && draw_ops->id != new_op->before;
1997 prev_op = draw_ops, draw_ops = draw_ops->next);
1998 if (prev_op == NULL) { /* prepend a new first element */
1999 g_object_set_data(widget, "draw_ops", new_op);
2000 new_op->next = draw_ops;
2001 } else if (draw_ops == NULL) /* append */
2002 prev_op->next = new_op;
2003 else { /* insert */
2004 new_op->next = draw_ops;
2005 prev_op->next = new_op;
2007 break;
2008 case REPLACE:
2009 for (prev_op = NULL, draw_ops = g_object_get_data(widget, "draw_ops");
2010 draw_ops != NULL && draw_ops->id != new_op->id;
2011 prev_op = draw_ops, draw_ops = draw_ops->next);
2012 if (draw_ops == NULL && prev_op == NULL) /* start a new list */
2013 g_object_set_data(widget, "draw_ops", new_op);
2014 else if (prev_op == NULL) { /* replace the first element */
2015 g_object_set_data(widget, "draw_ops", new_op);
2016 new_op->next = draw_ops->next;
2017 free(draw_ops->op_args);
2018 free(draw_ops);
2019 } else if (draw_ops == NULL) /* append */
2020 prev_op->next = new_op;
2021 else { /* replace some other element */
2022 new_op->next = draw_ops->next;
2023 prev_op->next = new_op;
2024 free(draw_ops->op_args);
2025 free(draw_ops);
2027 break;
2028 default:
2029 ABORT;
2030 break;
2032 return result;
2036 * Remove all elements with the given id from widget's "draw_ops" list
2038 static enum draw_op_stat
2039 rem_draw_op(GObject *widget, const char *data)
2041 char dummy;
2042 struct draw_op *op, *next_op, *prev_op = NULL;
2043 unsigned long long int id;
2045 if (sscanf(data, "%llu %c", &id, &dummy) != 1)
2046 return FAILURE;
2047 op = g_object_get_data(widget, "draw_ops");
2048 while (op != NULL) {
2049 next_op = op->next;
2050 if (op->id == id) {
2051 if (prev_op == NULL) /* list head */
2052 g_object_set_data(widget, "draw_ops", op->next);
2053 else
2054 prev_op->next = op->next;
2055 free(op->op_args);
2056 free(op);
2057 } else
2058 prev_op = op;
2059 op = next_op;
2061 return NEED_REDRAW;
2064 static gboolean
2065 refresh_widget(GtkWidget *widget)
2067 gint height = gtk_widget_get_allocated_height(widget);
2068 gint width = gtk_widget_get_allocated_width(widget);
2070 gtk_widget_queue_draw_area(widget, 0, 0, width, height);
2071 return G_SOURCE_REMOVE;
2074 static void
2075 update_drawing_area(struct ui_data *ud)
2077 enum draw_op_stat dost;
2079 if (eql(ud->action, "remove"))
2080 dost = rem_draw_op(ud->obj, ud->data);
2081 else
2082 dost = ins_draw_op(ud->obj, ud->action, ud->data);
2083 switch (dost) {
2084 case NEED_REDRAW:
2085 gdk_threads_add_idle_full(G_PRIORITY_LOW,
2086 (GSourceFunc) refresh_widget,
2087 GTK_WIDGET(ud->obj), NULL);
2088 break;
2089 case FAILURE:
2090 try_generic_cmds(ud);
2091 break;
2092 case SUCCESS:
2093 break;
2094 default:
2095 ABORT;
2096 break;
2100 static void
2101 update_entry(struct ui_data *ud)
2103 GtkEntry *entry = GTK_ENTRY(ud->obj);
2105 if (eql(ud->action, "set_text"))
2106 gtk_entry_set_text(entry, ud->data);
2107 else if (eql(ud->action, "set_placeholder_text"))
2108 gtk_entry_set_placeholder_text(entry, ud->data);
2109 else
2110 try_generic_cmds(ud);
2113 static void
2114 update_expander(struct ui_data *ud)
2116 GtkExpander *expander = GTK_EXPANDER(ud->obj);
2117 char dummy;
2118 unsigned int val;
2120 if (eql(ud->action, "set_expanded") &&
2121 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2122 gtk_expander_set_expanded(expander, val);
2123 else if (eql(ud->action, "set_label"))
2124 gtk_expander_set_label(expander, ud->data);
2125 else
2126 try_generic_cmds(ud);
2129 static void
2130 update_file_chooser_button(struct ui_data *ud)
2132 if (eql(ud->action, "set_filename"))
2133 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(ud->obj), ud->data);
2134 else
2135 try_generic_cmds(ud);
2138 static void
2139 update_file_chooser_dialog(struct ui_data *ud)
2141 GtkFileChooser *chooser = GTK_FILE_CHOOSER(ud->obj);
2143 if (eql(ud->action, "set_filename"))
2144 gtk_file_chooser_set_filename(chooser, ud->data);
2145 else if (eql(ud->action, "set_current_name"))
2146 gtk_file_chooser_set_current_name(chooser, ud->data);
2147 else if (update_class_window(ud));
2148 else
2149 try_generic_cmds(ud);
2152 static void
2153 update_font_button(struct ui_data *ud){
2154 GtkFontButton *font_button = GTK_FONT_BUTTON(ud->obj);
2156 if (eql(ud->action, "set_font_name"))
2157 gtk_font_button_set_font_name(font_button, ud->data);
2158 else
2159 try_generic_cmds(ud);
2162 static void
2163 update_frame(struct ui_data *ud)
2165 if (eql(ud->action, "set_label"))
2166 gtk_frame_set_label(GTK_FRAME(ud->obj), ud->data);
2167 else
2168 try_generic_cmds(ud);
2171 static void
2172 update_image(struct ui_data *ud)
2174 GtkIconSize size;
2175 GtkImage *image = GTK_IMAGE(ud->obj);
2177 gtk_image_get_icon_name(image, NULL, &size);
2178 if (eql(ud->action, "set_from_file"))
2179 gtk_image_set_from_file(image, ud->data);
2180 else if (eql(ud->action, "set_from_icon_name"))
2181 gtk_image_set_from_icon_name(image, ud->data, size);
2182 else
2183 try_generic_cmds(ud);
2186 static void
2187 update_label(struct ui_data *ud)
2189 if (eql(ud->action, "set_text"))
2190 gtk_label_set_text(GTK_LABEL(ud->obj), ud->data);
2191 else
2192 try_generic_cmds(ud);
2195 static void
2196 update_menu_item(struct ui_data *ud)
2198 try_generic_cmds(ud);
2201 static void
2202 update_notebook(struct ui_data *ud)
2204 char dummy;
2205 int val, n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(ud->obj));
2207 if (eql(ud->action, "set_current_page") &&
2208 sscanf(ud->data, "%d %c", &val, &dummy) == 1 &&
2209 val >= 0 && val < n_pages)
2210 gtk_notebook_set_current_page(GTK_NOTEBOOK(ud->obj), val);
2211 else
2212 try_generic_cmds(ud);
2215 static void
2216 update_nothing(struct ui_data *ud)
2218 (void) ud;
2221 static void
2222 update_print_dialog(struct ui_data *ud)
2224 GtkPageSetup *page_setup;
2225 GtkPrintJob *job;
2226 GtkPrintSettings *settings;
2227 GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG(ud->obj);
2228 GtkPrinter *printer;
2229 gint response_id;
2231 if (eql(ud->action, "print")) {
2232 response_id = gtk_dialog_run(GTK_DIALOG(dialog));
2233 switch (response_id) {
2234 case GTK_RESPONSE_OK:
2235 printer = gtk_print_unix_dialog_get_selected_printer(dialog);
2236 settings = gtk_print_unix_dialog_get_settings(dialog);
2237 page_setup = gtk_print_unix_dialog_get_page_setup(dialog);
2238 job = gtk_print_job_new(ud->data, printer, settings, page_setup);
2239 if (gtk_print_job_set_source_file(job, ud->data, NULL))
2240 gtk_print_job_send(job, NULL, NULL, NULL);
2241 else
2242 ign_cmd(ud->type, ud->cmd);
2243 g_clear_object(&settings);
2244 g_clear_object(&job);
2245 break;
2246 case GTK_RESPONSE_CANCEL:
2247 case GTK_RESPONSE_DELETE_EVENT:
2248 break;
2249 default:
2250 fprintf(stderr, "%s sent an unexpected response id (%d)\n",
2251 widget_id(GTK_BUILDABLE(dialog)), response_id);
2252 break;
2254 gtk_widget_hide(GTK_WIDGET(dialog));
2255 } else
2256 try_generic_cmds(ud);
2259 static void
2260 update_progress_bar(struct ui_data *ud)
2262 GtkProgressBar *progressbar = GTK_PROGRESS_BAR(ud->obj);
2263 char dummy;
2264 double frac;
2266 if (eql(ud->action, "set_text"))
2267 gtk_progress_bar_set_text(progressbar, *(ud->data) == '\0' ? NULL : ud->data);
2268 else if (eql(ud->action, "set_fraction") &&
2269 sscanf(ud->data, "%lf %c", &frac, &dummy) == 1)
2270 gtk_progress_bar_set_fraction(progressbar, frac);
2271 else
2272 try_generic_cmds(ud);
2275 static void
2276 update_scale(struct ui_data *ud)
2278 GtkRange *range = GTK_RANGE(ud->obj);
2279 char dummy;
2280 double val1, val2;
2282 if (eql(ud->action, "set_value") && sscanf(ud->data, "%lf %c", &val1, &dummy) == 1)
2283 gtk_range_set_value(range, val1);
2284 else if (eql(ud->action, "set_fill_level") &&
2285 sscanf(ud->data, "%lf %c", &val1, &dummy) == 1) {
2286 gtk_range_set_fill_level(range, val1);
2287 gtk_range_set_show_fill_level(range, TRUE);
2288 } else if (eql(ud->action, "set_fill_level") &&
2289 sscanf(ud->data, " %c", &dummy) < 1)
2290 gtk_range_set_show_fill_level(range, FALSE);
2291 else if (eql(ud->action, "set_range") &&
2292 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2293 gtk_range_set_range(range, val1, val2);
2294 else if (eql(ud->action, "set_increments") &&
2295 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2296 gtk_range_set_increments(range, val1, val2);
2297 else
2298 try_generic_cmds(ud);
2301 static void
2302 update_scrolled_window(struct ui_data *ud)
2304 GtkScrolledWindow *window = GTK_SCROLLED_WINDOW(ud->obj);
2305 GtkAdjustment *hadj = gtk_scrolled_window_get_hadjustment(window);
2306 GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(window);
2307 char dummy;
2308 double d0, d1;
2310 if (eql(ud->action, "hscroll") && sscanf(ud->data, "%lf %c", &d0, &dummy) == 1)
2311 gtk_adjustment_set_value(hadj, d0);
2312 else if (eql(ud->action, "vscroll") && sscanf(ud->data, "%lf %c", &d0, &dummy) == 1)
2313 gtk_adjustment_set_value(vadj, d0);
2314 else if (eql(ud->action, "hscroll_to_range") &&
2315 sscanf(ud->data, "%lf %lf %c", &d0, &d1, &dummy) == 2)
2316 gtk_adjustment_clamp_page(hadj, d0, d1);
2317 else if (eql(ud->action, "vscroll_to_range") &&
2318 sscanf(ud->data, "%lf %lf %c", &d0, &d1, &dummy) == 2)
2319 gtk_adjustment_clamp_page(vadj, d0, d1);
2320 else
2321 try_generic_cmds(ud);
2324 static void
2325 update_socket(struct ui_data *ud)
2327 GtkSocket *socket = GTK_SOCKET(ud->obj);
2328 Window id;
2329 char str[BUFLEN], dummy;
2331 if (eql(ud->action, "id") && sscanf(ud->data, " %c", &dummy) < 1) {
2332 id = gtk_socket_get_id(socket);
2333 snprintf(str, BUFLEN, "%lu", id);
2334 send_msg(ud->args->fout, GTK_BUILDABLE(socket), "id", str, NULL);
2335 } else
2336 try_generic_cmds(ud);
2339 static void
2340 update_spin_button(struct ui_data *ud)
2342 GtkSpinButton *spinbutton = GTK_SPIN_BUTTON(ud->obj);
2343 char dummy;
2344 double val1, val2;
2346 if (eql(ud->action, "set_text") && /* TODO: rename to "set_value" */
2347 sscanf(ud->data, "%lf %c", &val1, &dummy) == 1)
2348 gtk_spin_button_set_value(spinbutton, val1);
2349 else if (eql(ud->action, "set_range") &&
2350 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2351 gtk_spin_button_set_range(spinbutton, val1, val2);
2352 else if (eql(ud->action, "set_increments") &&
2353 sscanf(ud->data, "%lf %lf %c", &val1, &val2, &dummy) == 2)
2354 gtk_spin_button_set_increments(spinbutton, val1, val2);
2355 else
2356 try_generic_cmds(ud);
2359 static void
2360 update_spinner(struct ui_data *ud)
2362 GtkSpinner *spinner = GTK_SPINNER(ud->obj);
2363 char dummy;
2365 if (eql(ud->action, "start") && sscanf(ud->data, " %c", &dummy) < 1)
2366 gtk_spinner_start(spinner);
2367 else if (eql(ud->action, "stop") && sscanf(ud->data, " %c", &dummy) < 1)
2368 gtk_spinner_stop(spinner);
2369 else
2370 try_generic_cmds(ud);
2373 static void
2374 update_statusbar(struct ui_data *ud)
2376 GtkStatusbar *statusbar = GTK_STATUSBAR(ud->obj);
2377 char *ctx_msg, dummy;
2378 const char *status_msg;
2379 int ctx_len, t;
2381 /* TODO: remove "push", "pop", "remove_all"; rename "push_id" to "push", etc. */
2382 if ((ctx_msg = malloc(strlen(ud->data) + 1)) == NULL)
2383 OOM_ABORT;
2384 t = sscanf(ud->data, "%s %n%c", ctx_msg, &ctx_len, &dummy);
2385 status_msg = ud->data + ctx_len;
2386 if (eql(ud->action, "push"))
2387 gtk_statusbar_push(statusbar,
2388 gtk_statusbar_get_context_id(statusbar, "0"),
2389 ud->data);
2390 else if (eql(ud->action, "push_id") && t >= 1)
2391 gtk_statusbar_push(statusbar,
2392 gtk_statusbar_get_context_id(statusbar, ctx_msg),
2393 status_msg);
2394 else if (eql(ud->action, "pop") && t < 1)
2395 gtk_statusbar_pop(statusbar,
2396 gtk_statusbar_get_context_id(statusbar, "0"));
2397 else if (eql(ud->action, "pop_id") && t == 1)
2398 gtk_statusbar_pop(statusbar,
2399 gtk_statusbar_get_context_id(statusbar, ctx_msg));
2400 else if (eql(ud->action, "remove_all") && t < 1)
2401 gtk_statusbar_remove_all(statusbar,
2402 gtk_statusbar_get_context_id(statusbar, "0"));
2403 else if (eql(ud->action, "remove_all_id") && t == 1)
2404 gtk_statusbar_remove_all(statusbar,
2405 gtk_statusbar_get_context_id(statusbar, ctx_msg));
2406 else
2407 try_generic_cmds(ud);
2408 free(ctx_msg);
2411 static void
2412 update_switch(struct ui_data *ud)
2414 char dummy;
2415 unsigned int val;
2417 if (eql(ud->action, "set_active") &&
2418 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2419 gtk_switch_set_active(GTK_SWITCH(ud->obj), val);
2420 else
2421 try_generic_cmds(ud);
2424 static void
2425 update_text_view(struct ui_data *ud)
2427 FILE *sv;
2428 GtkTextView *view = GTK_TEXT_VIEW(ud->obj);
2429 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(view);
2430 GtkTextIter a, b;
2431 char dummy;
2432 int val;
2434 if (eql(ud->action, "set_text"))
2435 gtk_text_buffer_set_text(textbuf, ud->data, -1);
2436 else if (eql(ud->action, "delete") && sscanf(ud->data, " %c", &dummy) < 1) {
2437 gtk_text_buffer_get_bounds(textbuf, &a, &b);
2438 gtk_text_buffer_delete(textbuf, &a, &b);
2439 } else if (eql(ud->action, "insert_at_cursor"))
2440 gtk_text_buffer_insert_at_cursor(textbuf, ud->data, -1);
2441 else if (eql(ud->action, "place_cursor") && eql(ud->data, "end")) {
2442 gtk_text_buffer_get_end_iter(textbuf, &a);
2443 gtk_text_buffer_place_cursor(textbuf, &a);
2444 } else if (eql(ud->action, "place_cursor") &&
2445 sscanf(ud->data, "%d %c", &val, &dummy) == 1) {
2446 gtk_text_buffer_get_iter_at_offset(textbuf, &a, val);
2447 gtk_text_buffer_place_cursor(textbuf, &a);
2448 } else if (eql(ud->action, "place_cursor_at_line") &&
2449 sscanf(ud->data, "%d %c", &val, &dummy) == 1) {
2450 gtk_text_buffer_get_iter_at_line(textbuf, &a, val);
2451 gtk_text_buffer_place_cursor(textbuf, &a);
2452 } else if (eql(ud->action, "scroll_to_cursor") &&
2453 sscanf(ud->data, " %c", &dummy) < 1)
2454 gtk_text_view_scroll_to_mark(view, gtk_text_buffer_get_insert(textbuf),
2455 0., 0, 0., 0.);
2456 else if (eql(ud->action, "save") && ud->data != NULL &&
2457 (sv = fopen(ud->data, "w")) != NULL) {
2458 gtk_text_buffer_get_bounds(textbuf, &a, &b);
2459 send_msg(sv, GTK_BUILDABLE(view), "insert_at_cursor",
2460 gtk_text_buffer_get_text(textbuf, &a, &b, TRUE), NULL);
2461 fclose(sv);
2462 } else
2463 try_generic_cmds(ud);
2466 static void
2467 update_toggle_button(struct ui_data *ud)
2469 char dummy;
2470 unsigned int val;
2472 if (eql(ud->action, "set_label"))
2473 gtk_button_set_label(GTK_BUTTON(ud->obj), ud->data);
2474 else if (eql(ud->action, "set_active") &&
2475 sscanf(ud->data, "%u %c", &val, &dummy) == 1 && val < 2)
2476 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ud->obj), val);
2477 else
2478 try_generic_cmds(ud);
2482 * update_tree_view(), which runs inside gtk_main(), needs a few
2483 * helper functions
2487 * Check if s is a valid string representation of a GtkTreePath
2489 static bool
2490 is_path_string(char *s)
2492 return s != NULL &&
2493 strlen(s) == strspn(s, ":0123456789") &&
2494 strstr(s, "::") == NULL &&
2495 strcspn(s, ":") > 0;
2498 static void
2499 tree_model_insert_before(GtkTreeModel *model, GtkTreeIter *iter,
2500 GtkTreeIter *parent, GtkTreeIter *sibling)
2502 if (GTK_IS_TREE_STORE(model))
2503 gtk_tree_store_insert_before(GTK_TREE_STORE(model),
2504 iter, parent, sibling);
2505 else if (GTK_IS_LIST_STORE(model))
2506 gtk_list_store_insert_before(GTK_LIST_STORE(model),
2507 iter, sibling);
2508 else
2509 ABORT;
2512 static void
2513 tree_model_insert_after(GtkTreeModel *model, GtkTreeIter *iter,
2514 GtkTreeIter *parent, GtkTreeIter *sibling)
2516 if (GTK_IS_TREE_STORE(model))
2517 gtk_tree_store_insert_after(GTK_TREE_STORE(model),
2518 iter, parent, sibling);
2519 else if (GTK_IS_LIST_STORE(model))
2520 gtk_list_store_insert_after(GTK_LIST_STORE(model),
2521 iter, sibling);
2522 else
2523 ABORT;
2526 static void
2527 tree_model_move_before(GtkTreeModel *model, GtkTreeIter *iter,
2528 GtkTreeIter *position)
2530 if (GTK_IS_TREE_STORE(model))
2531 gtk_tree_store_move_before(GTK_TREE_STORE(model), iter, position);
2532 else if (GTK_IS_LIST_STORE(model))
2533 gtk_list_store_move_before(GTK_LIST_STORE(model), iter, position);
2534 else
2535 ABORT;
2538 static void
2539 tree_model_remove(GtkTreeModel *model, GtkTreeIter *iter)
2541 if (GTK_IS_TREE_STORE(model))
2542 gtk_tree_store_remove(GTK_TREE_STORE(model), iter);
2543 else if (GTK_IS_LIST_STORE(model))
2544 gtk_list_store_remove(GTK_LIST_STORE(model), iter);
2545 else
2546 ABORT;
2549 static void
2550 tree_model_clear(GtkTreeModel *model)
2552 if (GTK_IS_TREE_STORE(model))
2553 gtk_tree_store_clear(GTK_TREE_STORE(model));
2554 else if (GTK_IS_LIST_STORE(model))
2555 gtk_list_store_clear(GTK_LIST_STORE(model));
2556 else
2557 ABORT;
2560 static void
2561 tree_model_set(GtkTreeModel *model, GtkTreeIter *iter, ...)
2563 va_list ap;
2565 va_start(ap, iter);
2566 if (GTK_IS_TREE_STORE(model))
2567 gtk_tree_store_set_valist(GTK_TREE_STORE(model), iter, ap);
2568 else if (GTK_IS_LIST_STORE(model))
2569 gtk_list_store_set_valist(GTK_LIST_STORE(model), iter, ap);
2570 else
2571 ABORT;
2572 va_end(ap);
2576 * Create an empty row at path if it doesn't yet exist. Create older
2577 * siblings and parents as necessary.
2579 static void
2580 create_subtree(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter)
2582 GtkTreeIter iter_1; /* iter's predecessor */
2583 GtkTreePath *path_1; /* path's predecessor */
2585 if (gtk_tree_path_get_depth(path) > 0 &&
2586 gtk_tree_model_get_iter(model, iter, path))
2587 return;
2588 path_1 = gtk_tree_path_copy(path);
2589 if (gtk_tree_path_prev(path_1)) { /* need an older sibling */
2590 create_subtree(model, path_1, iter);
2591 iter_1 = *iter;
2592 tree_model_insert_after(model, iter, NULL, &iter_1);
2593 } else if (gtk_tree_path_up(path_1)) { /* need a parent */
2594 create_subtree(model, path_1, iter);
2595 if (gtk_tree_path_get_depth(path_1) == 0)
2596 /* first toplevel row */
2597 tree_model_insert_after(model, iter, NULL, NULL);
2598 else { /* first row in a lower level */
2599 iter_1 = *iter;
2600 tree_model_insert_after(model, iter, &iter_1, NULL);
2602 } /* neither prev nor up mean we're at the root of an empty tree */
2603 gtk_tree_path_free(path_1);
2606 static bool
2607 set_tree_view_cell(GtkTreeModel *model, GtkTreeIter *iter,
2608 const char *path_s, int col, const char *new_text)
2610 GType col_type = gtk_tree_model_get_column_type(model, col);
2611 GtkTreePath *path;
2612 bool ok = false;
2613 char dummy;
2614 double d;
2615 long long int n;
2617 path = gtk_tree_path_new_from_string(path_s);
2618 switch (col_type) {
2619 case G_TYPE_BOOLEAN:
2620 case G_TYPE_INT:
2621 case G_TYPE_LONG:
2622 case G_TYPE_INT64:
2623 case G_TYPE_UINT:
2624 case G_TYPE_ULONG:
2625 case G_TYPE_UINT64:
2626 if (new_text != NULL &&
2627 sscanf(new_text, "%lld %c", &n, &dummy) == 1) {
2628 create_subtree(model, path, iter);
2629 tree_model_set(model, iter, col, n, -1);
2630 ok = true;
2632 break;
2633 case G_TYPE_FLOAT:
2634 case G_TYPE_DOUBLE:
2635 if (new_text != NULL &&
2636 sscanf(new_text, "%lf %c", &d, &dummy) == 1) {
2637 create_subtree(model, path, iter);
2638 tree_model_set(model, iter, col, d, -1);
2639 ok = true;
2641 break;
2642 case G_TYPE_STRING:
2643 create_subtree(model, path, iter);
2644 tree_model_set(model, iter, col, new_text, -1);
2645 ok = true;
2646 break;
2647 default:
2648 fprintf(stderr, "column %d: %s not implemented\n",
2649 col, g_type_name(col_type));
2650 ok = true;
2651 break;
2653 gtk_tree_path_free(path);
2654 return ok;
2657 static void
2658 tree_view_set_cursor(GtkTreeView *view, GtkTreePath *path, GtkTreeViewColumn *col)
2660 /* GTK+ 3.14 requires this. For 3.18, path = NULL */
2661 /* is just fine and this function need not exist. */
2662 if (path == NULL)
2663 path = gtk_tree_path_new();
2664 gtk_tree_view_set_cursor(view, path, col, false);
2667 static void
2668 update_tree_view(struct ui_data *ud)
2670 GtkTreeView *view = GTK_TREE_VIEW(ud->obj);
2671 GtkTreeIter iter0, iter1;
2672 GtkTreeModel *model = gtk_tree_view_get_model(view);
2673 GtkTreePath *path = NULL;
2674 GtkTreeSelection *sel = gtk_tree_view_get_selection(view);
2675 bool iter0_valid, iter1_valid;
2676 char *tokens, *arg0, *arg1, *arg2;
2677 int col = -1; /* invalid column number */
2678 struct info ar;
2680 if (!GTK_IS_LIST_STORE(model) && !GTK_IS_TREE_STORE(model))
2682 fprintf(stderr, "missing model/");
2683 ign_cmd(ud->type, ud->cmd);
2684 return;
2686 if ((tokens = malloc(strlen(ud->data) + 1)) == NULL)
2687 OOM_ABORT;
2688 strcpy(tokens, ud->data);
2689 arg0 = strtok(tokens, WHITESPACE);
2690 arg1 = strtok(NULL, WHITESPACE);
2691 arg2 = strtok(NULL, "");
2692 iter0_valid = is_path_string(arg0) &&
2693 gtk_tree_model_get_iter_from_string(model, &iter0, arg0);
2694 iter1_valid = is_path_string(arg1) &&
2695 gtk_tree_model_get_iter_from_string(model, &iter1, arg1);
2696 if (is_path_string(arg1))
2697 col = strtol(arg1, NULL, 10);
2698 if (eql(ud->action, "set") &&
2699 col > -1 &&
2700 col < gtk_tree_model_get_n_columns(model) &&
2701 is_path_string(arg0)) {
2702 if (set_tree_view_cell(model, &iter0, arg0, col, arg2) == false)
2703 ign_cmd(ud->type, ud->cmd);
2704 } else if (eql(ud->action, "scroll") && iter0_valid && iter1_valid &&
2705 arg2 == NULL) {
2706 path = gtk_tree_path_new_from_string(arg0);
2707 gtk_tree_view_scroll_to_cell (view,
2708 path,
2709 gtk_tree_view_get_column(view, col),
2710 0, 0., 0.);
2711 } else if (eql(ud->action, "expand") && iter0_valid && arg1 == NULL) {
2712 path = gtk_tree_path_new_from_string(arg0);
2713 gtk_tree_view_expand_row(view, path, false);
2714 } else if (eql(ud->action, "expand_all") && iter0_valid && arg1 == NULL) {
2715 path = gtk_tree_path_new_from_string(arg0);
2716 gtk_tree_view_expand_row(view, path, true);
2717 } else if (eql(ud->action, "expand_all") && arg0 == NULL)
2718 gtk_tree_view_expand_all(view);
2719 else if (eql(ud->action, "collapse") && iter0_valid && arg1 == NULL) {
2720 path = gtk_tree_path_new_from_string(arg0);
2721 gtk_tree_view_collapse_row(view, path);
2722 } else if (eql(ud->action, "collapse") && arg0 == NULL)
2723 gtk_tree_view_collapse_all(view);
2724 else if (eql(ud->action, "set_cursor") && iter0_valid && arg1 == NULL) {
2725 path = gtk_tree_path_new_from_string(arg0);
2726 tree_view_set_cursor(view, path, NULL);
2727 } else if (eql(ud->action, "set_cursor") && arg0 == NULL) {
2728 tree_view_set_cursor(view, NULL, NULL);
2729 gtk_tree_selection_unselect_all(sel);
2730 } else if (eql(ud->action, "insert_row") &&
2731 eql(arg0, "end") && arg1 == NULL)
2732 tree_model_insert_before(model, &iter1, NULL, NULL);
2733 else if (eql(ud->action, "insert_row") && iter0_valid &&
2734 eql(arg1, "as_child") && arg2 == NULL)
2735 tree_model_insert_after(model, &iter1, &iter0, NULL);
2736 else if (eql(ud->action, "insert_row") && iter0_valid && arg1 == NULL)
2737 tree_model_insert_before(model, &iter1, NULL, &iter0);
2738 else if (eql(ud->action, "move_row") && iter0_valid &&
2739 eql(arg1, "end") && arg2 == NULL)
2740 tree_model_move_before(model, &iter0, NULL);
2741 else if (eql(ud->action, "move_row") && iter0_valid && iter1_valid && arg2 == NULL)
2742 tree_model_move_before(model, &iter0, &iter1);
2743 else if (eql(ud->action, "remove_row") && iter0_valid && arg1 == NULL)
2744 tree_model_remove(model, &iter0);
2745 else if (eql(ud->action, "clear") && arg0 == NULL) {
2746 tree_view_set_cursor(view, NULL, NULL);
2747 gtk_tree_selection_unselect_all(sel);
2748 tree_model_clear(model);
2749 } else if (eql(ud->action, "block") && arg0 != NULL) {
2750 ud->obj=G_OBJECT(sel);
2751 update_blocked(ud);
2752 } else if (eql(ud->action, "save") && arg0 != NULL &&
2753 (ar.fout = fopen(arg0, "w")) != NULL) {
2754 ar.obj = ud->obj;
2755 gtk_tree_model_foreach(model,
2756 (GtkTreeModelForeachFunc) save_tree_row_msg,
2757 &ar);
2758 fclose(ar.fout);
2759 } else
2760 try_generic_cmds(ud);
2761 free(tokens);
2762 gtk_tree_path_free(path);
2765 static void
2766 update_window(struct ui_data *ud)
2768 if (!update_class_window(ud))
2769 try_generic_cmds(ud);
2773 * The final UI update. Runs inside gtk_main().
2775 static void
2776 main_quit(struct ui_data *ud)
2778 char dummy;
2780 if (sscanf(ud->data, " %c", &dummy) < 1)
2781 gtk_main_quit();
2782 else
2783 try_generic_cmds(ud);
2787 * Don't update anything; just complain from inside gtk_main()
2789 static void
2790 complain(struct ui_data *ud)
2792 ign_cmd(ud->type, ud->cmd);
2796 * Parse command pointed to by ud, and act on ui accordingly. Runs
2797 * once per command inside gtk_main().
2799 static gboolean
2800 update_ui(struct ui_data *ud)
2802 char *lc = lc_numeric();
2804 (ud->fn)(ud);
2805 free(ud->cmd_tokens);
2806 free(ud->cmd);
2807 free(ud);
2808 lc_numeric_free(lc);
2809 return G_SOURCE_REMOVE;
2813 * Keep track of loading files to avoid recursive loading of the same
2814 * file. If filename = NULL, forget the most recently remembered file.
2816 static bool
2817 remember_loading_file(char *filename)
2819 static char *filenames[BUFLEN];
2820 static size_t latest = 0;
2821 size_t i;
2823 if (filename == NULL) { /* pop */
2824 if (latest < 1)
2825 ABORT;
2826 latest--;
2827 return false;
2828 } else { /* push */
2829 for (i = 1; i <= latest; i++)
2830 if (eql(filename, filenames[i]))
2831 return false;
2832 if (latest > BUFLEN -2)
2833 return false;
2834 filenames[++latest] = filename;
2835 return true;
2840 * Read lines from stream cmd and perform appropriate actions on the
2841 * GUI. Runs inside receiver thread.
2843 static void *
2844 digest_cmd(struct info *ar)
2846 static int recursion = -1; /* > 0 means this is a recursive call */
2848 recursion++;
2849 for (;;) {
2850 FILE *cmd = ar->fin;
2851 struct ui_data *ud = NULL;
2852 char first_char = '\0';
2853 char *id; /* widget id */
2854 size_t msg_size = 32;
2855 int id_start = 0, id_end = 0;
2856 int action_start = 0, action_end = 0;
2857 int data_start = 0;
2859 if (feof(cmd))
2860 break;
2861 if ((ud = malloc(sizeof(*ud))) == NULL)
2862 OOM_ABORT;
2863 if ((ud->cmd = malloc(msg_size)) == NULL)
2864 OOM_ABORT;
2865 ud->args = ar;
2866 ud->type = G_TYPE_INVALID;
2867 pthread_testcancel();
2868 if (recursion == 0)
2869 log_msg(ar->flog, NULL);
2870 data_start = read_buf(cmd, &ud->cmd, &msg_size);
2871 if (recursion == 0)
2872 log_msg(ar->flog, ud->cmd);
2873 if ((ud->cmd_tokens = malloc(strlen(ud->cmd) + 1)) == NULL)
2874 OOM_ABORT;
2875 sscanf(ud->cmd, " %c", &first_char);
2876 if (data_start == 0 || /* empty line */
2877 first_char == '#') { /* comment */
2878 ud->fn = update_nothing;
2879 goto exec;
2881 strcpy(ud->cmd_tokens, ud->cmd);
2882 sscanf(ud->cmd_tokens,
2883 " %n%*[0-9a-zA-Z_-]%n:%n%*[0-9a-zA-Z_]%n%*1[ \t]%n",
2884 &id_start, &id_end, &action_start, &action_end, &data_start);
2885 ud->cmd_tokens[id_end] = ud->cmd_tokens[action_end] = '\0';
2886 id = ud->cmd_tokens + id_start;
2887 ud->action = ud->cmd_tokens + action_start;
2888 ud->data = ud->cmd_tokens + data_start;
2889 if (eql(ud->action, "main_quit")) {
2890 ud->fn = main_quit;
2891 goto exec;
2893 if (eql(ud->action, "load") && strlen(ud->data) > 0 &&
2894 remember_loading_file(ud->data)) {
2895 struct info a = *ar;
2897 if ((a.fin = fopen(ud->data, "r")) != NULL) {
2898 digest_cmd(&a);
2899 fclose(a.fin);
2900 ud->fn = update_nothing;
2901 } else
2902 ud->fn = complain;
2903 remember_loading_file(NULL);
2904 goto exec;
2906 if ((ud->obj = (gtk_builder_get_object(ar->builder, id))) == NULL) {
2907 ud->fn = complain;
2908 goto exec;
2910 ud->type = G_TYPE_FROM_INSTANCE(ud->obj);
2911 if (ud->type == GTK_TYPE_DRAWING_AREA)
2912 ud->fn = update_drawing_area;
2913 else if (ud->type == GTK_TYPE_TREE_VIEW)
2914 ud->fn = update_tree_view;
2915 else if (ud->type == GTK_TYPE_COMBO_BOX_TEXT)
2916 ud->fn = update_combo_box_text;
2917 else if (ud->type == GTK_TYPE_LABEL)
2918 ud->fn = update_label;
2919 else if (ud->type == GTK_TYPE_IMAGE)
2920 ud->fn = update_image;
2921 else if (ud->type == GTK_TYPE_TEXT_VIEW)
2922 ud->fn = update_text_view;
2923 else if (ud->type == GTK_TYPE_NOTEBOOK)
2924 ud->fn = update_notebook;
2925 else if (ud->type == GTK_TYPE_EXPANDER)
2926 ud->fn = update_expander;
2927 else if (ud->type == GTK_TYPE_FRAME)
2928 ud->fn = update_frame;
2929 else if (ud->type == GTK_TYPE_SCROLLED_WINDOW)
2930 ud->fn = update_scrolled_window;
2931 else if (ud->type == GTK_TYPE_BUTTON)
2932 ud->fn = update_button;
2933 else if (ud->type == GTK_TYPE_MENU_ITEM)
2934 ud->fn = update_menu_item;
2935 else if (ud->type == GTK_TYPE_FILE_CHOOSER_DIALOG)
2936 ud->fn = update_file_chooser_dialog;
2937 else if (ud->type == GTK_TYPE_FILE_CHOOSER_BUTTON)
2938 ud->fn = update_file_chooser_button;
2939 else if (ud->type == GTK_TYPE_COLOR_BUTTON)
2940 ud->fn = update_color_button;
2941 else if (ud->type == GTK_TYPE_FONT_BUTTON)
2942 ud->fn = update_font_button;
2943 else if (ud->type == GTK_TYPE_PRINT_UNIX_DIALOG)
2944 ud->fn = update_print_dialog;
2945 else if (ud->type == GTK_TYPE_SWITCH)
2946 ud->fn = update_switch;
2947 else if (ud->type == GTK_TYPE_TOGGLE_BUTTON ||
2948 ud->type == GTK_TYPE_RADIO_BUTTON ||
2949 ud->type == GTK_TYPE_CHECK_BUTTON)
2950 ud->fn = update_toggle_button;
2951 else if (ud->type == GTK_TYPE_ENTRY)
2952 ud->fn = update_entry;
2953 else if (ud->type == GTK_TYPE_SPIN_BUTTON)
2954 ud->fn = update_spin_button;
2955 else if (ud->type == GTK_TYPE_SCALE)
2956 ud->fn = update_scale;
2957 else if (ud->type == GTK_TYPE_PROGRESS_BAR)
2958 ud->fn = update_progress_bar;
2959 else if (ud->type == GTK_TYPE_SPINNER)
2960 ud->fn = update_spinner;
2961 else if (ud->type == GTK_TYPE_STATUSBAR)
2962 ud->fn = update_statusbar;
2963 else if (ud->type == GTK_TYPE_CALENDAR)
2964 ud->fn = update_calendar;
2965 else if (ud->type == GTK_TYPE_SOCKET)
2966 ud->fn = update_socket;
2967 else if (ud->type == GTK_TYPE_WINDOW ||
2968 ud->type == GTK_TYPE_DIALOG)
2969 ud->fn = update_window;
2970 else
2971 ud->fn = try_generic_cmds;
2972 exec:
2973 pthread_testcancel();
2974 gdk_threads_add_timeout(0, (GSourceFunc) update_ui, ud);
2976 recursion--;
2977 return NULL;
2982 * ============================================================
2983 * Initialization
2984 * ============================================================
2988 * Return the first string xpath obtains from ui_file.
2989 * xmlFree(string) must be called when done
2991 static xmlChar *
2992 xpath1(xmlChar *xpath, const char *ui_file)
2994 xmlChar *r = NULL;
2995 xmlDocPtr doc = NULL;
2996 xmlNodeSetPtr nodes = NULL;
2997 xmlXPathContextPtr ctx = NULL;
2998 xmlXPathObjectPtr xpath_obj = NULL;
3000 if ((doc = xmlParseFile(ui_file)) == NULL)
3001 goto ret0;
3002 if ((ctx = xmlXPathNewContext(doc)) == NULL)
3003 goto ret1;
3004 if ((xpath_obj = xmlXPathEvalExpression(xpath, ctx)) == NULL)
3005 goto ret2;
3006 if ((nodes = xpath_obj->nodesetval) != NULL && nodes->nodeNr > 0)
3007 r = xmlNodeGetContent(nodes->nodeTab[0]);
3008 xmlXPathFreeObject(xpath_obj);
3009 ret2:
3010 xmlXPathFreeContext(ctx);
3011 ret1:
3012 xmlFreeDoc(doc);
3013 ret0:
3014 return r;
3018 * Attach key "col_number" to renderer. Associate "col_number" with
3019 * the corresponding column number in the underlying model.
3020 * Due to what looks like a gap in the GTK API, renderer id and column
3021 * number are taken directly from the XML .ui file.
3023 static bool
3024 tree_view_column_get_renderer_column(GtkBuilder *builder, const char *ui_file,
3025 GtkTreeViewColumn *t_col, int n,
3026 GtkCellRenderer **rnd)
3028 bool r = false;
3029 char *xp_bas1 = "//object[@class=\"GtkTreeViewColumn\" and @id=\"";
3030 char *xp_bas2 = "\"]/child[";
3031 char *xp_bas3 = "]/object[@class=\"GtkCellRendererText\""
3032 " or @class=\"GtkCellRendererToggle\"]/";
3033 char *xp_rnd_id = "@id";
3034 char *xp_text_col = "../attributes/attribute[@name=\"text\""
3035 " or @name=\"active\"]/text()";
3036 const char *tree_col_id = widget_id(GTK_BUILDABLE(t_col));
3037 size_t xp_rnd_nam_len, xp_mod_col_len;
3038 size_t xp_n_len = 3; /* Big Enough (TM) */
3039 xmlChar *xp_rnd_nam = NULL, *xp_mod_col = NULL;
3040 xmlChar *rnd_nam = NULL, *mod_col = NULL;
3042 /* find name of nth cell renderer under the GtkTreeViewColumn */
3043 /* tree_col_id */
3044 xp_rnd_nam_len = strlen(xp_bas1) + strlen(tree_col_id) +
3045 strlen(xp_bas2) + xp_n_len + strlen(xp_bas3) +
3046 strlen(xp_rnd_id) + sizeof('\0');
3047 if ((xp_rnd_nam = malloc(xp_rnd_nam_len)) == NULL)
3048 OOM_ABORT;
3049 snprintf((char *) xp_rnd_nam, xp_rnd_nam_len, "%s%s%s%d%s%s",
3050 xp_bas1, tree_col_id, xp_bas2, n,
3051 xp_bas3, xp_rnd_id);
3052 rnd_nam = xpath1(xp_rnd_nam, ui_file);
3053 /* find the model column that is attached to the nth cell */
3054 /* renderer under GtkTreeViewColumn tree_col_id */
3055 xp_mod_col_len = strlen(xp_bas1) + strlen(tree_col_id) +
3056 strlen(xp_bas2) + xp_n_len + strlen(xp_bas3) +
3057 strlen(xp_text_col) + sizeof('\0');
3058 if ((xp_mod_col = malloc(xp_mod_col_len)) == NULL)
3059 OOM_ABORT;
3060 snprintf((char *) xp_mod_col, xp_mod_col_len, "%s%s%s%d%s%s",
3061 xp_bas1, tree_col_id, xp_bas2, n, xp_bas3, xp_text_col);
3062 mod_col = xpath1(xp_mod_col, ui_file);
3063 if (rnd_nam) {
3064 *rnd = GTK_CELL_RENDERER(
3065 gtk_builder_get_object(builder, (char *) rnd_nam));
3066 if (mod_col) {
3067 g_object_set_data(G_OBJECT(*rnd), "col_number",
3068 GINT_TO_POINTER(strtol((char *) mod_col,
3069 NULL, 10)));
3070 r = true;
3073 free(xp_rnd_nam);
3074 free(xp_mod_col);
3075 xmlFree(rnd_nam);
3076 xmlFree(mod_col);
3077 return r;
3081 * Callbacks that forward a modification of a tree view cell to the
3082 * underlying model
3084 static void
3085 cb_tree_model_edit(GtkCellRenderer *renderer, const gchar *path_s,
3086 const gchar *new_text, struct info *ar)
3088 GtkTreeIter iter;
3089 int col = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer),
3090 "col_number"));
3092 gtk_tree_model_get_iter_from_string(ar->model, &iter, path_s);
3093 set_tree_view_cell(ar->model, &iter, path_s, col,
3094 new_text);
3095 send_tree_cell_msg_by(send_msg, path_s, &iter, col, ar);
3098 static void
3099 cb_tree_model_toggle(GtkCellRenderer *renderer, gchar *path_s, struct info *ar)
3101 GtkTreeIter iter;
3102 bool toggle_state;
3103 int col = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(renderer),
3104 "col_number"));
3106 gtk_tree_model_get_iter_from_string(ar->model, &iter, path_s);
3107 gtk_tree_model_get(ar->model, &iter, col, &toggle_state, -1);
3108 set_tree_view_cell(ar->model, &iter, path_s, col,
3109 toggle_state? "0" : "1");
3113 * Add new element containing id to the list of callback-handler ids
3114 * stored in obj's field named "signal_id"
3116 static void
3117 push_handler_id(gpointer *obj, unsigned int id)
3119 struct handler_id *prev_hid, *hid;
3121 prev_hid = g_object_get_data(G_OBJECT(obj), "signal-id");
3122 if ((hid = malloc(sizeof(struct handler_id))) == NULL)
3123 OOM_ABORT;
3124 hid->next = prev_hid;
3125 hid->id = id;
3126 hid->blocked = false;
3127 g_object_set_data(G_OBJECT(obj), "signal-id", hid);
3131 * Connect function cb to obj's widget signal sig, remembering the
3132 * handler id in a list in obj's field named "signal-id"
3134 static void
3135 sig_conn(gpointer *obj, char *sig, GCallback cb, struct info *ar)
3137 unsigned int handler_id = g_signal_connect(obj, sig, cb, ar);
3139 push_handler_id(obj, handler_id);
3142 static void
3143 sig_conn_swapped(gpointer *obj, char *sig, GCallback cb, void *data)
3145 unsigned int handler_id = g_signal_connect_swapped(obj, sig, cb, data);
3147 push_handler_id(obj, handler_id);
3150 static void
3151 connect_widget_signals(gpointer *obj, struct info *ar)
3153 GObject *obj2;
3154 GType type = G_TYPE_INVALID;
3155 char *suffix = NULL;
3156 const char *w_id = NULL;
3157 FILE *o = ar->fout;
3159 type = G_TYPE_FROM_INSTANCE(obj);
3160 if (GTK_IS_BUILDABLE(obj))
3161 w_id = widget_id(GTK_BUILDABLE(obj));
3162 if (type == GTK_TYPE_TREE_VIEW_COLUMN) {
3163 GList *cells = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(obj));
3164 GtkTreeViewColumn *tv_col = GTK_TREE_VIEW_COLUMN(obj);
3165 GObject *view = G_OBJECT(
3166 gtk_tree_view_column_get_tree_view(tv_col));
3167 unsigned int i, n_cells = g_list_length(cells);
3169 g_list_free(cells);
3170 sig_conn(obj, "clicked", G_CALLBACK(cb_simple), info_txt_new(o, "clicked"));
3171 for (i = 1; i <= n_cells; i++) {
3172 GtkCellRenderer *renderer;
3173 gboolean editable = FALSE;
3175 if (!tree_view_column_get_renderer_column(ar->builder, ar->txt, tv_col,
3176 i, &renderer))
3177 continue;
3178 if (GTK_IS_CELL_RENDERER_TEXT(renderer)) {
3179 g_object_get(renderer, "editable", &editable, NULL);
3180 if (editable) {
3181 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
3183 g_signal_connect(renderer, "edited",
3184 G_CALLBACK(cb_tree_model_edit),
3185 info_obj_new(o, view, model));
3187 } else if (GTK_IS_CELL_RENDERER_TOGGLE(renderer)) {
3188 g_object_get(renderer, "activatable", &editable, NULL);
3189 if (editable) {
3190 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
3192 g_signal_connect(renderer, "toggled",
3193 G_CALLBACK(cb_tree_model_toggle),
3194 info_obj_new(o, NULL, model));
3198 } else if (type == GTK_TYPE_BUTTON)
3199 /* Button associated with a GtkTextView. */
3200 if ((suffix = strstr(w_id, "_send_text")) != NULL &&
3201 GTK_IS_TEXT_VIEW(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3202 sig_conn(obj, "clicked", G_CALLBACK(cb_send_text),
3203 info_obj_new(o, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2))), NULL));
3204 else if ((suffix = strstr(w_id, "_send_selection")) != NULL &&
3205 GTK_IS_TEXT_VIEW(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3206 sig_conn(obj, "clicked", G_CALLBACK(cb_send_text_selection),
3207 info_obj_new(o, G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(obj2))), NULL));
3208 else {
3209 sig_conn(obj, "clicked", G_CALLBACK(cb_simple), info_txt_new(o, "clicked"));
3210 /* Buttons associated with (and part of) a GtkDialog.
3211 * (We shun response ids which could be returned from
3212 * gtk_dialog_run() because that would require the
3213 * user to define those response ids in Glade,
3214 * numerically */
3215 if ((suffix = strstr(w_id, "_cancel")) != NULL &&
3216 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3217 if (eql(widget_id(GTK_BUILDABLE(obj2)), MAIN_WIN))
3218 sig_conn_swapped(obj, "clicked",
3219 G_CALLBACK(gtk_main_quit), NULL);
3220 else
3221 sig_conn_swapped(obj, "clicked",
3222 G_CALLBACK(gtk_widget_hide), obj2);
3223 else if ((suffix = strstr(w_id, "_ok")) != NULL &&
3224 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id))) {
3225 if (GTK_IS_FILE_CHOOSER_DIALOG(obj2))
3226 sig_conn_swapped(obj, "clicked",
3227 G_CALLBACK(cb_send_file_chooser_dialog_selection),
3228 info_obj_new(o, obj2, NULL));
3229 if (eql(widget_id(GTK_BUILDABLE(obj2)), MAIN_WIN))
3230 sig_conn_swapped(obj, "clicked",
3231 G_CALLBACK(gtk_main_quit), NULL);
3232 else
3233 sig_conn_swapped(obj, "clicked",
3234 G_CALLBACK(gtk_widget_hide), obj2);
3235 } else if ((suffix = strstr(w_id, "_apply")) != NULL &&
3236 GTK_IS_FILE_CHOOSER_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3237 sig_conn_swapped(obj, "clicked",
3238 G_CALLBACK(cb_send_file_chooser_dialog_selection),
3239 info_obj_new(o, obj2, NULL));
3241 else if (GTK_IS_MENU_ITEM(obj))
3242 if ((suffix = strstr(w_id, "_invoke")) != NULL &&
3243 GTK_IS_DIALOG(obj2 = obj_sans_suffix(ar->builder, suffix, w_id)))
3244 sig_conn_swapped(obj, "activate",
3245 G_CALLBACK(gtk_widget_show), obj2);
3246 else
3247 sig_conn(obj, "activate",
3248 G_CALLBACK(cb_menu_item), info_txt_new(o, "active"));
3249 else if (GTK_IS_WINDOW(obj)) {
3250 sig_conn(obj, "delete-event",
3251 G_CALLBACK(cb_event_simple), info_txt_new(o, "closed"));
3252 if (eql(w_id, MAIN_WIN))
3253 sig_conn_swapped(obj, "delete-event",
3254 G_CALLBACK(gtk_main_quit), NULL);
3255 else
3256 sig_conn(obj, "delete-event",
3257 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
3258 } else if (type == GTK_TYPE_FILE_CHOOSER_BUTTON)
3259 sig_conn(obj, "file-set",
3260 G_CALLBACK(cb_file_chooser_button), info_txt_new(o, "file"));
3261 else if (type == GTK_TYPE_COLOR_BUTTON)
3262 sig_conn(obj, "color-set",
3263 G_CALLBACK(cb_color_button), info_txt_new(o, "color"));
3264 else if (type == GTK_TYPE_FONT_BUTTON)
3265 sig_conn(obj, "font-set",
3266 G_CALLBACK(cb_font_button), info_txt_new(o, "font"));
3267 else if (type == GTK_TYPE_SWITCH)
3268 sig_conn(obj, "notify::active",
3269 G_CALLBACK(cb_switch), info_txt_new(o, NULL));
3270 else if (type == GTK_TYPE_TOGGLE_BUTTON ||
3271 type == GTK_TYPE_RADIO_BUTTON ||
3272 type == GTK_TYPE_CHECK_BUTTON)
3273 sig_conn(obj, "toggled",
3274 G_CALLBACK(cb_toggle_button), info_txt_new(o, NULL));
3275 else if (type == GTK_TYPE_ENTRY)
3276 sig_conn(obj, "changed",
3277 G_CALLBACK(cb_editable), info_txt_new(o, "text"));
3278 else if (type == GTK_TYPE_SPIN_BUTTON)
3279 sig_conn(obj, "value_changed",
3280 G_CALLBACK(cb_spin_button), info_txt_new(o, "text")); /* TODO: rename to "value" */
3281 else if (type == GTK_TYPE_SCALE)
3282 sig_conn(obj, "value-changed",
3283 G_CALLBACK(cb_range), info_txt_new(o, "value"));
3284 else if (type == GTK_TYPE_CALENDAR) {
3285 sig_conn(obj, "day-selected-double-click",
3286 G_CALLBACK(cb_calendar), info_txt_new(o, "doubleclicked"));
3287 sig_conn(obj, "day-selected",
3288 G_CALLBACK(cb_calendar), info_txt_new(o, "clicked"));
3289 } else if (type == GTK_TYPE_TREE_SELECTION)
3290 sig_conn(obj, "changed",
3291 G_CALLBACK(cb_tree_selection), info_txt_new(o, "clicked"));
3292 else if (type == GTK_TYPE_SOCKET) {
3293 sig_conn(obj, "plug-added",
3294 G_CALLBACK(cb_simple), info_txt_new(o, "plug-added"));
3295 sig_conn(obj, "plug-removed",
3296 G_CALLBACK(cb_simple), info_txt_new(o, "plug-removed"));
3297 /* TODO: rename to plug_added, plug_removed */
3298 } else if (type == GTK_TYPE_DRAWING_AREA)
3299 sig_conn(obj, "draw", G_CALLBACK(cb_draw), NULL);
3300 else if (type == GTK_TYPE_EVENT_BOX) {
3301 gtk_widget_set_can_focus(GTK_WIDGET(obj), true);
3302 sig_conn(obj, "button-press-event",
3303 G_CALLBACK(cb_event_box_button),
3304 info_txt_new(o, "button_press"));
3305 sig_conn(obj, "button-release-event",
3306 G_CALLBACK(cb_event_box_button),
3307 info_txt_new(o, "button_release"));
3308 sig_conn(obj, "motion-notify-event",
3309 G_CALLBACK(cb_event_box_motion),
3310 info_txt_new(o, "motion"));
3311 sig_conn(obj, "key-press-event",
3312 G_CALLBACK(cb_event_box_key),
3313 info_txt_new(o, "key_press"));
3318 * We keep a style provider with each widget
3320 static void
3321 add_widget_style_provider(gpointer *obj, void *data)
3323 GtkCssProvider *style_provider;
3324 GtkStyleContext *context;
3326 (void) data;
3327 if (!GTK_IS_WIDGET(obj))
3328 return;
3329 style_provider = gtk_css_provider_new();
3330 context = gtk_widget_get_style_context(GTK_WIDGET(obj));
3331 gtk_style_context_add_provider(context,
3332 GTK_STYLE_PROVIDER(style_provider),
3333 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
3334 g_object_set_data(G_OBJECT(obj), "style_provider", style_provider);
3337 static void
3338 prepare_widgets(GtkBuilder *builder, char *ui_file, FILE *out)
3340 GSList *objects = NULL;
3341 struct info ar = {.builder = builder, .fout = out, .txt = ui_file};
3343 objects = gtk_builder_get_objects(builder);
3344 g_slist_foreach(objects, (GFunc) connect_widget_signals, &ar);
3345 g_slist_foreach(objects, (GFunc) add_widget_style_provider, NULL);
3346 g_slist_free(objects);
3350 main(int argc, char *argv[])
3352 GObject *main_window = NULL;
3353 bool bg = false;
3354 char *in_fifo = NULL, *out_fifo = NULL;
3355 char *ui_file = "pipeglade.ui", *log_file = NULL, *err_file = NULL;
3356 char *xid = NULL;
3357 char opt;
3358 pthread_t receiver;
3359 struct info ar;
3361 /* Disable runtime GLIB deprecation warnings: */
3362 setenv("G_ENABLE_DIAGNOSTIC", "0", 0);
3363 gtk_init(&argc, &argv);
3364 while ((opt = getopt(argc, argv, "bGhe:i:l:o:O:u:V")) != -1) {
3365 switch (opt) {
3366 case 'b': bg = true; break;
3367 case 'e': xid = optarg; break;
3368 case 'G': show_lib_versions(); break;
3369 case 'h': bye(EXIT_SUCCESS, stdout, USAGE); break;
3370 case 'i': in_fifo = optarg; break;
3371 case 'l': log_file = optarg; break;
3372 case 'o': out_fifo = optarg; break;
3373 case 'O': err_file = optarg; break;
3374 case 'u': ui_file = optarg; break;
3375 case 'V': bye(EXIT_SUCCESS, stdout, "%s\n", VERSION); break;
3376 case '?':
3377 default: bye(EXIT_FAILURE, stderr, USAGE); break;
3380 if (argv[optind] != NULL)
3381 bye(EXIT_FAILURE, stderr,
3382 "illegal parameter '%s'\n" USAGE, argv[optind]);
3383 redirect_stderr(err_file);
3384 ar.fin = open_fifo(in_fifo, "r", stdin, _IONBF);
3385 ar.fout = open_fifo(out_fifo, "w", stdout, _IOLBF);
3386 go_bg_if(bg, ar.fin, ar.fout, err_file);
3387 ar.builder = builder_from_file(ui_file);
3388 ar.flog = open_log(log_file);
3389 pthread_create(&receiver, NULL, (void *(*)(void *)) digest_cmd, &ar);
3390 main_window = find_main_window(ar.builder);
3391 xmlInitParser();
3392 LIBXML_TEST_VERSION;
3393 prepare_widgets(ar.builder, ui_file, ar.fout);
3394 xembed_if(xid, main_window);
3395 gtk_main();
3396 pthread_cancel(receiver);
3397 pthread_join(receiver, NULL);
3398 xmlCleanupParser();
3399 rm_unless(stdin, ar.fin, in_fifo);
3400 rm_unless(stdout, ar.fout, out_fifo);
3401 exit(EXIT_SUCCESS);