source: neercs/trunk/src/term.c @ 4791

Last change on this file since 4791 was 4791, checked in by Pascal Terjan, 10 years ago

Include util.h or libutil.h depending on what is available, should help building on FreeBSD

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