/**
 * Copyright (C) 2002, 2003 Doug Bell <drbell@users.sourceforge.net>
 *
 * Some mixer routines from mplayer, http://mplayer.sourceforge.net.
 * Copyright (C) 2000-2002. by A'rpi/ESP-team & others
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <sys/mman.h>
#include <string.h>
#include "utils.h"
#include "mixer.h"

static char *mixer_device = "/dev/mixer";
static int saved_volume = (50 << 8 & 0xFF00) | (50 & 0x00FF);
static int mixer_channel = SOUND_MIXER_LINE;
static int mixer_dev_mask = 1 << SOUND_MIXER_LINE;
static int muted = 0;
static int mutecount = 0;
static int fd = -1;

static int oss_get_volume( void )
{
    int v, cmd, devs;
    int curvol = 0;

    if( fd < 0 ) fd = open( mixer_device, O_RDONLY );
    if( fd != -1 ) {

        ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
        if( devs & mixer_dev_mask ) {
            cmd = MIXER_READ( mixer_channel );
        } else {
            return curvol;
        }

        ioctl( fd, cmd, &v );
        curvol = ( v & 0xFF00 ) >> 8;
    }

    return curvol;
}

static int oss_get_unmute_volume( void )
{
    if( muted ) {
        return saved_volume;
    } else {
        int v, cmd, devs;

        if( fd < 0 ) fd = open( mixer_device, O_RDONLY );
        if( fd != -1 ) {

            ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
            if( devs & mixer_dev_mask ) {
                cmd = MIXER_READ( mixer_channel );
            } else {
                return -1;
            }

            ioctl( fd, cmd, &v );
            return v;
        }
    }

    return -1;
}

static int oss_set_volume( int percentdiff )
{
    int v, cmd, devs, levelpercentage;

    levelpercentage = oss_get_volume();

    levelpercentage += percentdiff;
    if( levelpercentage > 100 ) levelpercentage = 100;
    if( levelpercentage < 0 ) levelpercentage = 0;

    if( fd < 0 ) fd = open( mixer_device, O_RDONLY );
    if( fd != -1 ) {
        ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
        if( devs & mixer_dev_mask ) {
            cmd = MIXER_WRITE( mixer_channel );
        } else {
            return 0;
        }

        v = ( levelpercentage << 8 ) | levelpercentage;
        ioctl( fd, cmd, &v );
        muted = 0;
        mutecount = 0;
        return v;
    }

    return 0;
}

static void oss_mute( int mute )
{
    int v, cmd, devs;

    /**
     * Make sure that if multiple users mute the card,
     * we only honour the last one.
     */
    if( !mute && mutecount ) mutecount--;
    if( mutecount ) return;

    if( fd < 0 ) fd = open( mixer_device, O_RDONLY );

    if( mute ) {
        mutecount++;
        if( fd != -1 ) {

            /* Save volume */
            ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
            if( devs & mixer_dev_mask ) {
                cmd = MIXER_READ( mixer_channel );
            } else {
                return;
            }

            ioctl( fd,cmd,&v );
            saved_volume = v;

            /* Now set volume to 0 */
            ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
            if( devs & mixer_dev_mask ) {
                cmd = MIXER_WRITE( mixer_channel );
            } else {
                return;
            }

            v = 0;
            ioctl( fd, cmd, &v );

            muted = 1;
            return;
        }
    } else {
        if( fd != -1 ) {
            ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
            if( devs & mixer_dev_mask ) {
                cmd = MIXER_WRITE( mixer_channel );
            } else {
                return;
            }

            v = saved_volume;
            ioctl( fd, cmd, &v );
            muted = 0;
            return;
        }
    }
}

static int oss_ismute( void )
{
    return muted;
}

static char *oss_core_devnames[] = SOUND_DEVICE_NAMES;

static int oss_set_device( const char *devname, int verbose )
{
    const char *channame;
    int found = 0;
    int i;

    mixer_device = strdup( devname );
    if( !mixer_device ) return -1;

    i = strcspn( mixer_device, ":" );
    if( i == strlen( mixer_device ) ) {
        channame = "line";
    } else {
        mixer_device[ i ] = 0;
        channame = mixer_device + i + 1;
    }
    if( !file_is_openable_for_read( mixer_device ) ) {
	if( verbose ) {
	    fprintf( stderr, "mixer: Can't open device %s, "
		     "mixer volume and mute unavailable.\n", mixer_device );
	}
        return -1;
    }

    mixer_channel = SOUND_MIXER_LINE;
    for( i = 0; i < SOUND_MIXER_NRDEVICES; i++ ) {
        if( !strcasecmp( channame, oss_core_devnames[ i ] ) ) {
            mixer_channel = i;
            found = 1;
            break;
        }
    }
    if( !found ) {
	if( verbose ) {
	    fprintf( stderr, "mixer: No such mixer channel '%s'.\n", channame );
	}
        return -1;
    }
    mixer_dev_mask = 1 << mixer_channel;
    return 0;
}

static void oss_set_state( int ismuted, int unmute_volume )
{
    /**
     * 1. we come back unmuted: Don't touch anything
     * 2. we don't have a saved volume: Don't touch anything
     * 3. we come back muted and we have a saved volume:
     *    - if tvtime muted it, unmute to old volume
     *    - if user did it, remember that we're muted and old volume
     */
    if( oss_get_volume() == 0 && unmute_volume > 0 ) {
        saved_volume = unmute_volume;
        muted = 1;

        if( !ismuted ) {
            oss_mute( 0 );
        }
    }
}

static void oss_close_device( void )
{
    if( fd >= 0 ) close( fd );
    saved_volume = (50 << 8 & 0xFF00) | (50 & 0x00FF);
    mixer_channel = SOUND_MIXER_LINE;
    mixer_dev_mask = 1 << SOUND_MIXER_LINE;
    muted = 0;
    mutecount = 0;
    fd = -1;
}

struct mixer oss_mixer = {
    .set_device = oss_set_device,
    .set_state = oss_set_state,
    .get_volume = oss_get_volume,
    .get_unmute_volume = oss_get_unmute_volume,
    .set_volume = oss_set_volume,
    .mute = oss_mute,
    .ismute = oss_ismute,
    .close_device = oss_close_device,
};
