khronosfws/openmax_al/src/vibra/xavibraitf.c
changeset 12 5a06f39ad45b
child 16 43d09473c595
equal deleted inserted replaced
0:71ca22bcf22a 12:5a06f39ad45b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <stdio.h>
       
    19 #include <stdlib.h>
       
    20 #include <assert.h>
       
    21 #include "xavibraitf.h"
       
    22 #ifdef _GSTREAMER_BACKEND_
       
    23 #include "XAVibraItfAdaptation.h"
       
    24 #endif
       
    25 #include "xathreadsafety.h"
       
    26 /* XAVibraItfImpl* GetImpl(XAVibraItf self)
       
    27  * Description: Validate interface pointer and cast it to implementation pointer.
       
    28 */
       
    29 static XAVibraItfImpl* GetImpl(XAVibraItf self)
       
    30 {
       
    31     if( self )
       
    32     {
       
    33         XAVibraItfImpl* impl = (XAVibraItfImpl*)(*self);
       
    34         if( impl && (impl == impl->self) )
       
    35         {
       
    36             return impl;
       
    37         }
       
    38     }
       
    39     return NULL;
       
    40 }
       
    41 
       
    42 /*****************************************************************************
       
    43  * Base interface XAVibraItf implementation
       
    44  *****************************************************************************/
       
    45 /*
       
    46  * XAresult XAVibraItfImpl_Vibrate ( XAVibraItf self, XAboolean vibrate )
       
    47  * Description: Activates or deactivates vibration for the I/O device.
       
    48  */
       
    49 XAresult XAVibraItfImpl_Vibrate ( XAVibraItf self,
       
    50                                   XAboolean vibrate )
       
    51 {
       
    52     XAresult ret = XA_RESULT_SUCCESS;
       
    53     XAVibraItfImpl* impl = GetImpl(self);
       
    54     DEBUG_API("->XAVibraItfImpl_Vibrate");
       
    55     XA_IMPL_THREAD_SAFETY_ENTRY(XATSVibra);
       
    56 
       
    57     if( !impl )
       
    58     {
       
    59         XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
    60         /* invalid parameter */
       
    61         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
    62         DEBUG_API("<-XAVibraItfImpl_Vibrate");
       
    63         return XA_RESULT_PARAMETER_INVALID;
       
    64     }
       
    65 
       
    66     /* check is vibrate mode changed */
       
    67     if( impl->vibrate != vibrate )
       
    68     {
       
    69 #ifdef _GSTREAMER_BACKEND_
       
    70         ret = XAVibraItfAdapt_Vibrate( impl->adapCtx, vibrate );
       
    71 #endif
       
    72         if ( ret == XA_RESULT_SUCCESS )
       
    73         {
       
    74             impl->vibrate = vibrate;
       
    75         }
       
    76     }
       
    77 
       
    78     XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
    79     DEBUG_API("<-XAVibraItfImpl_Vibrate");
       
    80     return ret;
       
    81 }
       
    82 /*
       
    83  * XAresult XAVibraItfImpl_IsVibrating ( XAVibraItf self, XAboolean * pVibrating )
       
    84  * Description: Returns whether the I/O device is vibrating.
       
    85  */
       
    86 XAresult XAVibraItfImpl_IsVibrating ( XAVibraItf self, XAboolean * pVibrating )
       
    87 {
       
    88     XAresult ret = XA_RESULT_SUCCESS;
       
    89     XAVibraItfImpl* impl = GetImpl(self);
       
    90     DEBUG_API("->XAVibraItfImpl_IsVibrating");
       
    91 
       
    92     if( !impl || !pVibrating)
       
    93     {
       
    94         /* invalid parameter */
       
    95         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
    96         DEBUG_API("<-XAVibraItfImpl_IsVibrating");
       
    97         return XA_RESULT_PARAMETER_INVALID;
       
    98     }
       
    99 
       
   100     *pVibrating = impl->vibrate;
       
   101 
       
   102     if( *pVibrating )
       
   103     {
       
   104         DEBUG_API("It's vibrating");
       
   105     }
       
   106     else
       
   107     {
       
   108         DEBUG_API("Vibrate is off.");
       
   109     }
       
   110 
       
   111     DEBUG_API("<-XAVibraItfImpl_IsVibrating");
       
   112     return ret;
       
   113 }
       
   114 /*
       
   115  * XAresult XAVibraItfImpl_SetFrequency ( XAVibraItf self,  XAmilliHertz frequency )
       
   116  * Description: Sets the vibration frequency of the I/O device.
       
   117  */
       
   118 XAresult XAVibraItfImpl_SetFrequency ( XAVibraItf self, XAmilliHertz frequency )
       
   119 {
       
   120     XAresult ret = XA_RESULT_SUCCESS;
       
   121     XAVibraItfImpl* impl = GetImpl(self);
       
   122 
       
   123     DEBUG_API_A1("->XAVibraItfImpl_SetFrequency %ld", frequency);
       
   124     XA_IMPL_THREAD_SAFETY_ENTRY(XATSVibra);
       
   125 
       
   126     if( !impl || frequency < MIN_FREQUENCY || frequency > MAX_FREQUENCY)
       
   127     {
       
   128         XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
   129         /* invalid parameter */
       
   130         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   131         DEBUG_API("<-XAVibraItfImpl_SetFrequency");
       
   132         return XA_RESULT_PARAMETER_INVALID;
       
   133     }
       
   134 
       
   135     /* check is vibration frequency changed */
       
   136     if( impl->frequency != frequency )
       
   137     {
       
   138 #ifdef _GSTREAMER_BACKEND_
       
   139         ret = XAVibraItfAdapt_SetFrequency( impl->adapCtx, frequency );
       
   140 #endif
       
   141         if( ret == XA_RESULT_SUCCESS )
       
   142         {
       
   143             impl->frequency = frequency;
       
   144         }
       
   145     }
       
   146 
       
   147     XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
   148     DEBUG_API("<-XAVibraItfImpl_SetFrequency");
       
   149     return ret;
       
   150 }
       
   151 /*
       
   152  * XAresult XAVibraItfImpl_GetFrequency ( XAVibraItf self,  XAmilliHertz * pFrequency )
       
   153  * Description: Returns the vibration frequency of the I/O device.
       
   154  */
       
   155 XAresult XAVibraItfImpl_GetFrequency ( XAVibraItf self, XAmilliHertz * pFrequency )
       
   156 {
       
   157     XAresult ret = XA_RESULT_SUCCESS;
       
   158     XAVibraItfImpl* impl = GetImpl(self);
       
   159     DEBUG_API("->XAVibraItfImpl_GetFrequency");
       
   160     if( !impl || !pFrequency)
       
   161     {
       
   162         /* invalid parameter */
       
   163         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   164         DEBUG_API("<-XAVibraItfImpl_GetFrequency");
       
   165         return XA_RESULT_PARAMETER_INVALID;
       
   166     }
       
   167 
       
   168     *pFrequency = impl->frequency;
       
   169 
       
   170     DEBUG_API_A1("Frequency is %ld", *pFrequency);
       
   171     DEBUG_API("<-XAVibraItfImpl_GetFrequency");
       
   172     return ret;
       
   173 }
       
   174 /*
       
   175  * XAresult XAVibraItfImpl_SetIntensity ( XAVibraItf self,  XApermille intensity )
       
   176  * Description: Sets the vibration intensity of the Vibra I/O device.
       
   177  */
       
   178 XAresult XAVibraItfImpl_SetIntensity ( XAVibraItf self, XApermille intensity )
       
   179 {
       
   180     XAresult ret = XA_RESULT_SUCCESS;
       
   181     XAVibraItfImpl* impl = GetImpl(self);
       
   182     DEBUG_API_A1("->XAVibraItfImpl_SetIntensity %d", intensity);
       
   183     XA_IMPL_THREAD_SAFETY_ENTRY(XATSVibra);
       
   184 
       
   185     if( !impl || intensity < MIN_INTENSITY || intensity > MAX_INTENSITY )
       
   186     {
       
   187         /* invalid parameter */
       
   188         XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
   189         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   190         DEBUG_API("<-XAVibraItfImpl_SetIntensity");
       
   191         return XA_RESULT_PARAMETER_INVALID;
       
   192     }
       
   193 
       
   194     /* check is vibration intensity changed */
       
   195     if( impl->intensity != intensity )
       
   196     {
       
   197 #ifdef _GSTREAMER_BACKEND_
       
   198         ret = XAVibraItfAdapt_SetIntensity( impl->adapCtx, intensity );
       
   199 #endif
       
   200         if( ret == XA_RESULT_SUCCESS )
       
   201         {
       
   202             impl->intensity = intensity;
       
   203         }
       
   204     }
       
   205 
       
   206     XA_IMPL_THREAD_SAFETY_EXIT(XATSVibra);
       
   207     DEBUG_API("<-XAVibraItfImpl_SetIntensity");
       
   208     return ret;
       
   209 }
       
   210 /*
       
   211  * XAresult XAVibraItfImpl_GetIntensity ( XAVibraItf self, XApermille * pIntensity )
       
   212  * Description: Returns the vibration intensity of the Vibra I/O device.
       
   213  */
       
   214 XAresult XAVibraItfImpl_GetIntensity ( XAVibraItf self, XApermille * pIntensity )
       
   215 {
       
   216     XAresult ret = XA_RESULT_SUCCESS;
       
   217     XAVibraItfImpl* impl = GetImpl(self);
       
   218     DEBUG_API("->XAVibraItfImpl_GetIntensity");
       
   219 
       
   220     if( !impl || !pIntensity)
       
   221     {
       
   222         /* invalid parameter */
       
   223         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
   224         DEBUG_API("<-XAVibraItfImpl_GetIntensity");
       
   225         return XA_RESULT_PARAMETER_INVALID;
       
   226     }
       
   227 
       
   228     *pIntensity = impl->intensity;
       
   229 
       
   230     DEBUG_API_A1("Intensity is %d", *pIntensity);
       
   231     DEBUG_API("<-XAVibraItfImpl_GetIntensity");
       
   232     return ret;
       
   233 }
       
   234 
       
   235 #ifdef _GSTREAMER_BACKEND_
       
   236 
       
   237 /*****************************************************************************
       
   238  * XAVibraItfImpl -specific methods
       
   239  *****************************************************************************/
       
   240 /* XAVibraItfImpl* XAVibraItfImpl_Create()
       
   241  * Description: Allocate and initialize VibraItfImpl
       
   242  */
       
   243 XAVibraItfImpl* XAVibraItfImpl_Create( XAAdaptationBaseCtx *adapCtx )
       
   244 {
       
   245     XAVibraItfImpl* self = (XAVibraItfImpl*) calloc(1,sizeof(XAVibraItfImpl));
       
   246     DEBUG_API("->XAVibraItfImpl_Create");
       
   247     if( self )
       
   248     {
       
   249         /* init itf default implementation*/
       
   250         self->itf.Vibrate = XAVibraItfImpl_Vibrate;
       
   251         self->itf.IsVibrating = XAVibraItfImpl_IsVibrating;
       
   252         self->itf.SetFrequency = XAVibraItfImpl_SetFrequency;
       
   253         self->itf.GetFrequency = XAVibraItfImpl_GetFrequency;
       
   254         self->itf.SetIntensity = XAVibraItfImpl_SetIntensity;
       
   255         self->itf.GetIntensity = XAVibraItfImpl_GetIntensity;
       
   256 
       
   257         /* init variables*/
       
   258         self->vibrate = XA_BOOLEAN_FALSE;
       
   259         self->frequency = 0;
       
   260         self->intensity = 0;
       
   261         self->adapCtx = adapCtx;
       
   262 
       
   263         self->self = self;
       
   264     }
       
   265 
       
   266     DEBUG_API("<-XAVibraItfImpl_Create");
       
   267     return self;
       
   268 }
       
   269 #endif
       
   270 /* void XAVibraItfImpl_Free(XAVibraItfImpl* self)
       
   271  * Description: Free all resources reserved at XAVibraItfImpl_Create
       
   272  */
       
   273 void XAVibraItfImpl_Free(XAVibraItfImpl* self)
       
   274 {
       
   275     DEBUG_API("->XAVibraItfImpl_Free");
       
   276     assert( self==self->self );
       
   277     free( self );
       
   278     DEBUG_API("<-XAVibraItfImpl_Free");
       
   279 }
       
   280