選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

134 行
3.2 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. #include "config.h"
  13. #define _XOPEN_SOURCE
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/types.h>
  19. #include <termios.h>
  20. #if defined HAVE_PTY_H
  21. # include <pty.h> /* for openpty and forkpty */
  22. #elif defined HAVE_UTIL_H
  23. # include <util.h> /* for OS X, OpenBSD and NetBSD */
  24. #elif defined HAVE_LIBUTIL_H
  25. # include <libutil.h> /* for FreeBSD */
  26. #endif
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <caca.h>
  30. #include "neercs.h"
  31. int create_pty(char *cmd, unsigned int w, unsigned int h, int *cpid)
  32. {
  33. char **argv;
  34. int fd;
  35. pid_t pid;
  36. pid = forkpty(&fd, NULL, NULL, NULL);
  37. if (pid < 0)
  38. {
  39. fprintf(stderr, "forkpty() error\n");
  40. return -1;
  41. }
  42. else if (pid == 0)
  43. {
  44. set_tty_size(0, w, h);
  45. putenv("CACA_DRIVER=slang");
  46. putenv("TERM=xterm");
  47. argv = malloc(2 * sizeof(char *));
  48. if (!argv)
  49. {
  50. fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
  51. __LINE__);
  52. return -1;
  53. }
  54. argv[0] = cmd;
  55. argv[1] = NULL;
  56. execvp(cmd, argv);
  57. fprintf(stderr, "execvp() error\n");
  58. return -1;
  59. }
  60. *cpid = pid;
  61. fcntl(fd, F_SETFL, O_NDELAY);
  62. return fd;
  63. }
  64. int create_pty_grab(long pid, unsigned int w, unsigned int h, int *newpid)
  65. {
  66. int fdm, fds;
  67. int ret = openpty(&fdm, &fds, NULL, NULL, NULL);
  68. if (ret < 0)
  69. {
  70. fprintf(stderr, "open() error\n");
  71. return -1;
  72. }
  73. set_tty_size(0, w, h);
  74. grab_process(pid, ptsname(fdm), fds, newpid);
  75. fcntl(fdm, F_SETFL, O_NDELAY);
  76. return fdm;
  77. }
  78. int set_tty_size(int fd, unsigned int w, unsigned int h)
  79. {
  80. struct winsize ws;
  81. memset(&ws, 0, sizeof(ws));
  82. ws.ws_row = h;
  83. ws.ws_col = w;
  84. ioctl(fd, TIOCSWINSZ, (char *)&ws);
  85. return 0;
  86. }
  87. int update_terms(struct screen_list *screen_list)
  88. {
  89. int i, refresh = 0;
  90. for (i = 0; i < screen_list->count; i++)
  91. {
  92. if (screen_list->screen[i]->total && !screen_list->dont_update_coords)
  93. {
  94. unsigned long int bytes;
  95. bytes = import_term(screen_list,
  96. screen_list->screen[i],
  97. screen_list->screen[i]->buf,
  98. screen_list->screen[i]->total);
  99. if (bytes > 0)
  100. {
  101. screen_list->screen[i]->total -= bytes;
  102. memmove(screen_list->screen[i]->buf,
  103. screen_list->screen[i]->buf + bytes,
  104. screen_list->screen[i]->total);
  105. if (screen_list->screen[i]->visible || screen_list->modals.mini)
  106. refresh = 1;
  107. }
  108. }
  109. }
  110. return refresh;
  111. }