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