5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Runtime.InteropServices;
28
using System.Collections.Generic;
30
namespace MonoDevelop.MacInterop
32
internal static class AppleEvent
34
const string AELib = Carbon.CarbonLib;
36
//FIXME: is "int" correct for size?
38
static extern AEDescStatus AECreateDesc (OSType typeCode, IntPtr dataPtr, int dataSize, out AEDesc desc);
41
static extern AEDescStatus AECreateDesc (OSType typeCode, byte[] data, int dataSize, out AEDesc desc);
44
static extern AEDescStatus AEGetNthPtr (ref AEDesc descList, int index, OSType desiredType, uint keyword,
45
out CarbonEventParameterType actualType, IntPtr buffer, int bufferSize, out int actualSize);
48
static extern AEDescStatus AEGetNthPtr (ref AEDesc descList, int index, OSType desiredType, uint keyword,
49
uint zero, IntPtr buffer, int bufferSize, int zero2);
52
static extern AEDescStatus AECountItems (ref AEDesc descList, out int count); //return an OSErr
55
static extern AEDescStatus AEGetNthPtr (ref AEDesc descList, int index, OSType desiredType, uint keyword,
56
uint zero, out IntPtr outPtr, int bufferSize, int zero2);
59
public static extern AEDescStatus AEDisposeDesc (ref AEDesc desc);
62
public static extern AEDescStatus AESizeOfNthItem (ref AEDesc descList, int index, ref OSType type, out int size);
65
static extern AEDescStatus AEGetDescData (ref AEDesc desc, IntPtr ptr, int maximumSize);
68
static extern int AEGetDescDataSize (ref AEDesc desc);
71
static extern AEDescStatus AECoerceDesc (ref AEDesc theAEDesc, DescType toType, ref AEDesc result);
73
public static void AECreateDesc (OSType typeCode, byte[] data, out AEDesc result)
75
CheckReturn (AECreateDesc (typeCode, data, data.Length, out result));
78
public static void AECreateDescUtf8 (string value, out AEDesc result)
80
var type = (OSType)(int)CarbonEventParameterType.UnicodeText;
81
var bytes = System.Text.Encoding.UTF8.GetBytes (value);
82
CheckReturn (AECreateDesc (type, bytes, bytes.Length, out result));
85
public static void AECreateDescAscii (string value, out AEDesc result)
87
var type = (OSType)(int)CarbonEventParameterType.Char;
88
var bytes = System.Text.Encoding.ASCII.GetBytes (value);
89
CheckReturn (AECreateDesc (type, bytes, bytes.Length, out result));
92
public static void AECreateDescNull (out AEDesc desc)
94
CheckReturn (AECreateDesc ((OSType)0, IntPtr.Zero, 0, out desc));
97
public static int AECountItems (ref AEDesc descList)
100
CheckReturn (AECountItems (ref descList, out count));
104
public static T AEGetNthPtr<T> (ref AEDesc descList, int index, OSType desiredType) where T : struct
106
int len = Marshal.SizeOf (typeof (T));
107
IntPtr bufferPtr = Marshal.AllocHGlobal (len);
109
CheckReturn (AEGetNthPtr (ref descList, index, desiredType, 0, 0, bufferPtr, len, 0));
110
T val = (T)Marshal.PtrToStructure (bufferPtr, typeof (T));
113
Marshal.FreeHGlobal (bufferPtr);
117
public static IntPtr AEGetNthPtr (ref AEDesc descList, int index, OSType desiredType)
120
CheckReturn (AEGetNthPtr (ref descList, index, desiredType, 0, 0, out ret, 4, 0));
124
//FIXME: this might not work in some encodings. need to test more.
125
static string GetUtf8StringFromAEPtr (ref AEDesc descList, int index)
128
var type = (OSType)(int)CarbonEventParameterType.UnicodeText;
129
if (AESizeOfNthItem (ref descList, index, ref type, out size) == AEDescStatus.Ok) {
130
IntPtr buffer = Marshal.AllocHGlobal (size);
132
if (AEGetNthPtr (ref descList, index, type, 0, 0, buffer, size, 0) == AEDescStatus.Ok)
133
return Marshal.PtrToStringAuto (buffer, size);
135
Marshal.FreeHGlobal (buffer);
141
public static string GetStringFromAEDesc (ref AEDesc desc)
143
int size = AEGetDescDataSize (ref desc);
145
IntPtr buffer = Marshal.AllocHGlobal (size);
147
if (AEGetDescData (ref desc, buffer, size) == AEDescStatus.Ok)
148
return Marshal.PtrToStringAuto (buffer, size);
150
Marshal.FreeHGlobal (buffer);
156
public static IList<string> GetUtf8StringListFromAEDesc (ref AEDesc list, bool skipEmpty)
158
long count = AppleEvent.AECountItems (ref list);
159
var items = new List<string> ();
160
for (int i = 1; i <= count; i++) {
161
string str = AppleEvent.GetUtf8StringFromAEPtr (ref list, i);
162
if (!string.IsNullOrEmpty (str))
168
public static T[] GetListFromAEDesc<T,TRef> (ref AEDesc list, AEDescValueSelector<TRef,T> sel, OSType type)
171
long count = AppleEvent.AECountItems (ref list);
172
T[] arr = new T[count];
173
for (int i = 1; i <= count; i++) {
174
TRef r = AppleEvent.AEGetNthPtr<TRef> (ref list, i, type);
175
arr [i - 1] = sel (ref r);
180
static void CheckReturn (AEDescStatus status)
182
if (status != AEDescStatus.Ok)
183
throw new Exception ("Failed with code " + status.ToString ());
187
public delegate T AEDescValueSelector<TRef,T> (ref TRef desc);
189
[StructLayout(LayoutKind.Sequential, Pack = 2)]
192
public uint descriptorType;
193
public IntPtr dataHandle;
196
public enum AEDescStatus
200
CoercionFail = -1700,
201
DescRecordNotFound = -1701,
202
WrongDataType = -1703,
204
ReplyNotArrived = -1718,
207
public enum AESendMode {
208
NoReply = 0x00000001,
209
QueueReply = 0x00000002,
210
WaitReply = 0x00000003,
211
DontReconnect = 0x00000080,
212
WantReceipt = 0x00000200,
213
NeverInteract = 0x00000010,
214
CanInteract = 0x00000020,
215
AlwaysInteract = 0x00000030,
216
CanSwitchLayer = 0x00000040,
217
DontRecord = 0x00001000,
218
DontExecute = 0x00002000,
219
ProcessNonReplyEvents = 0x00008000,
b'\\ No newline at end of file'