]> git.deb.at Git - pkg/beep.git/blob - debian/patches/06_event-option
Imported Debian patch 1.2.2-20
[pkg/beep.git] / debian / patches / 06_event-option
1 Index: beep-1.2.2/beep.c
2 ===================================================================
3 --- beep-1.2.2.orig/beep.c
4 +++ beep-1.2.2/beep.c
5 @@ -26,6 +26,7 @@
6  #include <sys/ioctl.h>
7  #include <sys/types.h>
8  #include <linux/kd.h>
9 +#include <linux/input.h>
10  
11  /* I don't know where this number comes from, I admit that freely.  A 
12     wonderful human named Raine M. Ekman used it in a program that played
13 @@ -87,18 +88,48 @@ typedef struct beep_parms_t {
14    struct beep_parms_t *next;  /* in case -n/--new is used. */
15  } beep_parms_t;
16  
17 +enum { BEEP_TYPE_CONSOLE, BEEP_TYPE_EVDEV };
18 +
19  /* Momma taught me never to use globals, but we need something the signal 
20     handlers can get at.*/
21  int console_fd = -1;
22 +int console_type = BEEP_TYPE_CONSOLE;
23 +char *console_device = NULL;
24 +
25 +
26 +void do_beep(int freq) {
27 +  if (console_type == BEEP_TYPE_CONSOLE) {
28 +    if(ioctl(console_fd, KIOCSOUND, freq != 0
29 +      ? (int)(CLOCK_TICK_RATE/freq)
30 +      : freq) < 0) {
31 +      printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
32 +      perror("ioctl");
33 +    }
34 +  } else {
35 +     /* BEEP_TYPE_EVDEV */
36 +     struct input_event e;
37 +
38 +     e.type = EV_SND;
39 +     e.code = SND_TONE;
40 +     e.value = freq;
41 +
42 +     write(console_fd, &e, sizeof(struct input_event));
43 +  }
44 +}
45 +
46  
47  /* If we get interrupted, it would be nice to not leave the speaker beeping in
48     perpetuity. */
49  void handle_signal(int signum) {
50 +
51 +  if(console_device)
52 +    free(console_device);
53 +
54    switch(signum) {
55    case SIGINT:
56      if(console_fd >= 0) {
57        /* Kill the sound, quit gracefully */
58 -      ioctl(console_fd, KIOCSOUND, 0);
59 +      do_beep(0);
60        close(console_fd);
61        exit(signum);
62      } else {
63 @@ -111,7 +142,7 @@ void handle_signal(int signum) {
64  /* print usage and exit */
65  void usage_bail(const char *executable_name) {
66    printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
67 -        "[-D delay] [-s] [-c] [--verbose | --debug]\n",
68 +        "[-D delay] [-s] [-c] [--verbose | --debug] [-e device]\n",
69          executable_name);
70    printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
71    printf("%s [-h] [--help]\n", executable_name);
72 @@ -143,13 +174,14 @@ void usage_bail(const char *executable_n
73  void parse_command_line(int argc, char **argv, beep_parms_t *result) {
74    int c;
75  
76 -  struct option opt_list[6] = {{"help", 0, NULL, 'h'},
77 +  struct option opt_list[7] = {{"help", 0, NULL, 'h'},
78                                {"version", 0, NULL, 'V'},
79                                {"new", 0, NULL, 'n'},
80                                {"verbose", 0, NULL, 'X'},
81                                {"debug", 0, NULL, 'X'},
82 +                              {"device", 1, NULL, 'e'},
83                                {0,0,0,0}};
84 -  while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
85 +  while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL))
86         != EOF) {
87      int argval = -1;    /* handle parsed numbers for various arguments */
88      float argfreq = -1; 
89 @@ -220,6 +252,9 @@ void parse_command_line(int argc, char *
90      case 'X' : /* --debug / --verbose */
91        result->verbose = 1;
92        break;
93 +    case 'e' : /* also --device */
94 +      console_device = strdup(optarg);
95 +      break;
96      case 'h' : /* notice that this is also --help */
97      default :
98        usage_bail(argv[0]);
99 @@ -238,24 +273,31 @@ void play_beep(beep_parms_t parms) {
100         parms.reps, parms.length, parms.delay, parms.end_delay, parms.freq);
101  
102    /* try to snag the console */
103 -  if((console_fd = open("/dev/tty0", O_WRONLY)) == -1) {
104 -    if((console_fd = open("/dev/vc/0", O_WRONLY)) == -1) {
105 -      fprintf(stderr, "Could not open /dev/tty0 or /dev/vc/0 for writing.\n");
106 -      printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
107 -      perror("open");
108 -      exit(1);
109 -    }
110 +  if(console_device)
111 +    console_fd = open(console_device, O_WRONLY);
112 +  else
113 +    if((console_fd = open("/dev/tty0", O_WRONLY)) == -1)
114 +      console_fd = open("/dev/vc/0", O_WRONLY);
115 +
116 +  if(console_fd == -1) {
117 +    fprintf(stderr, "Could not open %s for writing\n",
118 +      console_device != NULL ? console_device : "/dev/tty0 or /dev/vc/0");
119 +    printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
120 +    perror("open");
121 +    exit(1);
122    }
123 +
124 +  if (ioctl(console_fd, EVIOCGSND(0)) != -1)
125 +    console_type = BEEP_TYPE_EVDEV;
126 +  else
127 +    console_type = BEEP_TYPE_CONSOLE;
128    
129    /* Beep */
130    for (i = 0; i < parms.reps; i++) {                    /* start beep */
131 -    if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
132 -      printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
133 -      perror("ioctl");
134 -    }
135 +    do_beep(parms.freq);
136      /* Look ma, I'm not ansi C compatible! */
137      usleep(1000*parms.length);                          /* wait...    */
138 -    ioctl(console_fd, KIOCSOUND, 0);                    /* stop beep  */
139 +    do_beep(0);                                         /* stop beep  */
140      if(parms.end_delay || (i+1 < parms.reps))
141         usleep(1000*parms.delay);                        /* wait...    */
142    }                                                     /* repeat.    */
143 @@ -318,5 +360,8 @@ int main(int argc, char **argv) {
144      parms = next;
145    }
146  
147 +  if(console_device)
148 +    free(console_device);
149 +
150    return EXIT_SUCCESS;
151  }