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