~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/AudioToolbox/SystemSound.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// 
2
 
// AudioServices.cs:
 
2
// SystemSound.cs: AudioServices system sound
3
3
//
4
4
// Authors: Mono Team
 
5
//          Marek Safar (marek.safar@gmail.com)
5
6
//     
6
7
// Copyright 2009 Novell, Inc
 
8
// Copyright 2012 Xamarin Inc.
7
9
//
8
10
// Permission is hereby granted, free of charge, to any person obtaining
9
11
// a copy of this software and associated documentation files (the
29
31
using System.Runtime.InteropServices;
30
32
 
31
33
using MonoMac.Foundation;
 
34
using MonoMac.CoreFoundation;
32
35
using MonoMac.ObjCRuntime;
33
36
 
34
37
namespace MonoMac.AudioToolbox {
35
38
 
36
 
        enum AudioServiceErrors {
37
 
                None                      = 0,
38
 
                UnsupportedProperty       = 0x7074793f, // 'pty?'
39
 
                BadPropertySize           = 0x2173697a, // '!siz'
40
 
                BadSpecifierSize          = 0x21737063, // '!spc'
41
 
                SystemSoundUnspecified    = -1500,
42
 
                SystemSoundClientTimedOut = -1501,
43
 
        }
44
 
 
45
 
        delegate void SystemSoundCompletionCallback (uint ssID, IntPtr clientData);
46
 
 
47
39
        enum SystemSoundId : uint {
48
40
                Vibrate = 0x00000FFF,
49
41
        }
50
42
 
51
43
        public class SystemSound : INativeObject, IDisposable {
52
 
 
 
44
#if MONOMAC
 
45
                // TODO:
 
46
#else
53
47
                public static readonly SystemSound Vibrate = new SystemSound ((uint) SystemSoundId.Vibrate, false);
 
48
#endif
54
49
 
55
50
                uint soundId;
56
51
                bool ownsHandle;
57
52
 
 
53
                Action completionRoutine;
 
54
                GCHandle gc_handle;
 
55
                static readonly Action<SystemSoundId, IntPtr> SoundCompletionCallback = SoundCompletionShared;
 
56
 
58
57
                internal SystemSound (uint soundId, bool ownsHandle)
59
58
                {
60
59
                        this.soundId = soundId;
73
72
                        }
74
73
                }
75
74
 
 
75
                public bool IsUISound {
 
76
                        get {
 
77
                                uint out_size = sizeof (uint);
 
78
                                uint data;
 
79
 
 
80
                                var res = AudioServices.AudioServicesGetProperty (AudioServicesPropertyKey.IsUISound, sizeof (AudioServicesPropertyKey), ref soundId, out out_size, out data);
 
81
                                if (res != AudioServicesError.None)
 
82
                                        throw new ArgumentException (res.ToString ());
 
83
 
 
84
                                return data == 1;
 
85
                        }
 
86
 
 
87
                        set {
 
88
                                uint data = value ? (uint)1 : 0;
 
89
 
 
90
                                var res = AudioServices.AudioServicesSetProperty (AudioServicesPropertyKey.IsUISound, sizeof (AudioServicesPropertyKey), ref soundId, sizeof (uint), ref data);
 
91
                                if (res != AudioServicesError.None)
 
92
                                        throw new ArgumentException (res.ToString ());
 
93
                        }
 
94
                }
 
95
 
 
96
                public bool CompletePlaybackIfAppDies {
 
97
                        get {
 
98
                                uint out_size = sizeof (uint);
 
99
                                uint data;
 
100
 
 
101
                                var res = AudioServices.AudioServicesGetProperty (AudioServicesPropertyKey.CompletePlaybackIfAppDies, sizeof (AudioServicesPropertyKey), ref soundId, out out_size, out data);
 
102
                                if (res != AudioServicesError.None)
 
103
                                        throw new ArgumentException (res.ToString ());
 
104
 
 
105
                                return data == 1;
 
106
                        }
 
107
 
 
108
                        set {
 
109
                                uint data = value ? (uint)1 : 0;
 
110
 
 
111
                                var res = AudioServices.AudioServicesSetProperty (AudioServicesPropertyKey.CompletePlaybackIfAppDies, sizeof (AudioServicesPropertyKey), ref soundId, sizeof (uint), ref data);
 
112
                                if (res != AudioServicesError.None)
 
113
                                        throw new ArgumentException (res.ToString ());
 
114
                        }
 
115
                }
 
116
 
76
117
                void AssertNotDisposed ()
77
118
                {
78
119
                        if (soundId == 0)
91
132
                }
92
133
 
93
134
                [DllImport (Constants.AudioToolboxLibrary)]
94
 
