consctl.c 1.8 KB

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