~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to imagery/i.ortho.photo/i.photo.2target/curses.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <curses.h>
2
 
#include <stdlib.h>
3
 
#include <unistd.h>
4
 
#include <grass/gis.h>
5
 
#include <grass/glocale.h>
6
 
#include "globals.h"
7
 
#include "local_proto.h"
8
 
 
9
 
static int inited = 0;
10
 
 
11
 
static WINDOW *save;
12
 
WINDOW *newwin();
13
 
 
14
 
 
15
 
static Window *make_window(int top, int bottom, int left, int right)
16
 
{
17
 
    Window *window;
18
 
 
19
 
    if (top < 0 || bottom >= LINES || left < 0 || right >= COLS
20
 
        || bottom - top <= 1 || right - left <= 1) {
21
 
        End_curses();
22
 
        G_warning(_("make_window(%d,%d,%d,%d): illegal screen values."),
23
 
                  top, bottom, left, right);
24
 
        G_sleep(3);
25
 
        exit(1);
26
 
    }
27
 
    window = (Window *) G_malloc(sizeof(Window));
28
 
    window->top = top;
29
 
    window->bottom = bottom;
30
 
    window->left = left;
31
 
    window->right = right;
32
 
    Curses_clear_window(window);
33
 
    return window;
34
 
}
35
 
 
36
 
int Begin_curses(void)
37
 
{
38
 
    /* should only be called once at program outset */
39
 
 
40
 
    initscr();                  /* initialize curses standard screens   */
41
 
    raw();                      /* set tty modes via curses calls       */
42
 
    noecho();
43
 
    nonl();
44
 
 
45
 
    inited = 1;
46
 
 
47
 
    /* make a window to save stdscr */
48
 
    save = newwin(LINES, COLS, 0, 0);
49
 
 
50
 
    /* make_window (nrows, ncols, start_row, start_col) */
51
 
    INFO_WINDOW = make_window(0, LINES - 4, COLS / 2, COLS - 1);
52
 
    MENU_WINDOW = make_window(0, LINES - 4, 0, COLS / 2);
53
 
    PROMPT_WINDOW = make_window(LINES - 4, LINES - 1, 0, COLS - 1);
54
 
    refresh();
55
 
 
56
 
    return 0;
57
 
}
58
 
 
59
 
int End_curses(void)
60
 
{
61
 
    /* should only be called upon program exit */
62
 
 
63
 
    clear();                    /* clear the screen */
64
 
    refresh();
65
 
    endwin();                   /* let curses reset the tty now */
66
 
    return 0;
67
 
}
68
 
 
69
 
int Suspend_curses(void)
70
 
{
71
 
    overwrite(stdscr, save);
72
 
    clear();
73
 
    refresh();
74
 
    endwin();
75
 
 
76
 
    return 0;
77
 
}
78
 
 
79
 
int Resume_curses(void)
80
 
{
81
 
    clear();
82
 
    refresh();
83
 
    overwrite(save, stdscr);
84
 
    refresh();
85
 
    return 0;
86
 
}
87
 
 
88
 
int Curses_allow_interrupts(int ok)
89
 
{
90
 
    refresh();
91
 
    if (ok)
92
 
        noraw();
93
 
    else
94
 
        raw();
95
 
    return 0;
96
 
}
97
 
 
98
 
int Curses_clear_window(Window * window)
99
 
{
100
 
    int y, x;
101
 
 
102
 
    if (!inited)
103
 
        return 1;
104
 
    for (y = window->top + 1; y < window->bottom; y++) {
105
 
        move(y, x = window->left + 1);
106
 
        while (x++ < window->right)
107
 
            addch(' ');
108
 
    }
109
 
    Curses_outline_window(window);
110
 
    refresh();
111
 
    return 0;
112
 
}
113
 
 
114
 
int Curses_outline_window(Window * window)
115
 
