dslinux/user/pixil/libs/pixlib/platform/x86 backlight.c calibrate.c powermgmt.c sound_dsp.c sysinfo.c x86.c

amadeus dslinux_amadeus at user.in-berlin.de
Tue Oct 3 13:25:31 CEST 2006


Update of /cvsroot/dslinux/dslinux/user/pixil/libs/pixlib/platform/x86
In directory antilope:/tmp/cvs-serv11916/libs/pixlib/platform/x86

Added Files:
	backlight.c calibrate.c powermgmt.c sound_dsp.c sysinfo.c 
	x86.c 
Log Message:
adding pristine copy of pixil to HEAD so I can branch from it

--- NEW FILE: calibrate.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* Feature test switches */


/* System header files */
#include <string.h>

/* Local header files */
#include <pixlib/pixlib.h>

/* Typedef, macros, enum/struct/union defintions */


/* Global scope variables */


/* Local scope variables */


/* Static function prototypes */

/*-----------------------------------------------------------------------------*\
--
--	Static function definitions
--
\*-----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------*\
--
--	External function definitions
--
\*-----------------------------------------------------------------------------*/

/*******************************************************************************\
**
**	Function:	int pix_cal_GetDataPt()
**	Desc:		Gets the raw data (point data) from the device file and stores
**				it into the CalPt_t structure passed in
**	Accepts:	CalPt_t *ptdata = Ptr to storage of x/y coordinates
**	Returns:	int; 0 on successful read, -1 on error
**
\*******************************************************************************/
int
pix_cal_GetDataPt(CalPt_t * ptdata)
{
    int retval = PIXLIB_STUB_VAL;	/* Default to stubbed function */

    return (retval);
}				/* end of pix_cal_GetDataPt() */

/*******************************************************************************\
**
**	Function:	int pix_cal_Calibrate()
**	Desc:		ipaq calibration routine which calculates the calibration values
**				based on the control and user specific data points
**	Accepts:	int npts = Number of control/data points to process
**				CalPt_t *ctrldata = Array of npts control points
**				CalPt_t *userdata = Array of npts user points
**	Returns:	int; 0 on success, -1 on error, PIXLIB_STUB_VAL for a stubbed
**				function.
**
\*******************************************************************************/
int
pix_cal_Calibrate(int npts, CalPt_t * ctrldata, CalPt_t * userdata)
{
    int retval = PIXLIB_STUB_VAL;	/* Default to stubbed function */

    return (retval);
}				/* end of pix_cal_Calibrate() */

/*******************************************************************************\
**
**	Function:	int pix_cal_GetDrawPt()
**	Desc:		Gets the draw point (in case of any swapping/rotation that is
**				device specific)
**	Accepts:	CalPt_t *ctrldata = Ptr to the control points
**				CalPt_t *drawdata = Ptr to the draw points storage
**	Returns:	int; 0 on success, -1 on error, PIXLIB_STUB_VAL for stubbed fxn
**	
\*******************************************************************************/
int
pix_cal_GetDrawPt(CalPt_t * ctrldata, CalPt_t * drawdata)
{
    if (ctrldata == NULL || drawdata == NULL)
	return (-1);

    memcpy(drawdata, ctrldata, sizeof(CalPt_t));

    return (0);
}				/* end of pix_cal_GetDrawPt() */

/*******************************************************************************\
**
**	Function:	int pix_cal_GetCtrlPts()
**	Desc:		Creates the control point array and fills it in with the control
**				points for this platform
**	Acecpts:	int *npts = Ptr to the number of points
**				CalPt_t **ctrldata = Ptr to Ptr of array to be dynamically created
**				int w = screen width
**				int h = screen height
**				int bpp = bits per pixel
**	Returns:	int; 0 on success, -1 on error, PIXLIB_STUB_VAL for stubbed fxn
**
\*******************************************************************************/
int
pix_cal_GetCtrlPts(int *npts, CalPt_t ** ctrldata, int w, int h, int bpp)
{
    int retval = PIXLIB_STUB_VAL;

    return (retval);
}				/* end of pix_cal_GetCtrlPts() */

--- NEW FILE: x86.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* System header files */


/* Local header files */
#include <pixlib/pixlib.h>


/* Typedef, macros, enum/struct/union definitions */


/* Global scope variables */


/* Local scope variables */
static unsigned long f_capmask = 0;	/* No hardware specific is supported */

/* Static function prototypes */


/*******************************************************************************\
**
**	Static function definitions
**
\*******************************************************************************/


/*******************************************************************************\
**
**	Extern function definitions
**
\*******************************************************************************/

