contentLister/src/click.c File Reference

content lister - eReader key/pen click handling More...

#include <config.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <glib.h>
#include <liberregxml/erregapi.h>
#include "contentListerLog.h"
#include "click.h"

Go to the source code of this file.

Defines

#define SNDCTL_POWER_OFF   _SIO('P', 99)
#define SNDCTL_POWER_ON   _SIO('P', 100)
#define MIXER   "/dev/mixer"

Functions

static void * click_thread (void *arg)
static void destroy_waveforms ()
static gboolean read_waveforms ()
static void enable_audio_device (gboolean bEnable)
static void set_volume (int volume)
gboolean click_init ()
void handle_sound_settings_changed (int volume)
static void play_sound (playSoundType_e sound)
void click_key ()
void click_key_discard ()
void click_pen ()

Variables

static int g_volume_settings = 0
static wavFile_t g_wav_files [playUndefined]
static pthread_mutex_t g_click_mutex
static playSoundType_e g_play_sound


Detailed Description

content lister - eReader key/pen click handling

Generate key/pen clicks on request

Definition in file click.c.


Define Documentation

#define MIXER   "/dev/mixer"

Definition at line 53 of file click.c.

#define SNDCTL_POWER_OFF   _SIO('P', 99)

Definition at line 42 of file click.c.

#define SNDCTL_POWER_ON   _SIO('P', 100)

Definition at line 43 of file click.c.


Function Documentation

gboolean click_init (  ) 

Definition at line 150 of file click.c.

00151 {
00152     int err;
00153     pthread_t click_tid;
00154     regUserSetting_t *theUserSetting = NULL;
00155     
00156     // Retrieve user setting from registry
00157     // Setup the mixer volumes according to the users wishes
00158     theUserSetting = erRegGetUserSetting();
00159     if (theUserSetting)
00160     {
00161         g_volume_settings = theUserSetting->volume;
00162         erRegFreeUserSetting (theUserSetting);
00163     }
00164     else
00165     {
00166         // default is on
00167         g_volume_settings = 75;
00168     }
00169 
00170     // Turn on/off the audio device according to the settings
00171     enable_audio_device(g_volume_settings>0 ? TRUE : FALSE);
00172 
00173     if (g_volume_settings > 0)
00174     {
00175         // The sound is on, set the volume
00176         set_volume(g_volume_settings);
00177     }
00178 
00179     // Second, read the waveforms
00180     if (!read_waveforms())
00181     {
00182         CL_ERRORPRINTF("Error reading waveforms.");
00183         return FALSE;
00184     }
00185 
00186     // Initialize mutex
00187     err = pthread_mutex_init (&g_click_mutex, NULL);
00188     if (err != 0)
00189     {
00190         CL_ERRORPRINTF ("Mutex init error [%d]", err);
00191         destroy_waveforms();
00192         return FALSE;
00193     }
00194 
00195     // start click-thread
00196     err = pthread_create (&click_tid, NULL, click_thread, NULL);
00197     if (err != 0)
00198     {
00199         CL_ERRORPRINTF ("start click-thread error [%d]", err);
00200         destroy_waveforms();
00201         return FALSE;
00202     }
00203 
00204     // get here, success
00205     return TRUE;
00206 }

Here is the call graph for this function:

void click_key (  ) 

play click key sound

Definition at line 245 of file click.c.

00246 {
00247     play_sound(playClickedKey);
00248 }

Here is the call graph for this function:

void click_key_discard (  ) 

play click key discarded sound

Definition at line 250 of file click.c.

00251 {
00252     play_sound(playClickedKeyDiscarded);
00253 }

Here is the call graph for this function:

void click_pen (  ) 

play click pen sound

Definition at line 255 of file click.c.

00256 {
00257     play_sound(playClickedPen);
00258 }

Here is the call graph for this function:

static void * click_thread ( void *  arg  )  [static]

Definition at line 260 of file click.c.

00261 {
00262     int fd;   
00263  
00264     pthread_mutex_lock (&g_click_mutex);
00265     while (1)
00266     {
00267         char* waveform = NULL;
00268         int size=0;
00269        
00270         // wait for mutex
00271         pthread_mutex_lock (&g_click_mutex);
00272 
00273         // click once
00274         waveform = g_wav_files[g_play_sound].waveform;
00275         size     = g_wav_files[g_play_sound].wav_file_size;
00276 
00277         if (waveform != NULL && size != 0)
00278         {
00279             fd = open (AUDIO_DEVICE, O_WRONLY);
00280             if (fd <= 0)
00281             {
00282                 CL_ERRORPRINTF("Open error [%d] device [%s]", errno, AUDIO_DEVICE);
00283             }
00284             else
00285             {
00286                 write (fd, waveform, size);
00287                 close (fd);
00288             }
00289         }    
00290     }
00291 }

