1 | #include <gio/gio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | static void |
6 | activate (GApplication *application) |
7 | { |
8 | g_application_hold (application); |
9 | g_print (format: "activated\n" ); |
10 | g_application_release (application); |
11 | } |
12 | |
13 | static void |
14 | activate_action (GAction *action, |
15 | GVariant *parameter, |
16 | gpointer data) |
17 | { |
18 | GApplication *application = G_APPLICATION (data); |
19 | |
20 | g_application_hold (application); |
21 | g_print (format: "action %s activated\n" , g_action_get_name (action)); |
22 | g_application_release (application); |
23 | } |
24 | |
25 | static void |
26 | activate_toggle_action (GSimpleAction *action, |
27 | GVariant *parameter, |
28 | gpointer data) |
29 | { |
30 | GApplication *application = G_APPLICATION (data); |
31 | GVariant *state; |
32 | gboolean b; |
33 | |
34 | g_print (format: "action %s activated\n" , g_action_get_name (G_ACTION (action))); |
35 | |
36 | g_application_hold (application); |
37 | state = g_action_get_state (G_ACTION (action)); |
38 | b = g_variant_get_boolean (value: state); |
39 | g_variant_unref (value: state); |
40 | g_simple_action_set_state (simple: action, value: g_variant_new_boolean (value: !b)); |
41 | g_print (format: "state change %d -> %d\n" , b, !b); |
42 | g_application_release (application); |
43 | } |
44 | |
45 | static void |
46 | add_actions (GApplication *app) |
47 | { |
48 | GSimpleAction *action; |
49 | |
50 | action = g_simple_action_new (name: "simple-action" , NULL); |
51 | g_signal_connect (action, "activate" , G_CALLBACK (activate_action), app); |
52 | g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); |
53 | g_object_unref (object: action); |
54 | |
55 | action = g_simple_action_new_stateful (name: "toggle-action" , NULL, |
56 | state: g_variant_new_boolean (FALSE)); |
57 | g_signal_connect (action, "activate" , G_CALLBACK (activate_toggle_action), app); |
58 | g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); |
59 | g_object_unref (object: action); |
60 | } |
61 | |
62 | static void |
63 | describe_and_activate_action (GActionGroup *group, |
64 | const gchar *name) |
65 | { |
66 | const GVariantType *param_type; |
67 | GVariant *state; |
68 | gboolean enabled; |
69 | gchar *tmp; |
70 | |
71 | param_type = g_action_group_get_action_parameter_type (action_group: group, action_name: name); |
72 | state = g_action_group_get_action_state (action_group: group, action_name: name); |
73 | enabled = g_action_group_get_action_enabled (action_group: group, action_name: name); |
74 | |
75 | g_print (format: "action name: %s\n" , name); |
76 | tmp = param_type ? g_variant_type_dup_string (type: param_type) : NULL; |
77 | g_print (format: "parameter type: %s\n" , tmp ? tmp : "<none>" ); |
78 | g_free (mem: tmp); |
79 | g_print (format: "state type: %s\n" , |
80 | state ? g_variant_get_type_string (value: state) : "<none>" ); |
81 | tmp = state ? g_variant_print (value: state, FALSE) : NULL; |
82 | g_print (format: "state: %s\n" , tmp ? tmp : "<none>" ); |
83 | g_free (mem: tmp); |
84 | g_print (format: "enabled: %s\n" , enabled ? "true" : "false" ); |
85 | |
86 | if (state != NULL) |
87 | g_variant_unref (value: state); |
88 | |
89 | g_action_group_activate_action (action_group: group, action_name: name, NULL); |
90 | } |
91 | |
92 | int |
93 | main (int argc, char **argv) |
94 | { |
95 | GApplication *app; |
96 | int status; |
97 | |
98 | app = g_application_new (application_id: "org.gtk.TestApplication" , flags: 0); |
99 | g_signal_connect (app, "activate" , G_CALLBACK (activate), NULL); |
100 | g_application_set_inactivity_timeout (application: app, inactivity_timeout: 10000); |
101 | |
102 | add_actions (app); |
103 | |
104 | if (argc > 1 && strcmp (s1: argv[1], s2: "--simple-action" ) == 0) |
105 | { |
106 | g_application_register (application: app, NULL, NULL); |
107 | describe_and_activate_action (G_ACTION_GROUP (app), name: "simple-action" ); |
108 | exit (status: 0); |
109 | } |
110 | else if (argc > 1 && strcmp (s1: argv[1], s2: "--toggle-action" ) == 0) |
111 | { |
112 | g_application_register (application: app, NULL, NULL); |
113 | describe_and_activate_action (G_ACTION_GROUP (app), name: "toggle-action" ); |
114 | exit (status: 0); |
115 | } |
116 | |
117 | status = g_application_run (application: app, argc, argv); |
118 | |
119 | g_object_unref (object: app); |
120 | |
121 | return status; |
122 | } |
123 | |