summaryrefslogtreecommitdiffstats
path: root/mono/DBusType/Custom.cs
blob: 925606497e7013c510e385fdaabd439757cee5c9 (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
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// A named byte array, used for custom types.
  /// </summary>
  public class Custom : IDBusType
  {
    public const char Code = 'c';
    private DBus.Custom val;
    
    private Custom()
    {
    }
    
    public Custom(DBus.Custom val, Service service) 
    {
      this.val = val;
    }

    public Custom(IntPtr iter, Service service)
    {
      string name;
      IntPtr value;
      int len;

      if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) {
	throw new ApplicationException("Failed to get CUSTOM argument.");
      }

      this.val.Name = name;
      this.val.Data = new byte[len];
      Marshal.Copy(value, this.val.Data, 0, len);
    }
    
    public void Append(IntPtr iter)
    {
      IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length);
      try {
	Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length);
	if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) {
	  throw new ApplicationException("Failed to append CUSTOM argument:" + val);
	}
      } finally {
	Marshal.FreeCoTaskMem(data);
      }
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "DBus.Custom":
      case "DBus.Custom&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldobj, type);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldobj, type);
      if (!isReturn) {
	generator.Emit(OpCodes.Stobj, type);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "DBus.Custom":
      case "DBus.Custom&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'");
      }
    }

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_get_custom(IntPtr iter,
							    out string name,
							    out IntPtr value,
							    out int len);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_custom(IntPtr iter, 
							       string name,
							       IntPtr data,
							       int len);
  }
}