{
116
 
    int x, y;
117
 
 
118
 
    move(window->top, x = window->left + 1);
119
 
    while (x++ < window->right)
120
 
        addch('-');
121
 
    move(window->bottom, x = window->left + 1);
122
 
    while (x++ < window->right)
123
 
        addch('-');
124
 
    for (y = window->top + 1; y < window->bottom; y++) {
125
 
        move(y, window->left);
126
 
        addch('|');
127
 
        move(y, window->right);
128
 
        addch('|');
129
 
    }
130
 
    move(window->top, window->left);
131
 
    addch('+');
132
 
    move(window->top, window->right);
133
 
    addch('+');
134
 
    move(window->bottom, window->left);
135
 
    addch('+');
136
 
    if (window->bottom < LINES - 1 || window->right < COLS - 1) {
137
 
        move(window->bottom, window->right);
138
 
        addch('+');
139
 
    }
140
 
    return 0;
141
 
}
142
 
 
143
 
int Curses_write_window(Window * window, int line, int col, char *message)
144
 
{
145
 
    int y, x, i;
146
 
 
147
 
    if (!inited) {
148
 
        G_message(_("%s"), message);
149
 
        return 1;
150
 
    }
151
 
    if (line <= 0 || line >= window->bottom - window->top)
152
 
        return 1;
153
 
    if (col <= 0 || col >= window->right - window->left)
154
 
        return 1;
155
 
    move(y = window->top + line, x = window->left + col);
156
 
    while (*message != 0 && *message != '\n' && x < window->right) {
157
 
        addch(*message);
158
 
        message++;
159
 
        x++;
160
 
    }
161
 
    if (*message == '\n')
162
 
        for (i = x; i < window->right; i++)
163
 
            addch(' ');
164
 
    move(y, x);
165
 
    refresh();
166
 
    return 0;
167
 
}
168
 
 
169
 
 
170
 
int Curses_replot_screen(void)
171
 
{
172
 
    int x, y;
173
 
 
174
 
    getyx(stdscr, y, x);
175
 
    wrefresh(curscr);
176
 
    move(y, x);
177
 
    refresh();
178
 
    return 0;
179
 
}
180
 
 
181
 
int Curses_prompt_gets(char *prompt, char *answer)
182
 
{
183
 
    char c;
184
 
    int n;
185
 
    int y, x;
186
 
 
187
 
    *answer = 0;
188
 
    n = 0;
189
 
 
190
 
    Curses_write_window(PROMPT_WINDOW, 1, 1, "\n");
191
 
    Curses_write_window(PROMPT_WINDOW, 1, 1, prompt);
192
 
 
193
 
    for (;;) {
194
 
        refresh();
195
 
        c = Curses_getch(0);
196
 
        if (c == '\n' || c == '\r')
197
 
            break;
198
 
 
199
 
        getyx(stdscr, y, x);
200
 
        if (c > '\037' && c < '\177') { /* octal codes: accept space to '~' */
201
 
            if (x < PROMPT_WINDOW->right) {
202
 
                *answer++ = c;
203
 
                *answer = 0;
204
 
                addch(c);
205
 
                n++;
206
 
            }
207
 
            continue;
208
 
        }
209
 
        if (c == '\b' || c == '\177') { /* backspace or DEL (decimal 8,127) */
210
 
            if (n > 0) {
211
 
                answer--;
212
 
                *answer = 0;
213
 
                move(y, x - 1);
214
 
                addch(' ');
215
 
                move(y, x - 1);
216
 
                n--;
217
 
            }
218
 
            continue;
219
 
        }
220
 
        Beep();
221
 
    }
222
 
    return 0;
223
 
}
224
 
 
225
 
int Beep(void)
226
 
{
227
 
    putchar('\7');
228
 
    fflush(stdout);
229
 
    return 0;
230
 
}
231
 
 
232
 
int Curses_getch(int with_echo)
233
 
{
234
 
    char achar;
235
 
    int c;
236
 
    int kill;
237
 
 
238
 
    if (!inited)
239
 
        return 0;
240
 
    kill = 0;
241
 
    while (1) {
242
 
        c = getch() & 0177;
243
 
        if (c == interrupt_char) {
244
 
            if (kill++ >= 3) {
245
 
                End_curses();
246
 
                exit(0);
247
 
            }
248
 
            continue;
249
 
        }
250
 
        kill = 0;
251
 
        if (c != 18)
252
 
            break;
253
 
        Curses_replot_screen();
254
 
    }
255
 
    if (with_echo) {
256
 
        achar = c;
257
 
        addch(achar);
258
 
        refresh();
259
 
    }
260
 
    return c;
261
 
}