curses 1.1.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29be4733e95282f8d6b539903b8640797f272528
4
+ data.tar.gz: d5b25740572cf05ffd25adf75067eda26c553093
5
+ SHA512:
6
+ metadata.gz: 1935977c94e0df65ec9cc273de40895dad738a92ce2f1ebf60674f296724c04a4f46417c9b129257e056b4188f6a81b01ad0d08d957369e9498ecc610e38a003
7
+ data.tar.gz: 73de278ce9f4879414cdfea20cc99761335ca7b21222e532e64fbf7b6750d07f231bf36f140906a196fbdeab1d5dadb1b912f21892269eb98926153b677ca0e1
data/BSDL ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
+ SUCH DAMAGE.
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # curses
2
+
3
+ [![Build Status](https://travis-ci.org/ruby/curses.svg?branch=master)](https://travis-ci.org/ruby/curses)
4
+ [![Build status](https://ci.appveyor.com/api/projects/status/kdvksgjo4fyd3c4m/branch/master?svg=true)](https://ci.appveyor.com/project/ruby/curses/branch/master)
5
+
6
+ * https://github.com/ruby/curses
7
+ * https://github.com/ruby/curses/issues
8
+
9
+ ## Description
10
+
11
+ A Ruby binding for curses, ncurses, and PDCurses.
12
+ curses is an extension library for text UI applications.
13
+
14
+ Formerly part of the Ruby standard library, [curses was removed and placed in this gem][1]
15
+ with the release of Ruby 2.1.0. (see [ruby/ruby@9c5b2fd][2])
16
+
17
+ ## Install
18
+
19
+ $ gem install curses
20
+
21
+ ## Developers
22
+
23
+ After checking out the repo, run `bundle install` to install dependencies.
24
+
25
+ To compile the extension library, run `bundle exec rake compile`.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `curses.gemspec`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ### Cross compilation for Windows on Debian GNU/Linux based platforms
30
+
31
+ 1. Install development environment fo 32- and 64-bit Windows.
32
+
33
+ $ sudo apt-get install mingw-w64
34
+
35
+ 2. Install rake-compiler.
36
+
37
+ $ gem install rake-compiler
38
+
39
+ 3. Compile multiple versions of Ruby.
40
+
41
+ $ rake-compiler cross-ruby HOST=i686-w64-mingw32 VERSION=2.2.6
42
+ $ rake-compiler cross-ruby HOST=i686-w64-mingw32 VERSION=2.3.3
43
+ $ rake-compiler cross-ruby HOST=i686-w64-mingw32 VERSION=2.4.0
44
+ $ rake-compiler cross-ruby HOST=x86_64-w64-mingw32 VERSION=2.2.6
45
+ $ rake-compiler cross-ruby HOST=x86_64-w64-mingw32 VERSION=2.3.3
46
+ $ rake-compiler cross-ruby HOST=x86_64-w64-mingw32 VERSION=2.4.0
47
+
48
+ 3. Compile PDCurses.
49
+
50
+ $ rake build:pdcurses
51
+
52
+ 5. Compile curses.gem.
53
+
54
+ $ rake RUBY_CC_VERSION=2.3.3:2.4.0 cross clean compile native gem
55
+
56
+ Binary gems are generated in pkg/.
57
+
58
+ ## License
59
+
60
+ curses is released under the Ruby and 2-clause BSD licenses. See COPYING for
61
+ details.
62
+
63
+ Binary gems for mingw32 include a forked version of PDCurses, which is in
64
+ the public domain:
65
+
66
+ https://github.com/Bill-Gray/PDCurses
67
+
68
+ The version for Win32 console mode in the win32 subdirectory is used.
69
+
70
+ [1]: https://bugs.ruby-lang.org/issues/8584
71
+ [2]: https://github.com/ruby/ruby/commit/9c5b2fd8aa0fd343ad094d47a638cfd3f6ae0a81
<
@@ -0,0 +1,4618 @@
1
+ /* -*- C -*-
2
+ * $Id$
3
+ *
4
+ * ext/curses/curses.c
5
+ *
6
+ * by MAEDA Shugo (ender@pic-internet.or.jp)
7
+ * modified by Yukihiro Matsumoto (matz@netlab.co.jp),
8
+ * Toki Yoshinori,
9
+ * Hitoshi Takahashi,
10
+ * and Takaaki Tateishi (ttate@kt.jaist.ac.jp)
11
+ *
12
+ * maintainers:
13
+ * - Takaaki Tateishi (ttate@kt.jaist.ac.jp)
14
+ *
15
+ * documentation:
16
+ * - Vincent Batts (vbatts@hashbangbash.com)
17
+ */
18
+
19
+ #include "ruby.h"
20
+ #include "ruby/io.h"
21
+ #include "ruby/thread.h"
22
+
23
+ #if defined(HAVE_NCURSES_H)
24
+ # include <ncurses.h>
25
+ #elif defined(HAVE_NCURSESW_CURSES_H)
26
+ # include <ncursesw/curses.h>
27
+ #elif defined(HAVE_NCURSES_CURSES_H)
28
+ # include <ncurses/curses.h>
29
+ #elif defined(HAVE_CURSES_COLR_CURSES_H)
30
+ # ifdef HAVE_STDARG_PROTOTYPES
31
+ # include <stdarg.h>
32
+ # else
33
+ # include <varargs.h>
34
+ # endif
35
+ # include <curses_colr/curses.h>
36
+ #else
37
+ # include <curses.h>
38
+ # if defined(__bsdi__) || defined(__NetBSD__) || defined(__APPLE__)
39
+ # if !defined(_maxx)
40
+ # define _maxx maxx
41
+ # endif
42
+ # if !defined(_maxy)
43
+ # define _maxy maxy
44
+ # endif
45
+ # if !defined(_begx)
46
+ # define _begx begx
47
+ # endif
48
+ # if !defined(_begy)
49
+ # define _begy begy
50
+ # endif
51
+ # endif
52
+ #endif
53
+
54
+ #ifdef HAVE_INIT_COLOR
55
+ # define USE_COLOR 1
56
+ #endif
57
+
58
+ /* supports only ncurses mouse routines */
59
+ #ifdef NCURSES_MOUSE_VERSION
60
+ # define USE_MOUSE 1
61
+ #endif
62
+
63
+ #define NUM2CH NUM2CHR
64
+ #define CH2FIX CHR2FIX
65
+
66
+ static VALUE mCurses;
67
+ static VALUE mKey;
68
+ static VALUE cWindow;
69
+ static VALUE cPad;
70
+ #ifdef USE_MOUSE
71
+ static VALUE cMouseEvent;
72
+ #endif
73
+
74
+ static VALUE rb_stdscr;
75
+
76
+ static rb_encoding *keyboard_encoding;
77
+ static rb_encoding *terminal_encoding;
78
+
79
+ struct windata {
80
+ WINDOW *window;
81
+ };
82
+
83
+ static VALUE window_attroff(VALUE obj, VALUE attrs);
84
+ static VALUE window_attron(VALUE obj, VALUE attrs);
85
+ static VALUE window_attrset(VALUE obj, VALUE attrs);
86
+
87
+ static void
88
+ no_window(void)
89
+ {
90
+ rb_raise(rb_eRuntimeError, "already closed window");
91
+ }
92
+
93
+ #define GetWINDOW(obj, winp) do {\
94
+ if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)\
95
+ rb_raise(rb_eSecurityError, "Insecure: operation on untainted window");\
96
+ TypedData_Get_Struct((obj), struct windata, &windata_type, (winp));\
97
+ if ((winp)->window == 0) no_window();\
98
+ } while (0)
99
+
100
+ static void
101
+ window_free(void *p)
102
+ {
103
+ struct windata *winp = p;
104
+ if (winp->window && winp->window != stdscr) delwin(winp->window);
105
+ winp->window = 0;
106
+ xfree(winp);
107
+ }
108
+
109
+ static size_t
110
+ window_memsize(const void *p)
111
+ {
112
+ const struct windata *winp = p;
113
+ size_t size = sizeof(*winp);
114
+ if (!winp) return 0;
115
+ if (winp->window && winp->window != stdscr) size += sizeof(winp->window);
116
+ return size;
117
+ }
118
+
119
+ static const rb_data_type_t windata_type = {
120
+ "windata",
121
+ {0, window_free, window_memsize,}
122
+ };
123
+
124
+ static VALUE
125
+ prep_window(VALUE class, WINDOW *window)
126
+ {
127
+ VALUE obj;
128
+ struct windata *winp;
129
+
130
+ if (window == NULL) {
131
+ rb_raise(rb_eRuntimeError, "failed to create window");
132
+ }
133
+
134
+ obj = rb_obj_alloc(class);
135
+ TypedData_Get_Struct(obj, struct windata, &windata_type, winp);
136
+ winp->window = window;
137
+
138
+ return obj;
139
+ }
140
+
141
+ /*-------------------------- module Curses --------------------------*/
142
+
143
+ /*
144
+ * Document-method: Curses.init_screen
145
+ *
146
+ * Initialize a standard screen
147
+ *
148
+ * see also Curses.stdscr
149
+ */
150
+ static VALUE
151
+ curses_init_screen(void)
152
+ {
153
+ if (rb_stdscr) return rb_stdscr;
154
+ initscr();
155
+ if (stdscr == 0) {
156
+ rb_raise(rb_eRuntimeError, "can't initialize curses");
157
+ }
158
+ clear();
159
+ rb_stdscr = prep_window(cWindow, stdscr);
160
+ return rb_stdscr;
161
+ }
162
+
163
+ /*
164
+ * Document-method: Curses.stdscr
165
+ *
166
+ * The Standard Screen.
167
+ *
168
+ * Upon initializing curses, a default window called stdscr,
169
+ * which is the size of the terminal screen, is created.
170
+ *
171
+ * Many curses functions use this window.
172
+ */
173
+ #define curses_stdscr curses_init_screen
174
+
175
+ /*
176
+ * Document-method: Curses.close_screen
177
+ *
178
+ * A program should always call Curses.close_screen before exiting or
179
+ * escaping from curses mode temporarily. This routine
180
+ * restores tty modes, moves the cursor to the lower
181
+ * left-hand corner of the screen and resets the terminal
182
+ * into the proper non-visual mode.
183
+ *
184
+ * Calling Curses.refresh or Curses.doupdate after a temporary
185
+ * escape causes the program to resume visual mode.
186
+ *
187
+ */
188
+ static VALUE
189
+ curses_close_screen(void)
190
+ {
191
+ curses_stdscr();
192
+ #ifdef HAVE_ISENDWIN
193
+ if (!isendwin())
194
+ #endif
195
+ endwin();
196
+ rb_stdscr = 0;
197
+ return Qnil;
198
+ }
199
+
200
+ /*
201
+ * This is no runtime method,
202
+ * but a function called before the proc ends
203
+ *
204
+ * Similar to Curses.close_screen, except that it also
205
+ * garbage collects/unregisters the Curses.stdscr
206
+ */
207
+ static void
208
+ curses_finalize(VALUE dummy)
209
+ {
210
+ if (stdscr
211
+ #ifdef HAVE_ISENDWIN
212
+ && !isendwin()
213
+ #endif
214
+ )
215
+ endwin();
216
+ rb_stdscr = 0;
217
+ rb_gc_unregister_address(&rb_stdscr);
218
+ }
219
+
220
+ #ifdef HAVE_ISENDWIN
221
+ /*
222
+ * Document-method: Curses.closed?
223
+ *
224
+ * Returns +true+ if the window/screen has been closed,
225
+ * without any subsequent Curses.refresh calls,
226
+ * returns +false+ otherwise.
227
+ */
228
+ static VALUE
229
+ curses_closed(void)
230
+ {
231
+ curses_stdscr();
232
+ if (isendwin()) {
233
+ return Qtrue;
234
+ }
235
+ return Qfalse;
236
+ }
237
+ #else
238
+ #define curses_closed rb_f_notimplement
239
+ #endif
240
+
241
+ /*
242
+ * Document-method: Curses.clear
243
+ *
244
+ * Clears every position on the screen completely,
245
+ * so that a subsequent call by Curses.refresh for the screen/window
246
+ * will be repainted from scratch.
247
+ */
248
+ static VALUE
249
+ curses_clear(VALUE obj)
250
+ {
251
+ curses_stdscr();
252
+ wclear(stdscr);
253
+ return Qnil;
254
+ }
255
+
256
+ #ifdef HAVE_WERASE
257
+ /*
258
+ * Document-method: Curses.erase
259
+ *
260
+ * Erase the screen.
261
+ */
262
+ static VALUE
263
+ curses_erase(VALUE obj)
264
+ {
265
+ curses_stdscr();
266
+ werase(stdscr);
267
+ return Qnil;
268
+ }
269
+ #else
270
+ #define curses_erase rb_f_notimplement
271
+ #endif
272
+
273
+ /*
274
+ * Document-method: Curses.clrtoeol
275
+ *
276
+ * Clears to the end of line, that the cursor is currently on.
277
+ */
278
+ static VALUE
279
+ curses_clrtoeol(void)
280
+ {
281
+ curses_stdscr();
282
+ clrtoeol();
283
+ return Qnil;
284
+ }
285
+
286
+ /*
287
+ * Document-method: Curses.refresh
288
+ *
289
+ * Refreshes the windows and lines.
290
+ *
291
+ */
292
+ static VALUE
293
+ curses_refresh(VALUE obj)
294
+ {
295
+ curses_stdscr();
296
+ refresh();
297
+ return Qnil;
298
+ }
299
+
300
+ /*
301
+ * Document-method: Curses.doupdate
302
+ *
303
+ * Refreshes the windows and lines.
304
+ *
305
+ * Curses.doupdate allows multiple updates with
306
+ * more efficiency than Curses.refresh alone.
307
+ */
308
+ static VALUE
309
+ curses_doupdate(VALUE obj)
310
+ {
311
+ curses_stdscr();
312
+ #ifdef HAVE_DOUPDATE
313
+ doupdate();
314
+ #else
315
+ refresh();
316
+ #endif
317
+ return Qnil;
318
+ }
319
+
320
+ /*
321
+ * Document-method: Curses.echo
322
+ *
323
+ * Enables characters typed by the user
324
+ * to be echoed by Curses.getch as they are typed.
325
+ */
326
+ static VALUE
327
+ curses_echo(VALUE obj)
328
+ {
329
+ curses_stdscr();
330
+ echo();
331
+ return Qnil;
332
+ }
333
+
334
+ /*
335
+ * Document-method: Curses.noecho
336
+ *
337
+ * Disables characters typed by the user
338
+ * to be echoed by Curses.getch as they are typed.
339
+ */
340
+ static VALUE
341
+ curses_noecho(VALUE obj)
342
+ {
343
+ curses_stdscr();
344
+ noecho();
345
+ return Qnil;
346
+ }
347
+
348
+ /*
349
+ * Document-method: Curses.raw
350
+ *
351
+ * Put the terminal into raw mode.
352
+ *
353
+ * Raw mode is similar to Curses.cbreak mode, in that characters typed
354
+ * are immediately passed through to the user program.
355
+ *
356
+ * The differences are that in raw mode, the interrupt, quit,
357
+ * suspend, and flow control characters are all passed through
358
+ * uninterpreted, instead of generating a signal. The behavior
359
+ * of the BREAK key depends on other bits in the tty driver
360
+ * that are not set by curses.
361
+ */
362
+ static VALUE
363
+ curses_raw(VALUE obj)
364
+ {
365
+ curses_stdscr();
366
+ raw();
367
+ return Qnil;
368
+ }
369
+
370
+ /*
371
+ * Document-method: Curses.noraw
372
+ *
373
+ * Put the terminal out of raw mode.
374
+ *
375
+ * see Curses.raw for more detail
376
+ */
377
+ static VALUE
378
+ curses_noraw(VALUE obj)
379
+ {
380
+ curses_stdscr();
381
+ noraw();
382
+ return Qnil;
383
+ }
384
+
385
+ /*
386
+ * Document-method: Curses.cbreak
387
+ *
388
+ * Put the terminal into cbreak mode.
389
+ *
390
+ * Normally, the tty driver buffers typed characters until
391
+ * a newline or carriage return is typed. The Curses.cbreak
392
+ * routine disables line buffering and erase/kill
393
+ * character-processing (interrupt and flow control characters
394
+ * are unaffected), making characters typed by the user
395
+ * immediately available to the program.
396
+ *
397
+ * The Curses.nocbreak routine returns the terminal to normal (cooked) mode.
398
+ *
399
+ * Initially the terminal may or may not be in cbreak mode,
400
+ * as the mode is inherited; therefore, a program should
401
+ * call Curses.cbreak or Curses.nocbreak explicitly.
402
+ * Most interactive programs using curses set the cbreak mode.
403
+ * Note that Curses.cbreak overrides Curses.raw.
404
+ *
405
+ * see also Curses.raw
406
+ */
407
+ static VALUE
408
+ curses_cbreak(VALUE obj)
409
+ {
410
+ curses_stdscr();
411
+ cbreak();
412
+ return Qnil;
413
+ }
414
+
415
+ /*
416
+ * Document-method: Curses.nocbreak
417
+ *
418
+ * Put the terminal into normal mode (out of cbreak mode).
419
+ *
420
+ * See Curses.cbreak for more detail.
421
+ */
422
+ static VALUE
423
+ curses_nocbreak(VALUE obj)
424
+ {
425
+ curses_stdscr();
426
+ nocbreak();
427
+ return Qnil;
428
+ }
429
+
430
+ /*
431
+ * Document-method: Curses.nl
432
+ *
433
+ * Enable the underlying display device to translate
434
+ * the return key into newline on input, and whether it
435
+ * translates newline into return and line-feed on output
436
+ * (in either case, the call Curses.addch('\n') does the
437
+ * equivalent of return and line feed on the virtual screen).
438
+ *
439
+ * Initially, these translations do occur. If you disable
440
+ * them using Curses.nonl, curses will be able to make better use
441
+ * of the line-feed capability, resulting in faster cursor
442
+ * motion. Also, curses will then be able to detect the return key.
443
+ */
444
+ static VALUE
445
+ curses_nl(VALUE obj)
446
+ {
447
+ curses_stdscr();
448
+ nl();
449
+ return Qnil;
450
+ }
451
+
452
+ /*
453
+ * Document-method: Curses.nl
454
+ *
455
+ * Disable the underlying display device to translate
456
+ * the return key into newline on input
457
+ *
458
+ * See Curses.nl for more detail
459
+ */
460
+ static VALUE
461
+ curses_nonl(VALUE obj)
462
+ {
463
+ curses_stdscr();
464
+ nonl();
465
+ return Qnil;
466
+ }
467
+
468
+ /*
469
+ * Document-method: Curses.beep
470
+ *
471
+ * Sounds an audible alarm on the terminal, if possible;
472
+ * otherwise it flashes the screen (visual bell).
473
+ *
474
+ * see also Curses.flash
475
+ */
476
+ static VALUE
477
+ curses_beep(VALUE obj)
478
+ {
479
+ #ifdef HAVE_BEEP
480
+ curses_stdscr();
481
+ beep();
482
+ #endif
483
+ return Qnil;
484
+ }
485
+
486
+ /*
487
+ * Document-method: Curses.flash
488
+ *
489
+ * Flashes the screen, for visual alarm on the terminal, if possible;
490
+ * otherwise it sounds the alert.
491
+ *
492
+ * see also Curses.beep
493
+ */
494
+ static VALUE
495
+ curses_flash(VALUE obj)
496
+ {
497
+ #ifdef HAVE_FLASH
498
+ curses_stdscr();
499
+ flash();
500
+ #endif
501
+ return Qnil;
502
+ }
503
+
504
+ static int
505
+ curses_char(VALUE c)
506
+ {
507
+ if (FIXNUM_P(c)) {
508
+ return NUM2INT(c);
509
+ }
510
+ else {
511
+ int cc;
512
+
513
+ StringValue(c);
514
+ if (RSTRING_LEN(c) == 0 || RSTRING_LEN(c) > 1) {
515
+ rb_raise(rb_eArgError, "string not corresponding a character");
516
+ }
517
+ cc = RSTRING_PTR(c)[0];
518
+ if (cc > 0x7f) {
519
+ rb_raise(rb_eArgError, "no multibyte string supported (yet)");
520
+ }
521
+ return cc;
522
+ }
523
+ }
524
+
525
+ #ifdef HAVE_UNGETCH
526
+ /*
527
+ * Document-method: Curses.ungetch
528
+ * call-seq: ungetch(ch)
529
+ *
530
+ * Places +ch+ back onto the input queue to be returned by
531
+ * the next call to Curses.getch.
532
+ *
533
+ * There is just one input queue for all windows.
534
+ */
535
+ static VALUE
536
+ curses_ungetch(VALUE obj, VALUE ch)
537
+ {
538
+ int c = curses_char(ch);
539
+ curses_stdscr();
540
+ ungetch(c);
541
+ return Qnil;
542
+ }
543
+ #else
544
+ #define curses_ungetch rb_f_notimplement
545
+ #endif
546
+
547
+ /*
548
+ * Document-method: Curses.setpos
549
+ * call-seq: setpos(y, x)
550
+ *
551
+ * A setter for the position of the cursor,
552
+ * using coordinates +x+ and +y+
553
+ *
554
+ */
555
+ static VALUE
556
+ curses_setpos(VALUE obj, VALUE y, VALUE x)
557
+ {
558
+ curses_stdscr();
559
+ move(NUM2INT(y), NUM2INT(x));
560
+ return Qnil;
561
+ }
562
+
563
+ /*
564
+ * Document-method: Curses.standout
565
+ *
566
+ * Enables the best highlighting mode of the terminal.
567
+ *
568
+ * This is equivalent to Curses:Window.attron(A_STANDOUT)
569
+ *
570
+ * see also Curses::Window.attrset additional information
571
+ */
572
+ static VALUE
573
+ curses_standout(VALUE obj)
574
+ {
575
+ curses_stdscr();
576
+ standout();
577
+ return Qnil;
578
+ }
579
+
580
+ /*
581
+ * Document-method: Curses.standend
582
+ *
583
+ * Enables the Normal display (no highlight)
584
+ *
585
+ * This is equivalent to Curses.attron(A_NORMAL)
586
+ *
587
+ * see also Curses::Window.attrset for additional information.
588
+ */
589
+ static VALUE
590
+ curses_standend(VALUE obj)
591
+ {
592
+ curses_stdscr();
593
+ standend();
594
+ return Qnil;
595
+ }
596
+
597
+ /*
598
+ * Document-method: Curses.inch
599
+ *
600
+ * Returns the character at the current position.
601
+ */
602
+ static VALUE
603
+ curses_inch(VALUE obj)
604
+ {
605
+ curses_stdscr();
606
+ return CH2FIX(inch());
607
+ }
608
+
609
+ /*
610
+ * Document-method: Curses.addch
611
+ * call-seq: addch(ch)
612
+ *
613
+ * Add a character +ch+, with attributes, then advance the cursor.
614
+ *
615
+ * see also the system manual for curs_addch(3)
616
+ */
617
+ static VALUE
618
+ curses_addch(VALUE obj, VALUE ch)
619
+ {
620
+ curses_stdscr();
621
+ addch(NUM2CH(ch));
622
+ return Qnil;
623
+ }
624
+
625
+ /*
626
+ * Document-method: Curses.insch
627
+ * call-seq: insch(ch)
628
+ *
629
+ * Insert a character +ch+, before the cursor.
630
+ *
631
+ */
632
+ static VALUE
633
+ curses_insch(VALUE obj, VALUE ch)
634
+ {
635
+ curses_stdscr();
636
+ insch(NUM2CH(ch));
637
+ return Qnil;
638
+ }
639
+
640
+ /*
641
+ * Document-method: Curses.addstr
642
+ * call-seq: addstr(str)
643
+ *
644
+ * add a string of characters +str+, to the window and advance cursor
645
+ *
646
+ */
647
+ static VALUE
648
+ curses_addstr(VALUE obj, VALUE str)
649
+ {
650
+ StringValue(str);
651
+ str = rb_str_export_to_enc(str, terminal_encoding);
652
+ curses_stdscr();
653
+ if (!NIL_P(str)) {
654
+ addstr(StringValueCStr(str));
655
+ }
656
+ return Qnil;
657
+ }
658
+
659
+ static void *
660
+ getch_func(void *arg)
661
+ {
662
+ int *ip = (int *)arg;
663
+ *ip = getch();
664
+ return 0;
665
+ }
666
+
667
+ /*
668
+ * Document-method: Curses.getch
669
+ *
670
+ * Read and returns a character from the window.
671
+ *
672
+ * See Curses::Key to all the function KEY_* available
673
+ *
674
+ */
675
+ static VALUE
676
+ curses_getch(VALUE obj)
677
+ {
678
+ int c;
679
+
680
+ curses_stdscr();
681
+ rb_thread_call_without_gvl(getch_func, &c, RUBY_UBF_IO, 0);
682
+ if (c == EOF) return Qnil;
683
+ if (rb_isprint(c)) {
684
+ char ch = (char)c;
685
+
686
+ return rb_external_str_new_with_enc(&ch, 1, keyboard_encoding);
687
+ }
688
+ return UINT2NUM(c);
689
+ }
690
+
691
+ /* This should be big enough.. I hope */
692
+ #define GETSTR_BUF_SIZE 1024
693
+
694
+ static void *
695
+ getstr_func(void *arg)
696
+ {
697
+ char *rtn = (char *)arg;
698
+ #if defined(HAVE_GETNSTR)
699
+ getnstr(rtn,GETSTR_BUF_SIZE-1);
700
+ #else
701
+ getstr(rtn);
702
+ #endif
703
+ return 0;
704
+ }
705
+
706
+ /*
707
+ * Document-method: Curses.getstr
708
+ *
709
+ * This is equivalent to a series f Curses::Window.getch calls
710
+ *
711
+ */
712
+ static VALUE
713
+ curses_getstr(VALUE obj)
714
+ {
715
+ char rtn[GETSTR_BUF_SIZE];
716
+
717
+ curses_stdscr();
718
+ rb_thread_call_without_gvl(getstr_func, rtn, RUBY_UBF_IO, 0);
719
+ return rb_external_str_new_with_enc(rtn, strlen(rtn), keyboard_encoding);
720
+ }
721
+
722
+ /*
723
+ * Document-method: Curses.delch
724
+ *
725
+ * Delete the character under the cursor
726
+ *
727
+ */
728
+ static VALUE
729
+ curses_delch(VALUE obj)
730
+ {
731
+ curses_stdscr();
732
+ delch();
733
+ return Qnil;
734
+ }
735
+
736
+ /*
737
+ * Document-method: Curses.deleteln
738
+ *
739
+ * Delete the line under the cursor.
740
+ *
741
+ */
742
+ static VALUE
743
+ curses_deleteln(VALUE obj)
744
+ {
745
+ curses_stdscr();
746
+ #if defined(HAVE_DELETELN) || defined(deleteln)
747
+ deleteln();
748
+ #endif
749
+ return Qnil;
750
+ }
751
+
752
+ /*
753
+ * Document-method: Curses.insertln
754
+ *
755
+ * Inserts a line above the cursor, and the bottom line is lost
756
+ *
757
+ */
758
+ static VALUE
759
+ curses_insertln(VALUE obj)
760
+ {
761
+ curses_stdscr();
762
+ #if defined(HAVE_INSERTLN) || defined(insertln)
763
+ insertln();
764
+ #endif
765
+ return Qnil;
766
+ }
767
+
768
+ /*
769
+ * Document-method: Curses.keyname
770
+ * call-seq: keyname(c)
771
+ *
772
+ * Returns the character string corresponding to key +c+
773
+ */
774
+ static VALUE
775
+ curses_keyname(VALUE obj, VALUE c)
776
+ {
777
+ #ifdef HAVE_KEYNAME
778
+ int cc = curses_char(c);
779
+ const char *name;
780
+
781
+ curses_stdscr();
782
+ name = keyname(cc);
783
+ if (name) {
784
+ return rb_str_new_cstr(name);
785
+ }
786
+ else {
787
+ return Qnil;
788
+ }
789
+ #else
790
+ return Qnil;
791
+ #endif
792
+ }
793
+
794
+ /*
795
+ * Document-method: Curses.lines
796
+ *
797
+ * Returns the number of lines on the screen
798
+ */
799
+ static VALUE
800
+ curses_lines(void)
801
+ {
802
+ return INT2FIX(LINES);
803
+ }
804
+
805
+ /*
806
+ * Document-method: Curses.cols
807
+ *
808
+ * Returns the number of columns on the screen
809
+ */
810
+ static VALUE
811
+ curses_cols(void)
812
+ {
813
+ return INT2FIX(COLS);
814
+ }
815
+
816
+ /*
817
+ * Document-method: Curses.curs_set
818
+ * call-seq: curs_set(visibility)
819
+ *
820
+ * Sets Cursor Visibility.
821
+ * 0: invisible
822
+ * 1: visible
823
+ * 2: very visible
824
+ */
825
+ static VALUE
826
+ curses_curs_set(VALUE obj, VALUE visibility)
827
+ {
828
+ #ifdef HAVE_CURS_SET
829
+ int n;
830
+ curses_stdscr();
831
+ return (n = curs_set(NUM2INT(visibility)) != ERR) ? INT2FIX(n) : Qnil;
832
+ #else
833
+ return Qnil;
834
+ #endif
835
+ }
836
+
837
+ /*
838
+ * Document-method: Curses.scrl
839
+ * call-seq: scrl(num)
840
+ *
841
+ * Scrolls the current window Fixnum +num+ lines.
842
+ * The current cursor position is not changed.
843
+ *
844
+ * For positive +num+, it scrolls up.
845
+ *
846
+ * For negative +num+, it scrolls down.
847
+ *
848
+ */
849
+ static VALUE
850
+ curses_scrl(VALUE obj, VALUE n)
851
+ {
852
+ /* may have to raise exception on ERR */
853
+ #ifdef HAVE_SCRL
854
+ curses_stdscr();
855
+ return (scrl(NUM2INT(n)) == OK) ? Qtrue : Qfalse;
856
+ #else
857
+ return Qfalse;
858
+ #endif
859
+ }
860
+
861
+ /*
862
+ * Document-method: Curses.setscrreg
863
+ *
864
+ * call-seq:
865
+ * setscrreg(top, bottom)
866
+ *
867
+ * Set a software scrolling region in a window.
868
+ * +top+ and +bottom+ are lines numbers of the margin.
869
+ *
870
+ * If this option and Curses.scrollok are enabled, an attempt to move off
871
+ * the bottom margin line causes all lines in the scrolling region
872
+ * to scroll one line in the direction of the first line.
873
+ * Only the text of the window is scrolled.
874
+ *
875
+ */
876
+ static VALUE
877
+ curses_setscrreg(VALUE obj, VALUE top, VALUE bottom)
878
+ {
879
+ /* may have to raise exception on ERR */
880
+ #ifdef HAVE_SETSCRREG
881
+ curses_stdscr();
882
+ return (setscrreg(NUM2INT(top), NUM2INT(bottom)) == OK) ? Qtrue : Qfalse;
883
+ #else
884
+ return Qfalse;
885
+ #endif
886
+ }
887
+
888
+ /*
889
+ * Document-method: Curses.attroff
890
+ * call-seq: attroff(attrs)
891
+ *
892
+ * Turns off the named attributes +attrs+
893
+ * without affecting any others.
894
+ *
895
+ * See also Curses::Window.attrset for additional information.
896
+ */
897
+ static VALUE
898
+ curses_attroff(VALUE obj, VALUE attrs)
899
+ {
900
+ curses_stdscr();
901
+ return window_attroff(rb_stdscr,attrs);
902
+ /* return INT2FIX(attroff(NUM2INT(attrs))); */
903
+ }
904
+
905
+ /*
906
+ * Document-method: Curses.attron
907
+ * call-seq: attron(attrs)
908
+ *
909
+ * Turns on the named attributes +attrs+
910
+ * without turning any other attributes on or off.
911
+ *
912
+ * See also Curses::Window.attrset for additional information.
913
+ */
914
+ static VALUE
915
+ curses_attron(VALUE obj, VALUE attrs)
916
+ {
917
+ curses_stdscr();
918
+ return window_attron(rb_stdscr,attrs);
919
+ /* return INT2FIX(attroff(NUM2INT(attrs))); */
920
+ }
921
+
922
+ /*
923
+ * Document-method: Curses.attrset
924
+ * call-seq: attrset(attrs)
925
+ *
926
+ * Sets the current attributes of the given window to +attrs+.
927
+ *
928
+ * see also Curses::Window.attrset
929
+ *
930
+ */
931
+ static VALUE
932
+ curses_attrset(VALUE obj, VALUE attrs)
933
+ {
934
+ curses_stdscr();
935
+ return window_attrset(rb_stdscr,attrs);
936
+ /* return INT2FIX(attroff(NUM2INT(attrs))); */
937
+ }
938
+
939
+ /*
940
+ * Document-method: Curses.bkgdset
941
+ * call-seq: bkgdset(ch)
942
+ *
943
+ * Manipulate the background of the named window
944
+ * with character Integer +ch+
945
+ *
946
+ * The background becomes a property of the character
947
+ * and moves with the character through any scrolling
948
+ * and insert/delete line/character operations.
949
+ *
950
+ * see also the system manual for curs_bkgd(3)
951
+ */
952
+ static VALUE
953
+ curses_bkgdset(VALUE obj, VALUE ch)
954
+ {
955
+ #ifdef HAVE_BKGDSET
956
+ curses_stdscr();
957
+ bkgdset(NUM2CH(ch));
958
+ #endif
959
+ return Qnil;
960
+ }
961
+
962
+ /*
963
+ * call-seq: bkgd(ch)
964
+ *
965
+ * Window background manipulation routines.
966
+ *
967
+ * Set the background property of the current
968
+ * and then apply the character Integer +ch+ setting
969
+ * to every character position in that window.
970
+ *
971
+ * see also the system manual for curs_bkgd(3)
972
+ */
973
+ static VALUE
974
+ curses_bkgd(VALUE obj, VALUE ch)
975
+ {
976
+ #ifdef HAVE_BKGD
977
+ curses_stdscr();
978
+ return (bkgd(NUM2CH(ch)) == OK) ? Qtrue : Qfalse;
979
+ #else
980
+ return Qfalse;
981
+ #endif
982
+ }
983
+
984
+ #if defined(HAVE_USE_DEFAULT_COLORS)
985
+ /*
986
+ * tells the curses library to use terminal's default colors.
987
+ *
988
+ * see also the system manual for default_colors(3)
989
+ */
990
+ static VALUE
991
+ curses_use_default_colors(VALUE obj)
992
+ {
993
+ curses_stdscr();
994
+ use_default_colors();
995
+ return Qnil;
996
+ }
997
+ #else
998
+ #define curses_use_default_colors rb_f_notimplement
999
+ #endif
1000
+
1001
+ #if defined(HAVE_TABSIZE)
1002
+ /*
1003
+ * Document-method: Curses.TABSIZE=
1004
+ * call-seq: TABSIZE=(value)
1005
+ *
1006
+ * Sets the TABSIZE to Integer +value+
1007
+ */
1008
+ static VALUE
1009
+ curses_tabsize_set(VALUE obj, VALUE val)
1010
+ {
1011
+ TABSIZE = NUM2INT(val);
1012
+ return INT2NUM(TABSIZE);
1013
+ }
1014
+ #else
1015
+ #define curses_tabsize_set rb_f_notimplement
1016
+ #endif
1017
+
1018
+ #if defined(HAVE_TABSIZE)
1019
+ /*
1020
+ * Returns the number of positions in a tab.
1021
+ */
1022
+ static VALUE
1023
+ curses_tabsize_get(VALUE ojb)
1024
+ {
1025
+ return INT2NUM(TABSIZE);
1026
+ }
1027
+ #else
1028
+ #define curses_tabsize_get rb_f_notimplement
1029
+ #endif
1030
+
1031
+ #if defined(HAVE_ESCDELAY)
1032
+ /*
1033
+ * call-seq: Curses.ESCDELAY=(value)
1034
+ *
1035
+ * Sets the ESCDELAY to Integer +value+
1036
+ */
1037
+ static VALUE
1038
+ curses_escdelay_set(VALUE obj, VALUE val)
1039
+ {
1040
+ ESCDELAY = NUM2INT(val);
1041
+ return INT2NUM(ESCDELAY);
1042
+ }
1043
+ #else
1044
+ #define curses_escdelay_set rb_f_notimplement
1045
+ #endif
1046
+
1047
+ #if defined(HAVE_ESCDELAY)
1048
+ /*
1049
+ * Returns the total time, in milliseconds, for which
1050
+ * curses will await a character sequence, e.g., a function key
1051
+ */
1052
+ static VALUE
1053
+ curses_escdelay_get(VALUE obj)
1054
+ {
1055
+ return INT2NUM(ESCDELAY);
1056
+ }
1057
+ #else
1058
+ #define curses_escdelay_get rb_f_notimplement
1059
+ #endif
1060
+
1061
+ /*
1062
+ * Document-method: Curses.resize
1063
+ * call-seq: resize(lines, cols)
1064
+ *
1065
+ * alias for Curses.resizeterm
1066
+ *
1067
+ */
1068
+
1069
+ /*
1070
+ * Document-method: Curses.resizeterm
1071
+ * call-seq: resizeterm(lines, cols)
1072
+ *
1073
+ * Resize the current term to Fixnum +lines+ and Fixnum +cols+
1074
+ *
1075
+ */
1076
+ static VALUE
1077
+ curses_resizeterm(VALUE obj, VALUE lin, VALUE col)
1078
+ {
1079
+ #if defined(HAVE_RESIZETERM)
1080
+ curses_stdscr();
1081
+ return (resizeterm(NUM2INT(lin),NUM2INT(col)) == OK) ? Qtrue : Qfalse;
1082
+ #else
1083
+ return Qnil;
1084
+ #endif
1085
+ }
1086
+
1087
+ #ifdef USE_COLOR
1088
+ /*
1089
+ * Document-method: Curses.start_color
1090
+ *
1091
+ * Initializes the color attributes, for terminals that support it.
1092
+ *
1093
+ * This must be called, in order to use color attributes.
1094
+ * It is good practice to call it just after Curses.init_screen
1095
+ */
1096
+ static VALUE
1097
+ curses_start_color(VALUE obj)
1098
+ {
1099
+ /* may have to raise exception on ERR */
1100
+ curses_stdscr();
1101
+ return (start_color() == OK) ? Qtrue : Qfalse;
1102
+ }
1103
+
1104
+ /*
1105
+ * Document-method: Curses.init_pair
1106
+ * call-seq: init_pair(pair, f, b)
1107
+ *
1108
+ * Changes the definition of a color-pair.
1109
+ *
1110
+ * It takes three arguments: the number of the color-pair to be changed +pair+,
1111
+ * the foreground color number +f+, and the background color number +b+.
1112
+ *
1113
+ * If the color-pair was previously initialized, the screen is
1114
+ * refreshed and all occurrences of that color-pair are changed
1115
+ * to the new definition.
1116
+ *
1117
+ */
1118
+ static VALUE
1119
+ curses_init_pair(VALUE obj, VALUE pair, VALUE f, VALUE b)
1120
+ {
1121
+ /* may have to raise exception on ERR */
1122
+ curses_stdscr();
1123
+ return (init_pair(NUM2INT(pair),NUM2INT(f),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
1124
+ }
1125
+
1126
+ /*
1127
+ * Document-method: Curses.init_color
1128
+ * call-seq: init_color(color, r, g, b)
1129
+ *
1130
+ * Changes the definition of a color. It takes four arguments:
1131
+ * * the number of the color to be changed, +color+
1132
+ * * the amount of red, +r+
1133
+ * * the amount of green, +g+
1134
+ * * the amount of blue, +b+
1135
+ *
1136
+ * The value of the first argument must be between 0 and COLORS.
1137
+ * (See the section Colors for the default color index.) Each
1138
+ * of the last three arguments must be a value between 0 and 1000.
1139
+ * When Curses.init_color is used, all occurrences of that color
1140
+ * on the screen immediately change to the new definition.
1141
+ */
1142
+ static VALUE
1143
+ curses_init_color(VALUE obj, VALUE color, VALUE r, VALUE g, VALUE b)
1144
+ {
1145
+ /* may have to raise exception on ERR */
1146
+ curses_stdscr();
1147
+ return (init_color(NUM2INT(color),NUM2INT(r),
1148
+ NUM2INT(g),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
1149
+ }
1150
+
1151
+ /*
1152
+ * Document-method: Curses.has_colors?
1153
+ *
1154
+ * Returns +true+ or +false+ depending on whether the terminal has color capabilities.
1155
+ */
1156
+ static VALUE
1157
+ curses_has_colors(VALUE obj)
1158
+ {
1159
+ curses_stdscr();
1160
+ return has_colors() ? Qtrue : Qfalse;
1161
+ }
1162
+
1163
+ /*
1164
+ * Document-method: Curses.can_change_color?
1165
+ *
1166
+ * Returns +true+ or +false+ depending on whether the terminal can change color attributes
1167
+ */
1168
+ static VALUE
1169
+ curses_can_change_color(VALUE obj)
1170
+ {
1171
+ curses_stdscr();
1172
+ return can_change_color() ? Qtrue : Qfalse;
1173
+ }
1174
+
1175
+ #if defined(HAVE_COLORS)
1176
+ /*
1177
+ * Document-method: Curses.color
1178
+ *
1179
+ * returns COLORS
1180
+ */
1181
+ static VALUE
1182
+ curses_colors(VALUE obj)
1183
+ {
1184
+ return INT2FIX(COLORS);
1185
+ }
1186
+ #else
1187
+ #define curses_colors rb_f_notimplement
1188
+ #endif
1189
+
1190
+ /*
1191
+ * Document-method: Curses.color_content
1192
+ * call-seq: color_content(color)
1193
+ *
1194
+ * Returns an 3 item Array of the RGB values in +color+
1195
+ */
1196
+ static VALUE
1197
+ curses_color_content(VALUE obj, VALUE color)
1198
+ {
1199
+ short r,g,b;
1200
+
1201
+ curses_stdscr();
1202
+ color_content(NUM2INT(color),&r,&g,&b);
1203
+ return rb_ary_new3(3,INT2FIX(r),INT2FIX(g),INT2FIX(b));
1204
+ }
1205
+
1206
+
1207
+ #if defined(HAVE_COLOR_PAIRS)
1208
+ /*
1209
+ * Document-method: Curses.color_pairs
1210
+ *
1211
+ * Returns the COLOR_PAIRS available, if the curses library supports it.
1212
+ */
1213
+ static VALUE
1214
+ curses_color_pairs(VALUE obj)
1215
+ {
1216
+ return INT2FIX(COLOR_PAIRS);
1217
+ }
1218
+ #else
1219
+ #define curses_color_pairs rb_f_notimplement
1220
+ #endif
1221
+
1222
+ /*
1223
+ * Document-method: Curses.pair_content
1224
+ * call-seq: pair_content(pair)
1225
+ *
1226
+ * Returns a 2 item Array, with the foreground and
1227
+ * background color, in +pair+
1228
+ */
1229
+ static VALUE
1230
+ curses_pair_content(VALUE obj, VALUE pair)
1231
+ {
1232
+ short f,b;
1233
+
1234
+ curses_stdscr();
1235
+ pair_content(NUM2INT(pair),&f,&b);
1236
+ return rb_ary_new3(2,INT2FIX(f),INT2FIX(b));
1237
+ }
1238
+
1239
+ /*
1240
+ * Document-method: Curses.color_pair
1241
+ * call-seq: color_pair(attrs)
1242
+ *
1243
+ * Sets the color pair attributes to +attrs+.
1244
+ *
1245
+ * This should be equivalent to Curses.attrset(COLOR_PAIR(+attrs+))
1246
+ *
1247
+ * TODO: validate that equivalency
1248
+ */
1249
+ static VALUE
1250
+ curses_color_pair(VALUE obj, VALUE attrs)
1251
+ {
1252
+ return INT2FIX(COLOR_PAIR(NUM2INT(attrs)));
1253
+ }
1254
+
1255
+ /*
1256
+ * Document-method: Curses.pair_number
1257
+ * call-seq: pair_number(attrs)
1258
+ *
1259
+ * Returns the Fixnum color pair number of attributes +attrs+.
1260
+ */
1261
+ static VALUE
1262
+ curses_pair_number(VALUE obj, VALUE attrs)
1263
+ {
1264
+ curses_stdscr();
1265
+ return INT2FIX(PAIR_NUMBER(NUM2LONG(attrs)));
1266
+ }
1267
+ #endif /* USE_COLOR */
1268
+
1269
+ #ifdef USE_MOUSE
1270
+ struct mousedata {
1271
+ MEVENT *mevent;
1272
+ };
1273
+
1274
+ static void
1275
+ no_mevent(void)
1276
+ {
1277
+ rb_raise(rb_eRuntimeError, "no such mouse event");
1278
+ }
1279
+
1280
+ #define GetMOUSE(obj, data) do {\
1281
+ if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)\
1282
+ rb_raise(rb_eSecurityError, "Insecure: operation on untainted mouse");\
1283
+ TypedData_Get_Struct((obj), struct mousedata, &mousedata_type, (data));\
1284
+ if ((data)->mevent == 0) no_mevent();\
1285
+ } while (0)
1286
+
1287
+ static void
1288
+ curses_mousedata_free(void *p)
1289
+ {
1290
+ struct mousedata *mdata = p;
1291
+ if (mdata->mevent)
1292
+ xfree(mdata->mevent);
1293
+ }
1294
+
1295
+ static size_t
1296
+ curses_mousedata_memsize(const void *p)
1297
+ {
1298
+ const struct mousedata *mdata = p;
1299
+ size_t size = sizeof(*mdata);
1300
+ if (!mdata) return 0;
1301
+ if (mdata->mevent) size += sizeof(mdata->mevent);
1302
+ return size;
1303
+ }
1304
+
1305
+ static const rb_data_type_t mousedata_type = {
1306
+ "mousedata",
1307
+ {0, curses_mousedata_free, curses_mousedata_memsize,}
1308
+ };
1309
+
1310
+ /*
1311
+ * Document-method: Curses.getmouse
1312
+ *
1313
+ * Returns coordinates of the mouse.
1314
+ *
1315
+ * This will read and pop the mouse event data off the queue
1316
+ *
1317
+ * See the BUTTON*, ALL_MOUSE_EVENTS and REPORT_MOUSE_POSITION constants, to examine the mask of the event
1318
+ */
1319
+ static VALUE
1320
+ curses_getmouse(VALUE obj)
1321
+ {
1322
+ struct mousedata *mdata;
1323
+ VALUE val;
1324
+
1325
+ curses_stdscr();
1326
+ val = TypedData_Make_Struct(cMouseEvent,struct mousedata,
1327
+ &mousedata_type,mdata);
1328
+ mdata->mevent = (MEVENT*)xmalloc(sizeof(MEVENT));
1329
+ return (getmouse(mdata->mevent) == OK) ? val : Qnil;
1330
+ }
1331
+
1332
+ /*
1333
+ * Document-method: Curses.ungetmouse
1334
+ *
1335
+ * It pushes a KEY_MOUSE event onto the input queue, and associates with that
1336
+ * event the given state data and screen-relative character-cell coordinates.
1337
+ *
1338
+ * The Curses.ungetmouse function behaves analogously to Curses.ungetch.
1339
+ */
1340
+ static VALUE
1341
+ curses_ungetmouse(VALUE obj, VALUE mevent)
1342
+ {
1343
+ struct mousedata *mdata;
1344
+
1345
+ curses_stdscr();
1346
+ GetMOUSE(mevent,mdata);
1347
+ return (ungetmouse(mdata->mevent) == OK) ? Qtrue : Qfalse;
1348
+ }
1349
+
1350
+ /*
1351
+ * Document-method: Curses.mouseinterval
1352
+ * call-seq: mouseinterval(interval)
1353
+ *
1354
+ * The Curses.mouseinterval function sets the maximum time
1355
+ * (in thousands of a second) that can elapse between press
1356
+ * and release events for them to be recognized as a click.
1357
+ *
1358
+ * Use Curses.mouseinterval(0) to disable click resolution.
1359
+ * This function returns the previous interval value.
1360
+ *
1361
+ * Use Curses.mouseinterval(-1) to obtain the interval without
1362
+ * altering it.
1363
+ *
1364
+ * The default is one sixth of a second.
1365
+ */
1366
+ static VALUE
1367
+ curses_mouseinterval(VALUE obj, VALUE interval)
1368
+ {
1369
+ curses_stdscr();
1370
+ return mouseinterval(NUM2INT(interval)) ? Qtrue : Qfalse;
1371
+ }
1372
+
1373
+ /*
1374
+ * Document-method: Curses.mousemask
1375
+ * call-seq: mousemask(mask)
1376
+ *
1377
+ * Returns the +mask+ of the reportable events
1378
+ */
1379
+ static VALUE
1380
+ curses_mousemask(VALUE obj, VALUE mask)
1381
+ {
1382
+ curses_stdscr();
1383
+ return INT2NUM(mousemask(NUM2UINT(mask),NULL));
1384
+ }
1385
+
1386
+ #define DEFINE_MOUSE_GET_MEMBER(func_name,mem) \
1387
+ static VALUE func_name (VALUE mouse) \
1388
+ { \
1389
+ struct mousedata *mdata; \
1390
+ GetMOUSE(mouse, mdata); \
1391
+ return (UINT2NUM(mdata->mevent -> mem)); \
1392
+ }
1393
+
1394
+ /*
1395
+ * Document-method: Curses::MouseEvent.eid
1396
+ *
1397
+ * Returns the current mouse id
1398
+ */
1399
+ DEFINE_MOUSE_GET_MEMBER(curs_mouse_id, id)
1400
+ /*
1401
+ * Document-method: Curses::MouseEvent.x
1402
+ *
1403
+ * Returns the current mouse's X coordinate
1404
+ */
1405
+ DEFINE_MOUSE_GET_MEMBER(curs_mouse_x, x)
1406
+ /*
1407
+ * Document-method: Curses::MouseEvent.y
1408
+ *
1409
+ * Returns the current mouse's Y coordinate
1410
+ */
1411
+ DEFINE_MOUSE_GET_MEMBER(curs_mouse_y, y)
1412
+ /*
1413
+ * Document-method: Curses::MouseEvent.z
1414
+ *
1415
+ * Returns the current mouse's Z coordinate
1416
+ */
1417
+ DEFINE_MOUSE_GET_MEMBER(curs_mouse_z, z)
1418
+ /*
1419
+ * Document-method: Curses::MouseEvent.bstate
1420
+ *
1421
+ * Returns the current mouse's button state. Use this with the button state
1422
+ * constants to determine which buttons were pressed.
1423
+ */
1424
+ DEFINE_MOUSE_GET_MEMBER(curs_mouse_bstate, bstate)
1425
+ #undef define_curs_mouse_member
1426
+ #endif /* USE_MOUSE */
1427
+
1428
+ #ifdef HAVE_TIMEOUT
1429
+ /*
1430
+ * Document-method: Curses.timeout=
1431
+ * call-seq: timeout=(delay)
1432
+ *
1433
+ * Sets block and non-blocking reads for the window.
1434
+ * - If delay is negative, blocking read is used (i.e., waits indefinitely for input).
1435
+ * - If delay is zero, then non-blocking read is used (i.e., read returns ERR if no input is waiting).
1436
+ * - If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input.
1437
+ *
1438
+ */
1439
+ static VALUE
1440
+ curses_timeout(VALUE obj, VALUE delay)
1441
+ {
1442
+ curses_stdscr();
1443
+ timeout(NUM2INT(delay));
1444
+ return Qnil;
1445
+ }
1446
+ #else
1447
+ #define curses_timeout rb_f_notimplement
1448
+ #endif
1449
+
1450
+ #ifdef HAVE_DEF_PROG_MODE
1451
+ /*
1452
+ * Document-method: Curses.def_prog_mode
1453
+ *
1454
+ * Save the current terminal modes as the "program"
1455
+ * state for use by the Curses.reset_prog_mode
1456
+ *
1457
+ * This is done automatically by Curses.init_screen
1458
+ */
1459
+ static VALUE
1460
+ curses_def_prog_mode(VALUE obj)
1461
+ {
1462
+ curses_stdscr();
1463
+ return def_prog_mode() == OK ? Qtrue : Qfalse;
1464
+ }
1465
+ #else
1466
+ #define curses_def_prog_mode rb_f_notimplement
1467
+ #endif
1468
+
1469
+ #ifdef HAVE_RESET_PROG_MODE
1470
+ /*
1471
+ * Document-method: Curses.reset_prog_mode
1472
+ *
1473
+ * Reset the current terminal modes to the saved state
1474
+ * by the Curses.def_prog_mode
1475
+ *
1476
+ * This is done automatically by Curses.close_screen
1477
+ */
1478
+ static VALUE
1479
+ curses_reset_prog_mode(VALUE obj)
1480
+ {
1481
+ curses_stdscr();
1482
+ return reset_prog_mode() == OK ? Qtrue : Qfalse;
1483
+ }
1484
+ #else
1485
+ #define curses_reset_prog_mode rb_f_notimplement
1486
+ #endif
1487
+
1488
+ /*-------------------------- class Window --------------------------*/
1489
+
1490
+ /* returns a Curses::Window object */
1491
+ static VALUE
1492
+ window_s_allocate(VALUE class)
1493
+ {
1494
+ struct windata *winp;
1495
+
1496
+ return TypedData_Make_Struct(class, struct windata, &windata_type, winp);
1497
+ }
1498
+
1499
+ /*
1500
+ * Document-method: Curses::Window.new
1501
+ * call-seq: new(height, width, top, left)
1502
+ *
1503
+ * Construct a new Curses::Window with constraints of
1504
+ * +height+ lines, +width+ columns, begin at +top+ line, and begin +left+ most column.
1505
+ *
1506
+ * A new window using full screen is called as
1507
+ * Curses::Window.new(0,0,0,0)
1508
+ *
1509
+ */
1510
+ static VALUE
1511
+ window_initialize(VALUE obj, VALUE h, VALUE w, VALUE top, VALUE left)
1512
+ {
1513
+ struct windata *winp;
1514
+ WINDOW *window;
1515
+
1516
+ curses_init_screen();
1517
+ TypedData_Get_Struct(obj, struct windata, &windata_type, winp);
1518
+ if (winp->window) delwin(winp->window);
1519
+ window = newwin(NUM2INT(h), NUM2INT(w), NUM2INT(top), NUM2INT(left));
1520
+ wclear(window);
1521
+ winp->window = window;
1522
+
1523
+ return obj;
1524
+ }
1525
+
1526
+ /*
1527
+ * Document-method: Curses::Window.subwin
1528
+ * call-seq: subwin(height, width, top, left)
1529
+ *
1530
+ * Construct a new subwindow with constraints of
1531
+ * +height+ lines, +width+ columns, begin at +top+ line, and begin +left+ most column.
1532
+ *
1533
+ */
1534
+ static VALUE
1535
+ window_subwin(VALUE obj, VALUE height, VALUE width, VALUE top, VALUE left)
1536
+ {
1537
+ struct windata *winp;
1538
+ WINDOW *window;
1539
+ VALUE win;
1540
+ int h, w, t, l;
1541
+
1542
+ h = NUM2INT(height);
1543
+ w = NUM2INT(width);
1544
+ t = NUM2INT(top);
1545
+ l = NUM2INT(left);
1546
+ GetWINDOW(obj, winp);
1547
+ window = subwin(winp->window, h, w, t, l);
1548
+ win = prep_window(rb_obj_class(obj), window);
1549
+
1550
+ return win;
1551
+ }
1552
+
1553
+ /*
1554
+ * Document-method: Curses::Window.close
1555
+ *
1556
+ * Deletes the window, and frees the memory
1557
+ */
1558
+ static VALUE
1559
+ window_close(VALUE obj)
1560
+ {
1561
+ struct windata *winp;
1562
+
1563
+ GetWINDOW(obj, winp);
1564
+ delwin(winp->window);
1565
+ winp->window = 0;
1566
+
1567
+ return Qnil;
1568
+ }
1569
+
1570
+ /*
1571
+ * Document-method: Curses::Window.clear
1572
+ *
1573
+ * Clear the window.
1574
+ */
1575
+ static VALUE
1576
+ window_clear(VALUE obj)
1577
+ {
1578
+ struct windata *winp;
1579
+
1580
+ GetWINDOW(obj, winp);
1581
+ wclear(winp->window);
1582
+
1583
+ return Qnil;
1584
+ }
1585
+
1586
+ #ifdef HAVE_WERASE
1587
+ /*
1588
+ * Document-method: Curses::Window.erase
1589
+ *
1590
+ * Erase the window.
1591
+ */
1592
+ static VALUE
1593
+ window_erase(VALUE obj)
1594
+ {
1595
+ struct windata *winp;
1596
+
1597
+ GetWINDOW(obj, winp);
1598
+ werase(winp->window);
1599
+
1600
+ return Qnil;
1601
+ }
1602
+ #else
1603
+ #define curses_erase rb_f_notimplement
1604
+ #endif
1605
+
1606
+ /*
1607
+ * Document-method: Curses::Window.clrtoeol
1608
+ *
1609
+ * Clear the window to the end of line, that the cursor is currently on.
1610
+ */
1611
+ static VALUE
1612
+ window_clrtoeol(VALUE obj)
1613
+ {
1614
+ struct windata *winp;
1615
+
1616
+ GetWINDOW(obj, winp);
1617
+ wclrtoeol(winp->window);
1618
+
1619
+ return Qnil;
1620
+ }
1621
+
1622
+ /*
1623
+ * Document-method: Curses::Window.refresh
1624
+ *
1625
+ * Refreshes the windows and lines.
1626
+ *
1627
+ */
1628
+ static VALUE
1629
+ window_refresh(VALUE obj)
1630
+ {
1631
+ struct windata *winp;
1632
+
1633
+ GetWINDOW(obj, winp);
1634
+ wrefresh(winp->window);
1635
+
1636
+ return Qnil;
1637
+ }
1638
+
1639
+ /*
1640
+ * Document-method: Curses::Window.noutrefresh
1641
+ *
1642
+ * Refreshes the windows and lines.
1643
+ *
1644
+ * Curses::Window.noutrefresh allows multiple updates with
1645
+ * more efficiency than Curses::Window.refresh alone.
1646
+ */
1647
+ static VALUE
1648
+ window_noutrefresh(VALUE obj)
1649
+ {
1650
+ struct windata *winp;
1651
+
1652
+ GetWINDOW(obj, winp);
1653
+ #ifdef HAVE_DOUPDATE
1654
+ wnoutrefresh(winp->window);
1655
+ #else
1656
+ wrefresh(winp->window);
1657
+ #endif
1658
+
1659
+ return Qnil;
1660
+ }
1661
+
1662
+ #ifdef HAVE_REDRAWWIN
1663
+ /*
1664
+ * Document-method: Curses::Window.redraw
1665
+ *
1666
+ * Dedraws the entire window.
1667
+ */
1668
+ static VALUE
1669
+ window_redraw(VALUE obj)
1670
+ {
1671
+ struct windata *winp;
1672
+
1673
+ GetWINDOW(obj, winp);
1674
+ redrawwin(winp->window);
1675
+
1676
+ return Qnil;
1677
+ }
1678
+ #else
1679
+ #define window_redraw rb_f_notimplement
1680
+ #endif
1681
+
1682
+ /*
1683
+ * Document-method: Curses::Window.move
1684
+ * call-seq: move(y,x)
1685
+ *
1686
+ * Moves the window so that the upper left-hand corner is at position (+y+, +x+)
1687
+ */
1688
+ static VALUE
1689
+ window_move(VALUE obj, VALUE y, VALUE x)
1690
+ {
1691
+ struct windata *winp;
1692
+
1693
+ GetWINDOW(obj, winp);
1694
+ mvwin(winp->window, NUM2INT(y), NUM2INT(x));
1695
+
1696
+ return Qnil;
1697
+ }
1698
+
1699
+ /*
1700
+ * Document-method: Curses::Window.setpos
1701
+ * call-seq: setpos(y, x)
1702
+ *
1703
+ * A setter for the position of the cursor
1704
+ * in the current window,
1705
+ * using coordinates +x+ and +y+
1706
+ *
1707
+ */
1708
+ static VALUE
1709
+ window_setpos(VALUE obj, VALUE y, VALUE x)
1710
+ {
1711
+ struct windata *winp;
1712
+
1713
+ GetWINDOW(obj, winp);
1714
+ wmove(winp->window, NUM2INT(y), NUM2INT(x));
1715
+ return Qnil;
1716
+ }
1717
+
1718
+ /*
1719
+ * Document-method: Curses::Window.cury
1720
+ *
1721
+ * A getter for the current line (Y coord) of the window
1722
+ */
1723
+ static VALUE
1724
+ window_cury(VALUE obj)
1725
+ {
1726
+ struct windata *winp;
1727
+ int RB_UNUSED_VAR(x), y;
1728
+
1729
+ GetWINDOW(obj, winp);
1730
+ getyx(winp->window, y, x);
1731
+ return INT2FIX(y);
1732
+ }
1733
+
1734
+ /*
1735
+ * Document-method: Curses::Window.curx
1736
+ *
1737
+ * A getter for the current column (X coord) of the window
1738
+ */
1739
+ static VALUE
1740
+ window_curx(VALUE obj)
1741
+ {
1742
+ struct windata *winp;
1743
+ int x, RB_UNUSED_VAR(y);
1744
+
1745
+ GetWINDOW(obj, winp);
1746
+ getyx(winp->window, y, x);
1747
+ return INT2FIX(x);
1748
+ }
1749
+
1750
+ /*
1751
+ * Document-method: Curses::Window.maxy
1752
+ *
1753
+ * A getter for the maximum lines for the window
1754
+ */
1755
+ static VALUE
1756
+ window_maxy(VALUE obj)
1757
+ {
1758
+ struct windata *winp;
1759
+
1760
+ GetWINDOW(obj, winp);
1761
+ #if defined(getmaxy)
1762
+ return INT2FIX(getmaxy(winp->window));
1763
+ #elif defined(getmaxyx)
1764
+ {
1765
+ int x, y;
1766
+ getmaxyx(winp->window, y, x);
1767
+ return INT2FIX(y);
1768
+ }
1769
+ #else
1770
+ return INT2FIX(winp->window->_maxy+1);
1771
+ #endif
1772
+ }
1773
+
1774
+ /*
1775
+ * Document-method: Curses::Window.maxx
1776
+ *
1777
+ * A getter for the maximum columns for the window
1778
+ */
1779
+ static VALUE
1780
+ window_maxx(VALUE obj)
1781
+ {
1782
+ struct windata *winp;
1783
+
1784
+ GetWINDOW(obj, winp);
1785
+ #if defined(getmaxx)
1786
+ return INT2FIX(getmaxx(winp->window));
1787
+ #elif defined(getmaxyx)
1788
+ {
1789
+ int x, y;
1790
+ getmaxyx(winp->window, y, x);
1791
+ return INT2FIX(x);
1792
+ }
1793
+ #else
1794
+ return INT2FIX(winp->window->_maxx+1);
1795
+ #endif
1796
+ }
1797
+
1798
+ /*
1799
+ * Document-method: Curses::Window.begy
1800
+ *
1801
+ * A getter for the beginning line (Y coord) of the window
1802
+ */
1803
+ static VALUE
1804
+ window_begy(VALUE obj)
1805
+ {
1806
+ struct windata *winp;
1807
+ int RB_UNUSED_VAR(x), y;
1808
+
1809
+ GetWINDOW(obj, winp);
1810
+ #ifdef getbegyx
1811
+ getbegyx(winp->window, y, x);
1812
+ #else
1813
+ y = winp->window->_begy;
1814
+ #endif
1815
+ return INT2FIX(y);
1816
+ }
1817
+
1818
+ /*
1819
+ * Document-method: Curses::Window.begx
1820
+ *
1821
+ * A getter for the beginning column (X coord) of the window
1822
+ */
1823
+ static VALUE
1824
+ window_begx(VALUE obj)
1825
+ {
1826
+ struct windata *winp;
1827
+ int x, RB_UNUSED_VAR(y);
1828
+
1829
+ GetWINDOW(obj, winp);
1830
+ #ifdef getbegyx
1831
+ getbegyx(winp->window, y, x);
1832
+ #else
1833
+ x = winp->window->_begx;
1834
+ #endif
1835
+ return INT2FIX(x);
1836
+ }
1837
+
1838
+ /*
1839
+ * Document-method: Curses::Window.box
1840
+ * call-seq: box(vert, hor)
1841
+ *
1842
+ * set the characters to frame the window in.
1843
+ * The vertical +vert+ and horizontal +hor+ character.
1844
+ *
1845
+ * win = Curses::Window.new(5,5,5,5)
1846
+ * win.box(?|, ?-)
1847
+ *
1848
+ */
1849
+ static VALUE
1850
+ window_box(int argc, VALUE *argv, VALUE self)
1851
+ {
1852
+ struct windata *winp;
1853
+ VALUE vert, hor, corn;
1854
+
1855
+ rb_scan_args(argc, argv, "21", &vert, &hor, &corn);
1856
+
1857
+ GetWINDOW(self, winp);
1858
+ box(winp->window, NUM2CH(vert), NUM2CH(hor));
1859
+
1860
+ if (!NIL_P(corn)) {
1861
+ int cur_x, cur_y, x, y;
1862
+ chtype c;
1863
+
1864
+ c = NUM2CH(corn);
1865
+ getyx(winp->window, cur_y, cur_x);
1866
+ x = NUM2INT(window_maxx(self)) - 1;
1867
+ y = NUM2INT(window_maxy(self)) - 1;
1868
+ wmove(winp->window, 0, 0);
1869
+ waddch(winp->window, c);
1870
+ wmove(winp->window, y, 0);
1871
+ waddch(winp->window, c);
1872
+ wmove(winp->window, y, x);
1873
+ waddch(winp->window, c);
1874
+ wmove(winp->window, 0, x);
1875
+ waddch(winp->window, c);
1876
+ wmove(winp->window, cur_y, cur_x);
1877
+ }
1878
+
1879
+ return Qnil;
1880
+ }
1881
+
1882
+ /*
1883
+ * Document-method: Curses::Window.standout
1884
+ *
1885
+ * Enables the best highlighting mode of the terminal.
1886
+ *
1887
+ * This is equivalent to Curses::Window.attron(A_STANDOUT)
1888
+ *
1889
+ * see also Curses::Window.attrset
1890
+ */
1891
+ static VALUE
1892
+ window_standout(VALUE obj)
1893
+ {
1894
+ struct windata *winp;
1895
+
1896
+ GetWINDOW(obj, winp);
1897
+ wstandout(winp->window);
1898
+ return Qnil;
1899
+ }
1900
+
1901
+ /*
1902
+ * Document-method: Curses::Window.standend
1903
+ *
1904
+ * Enables the Normal display (no highlight)
1905
+ *
1906
+ * This is equivalent to Curses::Window.attron(A_NORMAL)
1907
+ *
1908
+ * see also Curses::Window.attrset
1909
+ */
1910
+ static VALUE
1911
+ window_standend(VALUE obj)
1912
+ {
1913
+ struct windata *winp;
1914
+
1915
+ GetWINDOW(obj, winp);
1916
+ wstandend(winp->window);
1917
+ return Qnil;
1918
+ }
1919
+
1920
+ /*
1921
+ * Document-method: Curses::Window.inch
1922
+ *
1923
+ * Returns the character at the current position of the window.
1924
+ */
1925
+ static VALUE
1926
+ window_inch(VALUE obj)
1927
+ {
1928
+ struct windata *winp;
1929
+
1930
+ GetWINDOW(obj, winp);
1931
+ return CH2FIX(winch(winp->window));
1932
+ }
1933
+
1934
+ /*
1935
+ * Document-method: Curses::Window.addch
1936
+ * call-seq: addch(ch)
1937
+ *
1938
+ * Add a character +ch+, with attributes, to the window, then advance the cursor.
1939
+ *
1940
+ * see also the system manual for curs_addch(3)
1941
+ */
1942
+ static VALUE
1943
+ window_addch(VALUE obj, VALUE ch)
1944
+ {
1945
+ struct windata *winp;
1946
+
1947
+ GetWINDOW(obj, winp);
1948
+ waddch(winp->window, NUM2CH(ch));
1949
+
1950
+ return Qnil;
1951
+ }
1952
+
1953
+ /*
1954
+ * Document-method: Curses::Window.insch
1955
+ * call-seq: insch(ch)
1956
+ *
1957
+ * Insert a character +ch+, before the cursor, in the current window
1958
+ *
1959
+ */
1960
+ static VALUE
1961
+ window_insch(VALUE obj, VALUE ch)
1962
+ {
1963
+ struct windata *winp;
1964
+
1965
+ GetWINDOW(obj, winp);
1966
+ winsch(winp->window, NUM2CH(ch));
1967
+
1968
+ return Qnil;
1969
+ }
1970
+
1971
+ /*
1972
+ * Document-method: Curses::Window.addstr
1973
+ * call-seq: addstr(str)
1974
+ *
1975
+ * add a string of characters +str+, to the window and advance cursor
1976
+ *
1977
+ */
1978
+ static VALUE
1979
+ window_addstr(VALUE obj, VALUE str)
1980
+ {
1981
+ if (!NIL_P(str)) {
1982
+ struct windata *winp;
1983
+
1984
+ StringValue(str);
1985
+ str = rb_str_export_to_enc(str, terminal_encoding);
1986
+ GetWINDOW(obj, winp);
1987
+ waddstr(winp->window, StringValueCStr(str));
1988
+ }
1989
+ return Qnil;
1990
+ }
1991
+
1992
+ /*
1993
+ * Document-method: Curses::Window.<<
1994
+ *
1995
+ * call-seq:
1996
+ * <<(str)
1997
+ *
1998
+ * Add String +str+ to the current string.
1999
+ *
2000
+ * See also Curses::Window.addstr
2001
+ */
2002
+ static VALUE
2003
+ window_addstr2(VALUE obj, VALUE str)
2004
+ {
2005
+ window_addstr(obj, str);
2006
+ return obj;
2007
+ }
2008
+
2009
+ struct wgetch_arg {
2010
+ WINDOW *win;
2011
+ int c;
2012
+ };
2013
+
2014
+ static void *
2015
+ wgetch_func(void *_arg)
2016
+ {
2017
+ struct wgetch_arg *arg = (struct wgetch_arg *)_arg;
2018
+ arg->c = wgetch(arg->win);
2019
+ return 0;
2020
+ }
2021
+
2022
+ /*
2023
+ * Document-method: Curses::Window.getch
2024
+ *
2025
+ * Read and returns a character from the window.
2026
+ *
2027
+ * See Curses::Key to all the function KEY_* available
2028
+ *
2029
+ */
2030
+ static VALUE
2031
+ window_getch(VALUE obj)
2032
+ {
2033
+ struct windata *winp;
2034
+ struct wgetch_arg arg;
2035
+ int c;
2036
+
2037
+ GetWINDOW(obj, winp);
2038
+ arg.win = winp->window;
2039
+ rb_thread_call_without_gvl(wgetch_func, (void *)&arg, RUBY_UBF_IO, 0);
2040
+ c = arg.c;
2041
+ if (c == EOF) return Qnil;
2042
+ if (rb_isprint(c)) {
2043
+ char ch = (char)c;
2044
+
2045
+ return rb_external_str_new_with_enc(&ch, 1, keyboard_encoding);
2046
+ }
2047
+ return UINT2NUM(c);
2048
+ }
2049
+
2050
+ struct wgetstr_arg {
2051
+ WINDOW *win;
2052
+ char rtn[GETSTR_BUF_SIZE];
2053
+ };
2054
+
2055
+ static void *
2056
+ wgetstr_func(void *_arg)
2057
+ {
2058
+ struct wgetstr_arg *arg = (struct wgetstr_arg *)_arg;
2059
+ #if defined(HAVE_WGETNSTR)
2060
+ wgetnstr(arg->win, arg->rtn, GETSTR_BUF_SIZE-1);
2061
+ #else
2062
+ wgetstr(arg->win, arg->rtn);
2063
+ #endif
2064
+ return 0;
2065
+ }
2066
+
2067
+ /*
2068
+ * Document-method: Curses::Window.getstr
2069
+ *
2070
+ * This is equivalent to a series f Curses::Window.getch calls
2071
+ *
2072
+ */
2073
+ static VALUE
2074
+ window_getstr(VALUE obj)
2075
+ {
2076
+ struct windata *winp;
2077
+ struct wgetstr_arg arg;
2078
+
2079
+ GetWINDOW(obj, winp);
2080
+ arg.win = winp->window;
2081
+ rb_thread_call_without_gvl(wgetstr_func, (void *)&arg, RUBY_UBF_IO, 0);
2082
+ return rb_external_str_new_with_enc(arg.rtn, strlen(arg.rtn),
2083
+ keyboard_encoding);
2084
+ }
2085
+
2086
+ /*
2087
+ * Document-method: Curses::Window.delch
2088
+ *
2089
+ * Delete the character under the cursor
2090
+ *
2091
+ */
2092
+ static VALUE
2093
+ window_delch(VALUE obj)
2094
+ {
2095
+ struct windata *winp;
2096
+
2097
+ GetWINDOW(obj, winp);
2098
+ wdelch(winp->window);
2099
+ return Qnil;
2100
+ }
2101
+
2102
+ /*
2103
+ * Document-method: Curses::Window.deleteln
2104
+ *
2105
+ * Delete the line under the cursor.
2106
+ *
2107
+ */
2108
+ static VALUE
2109
+ window_deleteln(VALUE obj)
2110
+ {
2111
+ #if defined(HAVE_WDELETELN) || defined(wdeleteln)
2112
+ struct windata *winp;
2113
+
2114
+ GetWINDOW(obj, winp);
2115
+ wdeleteln(winp->window);
2116
+ #endif
2117
+ return Qnil;
2118
+ }
2119
+
2120
+ /*
2121
+ * Document-method: Curses::Window.insertln
2122
+ *
2123
+ * Inserts a line above the cursor, and the bottom line is lost
2124
+ *
2125
+ */
2126
+ static VALUE
2127
+ window_insertln(VALUE obj)
2128
+ {
2129
+ #if defined(HAVE_WINSERTLN) || defined(winsertln)
2130
+ struct windata *winp;
2131
+
2132
+ GetWINDOW(obj, winp);
2133
+ winsertln(winp->window);
2134
+ #endif
2135
+ return Qnil;
2136
+ }
2137
+
2138
+ /*
2139
+ * Document-method: Curses::Window.scrollok
2140
+ * call-seq: scrollok(bool)
2141
+ *
2142
+ * Controls what happens when the cursor of a window
2143
+ * is moved off the edge of the window or scrolling region,
2144
+ * either as a result of a newline action on the bottom line,
2145
+ * or typing the last character of the last line.
2146
+ *
2147
+ * If disabled, (+bool+ is false), the cursor is left on the bottom line.
2148
+ *
2149
+ * If enabled, (+bool+ is true), the window is scrolled up one line
2150
+ * (Note that to get the physical scrolling effect on the terminal,
2151
+ * it is also necessary to call Curses::Window.idlok)
2152
+ */
2153
+ static VALUE
2154
+ window_scrollok(VALUE obj, VALUE bf)
2155
+ {
2156
+ struct windata *winp;
2157
+
2158
+ GetWINDOW(obj, winp);
2159
+ scrollok(winp->window, RTEST(bf) ? TRUE : FALSE);
2160
+ return Qnil;
2161
+ }
2162
+
2163
+ /*
2164
+ * Document-method: Curses::Window.idlok
2165
+ * call-seq: idlok(bool)
2166
+ *
2167
+ * If +bool+ is +true+ curses considers using the hardware insert/delete
2168
+ * line feature of terminals so equipped.
2169
+ *
2170
+ * If +bool+ is +false+, disables use of line insertion and deletion.
2171
+ * This option should be enabled only if the application needs insert/delete
2172
+ * line, for example, for a screen editor.
2173
+ *
2174
+ * It is disabled by default because insert/delete line tends to be visually
2175
+ * annoying when used in applications where it is not really needed.
2176
+ * If insert/delete line cannot be used, curses redraws the changed portions of all lines.
2177
+ *
2178
+ */
2179
+ static VALUE
2180
+ window_idlok(VALUE obj, VALUE bf)
2181
+ {
2182
+ struct windata *winp;
2183
+
2184
+ GetWINDOW(obj, winp);
2185
+ idlok(winp->window, RTEST(bf) ? TRUE : FALSE);
2186
+ return Qnil;
2187
+ }
2188
+
2189
+ /*
2190
+ * Document-method: Curses::Window.setscrreg
2191
+ * call-seq:
2192
+ * setscrreg(top, bottom)
2193
+ *
2194
+ * Set a software scrolling region in a window.
2195
+ * +top+ and +bottom+ are lines numbers of the margin.
2196
+ *
2197
+ * If this option and Curses::Window.scrollok are enabled, an attempt to move
2198
+ * off the bottom margin line causes all lines in the scrolling region to
2199
+ * scroll one line in the direction of the first line. Only the text of the
2200
+ * window is scrolled.
2201
+ *
2202
+ */
2203
+ static VALUE
2204
+ window_setscrreg(VALUE obj, VALUE top, VALUE bottom)
2205
+ {
2206
+ #ifdef HAVE_WSETSCRREG
2207
+ struct windata *winp;
2208
+ int res;
2209
+
2210
+ GetWINDOW(obj, winp);
2211
+ res = wsetscrreg(winp->window, NUM2INT(top), NUM2INT(bottom));
2212
+ /* may have to raise exception on ERR */
2213
+ return (res == OK) ? Qtrue : Qfalse;
2214
+ #else
2215
+ return Qfalse;
2216
+ #endif
2217
+ }
2218
+
2219
+ #if defined(USE_COLOR) && defined(HAVE_WCOLOR_SET)
2220
+ /*
2221
+ * Document-method: Curses::Window.color_set
2222
+ * call-seq: color_set(col)
2223
+ *
2224
+ * Sets the current color of the given window to the
2225
+ * foreground/background combination described by the Fixnum +col+.
2226
+ */
2227
+ static VALUE
2228
+ window_color_set(VALUE obj, VALUE col)
2229
+ {
2230
+ struct windata *winp;
2231
+ int res;
2232
+
2233
+ GetWINDOW(obj, winp);
2234
+ res = wcolor_set(winp->window, NUM2INT(col), NULL);
2235
+ return (res == OK) ? Qtrue : Qfalse;
2236
+ }
2237
+ #endif /* defined(USE_COLOR) && defined(HAVE_WCOLOR_SET) */
2238
+
2239
+ /*
2240
+ * Document-method: Curses::Window.scroll
2241
+ *
2242
+ * Scrolls the current window up one line.
2243
+ */
2244
+ static VALUE
2245
+ window_scroll(VALUE obj)
2246
+ {
2247
+ struct windata *winp;
2248
+
2249
+ GetWINDOW(obj, winp);
2250
+ /* may have to raise exception on ERR */
2251
+ return (scroll(winp->window) == OK) ? Qtrue : Qfalse;
2252
+ }
2253
+
2254
+ /*
2255
+ * Document-method: Curses::Window.scrl
2256
+ * call-seq: scrl(num)
2257
+ *
2258
+ * Scrolls the current window Fixnum +num+ lines.
2259
+ * The current cursor position is not changed.
2260
+ *
2261
+ * For positive +num+, it scrolls up.
2262
+ *
2263
+ * For negative +num+, it scrolls down.
2264
+ *
2265
+ */
2266
+ static VALUE
2267
+ window_scrl(VALUE obj, VALUE n)
2268
+ {
2269
+ #ifdef HAVE_WSCRL
2270
+ struct windata *winp;
2271
+
2272
+ GetWINDOW(obj, winp);
2273
+ /* may have to raise exception on ERR */
2274
+ return (wscrl(winp->window,NUM2INT(n)) == OK) ? Qtrue : Qfalse;
2275
+ #else
2276
+ return Qfalse;
2277
+ #endif
2278
+ }
2279
+
2280
+ /*
2281
+ * Document-method: Curses::Window.attroff
2282
+ * call-seq: attroff(attrs)
2283
+ *
2284
+ * Turns off the named attributes +attrs+
2285
+ * without affecting any others.
2286
+ *
2287
+ * See also Curses::Window.attrset
2288
+ */
2289
+ static VALUE
2290
+ window_attroff(VALUE obj, VALUE attrs)
2291
+ {
2292
+ #ifdef HAVE_WATTROFF
2293
+ struct windata *winp;
2294
+
2295
+ GetWINDOW(obj,winp);
2296
+ return INT2FIX(wattroff(winp->window,NUM2INT(attrs)));
2297
+ #else
2298
+ return Qtrue;
2299
+ #endif
2300
+ }
2301
+
2302
+ /*
2303
+ * Document-method: Curses::Window.attron
2304
+ * call-seq: attron(attrs)
2305
+ *
2306
+ * Turns on the named attributes +attrs+
2307
+ * without turning any other attributes on or off.
2308
+ *
2309
+ * See also Curses::Window.attrset
2310
+ */
2311
+ static VALUE
2312
+ window_attron(VALUE obj, VALUE attrs)
2313
+ {
2314
+ #ifdef HAVE_WATTRON
2315
+ struct windata *winp;
2316
+ VALUE val;
2317
+
2318
+ GetWINDOW(obj,winp);
2319
+ val = INT2FIX(wattron(winp->window,NUM2INT(attrs)));
2320
+ if (rb_block_given_p()) {
2321
+ rb_yield(val);
2322
+ wattroff(winp->window,NUM2INT(attrs));
2323
+ return val;
2324
+ }
2325
+ else{
2326
+ return val;
2327
+ }
2328
+ #else
2329
+ return Qtrue;
2330
+ #endif
2331
+ }
2332
+
2333
+ /*
2334
+ * Document-method: Curses::Window.attrset
2335
+ * call-seq: attrset(attrs)
2336
+ *
2337
+ * Sets the current attributes of the given window to +attrs+.