annotate xwd-application.c @ 22:9d00c0e07c47 version-3

Release version 3
author Guido Berhoerster <guido+xwrited@berhoerster.name>
date Sat, 30 Jun 2018 23:07:36 +0200
parents 683ebd334b21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
1 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+xwrited@berhoerster.name>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
3 *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
11 *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
14 *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
22 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
23
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
24 #include <errno.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
25 #include <fcntl.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
26 #include <glib-unix.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
27 #include <glib/gi18n.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
28 #include <stdio.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
29 #include <stdlib.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
30 #include <string.h>
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
31
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
32 #include "xwd-application.h"
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
33 #include "xwd-utmp.h"
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
34
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
35 #define BUFFER_TIMEOUT 500 /* ms */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
36
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
37 struct _XwdApplication {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
38 GApplication parent_instance;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
39 gint masterfd;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
40 gint slavefd;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
41 GIOChannel *master_pty_chan;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
42 guint buffer_timeout_id;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
43 GString *message_buf;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
44 };
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
45
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
46 G_DEFINE_TYPE(XwdApplication, xwd_application, G_TYPE_APPLICATION)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
47
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
48 static void xwd_application_quit(GSimpleAction *, GVariant *, gpointer);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
49
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
50 static const GOptionEntry cmd_options[] = {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
51 { "debug", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
52 N_("Enable debugging messages"), NULL },
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
53 { "quit", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
54 N_("Quit running instance of xwrited"), NULL },
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
55 { "version", 'V', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
56 N_("Print the version number and quit"), NULL },
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
57 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, NULL, NULL },
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
58 { NULL }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
59 };
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
60
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
61 static const GActionEntry xwd_application_actions[] = {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
62 { "quit", xwd_application_quit }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
63 };
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
64
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
65 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
66 xwd_application_quit(GSimpleAction *simple, GVariant *parameter,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
67 gpointer user_data)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
68 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
69 XwdApplication *self = user_data;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
70
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
71 g_application_quit(G_APPLICATION(self));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
72 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
73
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
74 static gboolean
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
75 on_signal(gpointer user_data)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
76 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
77 XwdApplication *self = user_data;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
78
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
79 g_debug("received signal, exiting");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
80 g_action_group_activate_action(G_ACTION_GROUP(G_APPLICATION(self)),
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
81 "quit", NULL);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
82
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
83 return (TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
84 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
85
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
86 static GString *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
87 string_copy(GString *dest, GString *src)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
88 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
89 g_string_truncate(dest, 0);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
90 g_string_append(dest, src->str);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
91
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
92 return (dest);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
93 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
94
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
95 static void
20
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
96 string_decode_octal(GString *string)
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
97 {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
98 GString *result;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
99 gsize remaining = string->len;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
100 gchar *p = string->str;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
101 gchar oct[4] = { '\0' };
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
102 guint64 num;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
103 char *end;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
104
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
105 result = g_string_sized_new(string->len);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
106
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
107 while (remaining > 0) {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
108 if ((remaining >= 4) && (*p == '\\')) {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
109 /* put octal number in NUL-terminated buffer */
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
110 memcpy(oct, p + 1, 3);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
111 /* convert valid octal number to byte */
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
112 num = g_ascii_strtoull(oct, &end, 8);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
113 if ((*end == '\0') && (num <= 0xff)) {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
114 /* conversion succeeded */
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
115 p += 4;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
116 remaining -= 4;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
117 g_string_append_c(result, num);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
118 continue;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
119 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
120 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
121
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
122 g_string_append_c(result, *p++);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
123 remaining--;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
124 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
125
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
126 string_copy(string, result);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
127 g_string_free(result, TRUE);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
128 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
129
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
130 static void
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
131 string_decode_meta_ascii(GString *string)
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
132 {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
133 GString *result;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
134 gchar *p = string->str;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
135 gsize remaining = string->len;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
136
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
137 result = g_string_sized_new(string->len);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
138
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
139 while (remaining > 0) {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
140 if ((remaining >= 3) && (*p == 'M') && (*(p + 1) == '-') &&
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
141 ((*(p + 2) & (1 << 7)) == 0)) {
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
142 /* restore 8th bit */
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
143 g_string_append_c(result, *(p + 2) | (1 << 7));
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
144 remaining -= 3;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
145 p += 3;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
146 continue;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
147 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
148
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
149 g_string_append_c(result, *p++);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
150 remaining--;
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
151 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
152
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
153 string_copy(string, result);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
154 g_string_free(result, TRUE);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
155 }
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
156
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
157 static void
19
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
158 string_to_valid_utf8(GString *string)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
159 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
160 GString *result;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
161 gchar *start = string->str;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
162 gchar *end;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
163 gsize remaining = string->len;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
164
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
165 result = g_string_sized_new(string->len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
166
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
167 while (remaining > 0) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
168 if (g_utf8_validate(start, remaining, (const gchar **)&end)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
169 /* remaining part is valid */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
170 g_string_append_len(result, start, remaining);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
171 break;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
172 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
173
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
174 /* append valid part */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
175 g_string_append_len(result, start, end - start);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
176 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
177 * replace first invalid byte with Unicode "REPLACEMENT
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
178 * CHARACTER" (U+FFFD)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
179 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
180 g_string_append(result, "\357\277\275");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
181 remaining -= (end - start) + 1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
182 start = end + 1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
183 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
184
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
185 string_copy(string, result);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
186 g_string_free(result, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
187 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
188
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
189 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
190 string_trim_lines(GString *string)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
191 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
192 GString *result;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
193 gchar *p = string->str;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
194 gchar *q;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
195 gsize line_len;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
196 gsize remaining = string->len;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
197
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
198 result = g_string_sized_new(string->len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
199
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
200 while (remaining > 0) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
201 q = memchr(p, '\n', remaining);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
202 if (q == NULL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
203 g_string_append_len(result, p, remaining);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
204 break;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
205 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
206 line_len = q - p - 1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
207 /* convert \r\n to \n */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
208 if ((line_len > 0) && (p[line_len - 1] == '\r')) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
209 line_len--;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
210 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
211 /* trim spaces on the right */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
212 while ((line_len > 0) && (p[line_len - 1] == ' ')) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
213 line_len--;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
214 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
215 g_string_append_len(result, p, line_len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
216 g_string_append_c(result, '\n');
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
217 remaining -= q + 1 - p;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
218 p = q + 1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
219 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
220
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
221 string_copy(string, result);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
222 g_string_free(result, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
223 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
224
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
225 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
226 string_filter_nonprintable(GString *string)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
227 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
228 GString *result;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
229 const gchar *p;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
230 gunichar c;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
231
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
232 result = g_string_sized_new(string->len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
233
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
234 for (p = string->str; *p != '\0'; p = g_utf8_next_char(p)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
235 c = g_utf8_get_char(p);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
236 if (g_unichar_isprint(c) || g_unichar_isspace(c)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
237 g_string_append_unichar(result, c);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
238 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
239 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
240
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
241 string_copy(string, result);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
242 g_string_free(result, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
243 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
244
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
245 static gchar *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
246 hexdumpa(const void *mem, gsize n)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
247 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
248 const guchar *bytes = mem;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
249 GString *string;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
250 gsize i;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
251 gsize j;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
252
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
253 string = g_string_sized_new((n / 16 + (n % 16 > 0)) * 76);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
254
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
255 for (i = 0; i < n; i += 16) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
256 g_string_append_printf(string, "%08zx ", i);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
257
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
258 for (j = 0; (i + j < n) && (j < 16); j++) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
259 g_string_append_printf(string, " %02x", bytes[i + j]);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
260 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
261 for (; j < 16; j++) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
262 g_string_append(string, " ");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
263 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
264
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
265 g_string_append(string, " ");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
266 for (j = 0; (i + j < n) && (j < 16); j++) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
267 g_string_append_printf(string, "%c",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
268 g_ascii_isprint(bytes[i + j]) ? bytes[i + j] : '.');
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
269 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
270
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
271 g_string_append_c(string, '\n');
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
272 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
273
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
274 return (g_string_free(string, FALSE));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
275 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
276
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
277 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
278 display_message(XwdApplication *self)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
279 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
280 gboolean enable_debug_logging;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
281 gchar *message_dump;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
282 GString *message;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
283 GIcon *icon;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
284 GNotification *notification;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
285
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
286 if (self->message_buf->len == 0) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
287 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
288 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
289
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
290 enable_debug_logging = (g_getenv("G_MESSAGES_DEBUG") != NULL);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
291 if (enable_debug_logging) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
292 message_dump = hexdumpa(self->message_buf->str,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
293 self->message_buf->len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
294 g_debug("raw message:\n%s", message_dump);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
295 g_free(message_dump);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
296 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
297
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
298 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
299 * There is no reliable way to determine the character encoding of the
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
300 * received message which, depending on the locale of the sender, may
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
301 * even differ for different messages. A user could even send binary
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
302 * data. It is thus assumed that messages are in UTF-8 encoding which
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
303 * should be the most common case on modern systems. Any invalid
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
304 * sequences are replaced with the Unicode "REPLACEMENT CHARACTER"
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
305 * (U+FFFD) and non-printable characters are removed. Additionally,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
306 * padding typically added by wall(1) implementations is removed in
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
307 * order to improve readability.
20
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
308 * Some write(1) and wall(1) implementations encode non-ASCII
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
309 * characters, in particular UTF-8 sequences, by prefixing them with
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
310 * "M-" and clearing the 8th bit while others (e.g. util-linux) use
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
311 * octal escape sequences. These encodings are reversed before messages
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
312 * are processed further. However some implementations such as NetBSD
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
313 * write(1) uncoditionally process each byte with toascii(3) which
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
314 * makes it impossible to restore the original value.
19
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
315 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
316 message = g_string_new_len(self->message_buf->str,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
317 self->message_buf->len);
20
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
318 string_decode_octal(message);
683ebd334b21 Try to decode octal and "Meta-ASCII" escapes
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents: 19
diff changeset
319 string_decode_meta_ascii(message);
19
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
320 string_to_valid_utf8(message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
321 string_filter_nonprintable(message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
322 string_trim_lines(message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
323
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
324 if (enable_debug_logging) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
325 message_dump = hexdumpa(message->str, message->len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
326 g_debug("message:\n%s", message_dump);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
327 g_free(message_dump);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
328 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
329
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
330 icon = g_themed_icon_new("utilities-terminal");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
331
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
332 notification = g_notification_new(_("Message received"));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
333 g_notification_set_icon(notification, icon);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
334 g_notification_set_body(notification, message->str);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
335 g_application_send_notification(G_APPLICATION(self), NULL,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
336 notification);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
337
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
338 g_object_unref(notification);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
339 g_object_unref(icon);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
340 g_string_free(message, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
341 g_string_truncate(self->message_buf, 0);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
342 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
343
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
344 static gboolean
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
345 on_buffer_timeout(gpointer user_data)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
346 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
347 XwdApplication *self = user_data;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
348
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
349 display_message(self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
350
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
351 self->buffer_timeout_id = 0;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
352
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
353 return (FALSE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
354 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
355
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
356 static gboolean
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
357 on_master_pty_readable(GIOChannel *source, GIOCondition cond,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
358 gpointer user_data)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
359 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
360 XwdApplication *self = user_data;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
361 GIOStatus status;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
362 gchar buf[BUFSIZ];
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
363 gsize buf_len = 0;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
364 GError *error = NULL;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
365
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
366 if (cond & G_IO_IN) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
367 /* read message data from master pty */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
368 memset(buf, 0, sizeof (buf));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
369 while ((status = g_io_channel_read_chars(source, (gchar *)&buf,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
370 sizeof (buf), &buf_len, &error)) == G_IO_STATUS_NORMAL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
371 if (buf_len > 0) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
372 g_debug("read %" G_GSSIZE_FORMAT " bytes from "
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
373 "master pty", buf_len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
374 g_string_append_len(self->message_buf, buf,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
375 buf_len);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
376 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
377 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
378 if (error != NULL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
379 g_critical("failed to read from master pty: %s",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
380 error->message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
381 g_error_free(error);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
382 return (FALSE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
383 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
384
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
385 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
386 * a message might be read in several parts and it is not
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
387 * possible to reliably detect the beginning or end of a
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
388 * message, so buffer read data until a short timeout is
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
389 * reached as this works well for a single message which should
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
390 * be the most common case
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
391 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
392 if (self->buffer_timeout_id == 0) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
393 self->buffer_timeout_id = g_timeout_add(BUFFER_TIMEOUT,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
394 on_buffer_timeout, self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
395 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
396 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
397
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
398 if (cond & (G_IO_HUP | G_IO_ERR)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
399 g_critical("connection to master pty broken");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
400 return (FALSE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
401 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
402
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
403 return (TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
404 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
405
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
406 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
407 xwd_application_startup(GApplication *application)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
408 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
409 XwdApplication *self = XWD_APPLICATION(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
410 gchar *slave_name;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
411 GIOFlags flags;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
412 GError *error = NULL;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
413
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
414 G_APPLICATION_CLASS(xwd_application_parent_class)->startup(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
415
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
416 /* create actions */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
417 g_action_map_add_action_entries(G_ACTION_MAP(self),
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
418 xwd_application_actions, G_N_ELEMENTS(xwd_application_actions),
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
419 self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
420
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
421 /* create signal watchers */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
422 g_unix_signal_add(SIGINT, on_signal, self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
423 g_unix_signal_add(SIGTERM, on_signal, self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
424 g_unix_signal_add(SIGHUP, on_signal, self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
425
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
426 /* open master pty */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
427 self->masterfd = posix_openpt(O_RDWR | O_NOCTTY);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
428 if (self->masterfd == -1) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
429 g_critical("failed to open master pty: %s", g_strerror(errno));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
430 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
431 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
432
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
433 /* create slave pty */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
434 if ((grantpt(self->masterfd) == -1) ||
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
435 (unlockpt(self->masterfd) == -1)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
436 g_critical("failed to create slave pty: %s", g_strerror(errno));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
437 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
438 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
439 slave_name = ptsname(self->masterfd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
440 if (slave_name == NULL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
441 g_critical("failed to obtain name of slave pty");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
442 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
443 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
444
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
445 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
446 * keep an open fd around order to prevent closing the master fd when
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
447 * receiving an EOF
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
448 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
449 self->slavefd = open(slave_name, O_RDWR);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
450 if (self->slavefd == -1) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
451 g_critical("failed to open slave pty: %s", g_strerror(errno));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
452 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
453 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
454
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
455 /* create a GIOChannel for monitoring the master pty for messages */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
456 self->master_pty_chan = g_io_channel_unix_new(self->masterfd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
457 /* make it non-blocking */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
458 flags = g_io_channel_get_flags(self->master_pty_chan);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
459 if (g_io_channel_set_flags(self->master_pty_chan,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
460 flags | G_IO_FLAG_NONBLOCK, &error) != G_IO_STATUS_NORMAL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
461 g_critical("failed set flags on the master pty channel: %s",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
462 error->message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
463 g_error_free(error);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
464 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
465 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
466 /* make the channel safe for encodings other than UTF-8 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
467 if (g_io_channel_set_encoding(self->master_pty_chan, NULL, &error) !=
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
468 G_IO_STATUS_NORMAL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
469 g_critical("failed set encoding on the master pty channel: %s",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
470 error->message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
471 g_error_free(error);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
472 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
473 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
474 if (!g_io_add_watch(self->master_pty_chan,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
475 G_IO_IN | G_IO_HUP | G_IO_ERR, on_master_pty_readable, self)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
476 g_critical("failed to add watch on master pty channel");
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
477 return;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
478 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
479
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
480 xwd_utmp_add_entry(self->masterfd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
481
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
482 /* keep GApplication running */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
483 g_application_hold(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
484 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
485
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
486 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
487 xwd_application_shutdown(GApplication *application)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
488 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
489 XwdApplication *self = XWD_APPLICATION(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
490 GApplicationClass *application_class =
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
491 G_APPLICATION_CLASS(xwd_application_parent_class);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
492
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
493 /* display any buffered data before exiting */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
494 display_message(self);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
495
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
496 if (self->master_pty_chan != NULL) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
497 g_io_channel_shutdown(self->master_pty_chan, FALSE, NULL);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
498 g_clear_pointer(&self->master_pty_chan,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
499 (GDestroyNotify)g_io_channel_unref);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
500 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
501
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
502 if (self->slavefd != -1) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
503 close(self->slavefd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
504 self->slavefd = -1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
505 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
506
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
507 if (self->masterfd != -1) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
508 close(self->masterfd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
509 self->masterfd = -1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
510 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
511
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
512 xwd_utmp_remove_entry(self->masterfd);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
513
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
514 /* remove signal watches and buffer timeout */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
515 while (g_source_remove_by_user_data(self)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
516 continue;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
517 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
518 self->buffer_timeout_id = 0;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
519
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
520 application_class->shutdown(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
521 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
522
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
523 static gint
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
524 xwd_application_handle_local_options(GApplication *application,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
525 GVariantDict *options)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
526 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
527 gchar **args = NULL;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
528 gchar *messages_debug;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
529 GError *error = NULL;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
530
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
531 /* filename arguments are not allowed */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
532 if (g_variant_dict_lookup(options, G_OPTION_REMAINING, "^a&ay",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
533 &args)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
534 g_printerr("invalid argument: \"%s\"\n", args[0]);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
535 g_free(args);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
536 return (1);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
537 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
538
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
539 if (g_variant_dict_contains(options, "version")) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
540 g_print("%s %s\n", PACKAGE, VERSION);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
541
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
542 /* quit */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
543 return (0);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
544 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
545
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
546 if (g_variant_dict_contains(options, "debug")) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
547 /* enable debug logging */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
548 messages_debug = g_strjoin(":", G_LOG_DOMAIN,
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
549 g_getenv("G_MESSAGES_DEBUG"), NULL);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
550 g_setenv("G_MESSAGES_DEBUG", messages_debug, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
551 g_free(messages_debug);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
552 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
553
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
554 /*
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
555 * register with the session bus so that it is possible to discern
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
556 * between remote and primary instance and that remote actions can be
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
557 * invoked, this causes the startup signal to be emitted which, in case
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
558 * of the primary instance, starts to instantiate the
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
559 * backend with the given values
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
560 */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
561 if (!g_application_register(application, NULL, &error)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
562 g_critical("g_application_register: %s", error->message);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
563 g_error_free(error);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
564 return (1);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
565 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
566
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
567 if (g_variant_dict_contains(options, "quit")) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
568 /* only valid if a remote instance is running */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
569 if (!g_application_get_is_remote(application)) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
570 g_printerr("%s is not running\n", g_get_prgname());
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
571 return (1);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
572 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
573
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
574 /* signal remote instance to quit */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
575 g_action_group_activate_action(G_ACTION_GROUP(application),
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
576 "quit", NULL);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
577
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
578 /* quit local instance */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
579 return (0);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
580 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
581
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
582 /* proceed with default command line processing */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
583 return (-1);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
584 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
585
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
586 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
587 xwd_application_activate(GApplication *application) {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
588 GApplicationClass *application_class =
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
589 G_APPLICATION_CLASS(xwd_application_parent_class);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
590
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
591 /* do nothing, implementation required by GApplication */
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
592
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
593 application_class->activate(application);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
594 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
595
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
596 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
597 xwd_application_finalize(GObject *object)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
598 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
599 XwdApplication *self = XWD_APPLICATION(object);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
600
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
601 g_string_free(self->message_buf, TRUE);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
602
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
603 G_OBJECT_CLASS(xwd_application_parent_class)->finalize(object);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
604 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
605
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
606 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
607 xwd_application_class_init(XwdApplicationClass *klass)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
608 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
609 GObjectClass *object_class = G_OBJECT_CLASS(klass);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
610 GApplicationClass *application_class = G_APPLICATION_CLASS(klass);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
611
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
612 object_class->finalize = xwd_application_finalize;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
613
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
614 application_class->startup = xwd_application_startup;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
615 application_class->shutdown = xwd_application_shutdown;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
616 application_class->handle_local_options =
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
617 xwd_application_handle_local_options;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
618 application_class->activate = xwd_application_activate;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
619 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
620
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
621 static void
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
622 xwd_application_init(XwdApplication *self)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
623 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
624 g_application_add_main_option_entries(G_APPLICATION(self),
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
625 cmd_options);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
626
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
627 self->masterfd = -1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
628 self->slavefd = -1;
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
629
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
630 self->message_buf = g_string_sized_new(BUFSIZ);
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
631 }
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
632
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
633 XwdApplication *
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
634 xwd_application_new(void)
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
635 {
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
636 return (g_object_new(XWD_TYPE_APPLICATION, "application-id",
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
637 APPLICATION_ID, NULL));
f0accfc74f7b Port to GLib version 2.48 or later
Guido Berhoerster <guido+xwrited@berhoerster.name>
parents:
diff changeset
638 }