                static extern AudioServiceErrors AudioServicesDisposeSystemSoundID (uint soundId);
 
135
                static extern AudioServicesError AudioServicesDisposeSystemSoundID (uint soundId);
95
136
 
96
137
                void Cleanup (bool checkForError)
97
138
                {
98
139
                        if (soundId == 0 || !ownsHandle)
99
140
                                return;
 
141
 
 
142
                        if (gc_handle.IsAllocated) {
 
143
                                gc_handle.Free ();
 
144
                        }
 
145
 
 
146
                        if (completionRoutine != null) {
 
147
                                RemoveSystemSoundCompletion ();
 
148
                        }
 
149
 
100
150
                        var error = AudioServicesDisposeSystemSoundID (soundId);
101
151
                        var oldId = soundId;
102
152
                        soundId = 0;
103
 
                        if (checkForError && error != AudioServiceErrors.None) {
 
153
                        if (checkForError && error != AudioServicesError.None) {
104
154
                                throw new InvalidOperationException (string.Format ("Error while disposing SystemSound with ID {0}: {1}",
105
155
                                                        oldId, error.ToString()));
106
156
                        }
128
178
                }
129
179
 
130
180
                [DllImport (Constants.AudioToolboxLibrary)]
131
 
                static extern AudioServiceErrors AudioServicesCreateSystemSoundID (IntPtr fileUrl, out uint soundId);
 
181
                static extern AudioServicesError AudioServicesCreateSystemSoundID (IntPtr fileUrl, out uint soundId);
132
182
 
133
183
                public SystemSound (NSUrl fileUrl)
134
184
                {
135
185
                        var error = AudioServicesCreateSystemSoundID (fileUrl.Handle, out soundId);
136
 
                        if (error != AudioServiceErrors.None)
 
186
                        if (error != AudioServicesError.None)
137
187
                                throw new InvalidOperationException (string.Format ("Could not create system sound ID for url {0}; error={1}",
138
188
                                                        fileUrl, error));
139
189
                        ownsHandle = true;
143
193
                {
144
194
                        uint soundId;
145
195
                        var error = AudioServicesCreateSystemSoundID (fileUrl.Handle, out soundId);
146
 
                        if (error != AudioServiceErrors.None)
 
196
                        if (error != AudioServicesError.None)
147
197
                                return null;
148
198
                        return new SystemSound (soundId, true);
149
199
                }
153
203
                        using (var url = new NSUrl (filename)){
154
204
                                uint soundId;
155
205
                                var error = AudioServicesCreateSystemSoundID (url.Handle, out soundId);
156
 
                                if (error != AudioServiceErrors.None)
 
206
                                if (error != AudioServicesError.None)
157
207
                                        return null;
158
208
                                return new SystemSound (soundId, true);
159
209
                        }
160
210
                }
 
211
 
 
212
                [DllImport (Constants.AudioToolboxLibrary)]
 
213
                static extern AudioServicesError AudioServicesAddSystemSoundCompletion (uint soundId, IntPtr runLoop, IntPtr runLoopMode, Action<SystemSoundId, IntPtr> completionRoutine, IntPtr clientData);
 
214
 
 
215
                [MonoPInvokeCallback (typeof (Action<SystemSoundId, IntPtr>))]
 
216
                static void SoundCompletionShared (SystemSoundId id, IntPtr clientData)
 
217
                {
 
218
                        GCHandle gch = GCHandle.FromIntPtr (clientData);
 
219
                        var ss = (SystemSound) gch.Target;
 
220
 
 
221
                        ss.completionRoutine ();
 
222
                }
 
223
 
 
224
                public AudioServicesError AddSystemSoundCompletion (Action routine, CFRunLoop runLoop = null)
 
225
                {
 
226
                        if (gc_handle.IsAllocated)
 
227
                                throw new ArgumentException ("Only single completion routine is supported");
 
228
 
 
229
                        gc_handle = GCHandle.Alloc (this);
 
230
                        completionRoutine = routine;
 
231
 
 
232
                        return AudioServicesAddSystemSoundCompletion (soundId,
 
233
                                                                      runLoop == null ? IntPtr.Zero : runLoop.Handle,
 
234
                                                                      IntPtr.Zero, // runLoopMode should be enum runLoopMode == null ? IntPtr.Zero : runLoopMode.Handle,
 
235
                                                                      SoundCompletionCallback, GCHandle.ToIntPtr (gc_handle));
 
236
                }
 
237
 
 
238
                [DllImport (Constants.AudioToolboxLibrary)]
 
239
                static extern void AudioServicesRemoveSystemSoundCompletion (uint soundId);
 
240
 
 
241
                public void RemoveSystemSoundCompletion ()
 
242
                {
 
243
                        completionRoutine = null;
 
244
                        AudioServicesRemoveSystemSoundCompletion (soundId);
 
245
                }
161
246
        }
162
247
}