consctl.c 1.5 KB

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