fifo.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * File created by David Delassus.
  4. * For license, see COPYING.
  5. */
  6. #include <sys/stat.h> /* access */
  7. #include "wmfs.h"
  8. #include "util.h"
  9. #include "config.h"
  10. #include "fifo.h"
  11. #define READ_SIZE (32768)
  12. static void
  13. fifo_open(void)
  14. {
  15. if(W->fifo.fd)
  16. close(W->fifo.fd);
  17. if(!(W->fifo.fd = open(W->fifo.path, O_RDONLY | O_NDELAY, 0)))
  18. warnxl("Can't open FIFO: %s\n", strerror(errno));
  19. }
  20. void
  21. fifo_init(void)
  22. {
  23. xasprintf(&(W->fifo.path), "%s/wmfs-%s.fifo", P_tmpdir, DisplayString(W->dpy));
  24. /* Check if fifo already exists */
  25. if(access(W->fifo.path, F_OK) != -1)
  26. unlink(W->fifo.path);
  27. if(mkfifo(W->fifo.path, 0644) < 0)
  28. warnxl("Can't create FIFO: %s\n", strerror(errno));
  29. fifo_open();
  30. }
  31. static void
  32. fifo_parse(char *cmd)
  33. {
  34. void (*func)(Uicb);
  35. char *p, *arg = NULL;
  36. /* remove trailing newline */
  37. if((p = strchr(cmd, '\n')))
  38. *p = '\0';
  39. /* If an argument is present, delimit function string */
  40. if((p = strchr(cmd, ' ')))
  41. {
  42. *p = '\0';
  43. arg = p + 1;
  44. }
  45. /* call the UICB function, p + 1 is command or NULL */
  46. if((func = uicb_name_func(cmd)))
  47. func(arg);
  48. XSync(W->dpy, false);
  49. }
  50. void
  51. fifo_read(void)
  52. {
  53. char buf[READ_SIZE] = { 0 };
  54. int ret;
  55. if((ret = read(W->fifo.fd, buf, sizeof(buf) - 1)) > 0)
  56. fifo_parse(buf);
  57. else if(!ret)
  58. fifo_open();
  59. }