1 | #include <gio/gio.h> |
2 | #include <string.h> |
3 | |
4 | static void |
5 | new_activated (GSimpleAction *action, |
6 | GVariant *parameter, |
7 | gpointer user_data) |
8 | { |
9 | GApplication *app = user_data; |
10 | |
11 | g_application_activate (application: app); |
12 | } |
13 | |
14 | static void |
15 | quit_activated (GSimpleAction *action, |
16 | GVariant *parameter, |
17 | gpointer user_data) |
18 | { |
19 | GApplication *app = user_data; |
20 | |
21 | g_application_quit (application: app); |
22 | } |
23 | |
24 | static void |
25 | action1_activated (GSimpleAction *action, |
26 | GVariant *parameter, |
27 | gpointer user_data) |
28 | { |
29 | g_print (format: "activate action1\n" ); |
30 | } |
31 | |
32 | static void |
33 | action2_activated (GSimpleAction *action, |
34 | GVariant *parameter, |
35 | gpointer user_data) |
36 | { |
37 | GVariant *state; |
38 | |
39 | state = g_action_get_state (G_ACTION (action)); |
40 | g_action_change_state (G_ACTION (action), value: g_variant_new_boolean (value: !g_variant_get_boolean (value: state))); |
41 | g_print (format: "activate action2 %d\n" , !g_variant_get_boolean (value: state)); |
42 | g_variant_unref (value: state); |
43 | } |
44 | |
45 | static void |
46 | change_action2 (GSimpleAction *action, |
47 | GVariant *state, |
48 | gpointer user_data) |
49 | { |
50 | g_print (format: "change action2 %d\n" , g_variant_get_boolean (value: state)); |
51 | } |
52 | |
53 | static void |
54 | startup (GApplication *app) |
55 | { |
56 | static GActionEntry actions[] = { |
57 | { "new" , new_activated, NULL, NULL, NULL }, |
58 | { "quit" , quit_activated, NULL, NULL, NULL }, |
59 | { "action1" , action1_activated, NULL, NULL, NULL }, |
60 | { "action2" , action2_activated, "b" , "false" , change_action2 } |
61 | }; |
62 | |
63 | g_action_map_add_action_entries (G_ACTION_MAP (app), |
64 | entries: actions, G_N_ELEMENTS (actions), |
65 | user_data: app); |
66 | } |
67 | |
68 | static void |
69 | activate (GApplication *application) |
70 | { |
71 | g_application_hold (application); |
72 | g_print (format: "activated\n" ); |
73 | g_application_release (application); |
74 | } |
75 | |
76 | static void |
77 | open (GApplication *application, |
78 | GFile **files, |
79 | gint n_files, |
80 | const gchar *hint) |
81 | { |
82 | gint i; |
83 | |
84 | g_application_hold (application); |
85 | |
86 | g_print (format: "open" ); |
87 | for (i = 0; i < n_files; i++) |
88 | { |
89 | gchar *uri = g_file_get_uri (file: files[i]); |
90 | g_print (format: " %s" , uri); |
91 | g_free (mem: uri); |
92 | } |
93 | g_print (format: "\n" ); |
94 | |
95 | g_application_release (application); |
96 | } |
97 | |
98 | static int |
99 | command_line (GApplication *application, |
100 | GApplicationCommandLine *cmdline) |
101 | { |
102 | gchar **argv; |
103 | gint argc; |
104 | gint i; |
105 | |
106 | g_application_hold (application); |
107 | argv = g_application_command_line_get_arguments (cmdline, argc: &argc); |
108 | |
109 | if (argc > 1) |
110 | { |
111 | if (g_strcmp0 (str1: argv[1], str2: "echo" ) == 0) |
112 | { |
113 | g_print (format: "cmdline" ); |
114 | for (i = 0; i < argc; i++) |
115 | g_print (format: " %s" , argv[i]); |
116 | g_print (format: "\n" ); |
117 | } |
118 | else if (g_strcmp0 (str1: argv[1], str2: "env" ) == 0) |
119 | { |
120 | const gchar * const *env; |
121 | |
122 | env = g_application_command_line_get_environ (cmdline); |
123 | g_print (format: "environment" ); |
124 | for (i = 0; env[i]; i++) |
125 | if (g_str_has_prefix (str: env[i], prefix: "TEST=" )) |
126 | g_print (format: " %s" , env[i]); |
127 | g_print (format: "\n" ); |
128 | } |
129 | else if (g_strcmp0 (str1: argv[1], str2: "getenv" ) == 0) |
130 | { |
131 | g_print (format: "getenv TEST=%s\n" , g_application_command_line_getenv (cmdline, name: "TEST" )); |
132 | } |
133 | else if (g_strcmp0 (str1: argv[1], str2: "print" ) == 0) |
134 | { |
135 | g_application_command_line_print (cmdline, format: "print %s\n" , argv[2]); |
136 | } |
137 | else if (g_strcmp0 (str1: argv[1], str2: "printerr" ) == 0) |
138 | { |
139 | g_application_command_line_printerr (cmdline, format: "printerr %s\n" , argv[2]); |
140 | } |
141 | else if (g_strcmp0 (str1: argv[1], str2: "file" ) == 0) |
142 | { |
143 | GFile *file; |
144 | |
145 | file = g_application_command_line_create_file_for_arg (cmdline, arg: argv[2]); |
146 | g_print (format: "file %s\n" , g_file_get_path (file)); |
147 | g_object_unref (object: file); |
148 | } |
149 | else if (g_strcmp0 (str1: argv[1], str2: "properties" ) == 0) |
150 | { |
151 | gboolean remote; |
152 | GVariant *data; |
153 | |
154 | g_object_get (object: cmdline, |
155 | first_property_name: "is-remote" , &remote, |
156 | NULL); |
157 | |
158 | data = g_application_command_line_get_platform_data (cmdline); |
159 | g_assert (remote); |
160 | g_assert (g_variant_is_of_type (data, G_VARIANT_TYPE ("a{sv}" ))); |
161 | g_variant_unref (value: data); |
162 | g_print (format: "properties ok\n" ); |
163 | } |
164 | else if (g_strcmp0 (str1: argv[1], str2: "cwd" ) == 0) |
165 | { |
166 | g_print (format: "cwd %s\n" , g_application_command_line_get_cwd (cmdline)); |
167 | } |
168 | else if (g_strcmp0 (str1: argv[1], str2: "busy" ) == 0) |
169 | { |
170 | g_application_mark_busy (application: g_application_get_default ()); |
171 | g_print (format: "busy\n" ); |
172 | } |
173 | else if (g_strcmp0 (str1: argv[1], str2: "idle" ) == 0) |
174 | { |
175 | g_application_unmark_busy (application: g_application_get_default ()); |
176 | g_print (format: "idle\n" ); |
177 | } |
178 | else if (g_strcmp0 (str1: argv[1], str2: "stdin" ) == 0) |
179 | { |
180 | GInputStream *stream; |
181 | |
182 | stream = g_application_command_line_get_stdin (cmdline); |
183 | |
184 | g_assert (stream == NULL || G_IS_INPUT_STREAM (stream)); |
185 | g_object_unref (object: stream); |
186 | |
187 | g_print (format: "stdin ok\n" ); |
188 | } |
189 | else |
190 | g_print (format: "unexpected command: %s\n" , argv[1]); |
191 | } |
192 | else |
193 | g_print (format: "got ./cmd %d\n" , g_application_command_line_get_is_remote (cmdline)); |
194 | |
195 | g_strfreev (str_array: argv); |
196 | g_application_release (application); |
197 | |
198 | return 0; |
199 | } |
200 | |
201 | static gboolean |
202 | action_cb (gpointer data) |
203 | { |
204 | gchar **argv = data; |
205 | GApplication *app; |
206 | gchar **actions; |
207 | gint i; |
208 | |
209 | if (g_strcmp0 (str1: argv[1], str2: "./actions" ) == 0) |
210 | { |
211 | app = g_application_get_default (); |
212 | |
213 | if (g_strcmp0 (str1: argv[2], str2: "list" ) == 0) |
214 | { |
215 | g_print (format: "actions" ); |
216 | actions = g_action_group_list_actions (G_ACTION_GROUP (app)); |
217 | for (i = 0; actions[i]; i++) |
218 | g_print (format: " %s" , actions[i]); |
219 | g_print (format: "\n" ); |
220 | g_strfreev (str_array: actions); |
221 | } |
222 | else if (g_strcmp0 (str1: argv[2], str2: "activate" ) == 0) |
223 | { |
224 | g_action_group_activate_action (G_ACTION_GROUP (app), |
225 | action_name: "action1" , NULL); |
226 | } |
227 | else if (g_strcmp0 (str1: argv[2], str2: "set-state" ) == 0) |
228 | { |
229 | g_action_group_change_action_state (G_ACTION_GROUP (app), |
230 | action_name: "action2" , |
231 | value: g_variant_new_boolean (TRUE)); |
232 | } |
233 | g_application_release (application: app); |
234 | } |
235 | |
236 | return G_SOURCE_REMOVE; |
237 | } |
238 | |
239 | int |
240 | main (int argc, char **argv) |
241 | { |
242 | GApplication *app; |
243 | int status; |
244 | |
245 | app = g_application_new (application_id: "org.gtk.TestApplication" , |
246 | flags: G_APPLICATION_SEND_ENVIRONMENT | |
247 | (g_strcmp0 (str1: argv[1], str2: "./cmd" ) == 0 |
248 | ? G_APPLICATION_HANDLES_COMMAND_LINE |
249 | : G_APPLICATION_HANDLES_OPEN)); |
250 | g_signal_connect (app, "startup" , G_CALLBACK (startup), NULL); |
251 | g_signal_connect (app, "activate" , G_CALLBACK (activate), NULL); |
252 | g_signal_connect (app, "open" , G_CALLBACK (open), NULL); |
253 | g_signal_connect (app, "command-line" , G_CALLBACK (command_line), NULL); |
254 | #ifdef STANDALONE |
255 | g_application_set_inactivity_timeout (app, 10000); |
256 | #else |
257 | g_application_set_inactivity_timeout (application: app, inactivity_timeout: 1000); |
258 | #endif |
259 | |
260 | if (g_strcmp0 (str1: argv[1], str2: "./actions" ) == 0) |
261 | { |
262 | g_application_set_inactivity_timeout (application: app, inactivity_timeout: 0); |
263 | g_application_hold (application: app); |
264 | g_idle_add (function: action_cb, data: argv); |
265 | } |
266 | |
267 | status = g_application_run (application: app, argc: argc - 1, argv: argv + 1); |
268 | |
269 | g_object_unref (object: app); |
270 | |
271 | g_print (format: "exit status: %d\n" , status); |
272 | |
273 | return 0; |
274 | } |
275 | |