consctl.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include "cons.h"
  5. /*
  6. * create a shared segment. Make is start 2 meg higher than the current
  7. * end of process memory.
  8. */
  9. static void*
  10. share(int len)
  11. {
  12. uchar *vastart;
  13. vastart = sbrk(0);
  14. vastart += 2*1024*1024;
  15. if(segattach(0, "shared", vastart, len) < 0)
  16. return 0;
  17. memset(vastart, 0, len);
  18. return vastart;
  19. }
  20. /*
  21. * bind a pipe onto consctl and keep reading it to
  22. * get changes to console state.
  23. */
  24. Consstate*
  25. consctl(void)
  26. {
  27. int i, n;
  28. int fd;
  29. int tries;
  30. char buf[128];
  31. Consstate *x;
  32. char *field[10];
  33. x = share(sizeof(Consstate));
  34. if(x == 0)
  35. return 0;
  36. /* a pipe to simulate consctl */
  37. if(bind("#|", "/mnt/cons/consctl", MBEFORE) < 0
  38. || bind("/mnt/cons/consctl/data1", "/dev/consctl", MREPL) < 0){
  39. fprint(2, "error simulating consctl\n");
  40. exits("/dev/consctl");
  41. }
  42. /* a pipe to simulate the /dev/cons */
  43. if(bind("#|", "/mnt/cons/cons", MREPL) < 0
  44. || bind("/mnt/cons/cons/data1", "/dev/cons", MREPL) < 0){
  45. fprint(2, "error simulating cons\n");
  46. exits("/dev/cons");
  47. }
  48. switch(fork()){
  49. case -1:
  50. return 0;
  51. case 0:
  52. break;
  53. default:
  54. return x;
  55. }
  56. notify(0);
  57. for(tries = 0; tries < 100; tries++){
  58. x->raw = 0;
  59. x->hold = 0;
  60. fd = open("/mnt/cons/consctl/data", OREAD);
  61. if(fd < 0)
  62. break;
  63. tries = 0;
  64. for(;;){
  65. n = read(fd, buf, sizeof(buf)-1);
  66. if(n <= 0)
  67. break;
  68. buf[n] = 0;
  69. n = getfields(buf, field, 10, 1, " ");
  70. for(i = 0; i < n; i++){
  71. if(strcmp(field[i], "rawon") == 0)
  72. x->raw = 1;
  73. else if(strcmp(field[i], "rawoff") == 0)
  74. x->raw = 0;
  75. else if(strcmp(field[i], "holdon") == 0)
  76. x->hold = 1;
  77. else if(strcmp(field[i], "holdoff") == 0)
  78. x->hold = 0;
  79. }
  80. }
  81. close(fd);
  82. }
  83. exits(0);
  84. return 0; /* dummy to keep compiler quiet*/
  85. }