static void destroy_waveforms (  )  [static]

Definition at line 73 of file click.c.

00074 {
00075     int i;
00076     for (i=0; i<playUndefined; i++)
00077     {
00078         if (g_wav_files[i].wav_file_size)
00079         {
00080             g_free(g_wav_files[i].waveform);
00081             g_wav_files[i].waveform = NULL;
00082             g_wav_files[i].wav_file_size = 0;
00083         }
00084     }
00085 }

static void enable_audio_device ( gboolean  bEnable  )  [static]

Definition at line 129 of file click.c.

00130 {
00131     int audio_fd = open(AUDIO_DEVICE, O_RDWR);
00132     g_assert(audio_fd >= 0);
00133     int result = ioctl(audio_fd, bEnable ? SNDCTL_POWER_ON : SNDCTL_POWER_OFF, 0);
00134     g_assert (result != -1);
00135     close(audio_fd);
00136 }

void handle_sound_settings_changed ( int  volume  ) 

Definition at line 208 of file click.c.

00209 {
00210     g_volume_settings = volume;
00211     
00212     // Turn on/off the audio device according to the settings
00213     enable_audio_device(g_volume_settings>0 ? TRUE : FALSE);
00214 
00215     if (g_volume_settings > 0)
00216     {
00217         // The sound is on, set the volume
00218         set_volume(g_volume_settings);
00219     }
00220 }

Here is the call graph for this function:

static void play_sound ( playSoundType_e  sound  )  [static]

Definition at line 223 of file click.c.

00224 {
00225     CL_LOGPRINTF("click: play %d sound",  g_play_sound);
00226 
00227     if (0 == g_volume_settings)
00228     {
00229         // The sound is disabled, do nothing
00230         return;
00231     }
00232 
00233     if ( (sound >= playClickedKey) && (sound < playUndefined) )
00234     {
00235         g_play_sound = sound;
00236     }
00237     else
00238     {
00239         g_play_sound = playClickedKey;
00240     }
00241    
00242     pthread_mutex_unlock (&g_click_mutex); 
00243 }

static gboolean read_waveforms (  )  [static]

Definition at line 88 of file click.c.

00089 {
00090     int i, n, fd;
00091     for (i=0; i<playUndefined; i++)
00092     {
00093         fd = open(g_wav_files[i].wav_file_path, O_RDONLY);
00094         if (fd <= 0)
00095         {
00096             CL_ERRORPRINTF ("Open error [%d] file [%s]", errno, g_wav_files[i].wav_file_path);
00097             goto Error;
00098         }
00099 
00100         g_wav_files[i].wav_file_size = lseek(fd, 0, SEEK_END);
00101         g_wav_files[i].waveform = g_new(char, g_wav_files[i].wav_file_size);
00102         if (NULL == g_wav_files[i].waveform)
00103         {
00104             CL_ERRORPRINTF("Error allocating memory for waveforms.");
00105             close(fd);
00106             goto Error;
00107         }
00108 
00109         lseek (fd, 0, SEEK_SET);
00110         n = read (fd, g_wav_files[i].waveform, g_wav_files[i].wav_file_size);
00111         if (n != g_wav_files[i].wav_file_size)
00112         {
00113             CL_ERRORPRINTF("Read error [%d] file [%s]", errno, g_wav_files[i].wav_file_path);
00114             close(fd);
00115             goto Error;
00116         }
00117 
00118         // Successfully read g_wav_files[i]
00119         close(fd);
00120     }
00121 
00122     return TRUE;
00123 
00124 Error:
00125     destroy_waveforms();
00126     return FALSE;
00127 }

Here is the call graph for this function:

static void set_volume ( int  volume  )  [static]

Definition at line 138 of file click.c.

00139 {
00140     int mixer_fd = open (MIXER, O_RDWR);
00141     g_assert (mixer_fd >= 0);
00142     volume = (volume << 8) | volume;   // both channels equally loud
00143     int result = ioctl (mixer_fd, SOUND_MIXER_WRITE_VOLUME, &volume);
00144     g_assert (result != -1);
00145     close (mixer_fd);
00146 }


Variable Documentation

pthread_mutex_t g_click_mutex [static]

Definition at line 65 of file click.c.

Definition at line 67 of file click.c.

int g_volume_settings = 0 [static]

Definition at line 56 of file click.c.

wavFile_t g_wav_files[playUndefined] [static]

Initial value:

 
{
    {FILENAME_KEY_CLICK, 0, NULL},
    {FILENAME_KEY_CLICK_DISCARDED, 0, NULL},
    {FILENAME_PEN_CLICK, 0, NULL}
}

Definition at line 58 of file click.c.


Generated on Sun Dec 14 17:13:06 2008 by  doxygen 1.5.6