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

Last change on this file since 2995 was 2995, checked in by Sam Hocevar, 12 years ago

Port neercs to the unified libcaca 0.99.beta15 API.

File size: 10.0 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 <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    s->pid = pid;
48
49    s->fd = create_pty_grab(pid, w, h);
50
51    if(s->fd < 0)
52    {
53        caca_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 = caca_create_canvas(w, h);
65    caca_set_color_ansi(s->cv, CACA_BLACK, CACA_BLACK);
66    caca_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        caca_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        caca_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(struct screen_list *screen_list)
176{
177    int i;
178
179    if(!screen_list->count) return;
180
181
182    debug("Current pty is %d\n", screen_list->pty);
183
184    screen_list->width  = caca_get_canvas_width(screen_list->cv);
185    screen_list->height = caca_get_canvas_height(screen_list->cv) - (screen_list->mini*6);
186
187    if(!screen_list->dont_update_coords)
188        update_windows_props(screen_list);
189
190    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
191    caca_clear_canvas(screen_list->cv);
192    caca_set_color_ansi(screen_list->cv, CACA_LIGHTRED, CACA_BLACK);
193
194    for(i = screen_list->count - 1; i >=0; i--)
195    {
196        if(i!=screen_list->pty && screen_list->screen[i]->visible)
197        {
198            caca_blit(screen_list->cv,
199                       screen_list->screen[i]->x,
200                       screen_list->screen[i]->y,
201                       screen_list->screen[i]->cv, NULL);
202
203            caca_draw_cp437_box(screen_list->cv,
204                                 screen_list->screen[i]->x - 1,
205                                 screen_list->screen[i]->y - 1,
206                                 screen_list->screen[i]->w + 2,
207                                 screen_list->screen[i]->h + 2);
208            if(screen_list->screen[i]->title)
209                caca_printf(screen_list->cv,
210                             screen_list->screen[i]->x,
211                             screen_list->screen[i]->y - 1,
212                             " %.*s ",
213                             screen_list->screen[i]->w - 3,
214                             screen_list->screen[i]->title);
215        }
216    }
217
218    caca_blit(screen_list->cv,
219               screen_list->screen[screen_list->pty]->x,
220               screen_list->screen[screen_list->pty]->y,
221               screen_list->screen[screen_list->pty]->cv, NULL);
222
223    if(screen_list->screen[screen_list->pty]->bell)
224    {
225        caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLACK);
226        screen_list->screen[screen_list->pty]->bell = 0;
227        screen_list->in_bell--;
228    }
229    else
230    {
231        caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLACK);
232    }
233
234    caca_draw_cp437_box(screen_list->cv,
235                         screen_list->screen[screen_list->pty]->x - 1,
236                         screen_list->screen[screen_list->pty]->y - 1,
237                         screen_list->screen[screen_list->pty]->w + 2,
238                         screen_list->screen[screen_list->pty]->h + 2);
239
240    if(screen_list->screen[screen_list->pty]->title)
241    {
242        caca_printf(screen_list->cv,
243                     screen_list->screen[screen_list->pty]->x,
244                     screen_list->screen[screen_list->pty]->y - 1,
245                     " %.*s ",
246                     screen_list->screen[screen_list->pty]->w - 3,
247                     screen_list->screen[screen_list->pty]->title);
248    }
249
250    caca_gotoxy(screen_list->cv,
251                 screen_list->screen[screen_list->pty]->x +
252                 caca_get_cursor_x(screen_list->screen[screen_list->pty]->cv),
253                 screen_list->screen[screen_list->pty]->y +
254                 caca_get_cursor_y(screen_list->screen[screen_list->pty]->cv));
255
256
257    if(screen_list->mini)
258    {
259        draw_thumbnails(screen_list);
260    }
261    if(screen_list->status)
262    {
263        draw_status(screen_list);
264    }
265    if(screen_list->help)
266    {
267        draw_help(screen_list);
268    }
269    if(screen_list->window_list)
270    {
271        draw_list(screen_list);
272    }
273}
274
275
276int update_screens_contents(struct screen_list* screen_list)
277{
278    int i, refresh = 0;
279    int maxfd = 0;
280    struct timeval tv;
281    fd_set fdset;
282    int ret;
283
284    /* Read data, if any */
285    FD_ZERO(&fdset);
286    for(i = 0; i < screen_list->count; i++)
287    {
288        if(screen_list->screen[i]->fd >= 0)
289            FD_SET(screen_list->screen[i]->fd, &fdset);
290        if(screen_list->screen[i]->fd > maxfd)
291            maxfd = screen_list->screen[i]->fd;
292    }
293    tv.tv_sec = 0;
294    tv.tv_usec = 50000;
295    ret = select(maxfd + 1, &fdset, NULL, NULL, &tv);
296
297    if(ret < 0)
298    {
299        if(errno == EINTR)
300            ; /* We probably got a SIGWINCH, ignore it */
301        else
302        {
303            for(i = 0; i < screen_list->count; i++)
304                if(screen_list->screen[i]->total)
305                    break;
306            if(i == screen_list->count)
307                return 0;
308        }
309    }
310    else if(ret)
311    {
312
313    for(i = 0; i < screen_list->count; i++)
314    {
315        /* FIXME: try a new strategy: read all filedescriptors until
316         * each of them starved at least once. */
317
318        if(screen_list->screen[i]->fd < 0 ||
319           !FD_ISSET(screen_list->screen[i]->fd, &fdset))
320            continue;
321
322        for(;;)
323        {
324            ssize_t nr;
325
326            screen_list->screen[i]->buf =
327                realloc(screen_list->screen[i]->buf,
328                        screen_list->screen[i]->total + 1024);
329            if(!screen_list->screen[i]->buf)
330                fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
331
332            nr = read(screen_list->screen[i]->fd,
333                      screen_list->screen[i]->buf +
334                      screen_list->screen[i]->total, 1024);
335
336            if(nr > 0)
337            {
338                screen_list->screen[i]->total += nr;
339                continue;
340            }
341
342            if(nr == 0 || errno != EWOULDBLOCK)
343            {
344                remove_screen(screen_list, i, 0);
345                if(i < screen_list->prevpty) screen_list->prevpty--;
346                if(i == screen_list->pty)
347                {
348                    screen_list->pty = screen_list->prevpty;
349                    screen_list->prevpty = 0;
350                }
351                if(i < (screen_list->pty)) (screen_list->pty)--;
352                /* Don't skip the element which is now at position i */
353                i--;
354                refresh = 1;
355            }
356
357            break;
358        }
359    }
360    }
361    return refresh;
362}
363
364
Note: See TracBrowser for help on using the repository browser.