Detect headphone or microphone connected via 3.5mm jack

I wrote a little program that tells you when any headphones or microphone are connected. I provide the file path for identification, is there a way to find out through the C program which is the file path for the microphone or headphones?

Here's the program:

#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

int main()
{
    int fd = -1;
    char name[256]= {'\0'};
    struct input_event event;

    /*On my system 
        /dev/input/event6 for headphone
        /dev/input/event5 for mic
    */
    if ((fd = open("/dev/input/event6", O_RDONLY)) < 0) {
        perror("evdev open");
        exit(1);
    }


    if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
        perror("evdev ioctl");
    }

    printf("The device name on path %s is ===  %s\n Now Plug in or Plug out the device\n",
             argv[1],name);


//    while(1)
    {

        read(fd, &event, sizeof(struct input_event));
        printf("Event type is %d\n", event.type);
        printf("Event code is %d\n", event.code);
        printf("Event value is %d\n", event.value);
    }
    close(fd);
    return 0;
}

      

Here I need to provide the path in the public domain, I want my program to determine the path to the microphone or headphones. Is there any way to do this? It would be great to help. thank,

+3


source to share





All Articles