/*******************************************************************************\
**
**	Function:	int pix_PDSupprt()
**	Desc:		Determines if the given platform support the specified capabilites
**	Accepts:	int capability = Flag for a given capability
**	Returns:	int; 0 = not supported, non-zero for capabilties supported
**	
\*******************************************************************************/
int
pix_PDSupport(int capability)
{
    return (capability & f_capmask);
}				/* end of pix_PDSupport() */

--- NEW FILE: sound_dsp.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/soundcard.h>

#include <pixlib/pixlib.h>
#include <sound_dsp.h>

#define AUDIO_FILE "/dev/dsp"

static int pix_io2linux_values[PIXIO_MIXER_COUNT] = {
    SOUND_MIXER_VOLUME,
    SOUND_MIXER_BASS,
    SOUND_MIXER_TREBLE,
    SOUND_MIXER_MIC,
    SOUND_MIXER_LINE1,
    SOUND_MIXER_LINE2
};

static int
OpenAudioDevice(int direction)
{
    int omode;
    int fd;

    if (direction == PIXIO_PLAY) {
	printf("set mode wronly\n");
	omode = O_WRONLY;
    } else
	omode = O_RDONLY | O_NONBLOCK;

    fd = open(AUDIO_FILE, omode, 0);

    if (fd == -1)
	perror("OPEN_AUDIO");

    return (fd);
}

int
dspOpenStream(int direction, pix_io_audio_t * settings)
{
    int err = PIXIO_ERROR;
    int tmp, bsize;
    int fd;

    fd = OpenAudioDevice(direction);

    if (fd < 0)
	return (err);

    /* Sync the device */
    if (ioctl(fd, SNDCTL_DSP_SYNC, NULL) < 0) {
	perror("OPEN_STREAM (SYNC)");
	goto exit_dspOpenStream;
    }

    /* Set the sample size */
    tmp = settings->sample;

    ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &tmp);

    if (tmp != settings->sample) {
	/* Failure - Try setting it to 8 bits */
	tmp = 8;
	ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &tmp);
	if (tmp != 8) {
	    perror("OPEN_STREAM (SIZE)");
	    goto exit_dspOpenStream;
	}
    }

    /* Set the sample rate */
    printf("speed [%d]\n", settings->speed);
    if (ioctl(fd, SNDCTL_DSP_SPEED, &settings->speed) < 0) {
	perror("OPEN_STREAM (SPEED)");
	goto exit_dspOpenStream;
    }

    /* Finally, set the stereo setting */
    if (settings->stereo)
	if (ioctl(fd, SNDCTL_DSP_STEREO, &settings->stereo) < 0)
	    settings->stereo = 0;

    /* Last step, grab the block size and return it to the caller */
    /* Get the biggest block we can read */

    bsize = -1;
    ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &bsize);

    if (bsize < 4 || bsize > 65536) {
	if (bsize == -1) {
	    perror("OPEN_STREAM (BLOCKSIZE)");
	    goto exit_dspOpenStream;
	}
	bsize = 1024;
    }

    settings->blocksize = bsize;
    err = PIXIO_OK;
    return (fd);

  exit_dspOpenStream:
    if (fd >= 0)
	close(fd);

    return (err);
}

static int
open_mixer()
{
    int fd;

    fd = open("/dev/mixer", O_RDWR);

    if (fd == -1)
	perror("OPEN_MIXER");

    return (fd);
}

static unsigned long
read_mixer(int device)
{
    int mixer = 0;
    int level = -1;

    if ((mixer = open_mixer()) == -1)
	return (-1);

    if (ioctl(mixer, MIXER_READ(device), &level) == -1) {
	perror("MIXER_READ (IOCTL)");
	level = -1;
    }

    close(mixer);
    return (level);
}

static int
write_mixer(int device, unsigned int level)
{
    int err = -1;
    int mixer = 0;

    if ((mixer = open_mixer()) == -1)
	return (-1);

    if (ioctl(mixer, MIXER_WRITE(device), &level) == -1) {
	perror("MIXER_WRITE (IOCTL)");
	err = -1;
    } else
	err = 0;

    close(mixer);
    return (err);
}

int
mixer_set_level(int device, pix_io_level_t * level)
{
    int linux_device;
    unsigned int set_level;

    if (device > PIXIO_MIXER_COUNT)
	return (PIXIO_BAD_DEVICE);
    else
	linux_device = pix_io2linux_values[device];

    set_level = ((level->right * 0x7f / 100)) << 8;
    set_level |= (level->left * 0x7f / 100);


    if (write_mixer(linux_device, (unsigned int) set_level) == -1)
	return (PIXIO_ERROR);
    else
	return (PIXIO_OK);
}

