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

Last change on this file since 4074 was 4074, checked in by Jean-Yves Lamoureux, 13 years ago
  • Moved ANSI emulation to ansi.c (term.c now just handles terminals stuff)
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: term.c 4074 2009-11-30 10:26:50Z jylam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#define _XOPEN_SOURCE
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#if defined HAVE_PTY_H
22#   include <pty.h>             /* for openpty and forkpty */
23#else
24#   include <util.h>            /* for OS X */
25#endif
26#include <unistd.h>
27#include <fcntl.h>
28
29#include <caca.h>
30
31#include "neercs.h"
32
33
34int create_pty(char *cmd, unsigned int w, unsigned int h, int *cpid)
35{
36    char **argv;
37    int fd;
38    pid_t pid;
39
40    pid = forkpty(&fd, NULL, NULL, NULL);
41    if (pid < 0)
42    {
43        fprintf(stderr, "forkpty() error\n");
44        return -1;
45    }
46    else if (pid == 0)
47    {
48        set_tty_size(0, w, h);
49        putenv("CACA_DRIVER=slang");
50        putenv("TERM=xterm");
51        argv = malloc(2 * sizeof(char *));
52        if (!argv)
53        {
54            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
55                    __LINE__);
56            return -1;
57        }
58        argv[0] = cmd;
59        argv[1] = NULL;
60        execvp(cmd, argv);
61        fprintf(stderr, "execvp() error\n");
62        return -1;
63    }
64
65    *cpid = pid;
66
67    fcntl(fd, F_SETFL, O_NDELAY);
68    return fd;
69}
70
71int create_pty_grab(long pid, unsigned int w, unsigned int h, int *newpid)
72{
73    int fdm, fds;
74
75    int ret = openpty(&fdm, &fds, NULL, NULL, NULL);
76
77    if (ret < 0)
78    {
79        fprintf(stderr, "open() error\n");
80        return -1;
81    }
82
83    set_tty_size(0, w, h);
84    grab_process(pid, ptsname(fdm), fds, newpid);
85
86    fcntl(fdm, F_SETFL, O_NDELAY);
87    return fdm;
88}
89
90int set_tty_size(int fd, unsigned int w, unsigned int h)
91{
92    struct winsize ws;
93
94    memset(&ws, 0, sizeof(ws));
95    ws.ws_row = h;
96    ws.ws_col = w;
97    ioctl(fd, TIOCSWINSZ, (char *)&ws);
98
99    return 0;
100}
101
102
103
104int update_terms(struct screen_list *screen_list)
105{
106    int i, refresh = 0;
107    for (i = 0; i < screen_list->count; i++)
108    {
109        if (screen_list->screen[i]->total && !screen_list->dont_update_coords)
110        {
111            unsigned long int bytes;
112
113            bytes = import_term(screen_list,
114                                screen_list->screen[i],
115                                screen_list->screen[i]->buf,
116                                screen_list->screen[i]->total);
117
118            if (bytes > 0)
119            {
120                screen_list->screen[i]->total -= bytes;
121                memmove(screen_list->screen[i]->buf,
122                        screen_list->screen[i]->buf + bytes,
123                        screen_list->screen[i]->total);
124                if (screen_list->screen[i]->visible || screen_list->modals.mini)
125                    refresh = 1;
126            }
127        }
128    }
129    return refresh;
130}
Note: See TracBrowser for help on using the repository browser.