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

Last change on this file since 3453 was 3453, checked in by Pascal Terjan, 11 years ago
  • First incomplete reduction of useless blits
File size: 10.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: 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    list->changed = 1;
126
127    return list->count-1;
128}
129
130int remove_screen(struct screen_list *list, int n, int please_kill)
131{
132
133    if(n>=list->count) return -1;
134
135    if(please_kill)
136    {
137        int status = 0;
138        int ret = 0;
139        /* FIXME */
140        close(list->screen[n]->fd);
141        list->screen[n]->fd = -1;
142        kill(list->screen[n]->pid, SIGINT);
143        ret = waitpid(list->screen[n]->pid, &status,
144                      WNOHANG|WUNTRACED|WCONTINUED);
145        if(!ret)
146            kill(list->screen[n]->pid, SIGQUIT);
147        ret = waitpid(list->screen[n]->pid, &status,
148                      WNOHANG|WUNTRACED|WCONTINUED);
149        if(!ret)
150            kill(list->screen[n]->pid, SIGABRT);
151        ret = waitpid(list->screen[n]->pid, &status,
152                      WNOHANG|WUNTRACED|WCONTINUED);
153        if(!ret)
154            kill(list->screen[n]->pid, SIGKILL);
155
156    }
157    destroy_screen(list->screen[n]);
158
159    memmove(&list->screen[n],
160            &list->screen[n+1],
161            sizeof(struct screen*)*(list->count-(n+1)));
162
163    list->screen = (struct screen**) realloc(list->screen,
164                                             sizeof(sizeof(struct screen*))
165                                             * (list->count));
166    if(!list->screen)
167        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
168
169
170
171    list->count--;
172    list->changed = 1;
173    return 1;
174}
175
176
177
178void refresh_screens(struct screen_list *screen_list)
179{
180    int i;
181
182    if(!screen_list->count) return;
183
184
185    debug("Current pty is %d\n", screen_list->pty);
186
187    screen_list->width  = caca_get_canvas_width(screen_list->cv);
188    screen_list->height = caca_get_canvas_height(screen_list->cv) - (screen_list->mini*6);
189
190    if(!screen_list->dont_update_coords)
191        update_windows_props(screen_list);
192
193    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
194    caca_clear_canvas(screen_list->cv);
195    caca_set_color_ansi(screen_list->cv, CACA_LIGHTRED, CACA_BLACK);
196
197    for(i = screen_list->count - 1; i >=0; i--)
198    {
199        if(i!=screen_list->pty && screen_list->screen[i]->visible)
200        {
201            caca_blit(screen_list->cv,
202                       screen_list->screen[i]->x,
203                       screen_list->screen[i]->y,
204                       screen_list->screen[i]->cv, NULL);
205
206            caca_draw_cp437_box(screen_list->cv,
207                                 screen_list->screen[i]->x - 1,
208                                 screen_list->screen[i]->y - 1,
209                                 screen_list->screen[i]->w + 2,
210                                 screen_list->screen[i]->h + 2);
211            if(screen_list->screen[i]->title)
212                caca_printf(screen_list->cv,
213                             screen_list->screen[i]->x,
214                             screen_list->screen[i]->y - 1,
215                             " %.*s ",
216                             screen_list->screen[i]->w - 3,
217                             screen_list->screen[i]->title);
218        }
219    }
220
221    caca_blit(screen_list->cv,
222               screen_list->screen[screen_list->pty]->x,
223               screen_list->screen[screen_list->pty]->y,
224               screen_list->screen[screen_list->pty]->cv, NULL);
225
226    if(screen_list->screen[screen_list->pty]->bell)
227    {
228        caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLACK);
229        screen_list->screen[screen_list->pty]->bell = 0;
230        screen_list->in_bell--;
231    }
232    else
233    {
234        caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLACK);
235    }
236
237    caca_draw_cp437_box(screen_list->cv,
238                         screen_list->screen[screen_list->pty]->x - 1,
239                         screen_list->screen[screen_list->pty]->y - 1,
240                         screen_list->screen[screen_list->pty]->w + 2,
241                         screen_list->screen[screen_list->pty]->h + 2);
242
243    if(screen_list->screen[screen_list->pty]->title)
244    {
245        caca_printf(screen_list->cv,
246                     screen_list->screen[screen_list->pty]->x,
247                     screen_list->screen[screen_list->pty]->y - 1,
248                     " %.*s ",
249                     screen_list->screen[screen_list->pty]->w - 3,
250                     screen_list->screen[screen_list->pty]->title);
251    }
252
253    caca_gotoxy(screen_list->cv,
254                 screen_list->screen[screen_list->pty]->x +
255                 caca_get_cursor_x(screen_list->screen[screen_list->pty]->cv),
256                 screen_list->screen[screen_list->pty]->y +
257                 caca_get_cursor_y(screen_list->screen[screen_list->pty]->cv));
258
259
260    if(screen_list->mini)
261    {
262        draw_thumbnails(screen_list);
263    }
264    if(screen_list->status)
265    {
266        draw_status(screen_list);
267    }
268    if(screen_list->help)
269    {
270        draw_help(screen_list);
271    }
272    if(screen_list->window_list)
273    {
274        draw_list(screen_list);
275    }
276    screen_list->changed = 0;
277    for(i = screen_list->count - 1; i >=0; i--)
278        screen_list->screen[i]->changed = 0;
279}
280
281
282int update_screens_contents(struct screen_list* screen_list)
283{
284    int i, refresh = 0;
285    int maxfd = 0;
286    struct timeval tv;
287    fd_set fdset;
288    int ret;
289
290    /* Read data, if any */
291    FD_ZERO(&fdset);
292    for(i = 0; i < screen_list->count; i++)
293    {
294        if(screen_list->screen[i]->fd >= 0)
295            FD_SET(screen_list->screen[i]->fd, &fdset);
296        if(screen_list->screen[i]->fd > maxfd)
297            maxfd = screen_list->screen[i]->fd;
298    }
299    tv.tv_sec = 0;
300    tv.tv_usec = 50000;
301    ret = select(maxfd + 1, &fdset, NULL, NULL, &tv);
302
303    if(ret < 0)
304    {
305        if(errno == EINTR)
306            ; /* We probably got a SIGWINCH, ignore it */
307        else
308        {
309            for(i = 0; i < screen_list->count; i++)
310                if(screen_list->screen[i]->total)
311                    break;
312            if(i == screen_list->count)
313                return 0;
314        }
315    }
316    else if(ret)
317    {
318
319    for(i = 0; i < screen_list->count; i++)
320    {
321        /* FIXME: try a new strategy: read all filedescriptors until
322         * each of them starved at least once. */
323
324        if(screen_list->screen[i]->fd < 0 ||
325           !FD_ISSET(screen_list->screen[i]->fd, &fdset))
326            continue;
327
328        for(;;)
329        {
330            ssize_t nr;
331
332            screen_list->screen[i]->buf =
333                realloc(screen_list->screen[i]->buf,
334                        screen_list->screen[i]->total + 1024);
335            if(!screen_list->screen[i]->buf)
336                fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
337
338            nr = read(screen_list->screen[i]->fd,
339                      screen_list->screen[i]->buf +
340                      screen_list->screen[i]->total, 1024);
341
342            if(nr > 0)
343            {
344                screen_list->screen[i]->total += nr;
345                continue;
346            }
347
348            if(nr == 0 || errno != EWOULDBLOCK)
349            {
350                remove_screen(screen_list, i, 0);
351                if(i < screen_list->prevpty) screen_list->prevpty--;
352                if(i == screen_list->pty)
353                {
354                    screen_list->pty = screen_list->prevpty;
355                    screen_list->prevpty = 0;
356                }
357                if(i < (screen_list->pty)) (screen_list->pty)--;
358                /* Don't skip the element which is now at position i */
359                i--;
360                refresh = 1;
361            }
362
363            break;
364        }
365    }
366    }
367    return refresh;
368}
369
370
Note: See TracBrowser for help on using the repository browser.