Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

141 wiersze
3.3 KiB

  1. /*
  2. * neercs console-based window manager
  3. * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #if !defined _WIN32
  16. #define _XOPEN_SOURCE
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22. #include <termios.h>
  23. #if defined HAVE_PTY_H
  24. # include <pty.h> /* for openpty and forkpty */
  25. #elif defined HAVE_UTIL_H
  26. # include <util.h> /* for OS X, OpenBSD and NetBSD */
  27. #elif defined HAVE_LIBUTIL_H
  28. # include <libutil.h> /* for FreeBSD */
  29. #endif
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <caca.h>
  33. #include "neercs.h"
  34. int create_pty(char *cmd, unsigned int w, unsigned int h, int *cpid)
  35. {
  36. char **argv;
  37. int fd;
  38. pid_t pid;
  39. pid = forkpty(&fd, NULL, NULL, NULL);
  40. if (pid < 0)
  41. {
  42. fprintf(stderr, "forkpty() error\n");
  43. return -1;
  44. }
  45. else if (pid == 0)
  46. {
  47. set_tty_size(0, w, h);
  48. putenv("CACA_DRIVER=slang");
  49. putenv("TERM=xterm");
  50. argv = malloc(2 * sizeof(char *));
  51. if (!argv)
  52. {
  53. fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
  54. __LINE__);
  55. return -1;
  56. }
  57. argv[0] = cmd;
  58. argv[1] = NULL;
  59. execvp(cmd, argv);
  60. fprintf(stderr, "execvp() error\n");
  61. return -1;
  62. }
  63. *cpid = pid;
  64. fcntl(fd, F_SETFL, O_NDELAY);
  65. return fd;
  66. }
  67. int create_pty_grab(long pid, unsigned int w, unsigned int h, int *newpid)
  68. {
  69. int fdm, fds;
  70. int ret = openpty(&fdm, &fds, NULL, NULL, NULL);
  71. if (ret < 0)
  72. {
  73. fprintf(stderr, "open() error\n");
  74. return -1;
  75. }
  76. set_tty_size(0, w, h);
  77. grab_process(pid, ptsname(fdm), fds, newpid);
  78. fcntl(fdm, F_SETFL, O_NDELAY);
  79. return fdm;
  80. }
  81. int set_tty_size(int fd, unsigned int w, unsigned int h)
  82. {
  83. struct winsize ws;
  84. memset(&ws, 0, sizeof(ws));
  85. ws.ws_row = h;
  86. ws.ws_col = w;
  87. ioctl(fd, TIOCSWINSZ, (char *)&ws);
  88. return 0;
  89. }
  90. int update_terms(struct screen_list *screen_list)
  91. {
  92. int i, refresh = 0;
  93. for (i = 0; i < screen_list->count; i++)
  94. {
  95. if (screen_list->screen[i]->total && !screen_list->dont_update_coords)
  96. {
  97. unsigned long int bytes;
  98. bytes = import_term(screen_list,
  99. screen_list->screen[i],
  100. screen_list->screen[i]->buf,
  101. screen_list->screen[i]->total);
  102. if (bytes > 0)
  103. {
  104. screen_list->screen[i]->total -= bytes;
  105. memmove(screen_list->screen[i]->buf,
  106. screen_list->screen[i]->buf + bytes,
  107. screen_list->screen[i]->total);
  108. if (screen_list->screen[i]->visible || screen_list->modals.mini)
  109. refresh = 1;
  110. }
  111. }
  112. }
  113. return refresh;
  114. }
  115. #endif