summaryrefslogtreecommitdiff
path: root/tools/inspect/command-signals.vala
blob: 2de96a8a2ae74ff21b9fb9982e2f62122abf2271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Philip Withnall <philip.withnall@collabora.co.uk>
 */

using Folks;
using Gee;
using GLib;

/*
 * signals — list signals we're currently connected to
 * signals connect ClassName — connect to all the signals on all the instances
 *     of that class
 * signals connect ClassName::signal — connect to the given signal on all the
 *     instances of that class
 * signals connect 0xdeadbeef — connect to all the signals on a particular class
 *     instance
 * signals connect 0xdeadbeef::signal — connect to the given signal on a
 *     particular class instance
 * signals disconnect (as above)
 * signals disconnect — signal handler ID
 * signals ClassName — list all the signals on all the instances of that class,
 *     highlighting the ones we're currently connected to
 * signals 0xdeadbeef — list all the signals on a particular class instance,
 *     highlighting the ones we're currently connected to
 * signals ClassName::signal — show the details of this signal
 * signals 0xdeadbeef::signal — show the details of this signal
 */

private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
{
  public override string name
    {
      get { return "signals"; }
    }

  public override string description
    {
      get
        {
          return "Allow connection to and display of signals emitted by " +
              "libfolks.";
        }
    }

  public override string help
    {
      get
        {
          return "signals                                            " +
              "List signals we're currently connected to.\n" +
              "signals connect [class name]                       " +
              "Connect to all the signals on all the instances of that " +
              "class.\n" +
              "signals connect [class name]::[signal name]        " +
              "Connect to the given signal on all the instances of that " +
              "class.\n" +
              "signals connect [object pointer]                   " +
              "Connect to all the signals on a particular class instance.\n" +
              "signals connect [object pointer]::[signal name]    " +
              "Connect to the given signal on a particular class instance.\n" +
              "signals disconnect                                 " +
              "(As for 'connect'.)\n" +
              "signals [class name]                               " +
              "List all the signals on all the instances of that class, " +
              "highlighting the ones we're currently connected to.\n" +
              "signals [object pointer]                           " +
              "List all the signals on a particular class instance, " +
              "highlighting the ones we're currently connected to.\n" +
              "signals [class name]::[signal name]                " +
              "Show the details of this signal.\n" +
              "signals [object pointer]::[signal name]            " +
              "Show the details of this signal.";
        }
    }

  public Signals (Client client)
    {
      base (client);
    }

  public override void run (string? command_string)
    {
      if (command_string == null)
        {
          /* List all the signals we're connected to */
          this.client.signal_manager.list_signals (Type.INVALID, null);
        }
      else
        {
          /* Parse subcommands */
          string[] parts = command_string.split (" ", 2);

          if (parts.length < 1)
            {
              Utils.print_line ("Unrecognised 'signals' command '%s'.",
                command_string);
              return;
            }

          Type class_type;
          Object class_instance;
          string signal_name;
          string detail_string;

          if (parts[0] == "connect" || parts[0] == "disconnect")
            {
              /* Connect to or disconnect from a signal */
              if (parts[1] == null || parts[1].strip () == "")
                {
                  Utils.print_line ("Unrecognised signal identifier '%s'.",
                      parts[1]);
                  return;
                }

              if (this.parse_signal_id (parts[1].strip (), out class_type,
                  out class_instance, out signal_name,
                  out detail_string) == false)
                {
                  return;
                }

              /* FIXME: Handle "disconnect <signal ID>" */
              if (parts[0] == "connect")
                {
                  uint signal_count =
                      this.client.signal_manager.connect_to_signal (class_type,
                          class_instance, signal_name, detail_string);
                  Utils.print_line ("Connected to %u signals.", signal_count);
                }
              else
                {
                  uint signal_count =
                      this.client.signal_manager.disconnect_from_signal (
                          class_type, class_instance, signal_name,
                              detail_string);
                  Utils.print_line ("Disconnected from %u signals.",
                      signal_count);
                }
            }
          else
            {
              /* List some of the signals we're connected to, or display
               * their details. */
              if (this.parse_signal_id (parts[0].strip (), out class_type,
                  out class_instance, out signal_name,
                  out detail_string) == false)
                {
                  return;
                }

              if (signal_name == null)
                {
                  this.client.signal_manager.list_signals (class_type,
                      class_instance);
                }
              else
                {
                  /* Get the class type from the instance */
                  if (class_type == Type.INVALID)
                    class_type = class_instance.get_type ();

                  this.client.signal_manager.show_signal_details (class_type,
                      signal_name, detail_string);
                }
            }
        }
    }

  public override string[]? complete_subcommand (string subcommand)
    {
      /* @subcommand should be a backend name */
      /* TODO */
      return Readline.completion_matches (subcommand,
          Utils.backend_name_completion_cb);
    }

  private bool parse_signal_id (string input,
      out Type class_type,
      out Object? class_instance,
      out string? signal_name,
      out string? detail_string)
    {
      /* We accept any of the following formats:
       *  ClassName::signal-name
       *  ClassName::signal-name::detail
       *  0xdeadbeef::signal-name
       *  0xdeadbeef::signal-name::detail
       *  ClassName
       *  0xdeadbeef
       *
       * We output exactly one of class_type and class_instance, and optionally
       * output signal_name and/or detail_string as appropriate.
       */
      assert (input != null && input != "");

      string[] parts = input.split ("::", 3);
      string class_name_or_instance = parts[0];
      string signal_name_inner = (parts.length > 1) ? parts[1] : null;
      string detail_string_inner = (parts.length > 2) ? parts[2] : null;

      if (signal_name_inner == "" || detail_string_inner == "")
        {
          Utils.print_line ("Invalid signal identifier '%s'.", input);
          return false;
        }

      if (class_name_or_instance.length > 2 &&
          class_name_or_instance[0] == '0' && class_name_or_instance[1] == 'x')
        {
          /* We have a class instance */
          ulong address = class_name_or_instance.to_ulong (null, 16);
          class_instance = (Object) address;
          assert (class_instance.get_type ().is_object ());
        }
      else
        {
          /* We have a class name */
          class_type = Type.from_name (class_name_or_instance);
          if (class_type == Type.INVALID ||
              (class_type.is_instantiatable () == false &&
               class_type.is_interface () == false))
            {
              Utils.print_line ("Unrecognised class name '%s'.",
                  class_name_or_instance);
              return false;
            }
        }

      signal_name = signal_name_inner;
      detail_string = detail_string_inner;

      return true;
    }
}