http://www.perlmonks.org?node_id=1086228


in reply to How to do ioctl?

I have now tried to following which does what I want:
use Inline "C"; open my $dev, "<", "/dev/input/event14" or die $!; grab($dev->fileno, 1); __END__ __C__ #include <linux/input.h> void grab(int fd, int act) { int r = ioctl(fd, EVIOCGRAB, act); printf("grab: %d\n", r); }
But it seems silly to me to resort to C just to issue an ioctl-call.

Does anybody know how to do this in pure Perl?

Replies are listed 'Best First'.
Re^2: How to do ioctl?
by Anonymous Monk on May 16, 2014 at 02:08 UTC
    What number is EVIOCGRAB? What does Inline think it is, and what does input.ph think it is?
      Good point.

      I've tried the following:

      printf("ph: %u\n", EVIOCGRAB()); open my $dev, "<", "/dev/input/event7" or die $!; grab($dev->fileno, 1); __END__ __C__ #include <linux/input.h> void grab(int fd, int act) { printf("c: %u\n", EVIOCGRAB); int r = ioctl(fd, EVIOCGRAB, act); printf("grab: %d\n", r); }
      And sure enough it prints:

      ph: 1073759632
      c: 1074021776

      So for whatever reason there is a mismatch.

      If I don't use the .ph-files but simply do ioctl $dev, 1074021776, 1;it works.

      So it seems the problem lies with h2ph....

      The way I generated the phs was I went to /usr/include and from there I issued "h2ph -a -d <some output-dir> linux/input.h"

      Was that the wrong way to do it? And is it normal that I have to do that on my own (as I said I installed via perlbrew and there were no .phs generated then?

      But at least I know how to do it now...

        Good point.

        :) thats why its on every checklist :)

        Was that the wrong way to do it?

        Nope, but it doesn't compile the c header it merely parses so there are https://metacpan.org/pod/h2ph#BUGS ... its old :)

        And is it normal that I have to do that on my own (as I said I installed via perlbrew and there were no .phs generated then?

        Yes, absolutely, h2ph just doesn't happen when building perl ... IIRC it didn't even as far back as 5.6.1

        The alternatives seem to be h2ph h2xs ExtUtils::Constant C::Scan::Constants ExtUtils::H2PM

        I like the ones that use a compiler to get the real number ...