source: neercs/trunk/src/screens.c @ 3942

Last change on this file since 3942 was 3942, checked in by Pascal Terjan, 11 years ago
  • Drop withespaces
  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: screens.c 3942 2009-11-18 15:31:49Z pterjan $
8 *
9 *  This program is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16#include "config.h"
17
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <signal.h>
23#include <sys/wait.h>
24#include <errno.h>
25#include <unistd.h>
26
27#include <caca.h>
28#include <caca.h>
29
30#include "neercs.h"
31
32struct screen* create_screen_grab(int w, int h, int pid)
33{
34    struct screen *s = (struct screen*) malloc(sizeof(struct screen));
35
36    s->cv = caca_create_canvas(w, h);
37    caca_set_color_ansi(s->cv, CACA_BLACK, CACA_BLACK);
38    caca_clear_canvas(s->cv);
39    s->init = 0;
40
41    s->buf = NULL;
42    s->title = NULL;
43    s->total = 0;
44    s->w = w+1;
45    s->h = h+1;
46    s->bell = 0;
47
48    s->fd = create_pty_grab(pid, w, h, &s->pid);
49
50    if(s->fd < 0)
51    {
52        caca_free_canvas(s->cv);
53        free(s);
54        return NULL;
55    }
56    return s;
57}
58
59struct screen* create_screen(int w, int h, char *command)
60{
61    struct screen *s = (struct screen*) malloc(sizeof(struct screen));
62
63    s->cv = caca_create_canvas(w, h);
64    caca_set_color_ansi(s->cv, CACA_BLACK, CACA_BLACK);
65    caca_clear_canvas(s->cv);
66    s->init = 0;
67
68    s->buf = NULL;
69    s->title = NULL;
70    s->total = 0;
71    s->w = w+1;
72    s->h = h+1;
73    s->bell = 0;
74    s->visible = 1;
75    s->scroll = 0;
76    s->fd = create_pty(command, w, h, &s->pid);
77
78    s->orig_w = s->w;
79    s->orig_h = s->h;
80    s->orig_x = s->x;
81    s->orig_y = s->y;
82
83
84    if(s->fd < 0)
85    {
86        caca_free_canvas(s->cv);
87        free(s);
88        return NULL;
89    }
90    return s;
91}
92
93int destroy_screen(struct screen *s)
94{
95    if(s->fd>=0)
96        close(s->fd);
97    if(s->buf)
98        free(s->buf);
99    if(s->title)
100        free(s->title);
101    s->buf = NULL;
102    if(s->cv)
103        caca_free_canvas(s->cv);
104    s->cv = NULL;
105    free(s);
106    return 1;
107}
108
109int add_screen(struct screen_list *list, struct screen *s)
110{
111    if(list == NULL || s == NULL) return -1;
112
113    else
114    {
115        list->screen = (struct screen**) realloc(list->screen,
116                                                 sizeof(sizeof(struct screen*))
117                                                 * (list->count+1));
118        if(!list->screen)
119            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
120        list->screen[list->count] = s;
121        list->count++;
122    }
123
124    list->changed = 1;
125
126    return list->count-1;
127}
128
129int remove_screen(struct screen_list *list, int n, int please_kill)
130{
131
132    if(n>=list->count) return -1;
133
134    if(please_kill)
135    {
136        int status = 0;
137        int ret = 0;
138        /* FIXME */
139        close(list->screen[n]->fd);
140        list->screen[n]->fd = -1;
141        kill(list->screen[n]->pid, SIGINT);
142        ret = waitpid(list->screen[n]->pid, &status,
143                      WNOHANG|WUNTRACED|WCONTINUED);
144        if(!ret)
145            kill(list->screen[n]->pid, SIGQUIT);
146        ret = waitpid(list->screen[n]->pid, &status,
147                      WNOHANG|WUNTRACED|WCONTINUED);
148        if(!ret)
149            kill(list->screen[n]->pid, SIGABRT);
150        ret = waitpid(list->screen[n]->pid, &status,
151                      WNOHANG|WUNTRACED|WCONTINUED);
152        if(!ret)
153            kill(list->screen[n]->pid, SIGKILL);
154
155    }
156    destroy_screen(list->screen[n]);
157
158    memmove(&list->screen[n],
159            &list->screen[n+1],
160            sizeof(struct screen*)*(list->count-(n+1)));
161
162    list->screen = (struct screen**) realloc(list->screen,
163                                             sizeof(sizeof(struct screen*))
164                                             * (list->count));
165    if(!list->screen)
166        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
167
168
169
170    list->count--;
171    list->changed = 1;
172    return 1;
173}
174
175
176
177void refresh_screens(struct screen_list *screen_list)
178{
179    int i;
180
181    if(!screen_list->count) return;
182
183
184    debug("Current pty is %d\n", screen_list->pty);
185
186    screen_list->width  = caca_get_canvas_width(screen_list->cv);
187    screen_list->height = caca_get_canvas_height(screen_list->cv) - (screen_list->mini*6) - screen_list->status;
188
189    if(!screen_list->dont_update_coords)
190    {
191        update_windows_props(screen_list);
192    }
193
194    if(screen_list->changed)
195    {
196        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
197        caca_clear_canvas(screen_list->cv);
198    }
199    caca_set_color_ansi(screen_list->cv, CACA_LIGHTRED, CACA_BLACK);
200
201    wm_refresh(screen_list);
202
203    caca_gotoxy(screen_list->cv,
204                screen_list->screen[screen_list->pty]->x +
205                caca_get_cursor_x(screen_list->screen[screen_list->pty]->cv),
206                screen_list->screen[screen_list->pty]->y +
207                caca_get_cursor_y(screen_list->screen[screen_list->pty]->cv));
208
209
210    if(screen_list->mini)
211    {
212        draw_thumbnails(screen_list);
213    }
214    if(screen_list->status)
215    {
216        draw_status(screen_list);
217    }
218    if(screen_list->help)
219    {
220        draw_help(screen_list);
221    }
222    if(screen_list->window_list)
223    {
224        draw_list(screen_list);
225    }
226    screen_list->changed = 0;
227    for(i = screen_list->count - 1; i >=0; i--)
228        screen_list->screen[i]->changed = 0;
229}
230
231
232int update_screens_contents(struct screen_list* screen_list)
233{
234    int i, refresh = 0;
235    int maxfd = 0;
236    struct timeval tv;
237    fd_set fdset;
238    int ret;
239
240    /* Read data, if any */
241    FD_ZERO(&fdset);
242    for(i = 0; i < screen_list->count; i++)
243    {
244        if(screen_list->screen[i]->fd >= 0)
245            FD_SET(screen_list->screen[i]->fd, &fdset);
246        if(screen_list->screen[i]->fd > maxfd)
247            maxfd = screen_list->screen[i]->fd;
248    }
249    tv.tv_sec = 0;
250    tv.tv_usec = 50000;
251    ret = select(maxfd + 1, &fdset, NULL, NULL, &tv);
252
253    if(ret < 0)
254    {
255        if(errno == EINTR)
256            ; /* We probably got a SIGWINCH, ignore it */
257        else
258        {
259            /* FIXME: Useless since break will mean that we return 0
260             * But I don't know what was the purpose of this code */
261            for(i = 0; i < screen_list->count; i++)
262                if(screen_list->screen[i]->total)
263                    break;
264            if(i == screen_list->count)
265                return 0;
266        }
267    }
268    else if(ret)
269    {
270
271    for(i = 0; i < screen_list->count; i++)
272    {
273        /* FIXME: try a new strategy: read all filedescriptors until
274         * each of them starved at least once. */
275
276        if(screen_list->screen[i]->fd < 0 ||
277           !FD_ISSET(screen_list->screen[i]->fd, &fdset))
278            continue;
279
280        for(;;)
281        {
282            ssize_t nr;
283
284            screen_list->screen[i]->buf =
285                realloc(screen_list->screen[i]->buf,
286                        screen_list->screen[i]->total + 1024);
287            if(!screen_list->screen[i]->buf)
288                fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
289
290            nr = read(screen_list->screen[i]->fd,
291                      screen_list->screen[i]->buf +
292                      screen_list->screen[i]->total, 1024);
293
294            if(nr > 0)
295            {
296                screen_list->screen[i]->total += nr;
297                continue;
298            }
299
300            if(nr == 0 || errno != EWOULDBLOCK)
301            {
302                remove_screen(screen_list, i, 0);
303                if(i < screen_list->prevpty) screen_list->prevpty--;
304                if(i == screen_list->pty)
305                {
306                    screen_list->pty = screen_list->prevpty;
307                    screen_list->prevpty = 0;
308                }
309                if(i < (screen_list->pty)) (screen_list->pty)--;
310                /* Don't skip the element which is now at position i */
311                i--;
312                refresh = 1;
313            }
314
315            break;
316        }
317    }
318    }
319    return refresh;
320}
321
322
Note: See TracBrowser for help on using the repository browser.