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

Last change on this file since 2435 was 2435, checked in by Pascal Terjan, 13 years ago
  • No need to close the fd just before calling destroy_screen
  • Don't skip one element when we are looping through the list and remove one
File size: 9.9 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
32#ifdef USE_GRAB
33struct screen* create_screen_grab(int w, int h, int pid)
34{
35    struct screen *s = (struct screen*) malloc(sizeof(struct screen));
36
37    s->cv = cucul_create_canvas(w, h);
38    cucul_set_color_ansi(s->cv, CUCUL_BLACK, CUCUL_BLACK);
39    cucul_clear_canvas(s->cv);
40    s->init = 0;
41
42    s->buf = NULL;
43    s->title = NULL;
44    s->total = 0;
45    s->w = w+1;
46    s->h = h+1;
47    s->bell = 0;
48    s->pid = pid;
49
50    s->fd = create_pty_grab(pid, w, h);
51
52    if(s->fd < 0)
53    {
54        cucul_free_canvas(s->cv);
55        free(s);
56        return NULL;
57    }
58    return s;
59}
60#endif
61
62struct screen* create_screen(int w, int h, char *command)
63{
64    struct screen *s = (struct screen*) malloc(sizeof(struct screen));
65
66    s->cv = cucul_create_canvas(w, h);
67    cucul_set_color_ansi(s->cv, CUCUL_BLACK, CUCUL_BLACK);
68    cucul_clear_canvas(s->cv);
69    s->init = 0;
70
71    s->buf = NULL;
72    s->title = NULL;
73    s->total = 0;
74    s->w = w+1;
75    s->h = h+1;
76    s->bell = 0;
77    s->visible = 1;
78    s->scroll = 0;
79    s->fd = create_pty(command, w, h, &s->pid);
80
81    if(s->fd < 0)
82    {
83        cucul_free_canvas(s->cv);
84        free(s);
85        return NULL;
86    }
87    return s;
88}
89
90int destroy_screen(struct screen *s)
91{
92    if(s->fd>=0)
93        close(s->fd);
94    if(s->buf)
95        free(s->buf);
96    if(s->title)
97        free(s->title);
98    s->buf = NULL;
99    if(s->cv)
100        cucul_free_canvas(s->cv);
101    s->cv = NULL;
102    free(s);
103    return 1;
104}
105
106int add_screen(struct screen_list *list, struct screen *s)
107{
108    if(list == NULL || s == NULL) return -1;
109
110    else
111    {
112        list->screen = (struct screen**) realloc(list->screen,
113                                                 sizeof(sizeof(struct screen*))
114                                                 * (list->count+1));
115        if(!list->screen)
116            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
117        list->screen[list->count] = s;
118        list->count++;
119    }
120
121    return list->count-1;
122}
123
124int remove_screen(struct screen_list *list, int n, int please_kill)
125{
126
127    if(n>=list->count) return -1;
128
129    if(please_kill)
130    {
131        int status = 0;
132        int ret = 0;
133        /* FIXME */
134        close(list->screen[n]->fd);
135        list->screen[n]->fd = -1;
136        kill(list->screen[n]->pid, SIGINT);
137        ret = waitpid(list->screen[n]->pid, &status,
138                      WNOHANG|WUNTRACED|WCONTINUED);
139        if(!ret)
140            kill(list->screen[n]->pid, SIGQUIT);
141        ret = waitpid(list->screen[n]->pid, &status,
142                      WNOHANG|WUNTRACED|WCONTINUED);
143        if(!ret)
144            kill(list->screen[n]->pid, SIGABRT);
145        ret = waitpid(list->screen[n]->pid, &status,
146                      WNOHANG|WUNTRACED|WCONTINUED);
147        if(!ret)
148            kill(list->screen[n]->pid, SIGKILL);
149
150    }
151    destroy_screen(list->screen[n]);
152
153    memmove(&list->screen[n],
154            &list->screen[n+1],
155            sizeof(struct screen*)*(list->count-(n+1)));
156
157    list->screen = (struct screen**) realloc(list->screen,
158                                             sizeof(sizeof(struct screen*))
159                                             * (list->count));
160    if(!list->screen)
161        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
162
163
164
165    list->count--;
166    return 1;
167}
168
169
170
171void refresh_screens(cucul_canvas_t *cv,
172                     caca_display_t *dp,
173                     struct screen_list *screen_list)
174{
175    int i;
176
177    if(!screen_list->count) return;
178
179    screen_list->width  = cucul_get_canvas_width(cv);
180    screen_list->height = cucul_get_canvas_height(cv) - (screen_list->mini*6);
181
182    update_windows_props(cv, screen_list);
183
184    if(screen_list->screen[screen_list->pty]->title)
185        caca_set_display_title(dp, screen_list->screen[screen_list->pty]->title);
186    else
187        caca_set_display_title(dp, PACKAGE_STRING);
188
189    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
190    cucul_clear_canvas(cv);
191    cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
192
193    for(i = screen_list->count - 1; i >=0; i--)
194    {
195        if(i!=screen_list->pty && screen_list->screen[i]->visible)
196        {
197            cucul_blit(cv,
198                       screen_list->screen[i]->x,
199                       screen_list->screen[i]->y,
200                       screen_list->screen[i]->cv, NULL);
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    caca_refresh_display(dp);
269}
270
271
272int update_screens_contents(struct screen_list* screen_list)
273{
274    int i, refresh = 0;
275    int maxfd = 0;
276    struct timeval tv;
277    fd_set fdset;
278    int ret;
279
280    /* Read data, if any */
281    FD_ZERO(&fdset);
282    for(i = 0; i < screen_list->count; i++)
283    {
284        if(screen_list->screen[i]->fd >= 0)
285            FD_SET(screen_list->screen[i]->fd, &fdset);
286        if(screen_list->screen[i]->fd > maxfd)
287            maxfd = screen_list->screen[i]->fd;
288    }
289    tv.tv_sec = 0;
290    tv.tv_usec = 50000;
291    ret = select(maxfd + 1, &fdset, NULL, NULL, &tv);
292
293    if(ret < 0)
294    {
295        if(errno == EINTR)
296            ; /* We probably got a SIGWINCH, ignore it */
297        else
298        {
299            for(i = 0; i < screen_list->count; i++)
300                if(screen_list->screen[i]->total)
301                    break;
302            if(i == screen_list->count)
303                return 0;
304        }
305    }
306    else if(ret)
307    {
308
309    for(i = 0; i < screen_list->count; i++)
310    {
311        /* FIXME: try a new strategy: read all filedescriptors until
312         * each of them starved at least once. */
313
314        if(screen_list->screen[i]->fd < 0 ||
315           !FD_ISSET(screen_list->screen[i]->fd, &fdset))
316            continue;
317
318        for(;;)
319        {
320            ssize_t nr;
321
322            screen_list->screen[i]->buf =
323                realloc(screen_list->screen[i]->buf,
324                        screen_list->screen[i]->total + 1024);
325            if(!screen_list->screen[i]->buf)
326                fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
327
328            nr = read(screen_list->screen[i]->fd,
329                      screen_list->screen[i]->buf +
330                      screen_list->screen[i]->total, 1024);
331
332            if(nr > 0)
333            {
334                screen_list->screen[i]->total += nr;
335                continue;
336            }
337
338            if(nr == 0 || errno != EWOULDBLOCK) {
339                destroy_screen(screen_list->screen[i]);
340                remove_screen(screen_list, i, 0);
341                if(i < screen_list->prevpty) screen_list->prevpty--;
342                if(i == screen_list->pty)
343                {
344                    screen_list->pty = screen_list->prevpty;
345                    screen_list->prevpty = 0;
346                }
347                if(i < (screen_list->pty)) (screen_list->pty)--;
348                /* Don't skip the element which is now at position i */
349                i--;
350                refresh = 1;
351            }
352
353            break;
354        }
355    }
356    }
357    return refresh;
358}
359
360
Note: See TracBrowser for help on using the repository browser.