curses 1.0.0

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