consctl.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include "cons.h"
  5. /*
  6. * bind a pipe onto consctl and keep reading it to
  7. * get changes to console state.
  8. */
  9. Consstate*
  10. consctl(void)
  11. {
  12. int i, n, fd, tries;
  13. char buf[128];
  14. Consstate *x;
  15. char *field[10];
  16. x = segattach(0, "shared", 0, sizeof *x);
  17. if(x == (void*)-1)
  18. sysfatal("segattach: %r");
  19. /* a pipe to simulate consctl */
  20. if(bind("#|", "/mnt/cons/consctl", MBEFORE) < 0
  21. || bind("/mnt/cons/consctl/data1", "/dev/consctl", MREPL) < 0)
  22. sysfatal("bind consctl: %r");
  23. /* a pipe to simulate the /dev/cons */
  24. if(bind("#|", "/mnt/cons/cons", MREPL) < 0
  25. || bind("/mnt/cons/cons/data1", "/dev/cons", MREPL) < 0)
  26. sysfatal("bind cons: %r");
  27. switch(fork()){
  28. case -1:
  29. sysfatal("fork: %r");
  30. case 0:
  31. break;
  32. default:
  33. return x;
  34. }
  35. notify(0);
  36. for(tries = 0; tries < 100; tries++){
  37. x->raw = 0;
  38. x->hold = 0;
  39. fd = open("/mnt/cons/consctl/data", OREAD);
  40. if(fd < 0)
  41. break;
  42. tries = 0;
  43. for(;;){
  44. n = read(fd, buf, sizeof(buf)-1);
  45. if(n <= 0)
  46. break;
  47. buf[n] = 0;
  48. n = getfields(buf, field, 10, 1, " ");
  49. for(i = 0; i < n; i++){
  50. if(strcmp(field[i], "rawon") == 0)
  51. x->raw = 1;
  52. else if(strcmp(field[i], "rawoff") == 0)
  53. x->raw = 0;
  54. else if(strcmp(field[i], "holdon") == 0)
  55. x->hold = 1;
  56. else if(strcmp(field[i], "holdoff") == 0)
  57. x->hold = 0;
  58. }
  59. }
  60. close(fd);
  61. }
  62. exits(0);
  63. return 0; /* dummy to keep compiler quiet*/
  64. }