drums3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD - The Framework for UserSpace Devices - Example program
  32. *
  33. * Jeremy Elson <jelson@circlemud.org>
  34. *
  35. * drums3.c: This example shows how to wait for both FUSD and non-FUSD
  36. * events at the same time. Instead of using fusd_run, we keep
  37. * control of main() by using our own select loop.
  38. *
  39. * Like the original drums.c, this example creates a bunch of devices
  40. * in the /dev/drums directory: /dev/drums/bam, /dev/drums/bum, etc.
  41. * However, it also prints a prompt to the console, asking the user if
  42. * how loud the drums should be.
  43. *
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <ctype.h>
  49. #include <errno.h>
  50. #include <unistd.h>
  51. #include "fusd.h"
  52. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  53. static char *drums_strings[] = {"bam", "bum", "beat", "boom",
  54. "bang", "crash", NULL};
  55. int volume = 2; /* default volume is 2 */
  56. ssize_t drums_read(struct fusd_file_info *file, char *user_buffer,
  57. size_t user_length, loff_t *offset)
  58. {
  59. int len;
  60. char sound[128], *c;
  61. /* 1st read returns the sound; 2nd returns EOF */
  62. if (*offset != 0)
  63. return 0;
  64. if (volume == 1)
  65. strcpy(sound, "(you hear nothing)");
  66. else {
  67. strcpy(sound, (char *) file->device_info);
  68. if (volume >= 3)
  69. for (c = sound; *c; c++)
  70. *c = toupper(*c);
  71. if (volume >= 4)
  72. strcat(sound, "!!!!!");
  73. }
  74. strcat(sound, "\n");
  75. /* NEVER return more data than the user asked for */
  76. len = MIN(user_length, strlen(sound));
  77. memcpy(user_buffer, sound, len);
  78. *offset += len;
  79. return len;
  80. }
  81. int do_open_or_close(struct fusd_file_info *file)
  82. {
  83. return 0; /* opens and closes always succeed */
  84. }
  85. struct fusd_file_operations drums_fops = {
  86. open: do_open_or_close,
  87. read: drums_read,
  88. close: do_open_or_close
  89. };
  90. void read_volume(int fd)
  91. {
  92. char buf[100];
  93. int new_vol = 0, retval;
  94. if (fd < 0)
  95. goto prompt;
  96. retval = read(fd, buf, sizeof(buf)-1);
  97. if (retval >= 0) {
  98. buf[retval] = '\0';
  99. if (*buf == 'q') {
  100. printf("Goodbye...\n");
  101. exit(0);
  102. }
  103. new_vol = atoi(buf);
  104. }
  105. if (new_vol >= 1 && new_vol <= 4) {
  106. volume = new_vol;
  107. printf("Volume changed to %d\n", volume);
  108. } else {
  109. printf("Invalid volume!\n");
  110. }
  111. prompt:
  112. printf("\nHow loud would you like the /dev/drums?\n");
  113. printf(" 1 - Inaudible\n");
  114. printf(" 2 - Quiet\n");
  115. printf(" 3 - Loud\n");
  116. printf(" 4 - Permanent ear damage!\n");
  117. printf(" q - Exit program\n");
  118. printf("Your choice? ");
  119. fflush(stdout);
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. int i;
  124. char buf[128];
  125. char devname[128];
  126. fd_set fds, tmp;
  127. int max;
  128. for (i = 0; drums_strings[i] != NULL; i++) {
  129. sprintf(buf, "/dev/drums/%s", drums_strings[i]);
  130. sprintf(devname, "drum%s", drums_strings[i]);
  131. if (fusd_register(buf, "drums", devname, 0666, drums_strings[i], &drums_fops) < 0)
  132. fprintf(stderr, "%s register failed: %m\n", drums_strings[i]);
  133. }
  134. /* print the initial prompt to the user */
  135. read_volume(-1);
  136. /* EXAMPLE START drums3.c */
  137. /* initialize the set */
  138. FD_ZERO(&fds);
  139. /* add stdin to the set */
  140. FD_SET(STDIN_FILENO, &fds);
  141. max = STDIN_FILENO;
  142. /* add all FUSD fds to the set */
  143. fusd_fdset_add(&fds, &max);
  144. while (1) {
  145. tmp = fds;
  146. if (select(max+1, &tmp, NULL, NULL, NULL) < 0)
  147. perror("selecting");
  148. else {
  149. /* if stdin is readable, read the user's response */
  150. if (FD_ISSET(STDIN_FILENO, &tmp))
  151. read_volume(STDIN_FILENO);
  152. /* call any FUSD callbacks that have messages waiting */
  153. fusd_dispatch_fdset(&tmp);
  154. }
  155. }
  156. /* EXAMPLE STOP drums3.c */
  157. }