File: findpowermate.cpp

package info (click to toggle)
marote 2.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 556 kB
  • ctags: 224
  • sloc: cpp: 1,925; makefile: 46
file content (59 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <linux/input.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "findpowermate.h"

#define NUM_VALID_PREFIXES 2
#define NUM_EVENT_DEVICES 16

using namespace std;

findpowermate::findpowermate()
{ 
}

findpowermate::~findpowermate()
{
}

int findpowermate::open_powermate(const char *dev, int mode)
{
    const char* valid_prefix[NUM_VALID_PREFIXES] = {"Griffin PowerMate","Griffin SoundKnob"};
    fdf = open(dev, mode);
    if(fdf < 0){
 fprintf(stderr, "Unable to open \"%s\": %s\n", dev, strerror(errno));
 return -1;
  }

  if(ioctl(fdf, EVIOCGNAME(sizeof(name)), name) < 0){
    fprintf(stderr, "\"%s\": EVIOCGNAME failed: %s\n", dev, strerror(errno));
    close(fdf);
    return -1;
  }

  // it's the correct device if the prefix matches what we expect it to be:
  for(i=0; i<NUM_VALID_PREFIXES; i++)
    if(!strncasecmp(name, valid_prefix[i], strlen(valid_prefix[i])))
      return fdf;

  close(fdf);
  return -1;
}

int findpowermate::find_powermate(int mode)
{
  char devname[256];
  int i, r;

  for(i=0; i<NUM_EVENT_DEVICES; i++){
    sprintf(devname, "/dev/input/event%d", i);    
    r = open_powermate(devname, mode);   
    if(r >= 0)
      return r; // r is the file descriptor
  }

  return -1;
}