drums3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * $Id$
  45. */
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <ctype.h>
  50. #include <errno.h>
  51. #include <unistd.h>
  52. #include "fusd.h"
  53. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  54. static char *drums_strings[] = {"bam", "bum", "beat", "boom",
  55. "bang", "crash", NULL};
  56. int volume = 2; /* default volume is 2 */
  57. ssize_t drums_read(struct fusd_file_info *file, char *user_buffer,
  58. size_t user_length, loff_t *offset)
  59. {
  60. int len;
  61. char sound[128], *c;
  62. /* 1st read returns the sound; 2nd returns EOF */
  63. if (*offset != 0)
  64. return 0;
  65. if (volume == 1)
  66. strcpy(sound, "(you hear nothing)");
  67. else {
  68. strcpy(sound, (char *) file->device_info);
  69. if (volume >= 3)
  70. for (c = sound; *c; c++)
  71. *c = toupper(*c);
  72. if (volume >= 4)
  73. strcat(sound, "!!!!!");
  74. }
  75. strcat(sound, "\n");
  76. /* NEVER return more data than the user asked for */
  77. len = MIN(user_length, strlen(sound));
  78. memcpy(user_buffer, sound, len);
  79. *offset += len;
  80. return len;
  81. }
  82. int do_open_or_close(struct fusd_file_info *file)
  83. {
  84. return 0; /* opens and closes always succeed */
  85. }
  86. struct fusd_file_operations drums_fops = {
  87. open: do_open_or_close,
  88. read: drums_read,
  89. close: do_open_or_close
  90. };
  91. void read_volume(int fd)
  92. {
  93. char buf[100];
  94. int new_vol = 0, retval;
  95. if (fd < 0)
  96. goto prompt;
  97. retval = read(fd, buf, sizeof(buf)-1);
  98. if (retval >= 0) {
  99. buf[retval] = '\0';
  100. if (*buf == 'q') {
  101. printf("Goodbye...\n");
  102. exit(0);
  103. }
  104. new_vol = atoi(buf);
  105. }
  106. if (new_vol >= 1 && new_vol <= 4) {
  107. volume = new_vol;
  108. printf("Volume changed to %d\n", volume);
  109. } else {
  110. printf("Invalid volume!\n");
  111. }
  112. prompt:
  113. printf("\nHow loud would you like the /dev/drums?\n");
  114. printf(" 1 - Inaudible\n");
  115. printf(" 2 - Quiet\n");
  116. printf(" 3 - Loud\n");
  117. printf(" 4 - Permanent ear damage!\n");
  118. printf(" q - Exit program\n");
  119. printf("Your choice? ");
  120. fflush(stdout);
  121. }
  122. int main(int argc, char *argv[])
  123. {
  124. int i;
  125. char buf[128];
  126. char devname[128];
  127. fd_set fds, tmp;
  128. int max;
  129. for (i = 0; drums_strings[i] != NULL; i++) {
  130. sprintf(buf, "/dev/drums/%s", drums_strings[i]);
  131. sprintf(devname, "drum%s", drums_strings[i]);
  132. if (fusd_register(buf, "drums", devname, 0666, drums_strings[i], &drums_fops) < 0)
  133. fprintf(stderr, "%s register failed: %m\n", drums_strings[i]);
  134. }
  135. /* print the initial prompt to the user */
  136. read_volume(-1);
  137. /* EXAMPLE START drums3.c */
  138. /* initialize the set */
  139. FD_ZERO(&fds);
  140. /* add stdin to the set */
  141. FD_SET(STDIN_FILENO, &fds);
  142. max = STDIN_FILENO;
  143. /* add all FUSD fds to the set */
  144. fusd_fdset_add(&fds, &max);
  145. while (1) {
  146. tmp = fds;
  147. if (select(max+1, &tmp, NULL, NULL, NULL) < 0)
  148. perror("selecting");
  149. else {
  150. /* if stdin is readable, read the user's response */
  151. if (FD_ISSET(STDIN_FILENO, &tmp))
  152. read_volume(STDIN_FILENO);
  153. /* call any FUSD callbacks that have messages waiting */
  154. fusd_dispatch_fdset(&tmp);
  155. }
  156. }
  157. /* EXAMPLE STOP drums3.c */
  158. }