OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [char/] [selection.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * linux/drivers/char/selection.c
3
 *
4
 * This module exports the functions:
5
 *
6
 *     'int set_selection(const unsigned long arg)'
7
 *     'void clear_selection(void)'
8
 *     'int paste_selection(struct tty_struct *tty)'
9
 *     'int sel_loadlut(const unsigned long arg)'
10
 *
11
 * Now that /dev/vcs exists, most of this can disappear again.
12
 */
13
 
14
#include <linux/tty.h>
15
#include <linux/sched.h>
16
#include <linux/mm.h>
17
#include <linux/malloc.h>
18
#include <linux/types.h>
19
 
20
#include <asm/segment.h>
21
 
22
#include "vt_kern.h"
23
#include "consolemap.h"
24
#include "selection.h"
25
 
26
#ifndef MIN
27
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
28
#endif
29
 
30
/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31
#define isspace(c)      ((c) == ' ')
32
 
33
/* Variables for selection control. */
34
/* Use a dynamic buffer, instead of static (Dec 1994) */
35
       int sel_cons = 0;         /* must not be disallocated */
36
static volatile int sel_start = -1;     /* cleared by clear_selection */
37
static int sel_end;
38
static int sel_buffer_lth = 0;
39
static char *sel_buffer = NULL;
40
 
41
#define sel_pos(n)   inverse_translate(scrw2glyph(screen_word(sel_cons, n, 1)))
42
 
43
/* clear_selection, highlight and highlight_pointer can be called
44
   from interrupt (via scrollback/front) */
45
 
46
/* set reverse video on characters s-e of console with selection. */
47
inline static void
48
highlight(const int s, const int e) {
49
        invert_screen(sel_cons, s, e-s+2, 1);
50
}
51
 
52
/* use complementary color to show the pointer */
53
inline static void
54
highlight_pointer(const int where) {
55
        complement_pos(sel_cons, where);
56
}
57
 
58
/* remove the current selection highlight, if any,
59
   from the console holding the selection. */
60
void
61
clear_selection(void) {
62
        highlight_pointer(-1); /* hide the pointer */
63
        if (sel_start != -1) {
64
                highlight(sel_start, sel_end);
65
                sel_start = -1;
66
        }
67
}
68
 
69
/*
70
 * User settable table: what characters are to be considered alphabetic?
71
 * 256 bits
72
 */
73
static u32 inwordLut[8]={
74
  0x00000000, /* control chars     */
75
  0x03FF0000, /* digits            */
76
  0x87FFFFFE, /* uppercase and '_' */
77
  0x07FFFFFE, /* lowercase         */
78
  0x00000000,
79
  0x00000000,
80
  0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
81
  0xFF7FFFFF  /* latin-1 accented letters, not division sign */
82
};
83
 
84
static inline int inword(const unsigned char c) {
85
        return ( inwordLut[c>>5] >> (c & 0x1F) ) & 1;
86
}
87
 
88
/* set inwordLut contents. Invoked by ioctl(). */
89
int sel_loadlut(const unsigned long arg)
90
{
91
        int i = verify_area(VERIFY_READ, (char *) arg, 36);
92
        if (i)
93
                return i;
94
        memcpy_fromfs(inwordLut, (u32 *)(arg+4), 32);
95
        return 0;
96
}
97
 
98
/* does screen address p correspond to character at LH/RH edge of screen? */
99
static inline int atedge(const int p)
100
{
101
        return (!(p % video_size_row) || !((p + 2) % video_size_row));
102
}
103
 
104
/* constrain v such that v <= u */
105
static inline unsigned short limit(const unsigned short v, const unsigned short u)
106
{
107
/* gcc miscompiles the ?: operator, so don't use it.. */
108
        if (v > u)
109
                return u;
110
        return v;
111
}
112
 
113
/* set the current selection. Invoked by ioctl() or by kernel code. */
114
int set_selection(const unsigned long arg, struct tty_struct *tty, int user)
115
{
116
        int sel_mode, new_sel_start, new_sel_end, spc;
117
        char *bp, *obp;
118
        int i, ps, pe;
119
 
120
        do_unblank_screen();
121
 
122
        { unsigned short *args, xs, ys, xe, ye;
123
 
124
          args = (unsigned short *)(arg + 1);
125
          if (user) {
126
                  int err;
127
                  err = verify_area(VERIFY_READ, args, sizeof(short) * 5);
128
                  if (err)
129
                        return err;
130
                  xs = get_user(args++) - 1;
131
                  ys = get_user(args++) - 1;
132
                  xe = get_user(args++) - 1;
133
                  ye = get_user(args++) - 1;
134
                  sel_mode = get_user(args);
135
          } else {
136
                  xs = *(args++) - 1; /* set selection from kernel */
137
                  ys = *(args++) - 1;
138
                  xe = *(args++) - 1;
139
                  ye = *(args++) - 1;
140
                  sel_mode = *args;
141
          }
142
          xs = limit(xs, video_num_columns - 1);
143
          ys = limit(ys, video_num_lines - 1);
144
          xe = limit(xe, video_num_columns - 1);
145
          ye = limit(ye, video_num_lines - 1);
146
          ps = ys * video_size_row + (xs << 1);
147
          pe = ye * video_size_row + (xe << 1);
148
 
149
          if (sel_mode == 4) {
150
              /* useful for screendump without selection highlights */
151
              clear_selection();
152
              return 0;
153
          }
154
 
155
          if (mouse_reporting() && (sel_mode & 16)) {
156
              mouse_report(tty, sel_mode & 15, xs, ys);
157
              return 0;
158
          }
159
        }
160
 
161
        if (ps > pe)    /* make sel_start <= sel_end */
162
        {
163
                int tmp = ps;
164
                ps = pe;
165
                pe = tmp;
166
        }
167
 
168
        if (sel_cons != fg_console) {
169
                clear_selection();
170
                sel_cons = fg_console;
171
        }
172
 
173
        switch (sel_mode)
174
        {
175
                case 0:  /* character-by-character selection */
176
                        new_sel_start = ps;
177
                        new_sel_end = pe;
178
                        break;
179
                case 1: /* word-by-word selection */
180
                        spc = isspace(sel_pos(ps));
181
                        for (new_sel_start = ps; ; ps -= 2)
182
                        {
183
                                if ((spc && !isspace(sel_pos(ps))) ||
184
                                    (!spc && !inword(sel_pos(ps))))
185
                                        break;
186
                                new_sel_start = ps;
187
                                if (!(ps % video_size_row))
188
                                        break;
189
                        }
190
                        spc = isspace(sel_pos(pe));
191
                        for (new_sel_end = pe; ; pe += 2)
192
                        {
193
                                if ((spc && !isspace(sel_pos(pe))) ||
194
                                    (!spc && !inword(sel_pos(pe))))
195
                                        break;
196
                                new_sel_end = pe;
197
                                if (!((pe + 2) % video_size_row))
198
                                        break;
199
                        }
200
                        break;
201
                case 2: /* line-by-line selection */
202
                        new_sel_start = ps - ps % video_size_row;
203
                        new_sel_end = pe + video_size_row
204
                                    - pe % video_size_row - 2;
205
                        break;
206
                case 3:
207
                        highlight_pointer(pe);
208
                        return 0;
209
                default:
210
                        return -EINVAL;
211
        }
212
 
213
        /* remove the pointer */
214
        highlight_pointer(-1);
215
 
216
        /* select to end of line if on trailing space */
217
        if (new_sel_end > new_sel_start &&
218
                !atedge(new_sel_end) && isspace(sel_pos(new_sel_end))) {
219
                for (pe = new_sel_end + 2; ; pe += 2)
220
                        if (!isspace(sel_pos(pe)) || atedge(pe))
221
                                break;
222
                if (isspace(sel_pos(pe)))
223
                        new_sel_end = pe;
224
        }
225
        if (sel_start == -1)    /* no current selection */
226
                highlight(new_sel_start, new_sel_end);
227
        else if (new_sel_start == sel_start)
228
        {
229
                if (new_sel_end == sel_end)     /* no action required */
230
                        return 0;
231
                else if (new_sel_end > sel_end) /* extend to right */
232
                        highlight(sel_end + 2, new_sel_end);
233
                else                            /* contract from right */
234
                        highlight(new_sel_end + 2, sel_end);
235
        }
236
        else if (new_sel_end == sel_end)
237
        {
238
                if (new_sel_start < sel_start)  /* extend to left */
239
                        highlight(new_sel_start, sel_start - 2);
240
                else                            /* contract from left */
241
                        highlight(sel_start, new_sel_start - 2);
242
        }
243
        else    /* some other case; start selection from scratch */
244
        {
245
                clear_selection();
246
                highlight(new_sel_start, new_sel_end);
247
        }
248
        sel_start = new_sel_start;
249
        sel_end = new_sel_end;
250
 
251
        if (sel_buffer)
252
                kfree(sel_buffer);
253
        sel_buffer = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
254
        if (!sel_buffer) {
255
                printk("selection: kmalloc() failed\n");
256
                clear_selection();
257
                return -ENOMEM;
258
        }
259
 
260
        obp = bp = sel_buffer;
261
        for (i = sel_start; i <= sel_end; i += 2) {
262
                *bp = sel_pos(i);
263
                if (!isspace(*bp++))
264
                        obp = bp;
265
                if (! ((i + 2) % video_size_row)) {
266
                        /* strip trailing blanks from line and add newline,
267
                           unless non-space at end of line. */
268
                        if (obp != bp) {
269
                                bp = obp;
270
                                *bp++ = '\r';
271
                        }
272
                        obp = bp;
273
                }
274
        }
275
        sel_buffer_lth = bp - sel_buffer;
276
        return 0;
277
}
278
 
279
/* Insert the contents of the selection buffer into the queue of the
280
   tty associated with the current console. Invoked by ioctl(). */
281
int paste_selection(struct tty_struct *tty)
282
{
283
        struct wait_queue wait = { current, NULL };
284
        char    *bp = sel_buffer;
285
        int     c = sel_buffer_lth;
286
        int     l;
287
        struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
288
 
289
        if (!bp || !c)
290
                return 0;
291
        do_unblank_screen();
292
        add_wait_queue(&vt->paste_wait, &wait);
293
        do {
294
                current->state = TASK_INTERRUPTIBLE;
295
                if (test_bit(TTY_THROTTLED, &tty->flags)) {
296
                        schedule();
297
                        continue;
298
                }
299
                l = MIN(c, tty->ldisc.receive_room(tty));
300
                tty->ldisc.receive_buf(tty, bp, 0, l);
301
                c -= l;
302
                bp += l;
303
        } while (c);
304
        remove_wait_queue(&vt->paste_wait, &wait);
305
        current->state = TASK_RUNNING;
306
        return 0;
307
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.