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

Last change on this file since 2639 was 2639, checked in by Jean-Yves Lamoureux, 13 years ago
  • We can now choose current window with ctrl-a-N where N is 0-9
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
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(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  = cucul_get_canvas_width(screen_list->cv);
185    screen_list->height = cucul_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    cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_DEFAULT);
191    cucul_clear_canvas(screen_list->cv);
192    cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTRED, CUCUL_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            cucul_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            cucul_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                cucul_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    cucul_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        cucul_set_color_ansi(screen_list->cv, CUCUL_RED, CUCUL_BLACK);
226        screen_list->screen[screen_list->pty]->bell = 0;
227        screen_list->in_bell--;
228    }
229    else
230    {
231        cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTGREEN, CUCUL_BLACK);
232    }
233
234    cucul_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        cucul_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    cucul_gotoxy(screen_list->cv,
251                 screen_list->screen[screen_list->pty]->x +
252                 cucul_get_cursor_x(screen_list->screen[screen_list->pty]->cv),
253                 screen_list->screen[screen_list->pty]->y +
254                 cucul_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}
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            {
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.