source: neercs/trunk/src/term.c

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

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