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

Last change on this file since 2479 was 2478, checked in by Pascal Terjan, 15 years ago
  • Don't use the caca_display in refresh_screens, it would make clean detach very hard
File size: 9.7 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: main.c 2401 2008-06-15 12:50:12Z jylam $
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 <cucul.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 = cucul_create_canvas(w, h);
37    cucul_set_color_ansi(s->cv, CUCUL_BLACK, CUCUL_BLACK);
38    cucul_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    s->pid = pid;
48
49    s->fd = create_pty_grab(pid, w, h);
50
51    if(s->fd < 0)
52    {
53        cucul_free_canvas(s->cv);
54        free(s);
55        return NULL;
56    }
57    return s;
58}
59
60struct screen* create_screen(int w, int h, char *command)
61{
62    struct screen *s = (struct screen*) malloc(sizeof(struct screen));
63
64    s->cv = cucul_create_canvas(w, h);
65    cucul_set_color_ansi(s->cv, CUCUL_BLACK, CUCUL_BLACK);
66    cucul_clear_canvas(s->cv);
67    s->init = 0;
68
69    s->buf = NULL;
70    s->title = NULL;
71    s->total = 0;
72    s->w = w+1;
73    s->h = h+1;
74    s->bell = 0;
75    s->visible = 1;
76    s->scroll = 0;
77    s->fd = create_pty(command, w, h, &s->pid);
78
79    s->orig_w = s->w;
80    s->orig_h = s->h;
81    s->orig_x = s->x;
82    s->orig_y = s->y;
83
84
85    if(s->fd < 0)
86    {
87        cucul_free_canvas(s->cv);
88        free(s);
89        return NULL;
90    }
91    return s;
92}
93
94int destroy_screen(struct screen *s)
95{
96    if(s->fd>=0)
97        close(s->fd);
98    if(s->buf)
99        free(s->buf);
100    if(s->title)
101        free(s->title);
102    s->buf = NULL;
103    if(s->cv)
104        cucul_free_canvas(s->cv);
105    s->cv = NULL;
106    free(s);
107    return 1;
108}
109
110int add_screen(struct screen_list *list, struct screen *s)
111{
112    if(list == NULL || s == NULL) return -1;
113
114    else
115    {
116        list->screen = (struct screen**) realloc(list->screen,
117                                                 sizeof(sizeof(struct screen*))
118                                                 * (list->count+1));
119        if(!list->screen)
120            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
121        list->screen[list->count] = s;
122        list->count++;
123    }
124
125    return list->count-1;
126}
127
128int remove_screen(struct screen_list *list, int n, int please_kill)
129{
130
131    if(n>=list->count) return -1;
132
133    if(please_kill)
134    {
135        int status = 0;
136        int ret = 0;
137        /* FIXME */
138        close(list->screen[n]->fd);
139        list->screen[n]->fd = -1;
140        kill(list->screen[n]->pid, SIGINT);
141        ret = waitpid(list->screen[n]->pid, &status,
142                      WNOHANG|WUNTRACED|WCONTINUED);
143        if(!ret)
144            kill(list->screen[n]->pid, SIGQUIT);
145        ret = waitpid(list->screen[n]->pid, &status,
146                      WNOHANG|WUNTRACED|WCONTINUED);
147        if(!ret)
148            kill(list->screen[n]->pid, SIGABRT);
149        ret = waitpid(list->screen[n]->pid, &status,
150                      WNOHANG|WUNTRACED|WCONTINUED);
151        if(!ret)
152            kill(list->screen[n]->pid, SIGKILL);
153
154    }
155    destroy_screen(list->screen[n]);
156
157    memmove(&list->screen[n],
158            &list->screen[n+1],
159            sizeof(struct screen*)*(list->count-(n+1)));
160
161    list->screen = (struct screen**) realloc(list->screen,
162                                             sizeof(sizeof(struct screen*))
163                                             * (list->count));
164    if(!list->screen)
165        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
166
167
168
169    list->count--;
170    return 1;
171}
172
173
174
175void refresh_screens(cucul_canvas_t *cv,
176                     struct screen_list *screen_list)
177{
178    int i;
179
180    if(!screen_list->count) return;
181
182    screen_list->width  = cucul_get_canvas_width(cv);
183    screen_list->height = cucul_get_canvas_height(cv) - (screen_list->mini*6);
184
185    if(!screen_list->dont_update_coords)
186        update_windows_props(cv, screen_list);
187
188    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
189    cucul_clear_canvas(cv);
190    cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
191
192    for(i = screen_list->count - 1; i >=0; i--)
193    {
194        if(i!=screen_list->pty && screen_list->screen[i]->visible)
195        {
196            cucul_blit(cv,
197                       screen_list->screen[i]->x,
198                       screen_list->screen[i]->y,
199                       screen_list->screen[i]->cv, NULL);
200
201            cucul_draw_cp437_box(cv,
202                                 screen_list->screen[i]->x - 1,
203                                 screen_list->screen[i]->y - 1,
204                                 screen_list->screen[i]->w + 2,
205                                 screen_list->screen[i]->h + 2);
206            if(screen_list->screen[i]->title)
207                cucul_printf(cv,
208                             screen_list->screen[i]->x,
209                             screen_list->screen[i]->y - 1,
210                             " %.*s ",
211                             screen_list->screen[i]->w - 3,
212                             screen_list->screen[i]->title);
213        }
214    }
215
216    cucul_blit(cv,
217               screen_list->screen[screen_list->pty]->x,
218               screen_list->screen[screen_list->pty]->y,
219               screen_list->screen[screen_list->pty]->cv, NULL);
220
221    if(screen_list->screen[screen_list->pty]->bell)
222    {
223        cucul_set_color_ansi(cv, CUCUL_RED, CUCUL_BLACK);
224        screen_list->screen[screen_list->pty]->bell = 0;
225        screen_list->in_bell--;
226    }
227    else
228    {
229        cucul_set_color_ansi(cv, CUCUL_LIGHTGREEN, CUCUL_BLACK);
230    }
231
232    cucul_draw_cp437_box(cv,
233                         screen_list->screen[screen_list->pty]->x - 1,
234                         screen_list->screen[screen_list->pty]->y - 1,
235                         screen_list->screen[screen_list->pty]->w + 2,
236                         screen_list->screen[screen_list->pty]->h + 2);
237
238    if(screen_list->screen[screen_list->pty]->title)
239    {
240        cucul_printf(cv,
241                     screen_list->screen[screen_list->pty]->x,
242                     screen_list->screen[screen_list->pty]->y - 1,
243                     " %.*s ",
244                     screen_list->screen[screen_list->pty]->w - 3,
245                     screen_list->screen[screen_list->pty]->title);
246    }
247
248    cucul_gotoxy(cv,
249                 screen_list->screen[screen_list->pty]->x +
250                 cucul_get_cursor_x(screen_list->screen[screen_list->pty]->cv),
251                 screen_list->screen[screen_list->pty]->y +
252                 cucul_get_cursor_y(screen_list->screen[screen_list->pty]->cv));
253
254
255    if(screen_list->mini)
256    {
257        draw_thumbnails(cv, screen_list);
258    }
259    if(screen_list->status)
260    {
261        draw_status(cv, screen_list);
262    }
263    if(screen_list->help)
264    {
265        draw_help(cv, screen_list);
266    }
267}
268
269
270int update_screens_contents(struct screen_list* screen_list)
271{
272    int i, refresh = 0;
273    int maxfd = 0;
274    struct timeval tv;
275    fd_set fdset;
276    int ret;
277
278    /* Read data, if any */
279    FD_ZERO(&fdset);
280    for(i = 0; i < screen_list->count; i++)
281    {
282        if(screen_list->screen[i]->fd >= 0)
283            FD_SET(screen_list->screen[i]->fd, &fdset);
284        if(screen_list->screen[i]->fd > maxfd)
285            maxfd = screen_list->screen[i]->fd;
286    }
287    tv.tv_sec = 0;
288    tv.tv_usec = 50000;
289    ret = select(maxfd + 1, &fdset, NULL, NULL, &tv);
290
291    if(ret < 0)
292    {
293        if(errno == EINTR)
294            ; /* We probably got a SIGWINCH, ignore it */
295        else
296        {
297            for(i = 0; i < screen_list->count; i++)
298                if(screen_list->screen[i]->total)
299                    break;
300            if(i == screen_list->count)
301                return 0;
302        }
303    }
304    else if(ret)
305    {
306
307    for(i = 0; i < screen_list->count; i++)
308    {
309        /* FIXME: try a new strategy: read all filedescriptors until
310         * each of them starved at least once. */
311
312        if(screen_list->screen[i]->fd < 0 ||
313           !FD_ISSET(screen_list->screen[i]->fd, &fdset))
314            continue;
315
316        for(;;)
317        {
318            ssize_t nr;
319
320            screen_list->screen[i]->buf =
321                realloc(screen_list->screen[i]->buf,
322                        screen_list->screen[i]->total + 1024);
323            if(!screen_list->screen[i]->buf)
324                fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
325
326            nr = read(screen_list->screen[i]->fd,
327                      screen_list->screen[i]->buf +
328                      screen_list->screen[i]->total, 1024);
329
330            if(nr > 0)
331            {
332                screen_list->screen[i]->total += nr;
333                continue;
334            }
335
336            if(nr == 0 || errno != EWOULDBLOCK)
337            {
338                remove_screen(screen_list, i, 0);
339                if(i < screen_list->prevpty) screen_list->prevpty--;
340                if(i == screen_list->pty)
341                {
342                    screen_list->pty = screen_list->prevpty;
343                    screen_list->prevpty = 0;
344                }
345                if(i < (screen_list->pty)) (screen_list->pty)--;
346                /* Don't skip the element which is now at position i */
347                i--;
348                refresh = 1;
349            }
350
351            break;
352        }
353    }
354    }
355    return refresh;
356}
357
358
Note: See TracBrowser for help on using the repository browser.