| 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 (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 (app); |
| 22 |
} |
| 23 |
|
| 24 |
static void |
| 25 |
action1_activated (GSimpleAction *action, |
| 26 |
GVariant *parameter, |
| 27 |
gpointer user_data) |
| 28 |
{ |
| 29 |
g_print ("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), g_variant_new_boolean (!g_variant_get_boolean (state))); |
| 41 |
g_print ("activate action2 %d\n", !g_variant_get_boolean (state)); |
| 42 |
g_variant_unref (state); |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
change_action2 (GSimpleAction *action, |
| 47 |
GVariant *state, |
| 48 |
gpointer user_data) |
| 49 |
{ |
| 50 |
g_print ("change action2 %d\n", g_variant_get_boolean (state)); |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
startup (GApplication *app) |
| 55 |
{ |
| 56 |
static GActionEntry actions[] = { |
| 57 |
{ "new", new_activated, NULL, NULL, NULL, { 0 } }, |
| 58 |
{ "quit", quit_activated, NULL, NULL, NULL, { 0 } }, |
| 59 |
{ "action1", action1_activated, NULL, NULL, NULL, { 0 } }, |
| 60 |
{ "action2", action2_activated, "b", "false", change_action2, { 0 } } |
| 61 |
}; |
| 62 |
|
| 63 |
g_action_map_add_action_entries (G_ACTION_MAP (app), |
| 64 |
actions, G_N_ELEMENTS (actions), |
| 65 |
app); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
activate (GApplication *application) |
| 70 |
{ |
| 71 |
g_application_hold (application); |
| 72 |
g_print ("activated\n"); |
| 73 |
g_application_release (application); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
app_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 ("open"); |
| 87 |
for (i = 0; i < n_files; i++) |
| 88 |
{ |
| 89 |
gchar *uri = g_file_get_uri (files[i]); |
| 90 |
g_print (" %s", uri); |
| 91 |
g_free (uri); |
| 92 |
} |
| 93 |
g_print ("\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); |
| 108 |
|
| 109 |
if (argc > 1) |
| 110 |
{ |
| 111 |
if (g_strcmp0 (argv[1], "echo") == 0) |
| 112 |
{ |
| 113 |
g_print ("cmdline"); |
| 114 |
for (i = 0; i < argc; i++) |
| 115 |
g_print (" %s", argv[i]); |
| 116 |
g_print ("\n"); |
| 117 |
} |
| 118 |
else if (g_strcmp0 (argv[1], "env") == 0) |
| 119 |
{ |
| 120 |
const gchar * const *env; |
| 121 |
|
| 122 |
env = g_application_command_line_get_environ (cmdline); |
| 123 |
g_print ("environment"); |
| 124 |
for (i = 0; env[i]; i++) |
| 125 |
if (g_str_has_prefix (env[i], "TEST=")) |
| 126 |
g_print (" %s", env[i]); |
| 127 |
g_print ("\n"); |
| 128 |
} |
| 129 |
else if (g_strcmp0 (argv[1], "getenv") == 0) |
| 130 |
{ |
| 131 |
g_print ("getenv TEST=%s\n", g_application_command_line_getenv (cmdline, "TEST")); |
| 132 |
} |
| 133 |
else if (g_strcmp0 (argv[1], "print") == 0) |
| 134 |
{ |
| 135 |
g_application_command_line_print (cmdline, "print %s\n", argv[2]); |
| 136 |
} |
| 137 |
else if (g_strcmp0 (argv[1], "printerr") == 0) |
| 138 |
{ |
| 139 |
g_application_command_line_printerr (cmdline, "printerr %s\n", argv[2]); |
| 140 |
} |
| 141 |
else if (g_strcmp0 (argv[1], "file") == 0) |
| 142 |
{ |
| 143 |
GFile *file; |
| 144 |
|
| 145 |
file = g_application_command_line_create_file_for_arg (cmdline, argv[2]); |
| 146 |
g_print ("file %s\n", g_file_get_path (file)); |
| 147 |
g_object_unref (file); |
| 148 |
} |
| 149 |
else if (g_strcmp0 (argv[1], "properties") == 0) |
| 150 |
{ |
| 151 |
gboolean remote; |
| 152 |
GVariant *data; |
| 153 |
|
| 154 |
g_object_get (cmdline, |
| 155 |
"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 (data); |
| 162 |
g_print ("properties ok\n"); |
| 163 |
} |
| 164 |
else if (g_strcmp0 (argv[1], "cwd") == 0) |
| 165 |
{ |
| 166 |
g_print ("cwd %s\n", g_application_command_line_get_cwd (cmdline)); |
| 167 |
} |
| 168 |
else if (g_strcmp0 (argv[1], "busy") == 0) |
| 169 |
{ |
| 170 |
g_application_mark_busy (g_application_get_default ()); |
| 171 |
g_print ("busy\n"); |
| 172 |
} |
| 173 |
else if (g_strcmp0 (argv[1], "idle") == 0) |
| 174 |
{ |
| 175 |
g_application_unmark_busy (g_application_get_default ()); |
| 176 |
g_print ("idle\n"); |
| 177 |
} |
| 178 |
else if (g_strcmp0 (argv[1], "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 (stream); |
| 186 |
|
| 187 |
g_print ("stdin ok\n"); |
| 188 |
} |
| 189 |
else |
| 190 |
g_print ("unexpected command: %s\n", argv[1]); |
| 191 |
} |
| 192 |
else |
| 193 |
g_print ("got ./cmd %d\n", g_application_command_line_get_is_remote (cmdline)); |
| 194 |
|
| 195 |
g_strfreev (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 (argv[1], "./actions") == 0) |
| 210 |
{ |
| 211 |
app = g_application_get_default (); |
| 212 |
|
| 213 |
if (g_strcmp0 (argv[2], "list") == 0) |
| 214 |
{ |
| 215 |
g_print ("actions"); |
| 216 |
actions = g_action_group_list_actions (G_ACTION_GROUP (app)); |
| 217 |
for (i = 0; actions[i]; i++) |
| 218 |
g_print (" %s", actions[i]); |
| 219 |
g_print ("\n"); |
| 220 |
g_strfreev (actions); |
| 221 |
} |
| 222 |
else if (g_strcmp0 (argv[2], "activate") == 0) |
| 223 |
{ |
| 224 |
g_action_group_activate_action (G_ACTION_GROUP (app), |
| 225 |
"action1", NULL); |
| 226 |
} |
| 227 |
else if (g_strcmp0 (argv[2], "set-state") == 0) |
| 228 |
{ |
| 229 |
g_action_group_change_action_state (G_ACTION_GROUP (app), |
| 230 |
"action2", |
| 231 |
g_variant_new_boolean (TRUE)); |
| 232 |
} |
| 233 |
g_application_release (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 ("org.gtk.TestApplication", |
| 246 |
G_APPLICATION_SEND_ENVIRONMENT | |
| 247 |
(g_strcmp0 (argv[1], "./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 (app_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 (app, 1000); |
| 258 |
#endif |
| 259 |
|
| 260 |
if (g_strcmp0 (argv[1], "./actions") == 0) |
| 261 |
{ |
| 262 |
g_application_set_inactivity_timeout (app, 0); |
| 263 |
g_application_hold (app); |
| 264 |
g_idle_add (action_cb, argv); |
| 265 |
} |
| 266 |
|
| 267 |
status = g_application_run (app, argc - 1, argv + 1); |
| 268 |
|
| 269 |
g_object_unref (app); |
| 270 |
|
| 271 |
g_print ("exit status: %d\n", status); |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |