summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/glib-sharp/ParamSpec.cs
blob: c5350dd3c66c90b5179bff39652645acf0244d37 (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
// ParamSpec.cs - GParamSpec class wrapper implementation
//
// Authors: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2008 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General 
// Public License as published by the Free Software Foundation.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


namespace Gst.GLib {

	using System;
	using System.Runtime.InteropServices;

	internal enum ParamFlags {
		None          = 0,
		Readable      = 1 << 0,
		Writable      = 1 << 1,
		Construct     = 1 << 2,
		ConstructOnly = 1 << 3,
	}

	public class ParamSpec {

		IntPtr handle;

		private static IntPtr CreateParamSpec (string name, string nick, string blurb, GType type, ParamFlags pflags) {
			int flags = (int) pflags;
			IntPtr ret;

			IntPtr p_name = GLib.Marshaller.StringToPtrGStrdup (name);
			IntPtr p_nick = GLib.Marshaller.StringToPtrGStrdup (nick);
			IntPtr p_blurb = GLib.Marshaller.StringToPtrGStrdup (blurb);

			if (type == GType.Char)
				ret = g_param_spec_char (p_name, p_nick, p_blurb, SByte.MinValue, SByte.MaxValue, 0, flags);
			else if (type == GType.UChar)
				ret = g_param_spec_uchar (p_name, p_nick, p_blurb, Byte.MinValue, Byte.MaxValue, 0, flags);
			else if (type == GType.Boolean)
				ret = g_param_spec_boolean (p_name, p_nick, p_blurb, false, flags);
			else if (type == GType.Int)
				ret = g_param_spec_int (p_name, p_nick, p_blurb, Int32.MinValue, Int32.MaxValue, 0, flags);
			else if (type == GType.UInt)
				ret = g_param_spec_uint (p_name, p_nick, p_blurb, 0, UInt32.MaxValue, 0, flags);
			else if (type == GType.Long)
				ret = g_param_spec_long (p_name, p_nick, p_blurb, IntPtr.Zero, IntPtr.Size == 4 ? new IntPtr (Int32.MaxValue) : new IntPtr (Int64.MaxValue), IntPtr.Zero, flags);
			else if (type == GType.ULong)
				ret = g_param_spec_ulong (p_name, p_nick, p_blurb, UIntPtr.Zero, UIntPtr.Size == 4 ? new UIntPtr (UInt32.MaxValue) : new UIntPtr (UInt64.MaxValue), UIntPtr.Zero, flags);
			else if (type == GType.Int64)
				ret = g_param_spec_int64 (p_name, p_nick, p_blurb, Int64.MinValue, Int64.MaxValue, 0, flags);
			else if (type == GType.UInt64)
				ret = g_param_spec_uint64 (p_name, p_nick, p_blurb, 0, UInt64.MaxValue, 0, flags);
			/*
			else if (type == GType.Enum)
			else if (type == GType.Flags)
			* TODO: 
			* Both g_param_spec_enum and g_param_spec_flags expect default property values and the members of the enum seemingly cannot be enumerated
			*/
			else if (type == GType.Float)
				ret = g_param_spec_float (p_name, p_nick, p_blurb, Single.MinValue, Single.MaxValue, 0.0f, flags);
			else if (type == GType.Double)
				ret = g_param_spec_double (p_name, p_nick, p_blurb, Double.MinValue, Double.MaxValue, 0.0, flags);
			else if (type == GType.String)
				ret = g_param_spec_string (p_name, p_nick, p_blurb, IntPtr.Zero, flags);
			else if (type == GType.Pointer)
				ret = g_param_spec_pointer (p_name, p_nick, p_blurb, flags);
			else if (type.Val == g_gtype_get_type ())
				ret = g_param_spec_gtype (p_name, p_nick, p_blurb, GType.None.Val, flags);
			else if (g_type_is_a (type.Val, GType.Boxed.Val))
				ret = g_param_spec_boxed (p_name, p_nick, p_blurb, type.Val, flags);
			else if (g_type_is_a (type.Val, GType.Object.Val))
				ret = g_param_spec_object (p_name, p_nick, p_blurb, type.Val, flags);
			else
				throw new ArgumentException ("type");

			GLib.Marshaller.Free (p_name);
			GLib.Marshaller.Free (p_nick);
			GLib.Marshaller.Free (p_blurb);

			return ret;
		}

		internal ParamSpec (string name, string nick, string blurb, GType type, ParamFlags pflags) {
			handle = CreateParamSpec (name, nick, blurb, type, pflags);
		}

		public ParamSpec (string name, string nick, string blurb, GType type, bool readable, bool writable)
		{
			ParamFlags pflags = ParamFlags.None;
			if (readable) pflags |= ParamFlags.Readable;
			if (writable) pflags |= ParamFlags.Writable;

			handle = CreateParamSpec (name, nick, blurb, type, pflags);
		}

		public ParamSpec (IntPtr native)
		{
			handle = native;
		}

		public IntPtr Handle {
			get { return handle; }
		}

		public GType ValueType {
			get {
				GParamSpec spec = (GParamSpec) Marshal.PtrToStructure (Handle, typeof (GParamSpec));
				return new GType (spec.value_type);
			}
		}

		public string Name {
			get {
				GParamSpec spec = (GParamSpec) Marshal.PtrToStructure (Handle, typeof (GParamSpec));
				return GLib.Marshaller.Utf8PtrToString (spec.name);
			}
		}

		struct GTypeInstance {
			IntPtr g_class;
		}

		struct GParamSpec {
			GTypeInstance  g_type_instance;

			public IntPtr name;
			ParamFlags flags;
			public IntPtr value_type;
			IntPtr owner_type;

			IntPtr _nick;
			IntPtr _blurb;
			IntPtr qdata;
			uint ref_count;
			uint param_id;
		}

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_char (IntPtr name, IntPtr nick, IntPtr blurb, sbyte min, sbyte max, sbyte dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_uchar (IntPtr name, IntPtr nick, IntPtr blurb, byte min, byte max, byte dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_boolean (IntPtr name, IntPtr nick, IntPtr blurb, bool dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_int (IntPtr name, IntPtr nick, IntPtr blurb, int min, int max, int dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_uint (IntPtr name, IntPtr nick, IntPtr blurb, uint min, uint max, uint dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_long (IntPtr name, IntPtr nick, IntPtr blurb, IntPtr min, IntPtr max, IntPtr dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_ulong (IntPtr name, IntPtr nick, IntPtr blurb, UIntPtr min, UIntPtr max, UIntPtr dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_int64 (IntPtr name, IntPtr nick, IntPtr blurb, long min, long max, long dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_uint64 (IntPtr name, IntPtr nick, IntPtr blurb, ulong min, ulong max, ulong dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_float (IntPtr name, IntPtr nick, IntPtr blurb, float min, float max, float dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_double (IntPtr name, IntPtr nick, IntPtr blurb, double min, double max, double dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_string (IntPtr name, IntPtr nick, IntPtr blurb, IntPtr dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_pointer (IntPtr name, IntPtr nick, IntPtr blurb, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_gtype (IntPtr name, IntPtr nick, IntPtr blurb, IntPtr dval, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_boxed (IntPtr name, IntPtr nick, IntPtr blurb, IntPtr return_type, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_param_spec_object (IntPtr name, IntPtr nick, IntPtr blurb, IntPtr return_type, int flags);

		[DllImport("libgobject-2.0-0.dll")]
		static extern IntPtr g_gtype_get_type ();

		[DllImport("libgobject-2.0-0.dll")]
		static extern bool g_type_is_a (IntPtr a, IntPtr b);

	}
}