consctl.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include "cons.h"
  13. /*
  14. * bind a pipe onto consctl and keep reading it to
  15. * get changes to console state.
  16. */
  17. Consstate*
  18. consctl(void)
  19. {
  20. int i, n, fd, tries;
  21. char buf[128];
  22. Consstate *x;
  23. char *field[10];
  24. x = segattach(0, "shared", 0, sizeof *x);
  25. if(x == (void*)-1)
  26. sysfatal("segattach: %r");
  27. /* a pipe to simulate consctl */
  28. if(bind("#|", "/mnt/cons/consctl", MBEFORE) < 0
  29. || bind("/mnt/cons/consctl/data1", "/dev/consctl", MREPL) < 0)
  30. sysfatal("bind consctl: %r");
  31. /* a pipe to simulate the /dev/cons */
  32. if(bind("#|", "/mnt/cons/cons", MREPL) < 0
  33. || bind("/mnt/cons/cons/data1", "/dev/cons", MREPL) < 0)
  34. sysfatal("bind cons: %r");
  35. switch(fork()){
  36. case -1:
  37. sysfatal("fork: %r");
  38. case 0:
  39. break;
  40. default:
  41. return x;
  42. }
  43. notify(0);
  44. for(tries = 0; tries < 100; tries++){
  45. x->raw = 0;
  46. x->hold = 0;
  47. fd = open("/mnt/cons/consctl/data", OREAD);
  48. if(fd < 0)
  49. break;
  50. tries = 0;
  51. for(;;){
  52. n = read(fd, buf, sizeof(buf)-1);
  53. if(n <= 0)
  54. break;
  55. buf[n] = 0;
  56. n = getfields(buf, field, 10, 1, " ");
  57. for(i = 0; i < n; i++){
  58. if(strcmp(field[i], "rawon") == 0)
  59. x->raw = 1;
  60. else if(strcmp(field[i], "rawoff") == 0)
  61. x->raw = 0;
  62. else if(strcmp(field[i], "holdon") == 0)
  63. x->hold = 1;
  64. else if(strcmp(field[i], "holdoff") == 0)
  65. x->hold = 0;
  66. }
  67. }
  68. close(fd);
  69. }
  70. exits(0);
  71. return 0; /* dummy to keep compiler quiet*/
  72. }