This file is indexed.

/usr/share/ada/adainclude/dbus-ada/d_bus-connection.adb is in libdbusada0.2-dev 0.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
--
--  D_Bus/Ada - An Ada binding to D-Bus
--
--  Copyright (C) 2011  Reto Buerki <reet@codelabs.ch>
--
--  This program is free software; you can redistribute it and/or
--  modify it under the terms of the GNU General Public License
--  as published by the Free Software Foundation; either version 2
--  of the License, or (at your option) any later version.
--
--  This program 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 General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; if not, write to the Free Software
--  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
--  USA.
--
--  As a special exception, if other files instantiate generics from this
--  unit,  or  you  link  this  unit  with  other  files  to  produce  an
--  executable   this  unit  does  not  by  itself  cause  the  resulting
--  executable to  be  covered by the  GNU General  Public License.  This
--  exception does  not  however  invalidate  any  other reasons why  the
--  executable file might be covered by the GNU Public License.
--

with Interfaces.C.Strings;

with dbus_bus_h;
with dbus_shared_h;
with dbus_errors_h;
with dbus_connection_h;
with dbus_message_h;
with dbus_types_h;
with dbus_arch_deps_h;

package body D_Bus.Connection is

   package C renames Interfaces.C;

   Bus_Types : constant array (Bus_Type) of dbus_shared_h.DBusBusType
     := (Bus_Session => dbus_shared_h.DBUS_BUS_SESSION,
         Bus_System  => dbus_shared_h.DBUS_BUS_SYSTEM,
         Bus_Starter => dbus_shared_h.DBUS_BUS_STARTER);
   --  Mapping of Ada bus type enum to low-level D-Bus bus types.

   procedure Check (Result : access dbus_errors_h.DBusError);
   --  Check D-Bus error object and raise an exception if error is set. The
   --  D-Bus error object is freed before the exception is raised.

   procedure Add_Args
     (D_Message : System.Address;
      Args      : Arguments.Argument_List_Type);
   --  Add arguments to low-level D-Bus message.

   -------------------------------------------------------------------------

   procedure Add_Args
     (D_Message : System.Address;
      Args      : Arguments.Argument_List_Type)
   is
      D_Args : aliased dbus_message_h.DBusMessageIter;
   begin
      dbus_message_h.dbus_message_iter_init_append
        (arg1 => D_Message,
         arg2 => D_Args'Access);

      Arguments.Serialize
        (Args   => Args,
         D_Args => D_Args'Access);
   end Add_Args;

   -------------------------------------------------------------------------

   procedure Add_Match
     (Connection : Connection_Type;
      Rule       : String)
   is
      D_Err : aliased dbus_errors_h.DBusError;
      C_Str : C.Strings.chars_ptr := C.Strings.New_String
        (Str => Rule);
   begin
      dbus_bus_h.dbus_bus_add_match
        (arg1 => Connection.Thin_Connection,
         arg2 => C_Str,
         arg3 => D_Err'Access);
      C.Strings.Free (Item => C_Str);
      Check (Result => D_Err'Access);
   end Add_Match;

   -------------------------------------------------------------------------

   function Call_Blocking
     (Connection  : Connection_Type;
      Destination : String;
      Path        : String;
      Iface       : String;
      Method      : String;
      Args        : Arguments.Argument_List_Type :=
        Arguments.Empty_Argument_List)
      return Arguments.Argument_List_Type
   is
      use type System.Address;
      use type C.int;

      D_Msg   : System.Address := System.Null_Address;
      D_Reply : System.Address := System.Null_Address;
      D_Err   : aliased dbus_errors_h.DBusError;

      C_Dest   : C.Strings.chars_ptr;
      C_Path   : C.Strings.chars_ptr;
      C_Iface  : C.Strings.chars_ptr;
      C_Method : C.Strings.chars_ptr;

      ----------------------------------------------------------------------

      procedure Free_Strings;
      --  Free allocated memory.

      procedure Free_Strings
      is
      begin
         C.Strings.Free (Item => C_Dest);
         C.Strings.Free (Item => C_Path);
         C.Strings.Free (Item => C_Iface);
         C.Strings.Free (Item => C_Method);
      end Free_Strings;

   begin
      C_Dest   := C.Strings.New_String (Str => Destination);
      C_Path   := C.Strings.New_String (Str => Path);
      C_Iface  := C.Strings.New_String (Str => Iface);
      C_Method := C.Strings.New_String (Str => Method);

      D_Msg := dbus_message_h.dbus_message_new_method_call
        (arg1 => C_Dest,
         arg2 => C_Path,
         arg3 => C_Iface,
         arg4 => C_Method);
      Free_Strings;

      if D_Msg = System.Null_Address then
         raise D_Bus_Error with "Could not allocate message";
      end if;

      Add_Args (D_Message => D_Msg,
                Args      => Args);

      D_Reply := dbus_connection_h.dbus_connection_send_with_reply_and_block
        (arg1 => Connection.Thin_Connection,
         arg2 => D_Msg,
         arg3 => -1,
         arg4 => D_Err'Access);
      dbus_message_h.dbus_message_unref (arg1 => D_Msg);
      D_Msg := System.Null_Address;

      if D_Reply = System.Null_Address then
         Check (Result => D_Err'Access);
      end if;

      declare
         use type dbus_types_h.dbus_bool_t;

         D_Args : aliased dbus_message_h.DBusMessageIter;
      begin
         if dbus_message_h.dbus_message_iter_init
           (arg1 => D_Reply,
            arg2 => D_Args'Access) = 0
         then
            dbus_message_h.dbus_message_unref (arg1 => D_Reply);
            return Arguments.Empty_Argument_List;
         end if;

         return A : Arguments.Argument_List_Type do
            A := Arguments.Deserialize (D_Args => D_Args'Access);
            dbus_message_h.dbus_message_unref (arg1 => D_Reply);
         end return;
      end;
   end Call_Blocking;

   -------------------------------------------------------------------------

   procedure Check (Result : access dbus_errors_h.DBusError)
   is
      use type dbus_types_h.dbus_bool_t;
   begin
      if dbus_errors_h.dbus_error_is_set (arg1 => Result) = 1 then
         declare
            Error_String : constant String := C.Strings.Value (Result.message);
         begin
            dbus_errors_h.dbus_error_free (arg1 => Result);
            raise D_Bus_Error with Error_String;
         end;
      end if;
   end Check;

   -------------------------------------------------------------------------

   function Connect (Bus : Bus_Type := Bus_Session) return Connection_Type
   is
      D_Conn : System.Address := System.Null_Address;
      D_Err  : aliased dbus_errors_h.DBusError;
   begin
      D_Conn := dbus_bus_h.dbus_bus_get
        (arg1 => Bus_Types (Bus),
         arg2 => D_Err'Access);
      Check (Result => D_Err'Access);

      return Connection_Type'(Thin_Connection => D_Conn);
   end Connect;

   -------------------------------------------------------------------------

   function Connect (Address : String) return Connection_Type
   is
      C_Addr : C.Strings.chars_ptr := C.Strings.New_String (Str => Address);
      D_Conn : System.Address      := System.Null_Address;
      D_Err  : aliased dbus_errors_h.DBusError;
   begin
      D_Conn := dbus_connection_h.dbus_connection_open
        (arg1 => C_Addr,
         arg2 => D_Err'Access);
      C.Strings.Free (Item => C_Addr);
      Check (Result => D_Err'Access);

      return Connection_Type'(Thin_Connection => D_Conn);
   end Connect;

   -------------------------------------------------------------------------

   procedure Dispatch
     (Connection : Connection_Type;
      Callback   : Callbacks.Message_Callback)
   is
      use type C.int;
      use type dbus_types_h.dbus_bool_t;

      function Call_Back
        (D_Conn   : System.Address;
         Msg      : System.Address;
         Usr_Data : System.Address)
         return dbus_shared_h.DBusHandlerResult;
      --  Dispatch deserialized message to given callback procedure.

      function Call_Back
        (D_Conn   : System.Address;
         Msg      : System.Address;
         Usr_Data : System.Address)
         return dbus_shared_h.DBusHandlerResult
      is
         pragma Unreferenced (D_Conn, Usr_Data);
      begin
         Callback (Msg => Messages.Create (D_Msg => Msg));

         return dbus_shared_h.DBUS_HANDLER_RESULT_HANDLED;
      end Call_Back;

      procedure Free_Usr_Data (arg1 : System.Address) is null;

      D_Res : dbus_types_h.dbus_bool_t;
   begin
      D_Res := dbus_connection_h.dbus_connection_add_filter
        (arg1 => Connection.Thin_Connection,
         arg2 => Call_Back'Access,
         arg3 => System.Null_Address,
         arg4 => Free_Usr_Data'Access);

      if D_Res = 0 then
         raise D_Bus_Error with "Could not add connection filter";
      end if;

      while dbus_connection_h.dbus_connection_read_write_dispatch
        (arg1 => Connection.Thin_Connection,
         arg2 => -1) = 1
      loop
         null;
      end loop;
   end Dispatch;

   -------------------------------------------------------------------------

   procedure Request_Name
     (Connection : Connection_Type;
      Name       : String)
   is
      use type C.int;
      use type C.unsigned;

      C_Res  : C.int;
      C_Name : C.Strings.chars_ptr := C.Strings.New_String (Str => Name);
      D_Err  : aliased dbus_errors_h.DBusError;
   begin
      C_Res := dbus_bus_h.dbus_bus_request_name
        (arg1 => Connection.Thin_Connection,
         arg2 => C_Name,
         arg3 => dbus_shared_h.DBUS_NAME_FLAG_REPLACE_EXISTING,
         arg4 => D_Err'Access);
      C.Strings.Free (Item => C_Name);

      Check (Result => D_Err'Access);

      if C_Res /= dbus_shared_h.DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then
         raise D_Bus_Error with "Not primary owner for '"
           & Name & "' (" & C_Res'Img & ")";
      end if;
   end Request_Name;

   -------------------------------------------------------------------------

   procedure Send
     (Connection : Connection_Type;
      Message    : Messages.Message_Type)
   is
      use type dbus_types_h.dbus_bool_t;

      D_Serial : aliased dbus_arch_deps_h.dbus_uint32_t := 0;
   begin
      if dbus_connection_h.dbus_connection_send
        (arg1 => Connection.Thin_Connection,
         arg2 => Messages.To_Thin (Msg => Message),
         arg3 => D_Serial'Access) = 0
      then
         raise D_Bus_Error with "Could not send message: out of memory";
      end if;
   end Send;

   -------------------------------------------------------------------------

   procedure Send_Signal
     (Connection  : Connection_Type;
      Object_Name : String;
      Iface       : String;
      Name        : String;
      Args        : Arguments.Argument_List_Type :=
        Arguments.Empty_Argument_List)
   is
      use type System.Address;

      D_Msg : System.Address := System.Null_Address;

      C_Object : C.Strings.chars_ptr;
      C_Iface  : C.Strings.chars_ptr;
      C_Name   : C.Strings.chars_ptr;

      ----------------------------------------------------------------------

      procedure Free_Strings;
      --  Free allocated memory.

      procedure Free_Strings
      is
      begin
         C.Strings.Free (Item => C_Object);
         C.Strings.Free (Item => C_Iface);
         C.Strings.Free (Item => C_Name);
      end Free_Strings;

   begin
      C_Object := C.Strings.New_String (Str => Object_Name);
      C_Iface  := C.Strings.New_String (Str => Iface);
      C_Name   := C.Strings.New_String (Str => Name);

      D_Msg := dbus_message_h.dbus_message_new_signal
        (arg1 => C_Object,
         arg2 => C_Iface,
         arg3 => C_Name);

      if D_Msg = System.Null_Address then
         Free_Strings;
         raise D_Bus_Error with "Could not allocate message";
      end if;

      begin
         Add_Args (D_Message => D_Msg,
                   Args      => Args);

      exception
         when D_Bus_Error =>
            Free_Strings;
            raise;
      end;

      declare
         use type C.unsigned;

         D_Serial : aliased dbus_arch_deps_h.dbus_uint32_t := 0;
      begin
         if dbus_connection_h.dbus_connection_send
           (arg1 => Connection.Thin_Connection,
            arg2 => D_Msg,
            arg3 => D_Serial'Access) = 0
         then
            Free_Strings;
            raise D_Bus_Error with "Could not send signal";
         end if;
      end;

      Free_Strings;
      dbus_message_h.dbus_message_unref (arg1 => D_Msg);
   end Send_Signal;

end D_Bus.Connection;