int
mixer_get_level(int device, pix_io_level_t * level)
{
    int linux_device;
    int levelin;
    int l, r;

    if (device > PIXIO_MIXER_COUNT)
	return (PIXIO_BAD_DEVICE);
    else
	linux_device = pix_io2linux_values[device];

    if ((levelin = read_mixer(linux_device)) == -1)
	return (PIXIO_ERROR);

    l = levelin & 0x7f;
    r = (levelin >> 8) & 0x7f;

    level->left = l * 100 / 0x7f;
    level->right = r * 100 / 0x7f;

    return (PIXIO_OK);
}

--- NEW FILE: sysinfo.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* Feature test switches */


/* System header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Local header files */
#include <pixlib/pixlib.h>


/* Typedef, macro, enum/struct/union definitions */


/* Global scope variables */


/* File scope variables */


/* Static function prototypes */


/*******************************************************************************\
**
**	Static function definitions
**
\*******************************************************************************/

/*******************************************************************************\
**
**	External function definitions
**
\*******************************************************************************/

/*******************************************************************************\
**
**	Function:	int pix_sys_cpuinfo()
**	Desc:		Parses /proc/cpu info and organizes the data in a system independant
**				way
**	Accepts:	pixCpuInfo_t *pcpu = Ptr to the cpu info
**	Returns:	int; 0 on success, -1 on error
**
\*******************************************************************************/
int
pix_sys_cpuinfo(pixCpuInfo_t * pcpu)
{
    char buffer[512],		/* Buffer */
      model_name[80];
    float cpu_speed = 0.0;
    int rc = -1;		/* Return code */
    FILE *fp;			/* File pointer */

    /* Validate the incoming arguments */
    if (pcpu == NULL)
	return (rc);

    if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
	return (rc);

    /*
       ** Start parsing the file.  The only fields that are interesting are 
       ** "model name" and "cpu MHZ"
     */
    while (fgets(buffer, sizeof(buffer), fp)) {
	char *cp;
	int len = strlen(buffer);

	if (buffer[len - 1] == '\n')
	    buffer[--len] = '\0';

	/* Set up cp to point to the data */
	if ((cp = strchr(buffer, ':')) == NULL)
	    continue;
	if (!memcmp(buffer, "model name", 10)) {
	    cp += 2;		/* Skip the space */
	    strcpy(model_name, cp);
	    rc++;
	} /* end of if */
	else if (!memcmp(buffer, "cpu MHz", 7)) {
	    cp += 2;
	    cpu_speed = atof(cp);
	    rc++;
	}			/* end of if */
    }				/* end of while */

    if (rc > -1) {
	sprintf(pcpu->cpu, "%s %.0f MHz", model_name, cpu_speed);
	rc = 0;
    }
    /* end of if */
    return (rc);
}				/* end of pix_sys_cpuinfo() */

--- NEW FILE: backlight.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* System header files */
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


/* Local header files */
#include <pixlib/pixlib.h>

/* Typedef, macros, enum/struct/union definitions */


/* Global scope variables */


/* File scope variables */
unsigned char f_maxval = -1;	/* Maximum backlight value */


/* Static function prototypes */


/*******************************************************************************\
**
**	Static function definitions
**
\*******************************************************************************/


/*******************************************************************************\
**
**	Externally callable function definitions 
**
\*******************************************************************************/

/*******************************************************************************\
**
**	Function:	int pix_bl_ctrl()
**	Desc:		Controls the backlight settings on the iPAQ
**	Accepts:	int pwr = Power settings (0 = off, 1 = on)
**				int level = Brightness level (0 - f_maxval) will be clamped
**	Returns:	int; 0 on success, -1 on error (with errno being set)
**
\*******************************************************************************/
int
pix_bl_ctrl(int pwr, int level)
{
    int retval = 0;		/* Default to success */

    return (retval);
}				/* end of pix_bl_ctrl() */

/*******************************************************************************\
**
**	Function:	int pix_bl_getmxval()
**	Desc:		Returns the maximum backlite value supported by hardware for the
**				given platform (default is 255)
**	Accepts:	Nothing (void)
**	Returns:	int; >= 0 on success, -1 on error
**
\*******************************************************************************/
int
pix_bl_getmxval(void)
{
    return (f_maxval);
}				/* end of bl_getmxval() */

--- NEW FILE: powermgmt.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* Feature test switches */


/* System header files */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


/* Local header files */
#include <pixlib/pixlib.h>


/* Typedef, macros, enum/struct/union definitions */
typedef struct
{
    char ldrvr_v[4 + 1], bios_v[4 + 1], metric[4 + 1];
    unsigned char apmflg, linests, batsts, batflg;
    int batper, batunit;
}
apm_values_t;


/* Global scope variables */


/* File scope variables */


/* Static function prototypes */
static int parseApm(apm_values_t * apmval);


/*******************************************************************************\
**
**	Static function definitions
**
\*******************************************************************************/

