tash_8.7.2_4c588c12/demos/watching_support.adb

 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
--  Copyright 2017-2022 Simon Wright <simon@pushface.org>
--
--  This unit is free software; you can redistribute it and/or modify
--  it as you wish. This unit 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.

--  This program (with watching.tcl) demonstrates the use of
--  Tcl_SetVar[2]() to get an Ada-domain value back into the Tcl
--  domain without the use of polling.

with CArgv;
with Interfaces.C.Strings;
with Tcl.Ada;
with Tcl.Async;

package body Watching_Support is

   --  Handy wrapper for C.Strings.Free, so it can be used to free
   --  results.
   procedure Freeproc (BlockPtr : Interfaces.C.Strings.chars_ptr);
   pragma Convention (C, Freeproc);

   function Square
     (Client_Data : Integer;
      Interp      : Tcl.Tcl_Interp;
      Argc        : Interfaces.C.int;
      Argv        : CArgv.Chars_Ptr_Ptr) return Interfaces.C.int;
   pragma Convention (C, Square);

   function Init (Interp : Tcl.Tcl_Interp) return Interfaces.C.int is

      package CreateCommands is new Tcl.Ada.Generic_Command (Integer);
      Command : Tcl.Tcl_Command;
      pragma Unreferenced (Command);

      use type Interfaces.C.int;

   begin

      if Tcl.Tcl_Init (Interp) = Tcl.TCL_ERROR then
         return Tcl.TCL_ERROR;
      end if;

      Tcl.Async.Register (Interp);

      Command := CreateCommands.Tcl_CreateCommand
        (Interp,
         "square",
         Square'Unrestricted_Access,
         0,
         null);

      return Tcl.TCL_OK;

   end Init;

   procedure Freeproc (BlockPtr : Interfaces.C.Strings.chars_ptr)
   is
      Tmp : Interfaces.C.Strings.chars_ptr := BlockPtr;
   begin
      Interfaces.C.Strings.Free (Tmp);
   end Freeproc;

   function Square
     (Client_Data : Integer;
      Interp      : Tcl.Tcl_Interp;
      Argc        : Interfaces.C.int;
      Argv        : CArgv.Chars_Ptr_Ptr) return Interfaces.C.int
   is
      pragma Unreferenced (Client_Data);
      Input : Integer;
      Squared : Integer;
      use type Interfaces.C.int;
   begin
      pragma Assert (Argc = 2, "'square' requires one integer argument");

      Input := Integer'Value
        (Interfaces.C.Strings.Value
          (CArgv.Argv_Pointer.Value (Argv) (1)));
      Squared := Input * Input;

      Tcl.Tcl_SetResult
        (Interp,
         Interfaces.C.Strings.New_String (Integer'Image (Squared)),
         Freeproc'Unrestricted_Access);

      Tcl.Async.Set (Tcl_Array => "tellback",
                     Index     => "42",
                     Value     => Integer'Image (Squared));

      return Tcl.TCL_OK;
   end Square;

end Watching_Support;