[Device] Implementation of system.device module including error handling.
[platform/core/csapi/system.git] / Tizen.System / Device / Haptic.cs
1 using System;
2 using System.Collections.Generic;
3
4 namespace Tizen.System
5 {
6     /// <summary>
7     /// The Haptic class provides properties and methods to control a vibrator.
8     /// </summary>
9     public class Vibrator : IDisposable
10     {
11         private readonly int _vibratorId;
12         private static readonly Dictionary<int, Vibrator> s_vibrators = new Dictionary<int, Vibrator>();
13         private IntPtr _hapticHandle;
14         private bool _disposedValue = false;
15         private Vibrator(int id)
16         {
17             _vibratorId = id;
18             DeviceError res = (DeviceError) Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
19             if (res != DeviceError.None)
20             {
21                 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
22             }
23         }
24
25         ~Vibrator()
26         {
27             Dispose(false);
28         }
29
30         /// <summary>
31         /// Get the Vibrator instance for the given vibrator index.
32         /// </summary>
33         /// <param name="deviceId"> The index of the vibrator.
34         /// It can be greater than or equal to 0 and less than the number of vibrators. </param>
35         public static Vibrator GetVibrator(int deviceNumber)
36         {
37             if (deviceNumber < 0 || deviceNumber >= NumberOfVibrators)
38             {
39                 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid vibrator number");
40             }
41             if (!s_vibrators.ContainsKey(deviceNumber))
42             {
43                 s_vibrators.Add(deviceNumber, new Vibrator(deviceNumber));
44             }
45             return s_vibrators[deviceNumber];
46         }
47         /// <summary>
48         /// Gets the number of vibrators.
49         /// </summary>
50         public static int NumberOfVibrators
51         {
52             get
53             {
54                 int count = 0;
55                 DeviceError res = (DeviceError) Interop.Device.DeviceHapticGetCount(out count);
56                 if (res != DeviceError.None)
57                 {
58                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
59                 }
60                 return count;
61             }
62         }
63         /// <summary>
64         /// Vibrates during the specified time with a constant intensity.
65         /// This function can be used to start monotonous vibration for the specified time.
66         /// </summary>
67         /// <param name="duration">The play duration in milliseconds </param>
68         /// <param name="feedback">The amount of the intensity variation (0 ~ 100) </param>
69         public void Vibrate(int duration, int feedback)
70         {
71             IntPtr effect;
72             DeviceError res = DeviceError.None;
73             if (_hapticHandle == IntPtr.Zero)
74             {
75                 res = (DeviceError) Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
76                 if (res != DeviceError.None)
77                 {
78                     throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
79                 }
80             }
81
82             res = (DeviceError) Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
83             if (res != DeviceError.None)
84             {
85                 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
86             }
87         }
88         /// <summary>
89         /// Stops all vibration effects which are being played.
90         /// This function can be used to stop all effects started by Vibrate().
91         /// </summary>
92         public void Stop()
93         {
94             if (_hapticHandle != IntPtr.Zero)
95             {
96                 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
97                 if (res != DeviceError.None)
98                 {
99                     throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
100                 }
101             }
102         }
103         /// <summary>
104         /// Dispose API for closing the internal resources.
105         /// This function can be used to stop all effects started by Vibrate().
106         /// </summary>
107         public void Dispose()
108         {
109             Dispose(true);
110             GC.SuppressFinalize(this);
111         }
112
113         protected virtual void Dispose(bool disposing)
114         {
115             if (!_disposedValue)
116             {
117                 if (_hapticHandle != IntPtr.Zero)
118                 {
119                     Interop.Device.DeviceHapticClose(_hapticHandle);
120                     _hapticHandle = IntPtr.Zero;
121                 }
122                 _disposedValue = true;
123             }
124         }
125     }
126 }