/*******************************************************************************\
**
**	Function:	static int parseApm()
**	Desc:		Reads the /proc/apm file and fills in the apm_values_t struct
**	Accepts:	apm_values_t *apmval = Ptr to the struct to fill in
**	Returns:	int; 0 on success, -1 on error
**
\*******************************************************************************/
static int
parseApm(apm_values_t * apmval)
{
    int apmflg, batsts, batflg, linests, retval = -1;	/* Return value */
    char apmbuf[255];		/* Buffer */
    FILE *fin;			/* File ptr */

    if (apmval == NULL)
	return (retval);

    if ((fin = fopen("/proc/apm", "r")) == NULL)
	return (retval);

    /* Get the first line */
    if (fgets(apmbuf, sizeof(apmbuf), fin) != NULL) {
	sscanf(apmbuf, "%s %s %i %i %i %i %d%*c %d %s", apmval->ldrvr_v,
	       apmval->bios_v, &apmflg, &linests, &batsts, &batflg,
	       &apmval->batper, &apmval->batunit, apmval->metric);
	apmval->apmflg = (unsigned char) apmflg;
	apmval->linests = (unsigned char) linests;
	apmval->batsts = (unsigned char) batsts;
	apmval->batflg = (unsigned char) batflg;
	retval = 0;
    }
    /* end of if */
    fclose(fin);

    return (retval);
}				/* end of parseApm() */


/******************************************************************************\
**
**	Externally callable function definitions
**
\*******************************************************************************/

/*******************************************************************************\
**
**	Function:	int pix_pwr_getbat()
**	Desc:		Returns the current battery level from the device
**	Acecpts:	int flags = Flag indicating how to return the value;
**					PWR_BAT_PERCENT = return as a percent (int)
**					PWR_BAT_SECONDS = return as # of seconds remaining
**	Returns:	int; value as described (>= 0), -1 on unknown, PIXLIB_STUB_VAL
**					for stubbed functions
**
\*******************************************************************************/

int
pix_pwr_getbat(int flags)
{
    int retval = -1;		/* Return value */
    apm_values_t apmval;	/* Apm values from the device */

    int ret = parseApm(&apmval);
    if (ret == -1) return -1;  /* No APM available */   

    if (apmval.linests == 0x00) {
      if (flags == PWR_BAT_PERCENT) retval = apmval.batper;
    }
    else {
      if (!memcmp(apmval.metric, "min", 3))
	retval = apmval.batunit * 60;
      else if (!memcmp(apmval.metric, "sec", 3))
	retval = apmval.batunit;
    }	

    return retval;
}	

/*******************************************************************************\
**
**	Function:	int pix_pwr_isCharging()
**	Desc:		Attempts to determine if the device's battery is charging
**	Accepts:	Nothing (void)
**	Returns:	int; 1 for charging, 0 for not charging, -1 on unknown, PIXLIB_STUB_VAL
**					if stubbed
**
\*******************************************************************************/

int
pix_pwr_isCharging(void)
{
    int retval = 0;		/* Default to Not charging */
    apm_values_t apmval;	/* Apm values */

    int ret = parseApm(&apmval);
    if (ret == -1) return -1;  /* No APM available */   

    if (apmval.batflg != 0x80) {
      if (apmval.batsts == 0x03 || apmval.batflg == 0x03) 
	    retval = 1;
    }

    return retval;
} 

/*******************************************************************************\
**
**	Function:	int pix_pwr_OnBattery()
**	Desc:		Determines if the device is on batter power
**	Accepts:	Nothing (void)
**	Returns:	int; 1 for on battery, 0 on line power, -1 on unknown, PIXLIB_STUB_VAL
**					if stubbed.
**
\*******************************************************************************/
int
pix_pwr_onBattery(void)
{
    int retval = 0;		/* Default to unknown */
    apm_values_t apmval;	/* apm values */
    
    int ret = parseApm(&apmval); 
    if (ret == -1) return -1;

    if (apmval.linests == 0x00)
      retval = 1;
    else if (apmval.linests == 0xff)
      retval = -1;

    return (retval);
}	

/*******************************************************************************\
**
**	Function:	int pix_pwr_suspend()
**	Desc:	suspend the device	
**	Accepts:	Nothing (void)
**	Returns:	int; 0 on succes -1 on failure, PIXLIB_STUB_VAL
**					if stubbed.
**
\*******************************************************************************/
int
pix_pwr_suspend(void)
{
    int retval = -1;		/* Default to unknown */
    char *command = "/usr/bin/apm --suspend";

    printf("running [%s]\n", command);
    retval = system(command);
    if (127 == retval || 0 > retval) {
	retval = -1;
    } else
	retval = 0;
    return (retval);
}				/* end of pix_pwr_suspend() */




More information about the dslinux-commit mailing list