掌機 - Miyoo Mini Plus - C/C++ - Set Volume



main.c

#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/time.h>
#include <syslog.h>
#include <linux/input.h>

#include "mi_ao.h"
#include "mi_sys.h"
#include "mi_common_datatype.h"

#define SND_DEV             "/dev/mi_ao"
#define MIN_RAW_VALUE       -60
#define MAX_RAW_VALUE       30
#define MI_AO_SETVOLUME     0x4008690b
#define MI_AO_GETVOLUME     0xc008690c
#define MI_AO_SETMUTE       0x4008690d

static int set_vol(int vol)
{
    int fd = -1;
    int pre_val = 0;
    int val[2] = { 0 };
    uint64_t buf[] = { sizeof(val), (uintptr_t)val };

    fd = open(SND_DEV, O_RDWR);
    if (fd < 0) {
        printf("failed to open \"%s\"\n", SND_DEV);
        return 0;
    }

    if (vol > MAX_RAW_VALUE) {
        vol = MAX_RAW_VALUE;
    }
    else if (vol < MIN_RAW_VALUE) {
        vol = MIN_RAW_VALUE;
    }

    ioctl(fd, MI_AO_GETVOLUME, buf);
    pre_val = val[1];

    val[1] = vol;
    ioctl(fd, MI_AO_SETVOLUME, buf);
    if ((pre_val <= MIN_RAW_VALUE) && (vol > MIN_RAW_VALUE)) {
        val[1] = 0;
        ioctl(fd, MI_AO_SETMUTE, buf);
    }
    else if ((pre_val > MIN_RAW_VALUE) && (vol <= MIN_RAW_VALUE)) {
        val[1] = 1;
        ioctl(fd, MI_AO_SETMUTE, buf);
    }
    close(fd);

    return vol;
}

int main(int argc, char *argv[])
{
    int fd = -1;
    int vol = -10;
    struct input_event ev = {0};
  
    fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
    if (fd < 0) {
        printf("failed to open event0\n");
        return -1;
    }

    set_vol(vol);
    while (read(fd, &ev, sizeof(struct input_event))) {
        if (ev.type == EV_KEY) {
            if ((ev.code == 115) && (ev.value == 1)) {
                vol += 3;
                set_vol(vol);
            }
            if ((ev.code == 114) && (ev.value == 1)) {
                vol -= 3;
                set_vol(vol);
            }
        }
    }
    close(fd);

    return 0;
}