ext/curses/curses.c


DEFINITIONS

This source file includes following functions.
  1. no_window
  2. free_window
  3. prep_window
  4. curses_init_screen
  5. curses_close_screen
  6. curses_finalize
  7. curses_closed
  8. curses_clear
  9. curses_refresh
  10. curses_doupdate
  11. curses_echo
  12. curses_noecho
  13. curses_raw
  14. curses_noraw
  15. curses_cbreak
  16. curses_nocbreak
  17. curses_nl
  18. curses_nonl
  19. curses_beep
  20. curses_flash
  21. curses_ungetch
  22. curses_setpos
  23. curses_standout
  24. curses_standend
  25. curses_inch
  26. curses_addch
  27. curses_insch
  28. curses_addstr
  29. curses_getch
  30. curses_getstr
  31. curses_delch
  32. curses_deleteln
  33. curses_keyname
  34. curses_lines
  35. curses_cols
  36. curses_curs_set
  37. curses_scrl
  38. curses_setscrreg
  39. curses_attroff
  40. curses_attron
  41. curses_attrset
  42. curses_bkgdset
  43. curses_bkgd
  44. curses_resizeterm
  45. curses_start_color
  46. curses_init_pair
  47. curses_init_color
  48. curses_has_colors
  49. curses_can_change_color
  50. curses_color_content
  51. curses_pair_content
  52. curses_color_pair
  53. curses_pair_number
  54. no_mevent
  55. curses_mousedata_free
  56. curses_getmouse
  57. curses_ungetmouse
  58. curses_mouseinterval
  59. curses_mousemask
  60. window_s_allocate
  61. window_initialize
  62. window_subwin
  63. window_close
  64. window_clear
  65. window_refresh
  66. window_noutrefresh
  67. window_move
  68. window_setpos
  69. window_cury
  70. window_curx
  71. window_maxy
  72. window_maxx
  73. window_begy
  74. window_begx
  75. window_box
  76. window_standout
  77. window_standend
  78. window_inch
  79. window_addch
  80. window_insch
  81. window_addstr
  82. window_addstr2
  83. window_getch
  84. window_getstr
  85. window_delch
  86. window_deleteln
  87. window_scrollok
  88. window_idlok
  89. window_setscrreg
  90. window_scroll
  91. window_scrl
  92. window_attroff
  93. window_attron
  94. window_attrset
  95. window_bkgdset
  96. window_bkgd
  97. window_getbkgd
  98. window_resize
  99. window_keypad
  100. Init_curses


   1  /* -*- C -*-
   2   * $Id: curses.c,v 1.14 2002/08/04 13:32:01 nobu Exp $
   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  
  16  #ifdef HAVE_NCURSES_H
  17  # include <ncurses.h>
  18  #else
  19  # ifdef HAVE_NCURSES_CURSES_H
  20  #  include <ncurses/curses.h>
  21  #else
  22  # ifdef HAVE_CURSES_COLR_CURSES_H
  23  #  include <varargs.h>
  24  #  include <curses_colr/curses.h>
  25  # else
  26  #  include <curses.h>
  27  #  if (defined(__bsdi__) || defined(__NetBSD__) || defined(__APPLE__)) && !defined(_maxx)
  28  #   define _maxx maxx
  29  #  endif
  30  #  if (defined(__bsdi__) || defined(__NetBSD__) || defined(__APPLE__)) && !defined(_maxy)
  31  #   define _maxy maxy
  32  #  endif
  33  #  if (defined(__bsdi__) || defined(__NetBSD__) || defined(__APPLE__)) && !defined(_begx)
  34  #   define _begx begx
  35  #  endif
  36  #  if (defined(__bsdi__) || defined(__NetBSD__) || defined(__APPLE__)) && !defined(_begy)
  37  #   define _begy begy
  38  #  endif
  39  # endif
  40  #endif
  41  #endif
  42  
  43  #ifdef HAVE_INIT_COLOR
  44  # define USE_COLOR 1
  45  #endif
  46  
  47  /* supports only ncurses mouse routines */
  48  #ifdef NCURSES_MOUSE_VERSION
  49  # define USE_MOUSE 1
  50  #endif
  51  
  52  #include "stdio.h"
  53  #include "ruby.h"
  54  #include "rubyio.h"
  55  
  56  static VALUE mCurses;
  57  static VALUE mKey;
  58  static VALUE cWindow;
  59  #ifdef USE_MOUSE
  60  static VALUE cMouseEvent;
  61  #endif
  62  
  63  static VALUE rb_stdscr;
  64  
  65  struct windata {
  66      WINDOW *window;
  67  };
  68  
  69  #define CHECK(c) c
  70  
  71  static VALUE window_attroff();
  72  static VALUE window_attron();
  73  static VALUE window_attrset();
  74  
  75  static void
  76  no_window()
  77  {
  78      rb_raise(rb_eRuntimeError, "already closed window");
  79  }
  80  
  81  
  82  #define GetWINDOW(obj, winp) {\
  83      Data_Get_Struct(obj, struct windata, winp);\
  84      if (winp->window == 0) no_window();\
  85  }
  86  
  87  
  88  static void
  89  free_window(winp)
  90      struct windata *winp;
  91  {
  92      if (winp->window && winp->window != stdscr) delwin(winp->window);
  93      winp->window = 0;
  94      free(winp);
  95  }
  96  
  97  static VALUE
  98  prep_window(class, window)
  99      VALUE class;
 100      WINDOW *window;
 101  {
 102      VALUE obj;
 103      struct windata *winp;
 104  
 105      if (window == NULL) {
 106          rb_raise(rb_eRuntimeError, "failed to create window");
 107      }
 108  
 109      obj = rb_obj_alloc(class);
 110      Data_Get_Struct(obj, struct windata, winp);
 111      winp->window = window;
 112      
 113      return obj;    
 114  }
 115  
 116  /*-------------------------- module Curses --------------------------*/
 117  
 118  /* def init_screen */
 119  static VALUE
 120  curses_init_screen()
 121  {
 122      if (rb_stdscr) return rb_stdscr;
 123      initscr();
 124      if (stdscr == 0) {
 125          rb_raise(rb_eRuntimeError, "cannot initialize curses");
 126      }
 127      clear();
 128      rb_stdscr = prep_window(cWindow, stdscr);
 129      return rb_stdscr;
 130  }
 131  
 132  /* def stdscr */
 133  #define curses_stdscr curses_init_screen
 134  
 135  /* def close_screen */
 136  static VALUE
 137  curses_close_screen()
 138  {
 139  #ifdef HAVE_ISENDWIN
 140      if (!isendwin())
 141  #endif
 142          endwin();
 143      rb_stdscr = 0;
 144      return Qnil;
 145  }
 146  
 147  static void
 148  curses_finalize()
 149  {
 150      if (stdscr
 151  #ifdef HAVE_ISENDWIN
 152          && !isendwin()
 153  #endif
 154          )
 155          endwin();
 156      rb_stdscr = 0;
 157      rb_gc_unregister_address(&rb_stdscr);
 158  }
 159  
 160  /* def closed? */
 161  static VALUE
 162  curses_closed()
 163  {
 164  #ifdef HAVE_ISENDWIN
 165      if (isendwin()) {
 166          return Qtrue;
 167      }
 168      return Qfalse;
 169  #else
 170      rb_notimplement();
 171  #endif
 172  }
 173  
 174  /* def clear */
 175  static VALUE
 176  curses_clear(obj)
 177      VALUE obj;
 178  {
 179      wclear(stdscr);
 180      return Qnil;
 181  }
 182  
 183  /* def refresh */
 184  static VALUE
 185  curses_refresh(obj)
 186      VALUE obj;
 187  {
 188      refresh();
 189      return Qnil;
 190  }
 191  
 192  /* def doupdate */
 193  static VALUE
 194  curses_doupdate(obj)
 195      VALUE obj;
 196  {
 197  #ifdef HAVE_DOUPDATE
 198      doupdate();
 199  #else
 200      refresh();
 201  #endif
 202      return Qnil;
 203  }
 204  
 205  /* def echo */
 206  static VALUE
 207  curses_echo(obj)
 208      VALUE obj;
 209  {
 210      echo();
 211      return Qnil;
 212  }
 213  
 214  /* def noecho */
 215  static VALUE
 216  curses_noecho(obj)
 217      VALUE obj;
 218  {
 219      noecho();
 220      return Qnil;
 221  }
 222  
 223  /* def raw */
 224  static VALUE
 225  curses_raw(obj)
 226      VALUE obj;
 227  {
 228      raw();
 229      return Qnil;
 230  }
 231  
 232  /* def noraw */
 233  static VALUE
 234  curses_noraw(obj)
 235      VALUE obj;
 236  {
 237      noraw();
 238      return Qnil;
 239  }
 240  
 241  /* def cbreak */
 242  static VALUE
 243  curses_cbreak(obj)
 244      VALUE obj;
 245  {
 246      cbreak();
 247      return Qnil;
 248  }
 249  
 250  /* def nocbreak */
 251  static VALUE
 252  curses_nocbreak(obj)
 253      VALUE obj;
 254  {
 255      nocbreak();
 256      return Qnil;
 257  }
 258  
 259  /* def nl */
 260  static VALUE
 261  curses_nl(obj)
 262      VALUE obj;
 263  {
 264      nl();
 265      return Qnil;
 266  }
 267  
 268  /* def nonl */
 269  static VALUE
 270  curses_nonl(obj)
 271      VALUE obj;
 272  {
 273      nonl();
 274      return Qnil;
 275  }
 276  
 277  /* def beep */
 278  static VALUE
 279  curses_beep(obj)
 280      VALUE obj;
 281  {
 282  #ifdef HAVE_BEEP
 283      beep();
 284  #endif
 285      return Qnil;
 286  }
 287  
 288  /* def flash */
 289  static VALUE
 290  curses_flash(obj)
 291      VALUE obj;
 292  {
 293  #ifdef HAVE_FLASH
 294      flash();
 295  #endif
 296      return Qnil;
 297  }
 298  
 299  /* def ungetch */
 300  static VALUE
 301  curses_ungetch(obj, ch)
 302      VALUE obj;
 303      VALUE ch;
 304  {
 305  #ifdef HAVE_UNGETCH
 306      ungetch(NUM2INT(ch));
 307  #else
 308      rb_notimplement();
 309  #endif
 310      return Qnil;
 311  }
 312  
 313  /* def setpos(y, x) */
 314  static VALUE
 315  curses_setpos(obj, y, x)
 316      VALUE obj;
 317      VALUE y;
 318      VALUE x;
 319  {
 320      move(NUM2INT(y), NUM2INT(x));
 321      return Qnil;
 322  }
 323  
 324  /* def standout */
 325  static VALUE
 326  curses_standout(obj)
 327      VALUE obj;
 328  {
 329      standout();
 330      return Qnil;
 331  }
 332  
 333  /* def standend */
 334  static VALUE
 335  curses_standend(obj)
 336      VALUE obj;
 337  {
 338      standend();
 339      return Qnil;
 340  }
 341  
 342  /* def inch */
 343  static VALUE
 344  curses_inch(obj)
 345      VALUE obj;
 346  {
 347      return CHR2FIX(inch());
 348  }
 349  
 350  /* def addch(ch) */
 351  static VALUE
 352  curses_addch(obj, ch)
 353      VALUE obj;
 354      VALUE ch;
 355  {
 356      addch(NUM2CHR(ch));
 357      return Qnil;
 358  }
 359  
 360  /* def insch(ch) */
 361  static VALUE
 362  curses_insch(obj, ch)
 363      VALUE obj;
 364      VALUE ch;
 365  {
 366      insch(NUM2CHR(ch));
 367      return Qnil;
 368  }
 369  
 370  /* def addstr(str) */
 371  static VALUE
 372  curses_addstr(obj, str)
 373      VALUE obj;
 374      VALUE str;
 375  {
 376      if (!NIL_P(str)) {
 377          addstr(STR2CSTR(str));
 378      }
 379      return Qnil;
 380  }
 381  
 382  /* def getch */
 383  static VALUE
 384  curses_getch(obj)
 385      VALUE obj;
 386  {
 387      rb_read_check(stdin);
 388      return UINT2NUM(getch());
 389  }
 390  
 391  /* def getstr */
 392  static VALUE
 393  curses_getstr(obj)
 394      VALUE obj;
 395  {
 396      char rtn[1024]; /* This should be big enough.. I hope */
 397  
 398      rb_read_check(stdin);
 399  #if defined(HAVE_GETNSTR)
 400      getnstr(rtn,1023);
 401  #else
 402      getstr(rtn);
 403  #endif
 404      return rb_tainted_str_new2(rtn);
 405  }
 406  
 407  /* def delch */
 408  static VALUE
 409  curses_delch(obj)
 410      VALUE obj;
 411  {
 412      delch();
 413      return Qnil;
 414  }
 415  
 416  /* def delelteln */
 417  static VALUE
 418  curses_deleteln(obj)
 419      VALUE obj;
 420  {
 421  #if defined(HAVE_DELETELN) || defined(deleteln)
 422      deleteln();
 423  #endif
 424      return Qnil;
 425  }
 426  
 427  /* def keyname */
 428  static VALUE
 429  curses_keyname(obj, c)
 430      VALUE obj;
 431      VALUE c;
 432  {
 433  #ifdef HAVE_KEYNAME
 434    const char *name;
 435  
 436    name = keyname(NUM2INT(c));
 437    if (name) {
 438      return rb_str_new2(name);
 439    } else {
 440      return Qnil;
 441    }
 442  #else
 443    return Qnil;
 444  #endif
 445  }
 446  
 447  static VALUE
 448  curses_lines()
 449  {
 450      return INT2FIX(LINES);
 451  }
 452  
 453  static VALUE
 454  curses_cols()
 455  {
 456      return INT2FIX(COLS);
 457  }
 458  
 459  static VALUE
 460  curses_curs_set(VALUE obj, VALUE visibility)
 461  {
 462    int n;
 463    return (n = curs_set(NUM2INT(visibility)) != ERR) ? INT2FIX(n) : Qnil;
 464  }
 465  
 466  static VALUE
 467  curses_scrl(VALUE obj, VALUE n)
 468  {
 469    /* may have to raise exception on ERR */
 470    return (scrl(NUM2INT(n)) == OK) ? Qtrue : Qfalse;
 471  }
 472  
 473  static VALUE
 474  curses_setscrreg(VALUE obj, VALUE top, VALUE bottom)
 475  {
 476    /* may have to raise exception on ERR */
 477    return (setscrreg(NUM2INT(top), NUM2INT(bottom)) == OK) ? Qtrue : Qfalse;
 478  }
 479  
 480  static VALUE
 481  curses_attroff(VALUE obj, VALUE attrs)
 482  {
 483    return window_attroff(rb_stdscr,attrs);  
 484    /* return INT2FIX(attroff(NUM2INT(attrs))); */
 485  }
 486  
 487  static VALUE
 488  curses_attron(VALUE obj, VALUE attrs)
 489  {
 490    return window_attron(rb_stdscr,attrs);
 491    /* return INT2FIX(attroff(NUM2INT(attrs))); */
 492  }
 493  
 494  static VALUE
 495  curses_attrset(VALUE obj, VALUE attrs)
 496  {
 497    return window_attrset(rb_stdscr,attrs);
 498    /* return INT2FIX(attroff(NUM2INT(attrs))); */
 499  }
 500  
 501  static VALUE
 502  curses_bkgdset(VALUE obj, VALUE ch)
 503  {
 504    bkgdset(NUM2CHR(ch));
 505    return Qnil;
 506  }
 507  
 508  static VALUE
 509  curses_bkgd(VALUE obj, VALUE ch)
 510  {
 511    return CHR2FIX(bkgd(NUM2CHR(ch)));
 512  }
 513  
 514  static VALUE
 515  curses_resizeterm(VALUE obj, VALUE lines, VALUE columns)
 516  {
 517  #if defined(HAVE_RESIZETERM)
 518    return (resizeterm(NUM2INT(lines),NUM2INT(columns)) == OK) ? Qtrue : Qfalse;
 519  #else
 520    return Qnil;
 521  #endif
 522  }
 523  
 524  #ifdef USE_COLOR
 525  static VALUE
 526  curses_start_color(VALUE obj)
 527  {
 528    /* may have to raise exception on ERR */
 529    return (start_color() == OK) ? Qtrue : Qfalse;
 530  }
 531  
 532  static VALUE
 533  curses_init_pair(VALUE obj, VALUE pair, VALUE f, VALUE b)
 534  {
 535    /* may have to raise exception on ERR */
 536    return (init_pair(NUM2INT(pair),NUM2INT(f),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
 537  }
 538  
 539  static VALUE
 540  curses_init_color(VALUE obj, VALUE color, VALUE r, VALUE g, VALUE b)
 541  {
 542    /* may have to raise exception on ERR */
 543    return (init_color(NUM2INT(color),NUM2INT(r),
 544                       NUM2INT(g),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
 545  }
 546  
 547  static VALUE
 548  curses_has_colors(VALUE obj)
 549  {
 550    return has_colors() ? Qtrue : Qfalse;
 551  }
 552  
 553  static VALUE
 554  curses_can_change_color(VALUE obj)
 555  {
 556    return can_change_color() ? Qtrue : Qfalse;
 557  }
 558  
 559  static VALUE
 560  curses_color_content(VALUE obj, VALUE color)
 561  {
 562    short r,g,b;
 563  
 564    color_content(NUM2INT(color),&r,&g,&b);
 565    return rb_ary_new3(3,INT2FIX(r),INT2FIX(g),INT2FIX(b));
 566  }
 567  
 568  static VALUE
 569  curses_pair_content(VALUE obj, VALUE pair)
 570  {
 571    short f,b;
 572  
 573    pair_content(NUM2INT(pair),&f,&b);
 574    return rb_ary_new3(2,INT2FIX(f),INT2FIX(b));
 575  }
 576  
 577  static VALUE
 578  curses_color_pair(VALUE obj, VALUE attrs)
 579  {
 580    return INT2FIX(COLOR_PAIR(NUM2INT(attrs)));
 581  }
 582  
 583  static VALUE
 584  curses_pair_number(VALUE obj, VALUE attrs)
 585  {
 586    return INT2FIX(PAIR_NUMBER(NUM2INT(attrs)));
 587  }
 588  #endif
 589  
 590  #ifdef USE_MOUSE
 591  struct mousedata {
 592    MEVENT *mevent;
 593  };
 594  
 595  static void
 596  no_mevent()
 597  {
 598    rb_raise(rb_eRuntimeError, "no such mouse event");
 599  };
 600  
 601  #define GetMOUSE(obj, data) {\
 602      Data_Get_Struct(obj, struct mousedata, data);\
 603      if (data->mevent == 0) no_mevent();\
 604  }
 605  
 606  static void
 607  curses_mousedata_free(struct mousedata *mdata)
 608  {
 609    if (mdata->mevent)
 610      free(mdata->mevent);
 611  };
 612  
 613  static VALUE
 614  curses_getmouse(VALUE obj)
 615  {
 616    struct mousedata *mdata;
 617    VALUE val;
 618  
 619    val = Data_Make_Struct(cMouseEvent,struct mousedata,
 620                           0,curses_mousedata_free,mdata);
 621    mdata->mevent = (MEVENT*)malloc(sizeof(MEVENT));
 622    return (getmouse(mdata->mevent) == OK) ? val : Qnil;
 623  };
 624  
 625  static VALUE
 626  curses_ungetmouse(VALUE obj, VALUE mevent)
 627  {
 628    struct mousedata *mdata;
 629  
 630    GetMOUSE(mevent,mdata);
 631    return (ungetmouse(mdata->mevent) == OK) ? Qtrue : Qfalse;
 632  };
 633  
 634  static VALUE
 635  curses_mouseinterval(VALUE obj, VALUE interval)
 636  {
 637    return mouseinterval(NUM2INT(interval)) ? Qtrue : Qfalse;
 638  };
 639  
 640  static VALUE
 641  curses_mousemask(VALUE obj, VALUE mask)
 642  {
 643    return INT2NUM(mousemask(NUM2UINT(mask),NULL));
 644  };
 645  
 646  #define DEFINE_MOUSE_GET_MEMBER(func_name,mem) \
 647  static VALUE func_name (VALUE mouse) \
 648  { \
 649    struct mousedata *mdata; \
 650    GetMOUSE(mouse, mdata); \
 651    return (UINT2NUM(mdata->mevent -> mem)); \
 652  }
 653  
 654  DEFINE_MOUSE_GET_MEMBER(curs_mouse_id, id);
 655  DEFINE_MOUSE_GET_MEMBER(curs_mouse_x, x);
 656  DEFINE_MOUSE_GET_MEMBER(curs_mouse_y, y);
 657  DEFINE_MOUSE_GET_MEMBER(curs_mouse_z, z);
 658  DEFINE_MOUSE_GET_MEMBER(curs_mouse_bstate, bstate);
 659  #undef define_curs_mouse_member
 660  #endif /* USE_MOUSE */
 661  
 662  /*-------------------------- class Window --------------------------*/
 663  
 664  /* def self.allocate */
 665  static VALUE
 666  window_s_allocate(class)
 667      VALUE class;
 668  {
 669      struct windata *winp;
 670  
 671      return Data_Make_Struct(class, struct windata, 0, free_window, winp);
 672  }
 673  
 674  /* def initialize(h, w, top, left) */
 675  static VALUE
 676  window_initialize(obj, h, w, top, left)
 677      VALUE obj;
 678      VALUE h;
 679      VALUE w;
 680      VALUE top;
 681      VALUE left;
 682  {
 683      struct windata *winp;
 684      WINDOW *window;
 685  
 686      curses_init_screen();
 687      Data_Get_Struct(obj, struct windata, winp);
 688      if (winp->window) delwin(winp->window);
 689      window = newwin(NUM2INT(h), NUM2INT(w), NUM2INT(top), NUM2INT(left));
 690      wclear(window);
 691      winp->window = window;
 692  
 693      return obj;
 694  }
 695  
 696  /* def subwin(h, w, top, left) */
 697  static VALUE
 698  window_subwin(obj, h, w, top, left)
 699      VALUE obj;
 700      VALUE h;
 701      VALUE w;
 702      VALUE top;
 703      VALUE left;
 704  {
 705      struct windata *winp;
 706      WINDOW *window;
 707      VALUE win;
 708  
 709      GetWINDOW(obj, winp);
 710      window = subwin(winp->window, NUM2INT(h), NUM2INT(w),
 711                                    NUM2INT(top), NUM2INT(left));
 712      win = prep_window(rb_obj_class(obj), window);
 713  
 714      return win;
 715  }
 716  
 717  /* def close */
 718  static VALUE
 719  window_close(obj)
 720      VALUE obj;
 721  {
 722      struct windata *winp;
 723      
 724      GetWINDOW(obj, winp);
 725      delwin(winp->window);
 726      winp->window = 0;
 727  
 728      return Qnil;
 729  }
 730  
 731  /* def clear */
 732  static VALUE
 733  window_clear(obj)
 734      VALUE obj;
 735  {
 736      struct windata *winp;
 737      
 738      GetWINDOW(obj, winp);
 739      wclear(winp->window);
 740      
 741      return Qnil;
 742  }
 743  
 744  /* def refresh */
 745  static VALUE
 746  window_refresh(obj)
 747      VALUE obj;
 748  {
 749      struct windata *winp;
 750      
 751      GetWINDOW(obj, winp);
 752      wrefresh(winp->window);
 753      
 754      return Qnil;
 755  }
 756  
 757  /* def noutrefresh */
 758  static VALUE
 759  window_noutrefresh(obj)
 760      VALUE obj;
 761  {
 762      struct windata *winp;
 763  
 764      GetWINDOW(obj, winp);
 765  #ifdef HAVE_DOUPDATE
 766      wnoutrefresh(winp->window);
 767  #else
 768      wrefresh(winp->window);
 769  #endif
 770  
 771      return Qnil;
 772  }
 773  
 774  /* def move(y, x) */
 775  static VALUE
 776  window_move(obj, y, x)
 777      VALUE obj;
 778      VALUE y;
 779      VALUE x;
 780  {
 781      struct windata *winp;
 782      
 783      GetWINDOW(obj, winp);
 784      mvwin(winp->window, NUM2INT(y), NUM2INT(x));
 785  
 786      return Qnil;
 787  }
 788  
 789  /* def setpos(y, x) */
 790  static VALUE
 791  window_setpos(obj, y, x)
 792      VALUE obj;
 793      VALUE y;
 794      VALUE x;
 795  {
 796      struct windata *winp;
 797      
 798      GetWINDOW(obj, winp);
 799      wmove(winp->window, NUM2INT(y), NUM2INT(x));
 800      return Qnil;
 801  }
 802  
 803  /* def cury */
 804  static VALUE
 805  window_cury(obj)
 806      VALUE obj;
 807  {
 808      struct windata *winp;
 809      int x, y;
 810  
 811      GetWINDOW(obj, winp);
 812      getyx(winp->window, y, x);
 813      return INT2FIX(y);
 814  }
 815  
 816  /* def curx */
 817  static VALUE
 818  window_curx(obj)
 819      VALUE obj;
 820  {
 821      struct windata *winp;
 822      int x, y;
 823  
 824      GetWINDOW(obj, winp);
 825      getyx(winp->window, y, x);
 826      return INT2FIX(x);
 827  }
 828  
 829  /* def maxy */
 830  static VALUE
 831  window_maxy(obj)
 832      VALUE obj;
 833  {
 834      struct windata *winp;
 835      int x, y;
 836  
 837      GetWINDOW(obj, winp);
 838  #ifdef getmaxy
 839      return INT2FIX(getmaxy(winp->window));
 840  #else
 841  #ifdef getmaxyx
 842      getmaxyx(winp->window, y, x);
 843      return INT2FIX(y);
 844  #else
 845      return INT2FIX(winp->window->_maxy+1);
 846  #endif
 847  #endif
 848  }
 849  
 850  /* def maxx */
 851  static VALUE
 852  window_maxx(obj)
 853      VALUE obj;
 854  {
 855      struct windata *winp;
 856      int x, y;
 857  
 858      GetWINDOW(obj, winp);
 859  #ifdef getmaxx
 860      return INT2FIX(getmaxx(winp->window));
 861  #else
 862  #ifdef getmaxyx
 863      getmaxyx(winp->window, y, x);
 864      return INT2FIX(x);
 865  #else
 866      return INT2FIX(winp->window->_maxx+1);
 867  #endif
 868  #endif
 869  }
 870  
 871  /* def begy */
 872  static VALUE
 873  window_begy(obj)
 874      VALUE obj;
 875  {
 876      struct windata *winp;
 877      int x, y;
 878  
 879      GetWINDOW(obj, winp);
 880  #ifdef getbegyx
 881      getbegyx(winp->window, y, x);
 882      return INT2FIX(y);
 883  #else
 884      return INT2FIX(winp->window->_begy);
 885  #endif
 886  }
 887  
 888  /* def begx */
 889  static VALUE
 890  window_begx(obj)
 891      VALUE obj;
 892  {
 893      struct windata *winp;
 894      int x, y;
 895  
 896      GetWINDOW(obj, winp);
 897  #ifdef getbegyx
 898      getbegyx(winp->window, y, x);
 899      return INT2FIX(x);
 900  #else
 901      return INT2FIX(winp->window->_begx);
 902  #endif
 903  }
 904  
 905  /* def box(vert, hor) */
 906  static VALUE
 907  window_box(argc, argv, self)
 908      int argc;
 909      VALUE argv[], self;
 910  {
 911      struct windata *winp; 
 912      VALUE vert, hor, corn;
 913  
 914      rb_scan_args(argc, argv, "21", &vert, &hor, &corn);
 915  
 916      GetWINDOW(self, winp);
 917      box(winp->window, NUM2CHR(vert), NUM2CHR(hor));
 918  
 919      if( argc == 3 ){
 920        int cur_x, cur_y, x, y;
 921        char c;
 922  
 923        c = NUM2CHR(corn);
 924        getyx(winp->window, cur_y, cur_x);
 925        x = NUM2INT(window_maxx(self)) - 1;
 926        y = NUM2INT(window_maxy(self)) - 1;
 927        wmove(winp->window, 0, 0);
 928        waddch(winp->window, c);
 929        wmove(winp->window, y, 0);
 930        waddch(winp->window, c);
 931        wmove(winp->window, y, x);
 932        waddch(winp->window, c);
 933        wmove(winp->window, 0, x);
 934        waddch(winp->window, c);
 935        wmove(winp->window, cur_y, cur_x);
 936      }
 937      
 938      return Qnil;
 939  }
 940  
 941  /* def standout */
 942  static VALUE
 943  window_standout(obj)
 944      VALUE obj;
 945  {
 946      struct windata *winp;
 947      
 948      GetWINDOW(obj, winp);
 949      wstandout(winp->window);
 950      return Qnil;
 951  }
 952  
 953  /* def standend */
 954  static VALUE
 955  window_standend(obj)
 956      VALUE obj;
 957  {
 958      struct windata *winp;
 959      
 960      GetWINDOW(obj, winp);
 961      wstandend(winp->window);
 962      return Qnil;
 963  }
 964  
 965  /* def inch */
 966  static VALUE
 967  window_inch(obj)
 968      VALUE obj;
 969  {
 970      struct windata *winp;
 971      
 972      GetWINDOW(obj, winp);
 973      return CHR2FIX(winch(winp->window));
 974  }
 975  
 976  /* def addch(ch) */
 977  static VALUE
 978  window_addch(obj, ch)
 979      VALUE obj;
 980      VALUE ch;
 981  {
 982      struct windata *winp;
 983      
 984      GetWINDOW(obj, winp);
 985      waddch(winp->window, NUM2CHR(ch));
 986      
 987      return Qnil;
 988  }
 989  
 990  /* def insch(ch) */
 991  static VALUE
 992  window_insch(obj, ch)
 993      VALUE obj;
 994      VALUE ch;
 995  {
 996      struct windata *winp;
 997      
 998      GetWINDOW(obj, winp);
 999      winsch(winp->window, NUM2CHR(ch));
1000      
1001      return Qnil;
1002  }
1003  
1004  /* def addstr(str) */
1005  static VALUE
1006  window_addstr(obj, str)
1007      VALUE obj;
1008      VALUE str;
1009  {
1010      if (!NIL_P(str)) {
1011          struct windata *winp;
1012  
1013          GetWINDOW(obj, winp);
1014          waddstr(winp->window, STR2CSTR(str));
1015      }
1016      return Qnil;
1017  }
1018  
1019  /* def <<(str) */
1020  static VALUE
1021  window_addstr2(obj, str)
1022      VALUE obj;
1023      VALUE str;
1024  {
1025      window_addstr(obj, str);
1026      return obj;
1027  }
1028  
1029  /* def getch */
1030  static VALUE
1031  window_getch(obj)
1032      VALUE obj;
1033  {
1034      struct windata *winp;
1035      
1036      rb_read_check(stdin);
1037      GetWINDOW(obj, winp);
1038      return UINT2NUM(wgetch(winp->window));
1039  }
1040  
1041  /* def getstr */
1042  static VALUE
1043  window_getstr(obj)
1044      VALUE obj;
1045  {
1046      struct windata *winp;
1047      char rtn[1024]; /* This should be big enough.. I hope */
1048      
1049      GetWINDOW(obj, winp);
1050      rb_read_check(stdin);
1051  #if defined(HAVE_WGETNSTR)
1052      wgetnstr(winp->window, rtn, 1023);
1053  #else
1054      wgetstr(winp->window, rtn);
1055  #endif
1056      return rb_tainted_str_new2(rtn);
1057  }
1058  
1059  /* def delch */
1060  static VALUE
1061  window_delch(obj)
1062      VALUE obj;
1063  {
1064      struct windata *winp;
1065      
1066      GetWINDOW(obj, winp);
1067      wdelch(winp->window);
1068      return Qnil;
1069  }
1070  
1071  /* def delelteln */
1072  static VALUE
1073  window_deleteln(obj)
1074      VALUE obj;
1075  {
1076  #if defined(HAVE_WDELETELN) || defined(wdeleteln)
1077      struct windata *winp;
1078      
1079      GetWINDOW(obj, winp);
1080      wdeleteln(winp->window);
1081  #endif
1082      return Qnil;
1083  }
1084  
1085  static VALUE
1086  window_scrollok(VALUE obj, VALUE bf)
1087  {
1088    struct windata *winp;
1089  
1090    GetWINDOW(obj, winp);
1091    scrollok(winp->window, RTEST(bf) ? TRUE : FALSE);
1092    return Qnil;
1093  }
1094  
1095  static VALUE
1096  window_idlok(VALUE obj, VALUE bf)
1097  {
1098    struct windata *winp;
1099    int res;
1100  
1101    GetWINDOW(obj, winp);
1102    idlok(winp->window, RTEST(bf) ? TRUE : FALSE);
1103    return Qnil;
1104  }
1105  
1106  static VALUE
1107  window_setscrreg(VALUE obj, VALUE top, VALUE bottom)
1108  {
1109    struct windata *winp;
1110    int res;
1111  
1112    GetWINDOW(obj, winp);
1113    res = wsetscrreg(winp->window, NUM2INT(top), NUM2INT(bottom));
1114    /* may have to raise exception on ERR */
1115    return (res == OK) ? Qtrue : Qfalse;
1116  };
1117  
1118  static VALUE
1119  window_scroll(VALUE obj)
1120  {
1121    struct windata *winp;
1122  
1123    GetWINDOW(obj, winp);
1124    /* may have to raise exception on ERR */
1125    return (scroll(winp->window) == OK) ? Qtrue : Qfalse;
1126  }
1127  
1128  static VALUE
1129  window_scrl(VALUE obj, VALUE n)
1130  {
1131    struct windata *winp;
1132  
1133    GetWINDOW(obj, winp);
1134    /* may have to raise exception on ERR */
1135    return (wscrl(winp->window,NUM2INT(n)) == OK) ? Qtrue : Qfalse;
1136  }
1137  
1138  static VALUE
1139  window_attroff(VALUE obj, VALUE attrs)
1140  {
1141    struct windata *winp;
1142  
1143    GetWINDOW(obj,winp);
1144    return INT2FIX(wattroff(winp->window,NUM2INT(attrs)));
1145  };
1146  
1147  static VALUE
1148  window_attron(VALUE obj, VALUE attrs)
1149  {
1150    struct windata *winp;
1151    VALUE val;
1152  
1153    GetWINDOW(obj,winp);
1154    val = INT2FIX(wattron(winp->window,NUM2INT(attrs)));
1155    if( rb_block_given_p() ){
1156      rb_yield(val);
1157      wattroff(winp->window,NUM2INT(attrs));
1158      return val;
1159    }
1160    else{
1161      return val;
1162    };
1163  };
1164  
1165  static VALUE
1166  window_attrset(VALUE obj, VALUE attrs)
1167  {
1168    struct windata *winp;
1169  
1170    GetWINDOW(obj,winp);
1171    return INT2FIX(wattrset(winp->window,NUM2INT(attrs)));
1172  }
1173  
1174  static VALUE
1175  window_bkgdset(VALUE obj, VALUE ch)
1176  {
1177    struct windata *winp;
1178  
1179    GetWINDOW(obj,winp);
1180    wbkgdset(winp->window, NUM2CHR(ch));
1181    return Qnil;
1182  }
1183  
1184  static VALUE
1185  window_bkgd(VALUE obj, VALUE ch)
1186  {
1187    struct windata *winp;
1188  
1189    GetWINDOW(obj,winp);
1190    return CHR2FIX(wbkgd(winp->window, NUM2CHR(ch)));
1191  }
1192  
1193  static VALUE
1194  window_getbkgd(VALUE obj)
1195  {
1196    struct windata *winp;
1197  
1198    GetWINDOW(obj,winp);
1199    return CHR2FIX(getbkgd(winp->window));
1200  }
1201  
1202  static VALUE
1203  window_resize(VALUE obj, VALUE lines, VALUE columns)
1204  {
1205  #if defined(HAVE_WRESIZE)
1206    struct windata *winp;
1207  
1208    GetWINDOW(obj,winp);
1209    return wresize(winp->window, NUM2INT(lines), NUM2INT(columns)) == OK ? Qtrue : Qfalse;
1210  #else
1211    return Qnil;
1212  #endif
1213  }
1214  
1215  
1216  #ifdef HAVE_KEYPAD
1217  static VALUE
1218  window_keypad(VALUE obj, VALUE val)
1219  {
1220    struct windata *winp;
1221  
1222    GetWINDOW(obj,winp);
1223    /* keypad() of NetBSD's libcurses returns no value */
1224  #if defined(__NetBSD__) && !defined(NCURSES_VERSION)
1225    keypad(winp->window,(RTEST(val) ? TRUE : FALSE));
1226    return Qnil;
1227  #else
1228    /* may have to raise exception on ERR */
1229    return (keypad(winp->window,RTEST(val) ? TRUE : FALSE)) == OK ?
1230      Qtrue : Qfalse;
1231  #endif
1232  };
1233  #endif /* HAVE_KEYPAD */
1234  
1235  /*------------------------- Initialization -------------------------*/
1236  void
1237  Init_curses()
1238  {
1239      mCurses    = rb_define_module("Curses");
1240      mKey       = rb_define_module_under(mCurses, "Key");
1241  
1242      rb_gc_register_address(&rb_stdscr);
1243  
1244  #ifdef USE_MOUSE
1245      cMouseEvent = rb_define_class_under(mCurses,"MouseEvent",rb_cObject);
1246      rb_undef_method(CLASS_OF(cMouseEvent),"new");
1247      rb_define_method(cMouseEvent, "eid", curs_mouse_id, 0);
1248      rb_define_method(cMouseEvent, "x", curs_mouse_x, 0);
1249      rb_define_method(cMouseEvent, "y", curs_mouse_y, 0);
1250      rb_define_method(cMouseEvent, "z", curs_mouse_z, 0);
1251      rb_define_method(cMouseEvent, "bstate", curs_mouse_bstate, 0);
1252  #endif /* USE_MOUSE */
1253  
1254      rb_define_module_function(mCurses, "init_screen", curses_init_screen, 0);
1255      rb_define_module_function(mCurses, "close_screen", curses_close_screen, 0);
1256      rb_define_module_function(mCurses, "closed?", curses_closed, 0);
1257      rb_define_module_function(mCurses, "stdscr", curses_stdscr, 0);
1258      rb_define_module_function(mCurses, "refresh", curses_refresh, 0);
1259      rb_define_module_function(mCurses, "doupdate", curses_doupdate, 0);
1260      rb_define_module_function(mCurses, "clear", curses_clear, 0);
1261      rb_define_module_function(mCurses, "echo", curses_echo, 0);
1262      rb_define_module_function(mCurses, "noecho", curses_noecho, 0);
1263      rb_define_module_function(mCurses, "raw", curses_raw, 0);
1264      rb_define_module_function(mCurses, "noraw", curses_noraw, 0);
1265      rb_define_module_function(mCurses, "cbreak", curses_cbreak, 0);
1266      rb_define_module_function(mCurses, "nocbreak", curses_nocbreak, 0);
1267      rb_define_alias(mCurses, "crmode", "cbreak");
1268      rb_define_alias(mCurses, "nocrmode", "nocbreak");
1269      rb_define_module_function(mCurses, "nl", curses_nl, 0);
1270      rb_define_module_function(mCurses, "nonl", curses_nonl, 0);
1271      rb_define_module_function(mCurses, "beep", curses_beep, 0);
1272      rb_define_module_function(mCurses, "flash", curses_flash, 0);
1273      rb_define_module_function(mCurses, "ungetch", curses_ungetch, 1);
1274      rb_define_module_function(mCurses, "setpos", curses_setpos, 2);
1275      rb_define_module_function(mCurses, "standout", curses_standout, 0);
1276      rb_define_module_function(mCurses, "standend", curses_standend, 0);
1277      rb_define_module_function(mCurses, "inch", curses_inch, 0);
1278      rb_define_module_function(mCurses, "addch", curses_addch, 1);
1279      rb_define_module_function(mCurses, "insch", curses_insch, 1);
1280      rb_define_module_function(mCurses, "addstr", curses_addstr, 1);
1281      rb_define_module_function(mCurses, "getch", curses_getch, 0);
1282      rb_define_module_function(mCurses, "getstr", curses_getstr, 0);
1283      rb_define_module_function(mCurses, "delch", curses_delch, 0);
1284      rb_define_module_function(mCurses, "deleteln", curses_deleteln, 0);
1285      rb_define_module_function(mCurses, "keyname", curses_keyname, 1);
1286      rb_define_module_function(mCurses, "lines", curses_lines, 0);
1287      rb_define_module_function(mCurses, "cols", curses_cols, 0);
1288      rb_define_module_function(mCurses, "curs_set", curses_curs_set, 1);
1289      rb_define_module_function(mCurses, "scrl", curses_scrl, 1);
1290      rb_define_module_function(mCurses, "setscrreg", curses_setscrreg, 2);
1291      rb_define_module_function(mCurses, "attroff", curses_attroff, 1);
1292      rb_define_module_function(mCurses, "attron", curses_attron, 1);
1293      rb_define_module_function(mCurses, "attrset", curses_attrset, 1);
1294      rb_define_module_function(mCurses, "bkgdset", curses_bkgdset, 1);
1295      rb_define_module_function(mCurses, "bkgd", curses_bkgd, 1);
1296      rb_define_module_function(mCurses, "resizeterm", curses_resizeterm, 2);
1297      rb_define_module_function(mCurses, "resize", curses_resizeterm, 2);
1298  #ifdef USE_COLOR
1299      rb_define_module_function(mCurses, "start_color", curses_start_color, 0);
1300      rb_define_module_function(mCurses, "init_pair", curses_init_pair, 3);
1301      rb_define_module_function(mCurses, "init_color", curses_init_color, 4);
1302      rb_define_module_function(mCurses, "has_colors?", curses_has_colors, 0);
1303      rb_define_module_function(mCurses, "can_change_color?",
1304                                curses_can_change_color, 0);
1305      rb_define_module_function(mCurses, "color_content", curses_color_content, 1);
1306      rb_define_module_function(mCurses, "pair_content", curses_pair_content, 1);
1307      rb_define_module_function(mCurses, "color_pair", curses_color_pair, 1);
1308      rb_define_module_function(mCurses, "pair_number", curses_pair_number, 1);
1309  #endif /* USE_COLOR */
1310  #ifdef USE_MOUSE
1311      rb_define_module_function(mCurses, "getmouse", curses_getmouse, 0);
1312      rb_define_module_function(mCurses, "ungetmouse", curses_getmouse, 1);
1313      rb_define_module_function(mCurses, "mouseinterval", curses_mouseinterval, 1);
1314      rb_define_module_function(mCurses, "mousemask", curses_mousemask, 1);
1315  #endif /* USE_MOUSE */
1316  
1317      cWindow = rb_define_class_under(mCurses, "Window", rb_cData);
1318      rb_define_singleton_method(cWindow, "allocate", window_s_allocate, 0);
1319      rb_define_method(cWindow, "initialize", window_initialize, 4);
1320      rb_define_method(cWindow, "subwin", window_subwin, 4);
1321      rb_define_method(cWindow, "close", window_close, 0);
1322      rb_define_method(cWindow, "clear", window_clear, 0);
1323      rb_define_method(cWindow, "refresh", window_refresh, 0);
1324      rb_define_method(cWindow, "noutrefresh", window_noutrefresh, 0);
1325      rb_define_method(cWindow, "box", window_box, -1);
1326      rb_define_method(cWindow, "move", window_move, 2);
1327      rb_define_method(cWindow, "setpos", window_setpos, 2);
1328      rb_define_method(cWindow, "cury", window_cury, 0);
1329      rb_define_method(cWindow, "curx", window_curx, 0);
1330      rb_define_method(cWindow, "maxy", window_maxy, 0);
1331      rb_define_method(cWindow, "maxx", window_maxx, 0);
1332      rb_define_method(cWindow, "begy", window_begy, 0);
1333      rb_define_method(cWindow, "begx", window_begx, 0);
1334      rb_define_method(cWindow, "standout", window_standout, 0);
1335      rb_define_method(cWindow, "standend", window_standend, 0);
1336      rb_define_method(cWindow, "inch", window_inch, 0);
1337      rb_define_method(cWindow, "addch", window_addch, 1);
1338      rb_define_method(cWindow, "insch", window_insch, 1);
1339      rb_define_method(cWindow, "addstr", window_addstr, 1);
1340      rb_define_method(cWindow, "<<", window_addstr2, 1);
1341      rb_define_method(cWindow, "getch", window_getch, 0);
1342      rb_define_method(cWindow, "getstr", window_getstr, 0);
1343      rb_define_method(cWindow, "delch", window_delch, 0);
1344      rb_define_method(cWindow, "deleteln", window_deleteln, 0);
1345      rb_define_method(cWindow, "scroll", window_scroll, 0);
1346      rb_define_method(cWindow, "scrollok", window_scrollok, 1);
1347      rb_define_method(cWindow, "idlok", window_idlok, 1);
1348      rb_define_method(cWindow, "setscrreg", window_setscrreg, 2);
1349      rb_define_method(cWindow, "scrl", window_scrl, 1);
1350      rb_define_method(cWindow, "resize", window_resize, 2);
1351  #ifdef HAVE_KEYPAD
1352      rb_define_method(cWindow, "keypad", window_keypad, 1);
1353      rb_define_method(cWindow, "keypad=", window_keypad, 1);
1354  #endif
1355  #ifdef USE_COLOR
1356      rb_define_method(cWindow, "attroff", window_attroff, 1);
1357      rb_define_method(cWindow, "attron", window_attron, 1);
1358      rb_define_method(cWindow, "attrset", window_attrset, 1);
1359      rb_define_method(cWindow, "bkgdset", window_bkgdset, 1);
1360      rb_define_method(cWindow, "bkgd", window_bkgd, 1);
1361      rb_define_method(cWindow, "getbkgd", window_getbkgd, 0);
1362  #endif /* USE_COLOR */
1363  
1364  
1365  #define rb_curses_define_const(c) rb_define_const(mCurses,#c,UINT2NUM(c))
1366  
1367  #ifdef USE_COLOR
1368      rb_curses_define_const(A_ATTRIBUTES);
1369  #ifdef A_NORMAL
1370      rb_curses_define_const(A_NORMAL);
1371  #endif
1372      rb_curses_define_const(A_STANDOUT);
1373      rb_curses_define_const(A_UNDERLINE);
1374      rb_curses_define_const(A_REVERSE);
1375      rb_curses_define_const(A_BLINK);
1376      rb_curses_define_const(A_DIM);
1377      rb_curses_define_const(A_BOLD);
1378      rb_curses_define_const(A_PROTECT);
1379  #ifdef A_INVIS /* for NetBSD */
1380      rb_curses_define_const(A_INVIS);
1381  #endif
1382      rb_curses_define_const(A_ALTCHARSET);
1383      rb_curses_define_const(A_CHARTEXT);
1384  #ifdef A_HORIZONTAL
1385      rb_curses_define_const(A_HORIZONTAL);
1386  #endif
1387  #ifdef A_LEFT
1388      rb_curses_define_const(A_LEFT);
1389  #endif
1390  #ifdef A_LOW
1391      rb_curses_define_const(A_LOW);
1392  #endif
1393  #ifdef A_RIGHT
1394      rb_curses_define_const(A_RIGHT);
1395  #endif
1396  #ifdef A_TOP
1397      rb_curses_define_const(A_TOP);
1398  #endif
1399  #ifdef A_VERTICAL
1400      rb_curses_define_const(A_VERTICAL);
1401  #endif
1402      rb_curses_define_const(A_COLOR);
1403  
1404  #ifdef COLORS
1405      rb_curses_define_const(COLORS);
1406  #endif
1407      rb_curses_define_const(COLOR_BLACK);
1408      rb_curses_define_const(COLOR_RED);
1409      rb_curses_define_const(COLOR_GREEN);
1410      rb_curses_define_const(COLOR_YELLOW);
1411      rb_curses_define_const(COLOR_BLUE);
1412      rb_curses_define_const(COLOR_MAGENTA);
1413      rb_curses_define_const(COLOR_CYAN);
1414      rb_curses_define_const(COLOR_WHITE);
1415  #endif /* USE_COLOR */
1416  #ifdef USE_MOUSE
1417  #ifdef BUTTON1_PRESSED
1418      rb_curses_define_const(BUTTON1_PRESSED);
1419  #endif
1420  #ifdef BUTTON1_RELEASED
1421      rb_curses_define_const(BUTTON1_RELEASED);
1422  #endif
1423  #ifdef BUTTON1_CLICKED
1424      rb_curses_define_const(BUTTON1_CLICKED);
1425  #endif
1426  #ifdef BUTTON1_DOUBLE_CLICKED
1427      rb_curses_define_const(BUTTON1_DOUBLE_CLICKED);
1428  #endif
1429  #ifdef BUTTON1_TRIPLE_CLICKED
1430      rb_curses_define_const(BUTTON1_TRIPLE_CLICKED);
1431  #endif
1432  #ifdef BUTTON2_PRESSED
1433      rb_curses_define_const(BUTTON2_PRESSED);
1434  #endif
1435  #ifdef BUTTON2_RELEASED
1436      rb_curses_define_const(BUTTON2_RELEASED);
1437  #endif
1438  #ifdef BUTTON2_CLICKED
1439      rb_curses_define_const(BUTTON2_CLICKED);
1440  #endif
1441  #ifdef BUTTON2_DOUBLE_CLICKED
1442      rb_curses_define_const(BUTTON2_DOUBLE_CLICKED);
1443  #endif
1444  #ifdef BUTTON2_TRIPLE_CLICKED
1445      rb_curses_define_const(BUTTON2_TRIPLE_CLICKED);
1446  #endif
1447  #ifdef BUTTON3_PRESSED
1448      rb_curses_define_const(BUTTON3_PRESSED);
1449  #endif
1450  #ifdef BUTTON3_RELEASED
1451      rb_curses_define_const(BUTTON3_RELEASED);
1452  #endif
1453  #ifdef BUTTON3_CLICKED
1454      rb_curses_define_const(BUTTON3_CLICKED);
1455  #endif
1456  #ifdef BUTTON3_DOUBLE_CLICKED
1457      rb_curses_define_const(BUTTON3_DOUBLE_CLICKED);
1458  #endif
1459  #ifdef BUTTON3_TRIPLE_CLICKED
1460      rb_curses_define_const(BUTTON3_TRIPLE_CLICKED);
1461  #endif
1462  #ifdef BUTTON4_PRESSED
1463      rb_curses_define_const(BUTTON4_PRESSED);
1464  #endif
1465  #ifdef BUTTON4_RELEASED
1466      rb_curses_define_const(BUTTON4_RELEASED);
1467  #endif
1468  #ifdef BUTTON4_CLICKED
1469      rb_curses_define_const(BUTTON4_CLICKED);
1470  #endif
1471  #ifdef BUTTON4_DOUBLE_CLICKED
1472      rb_curses_define_const(BUTTON4_DOUBLE_CLICKED);
1473  #endif
1474  #ifdef BUTTON4_TRIPLE_CLICKED
1475      rb_curses_define_const(BUTTON4_TRIPLE_CLICKED);
1476  #endif
1477  #ifdef BUTTON_SHIFT
1478      rb_curses_define_const(BUTTON_SHIFT);
1479  #endif
1480  #ifdef BUTTON_CTRL
1481      rb_curses_define_const(BUTTON_CTRL);
1482  #endif
1483  #ifdef BUTTON_ALT
1484      rb_curses_define_const(BUTTON_ALT);
1485  #endif
1486  #ifdef ALL_MOUSE_EVENTS
1487      rb_curses_define_const(ALL_MOUSE_EVENTS);
1488  #endif
1489  #ifdef REPORT_MOUSE_POSITION
1490      rb_curses_define_const(REPORT_MOUSE_POSITION);
1491  #endif
1492  #endif /* USE_MOUSE */
1493  
1494  #if defined(KEY_MOUSE) && defined(USE_MOUSE)
1495      rb_curses_define_const(KEY_MOUSE);
1496      rb_define_const(mKey, "MOUSE", INT2NUM(KEY_MOUSE));
1497  #endif
1498  #ifdef KEY_MIN
1499      rb_curses_define_const(KEY_MIN);
1500      rb_define_const(mKey, "MIN", INT2NUM(KEY_MIN));
1501  #endif
1502  #ifdef KEY_BREAK
1503      rb_curses_define_const(KEY_BREAK);
1504      rb_define_const(mKey, "BREAK", INT2NUM(KEY_BREAK));
1505  #endif
1506  #ifdef KEY_DOWN
1507      rb_curses_define_const(KEY_DOWN);
1508      rb_define_const(mKey, "DOWN", INT2NUM(KEY_DOWN));
1509  #endif
1510  #ifdef KEY_UP
1511      rb_curses_define_const(KEY_UP);
1512      rb_define_const(mKey, "UP", INT2NUM(KEY_UP));
1513  #endif
1514  #ifdef KEY_LEFT
1515      rb_curses_define_const(KEY_LEFT);
1516      rb_define_const(mKey, "LEFT", INT2NUM(KEY_LEFT));
1517  #endif
1518  #ifdef KEY_RIGHT
1519      rb_curses_define_const(KEY_RIGHT);
1520      rb_define_const(mKey, "RIGHT", INT2NUM(KEY_RIGHT));
1521  #endif
1522  #ifdef KEY_HOME
1523      rb_curses_define_const(KEY_HOME);
1524      rb_define_const(mKey, "HOME", INT2NUM(KEY_HOME));
1525  #endif
1526  #ifdef KEY_BACKSPACE
1527      rb_curses_define_const(KEY_BACKSPACE);
1528      rb_define_const(mKey, "BACKSPACE", INT2NUM(KEY_BACKSPACE));
1529  #endif
1530  #ifdef KEY_F
1531      /* KEY_F(n) : 0 <= n <= 63 */
1532      {
1533        int i;
1534        char c[8];
1535        for( i=0; i<64; i++ ){
1536          sprintf(c, "KEY_F%d", i);
1537          rb_define_const(mCurses, c, INT2NUM(KEY_F(i)));
1538          sprintf(c, "F%d", i);
1539          rb_define_const(mKey, c, INT2NUM(KEY_F(i)));
1540        };
1541      };
1542  #endif
1543  #ifdef KEY_DL
1544      rb_curses_define_const(KEY_DL);
1545      rb_define_const(mKey, "DL", INT2NUM(KEY_DL));
1546  #endif
1547  #ifdef KEY_IL
1548      rb_curses_define_const(KEY_IL);
1549      rb_define_const(mKey, "IL", INT2NUM(KEY_IL));
1550  #endif
1551  #ifdef KEY_DC
1552      rb_curses_define_const(KEY_DC);
1553      rb_define_const(mKey, "DC", INT2NUM(KEY_DC));
1554  #endif
1555  #ifdef KEY_IC
1556      rb_curses_define_const(KEY_IC);
1557      rb_define_const(mKey, "IC", INT2NUM(KEY_IC));
1558  #endif
1559  #ifdef KEY_EIC
1560      rb_curses_define_const(KEY_EIC);
1561      rb_define_const(mKey, "EIC", INT2NUM(KEY_EIC));
1562  #endif
1563  #ifdef KEY_CLEAR
1564      rb_curses_define_const(KEY_CLEAR);
1565      rb_define_const(mKey, "CLEAR", INT2NUM(KEY_CLEAR));
1566  #endif
1567  #ifdef KEY_EOS
1568      rb_curses_define_const(KEY_EOS);
1569      rb_define_const(mKey, "EOS", INT2NUM(KEY_EOS));
1570  #endif
1571  #ifdef KEY_EOL
1572      rb_curses_define_const(KEY_EOL);
1573      rb_define_const(mKey, "EOL", INT2NUM(KEY_EOL));
1574  #endif
1575  #ifdef KEY_SF
1576      rb_curses_define_const(KEY_SF);
1577      rb_define_const(mKey, "SF", INT2NUM(KEY_SF));
1578  #endif
1579  #ifdef KEY_SR
1580      rb_curses_define_const(KEY_SR);
1581      rb_define_const(mKey, "SR", INT2NUM(KEY_SR));
1582  #endif
1583  #ifdef KEY_NPAGE
1584      rb_curses_define_const(KEY_NPAGE);
1585      rb_define_const(mKey, "NPAGE", INT2NUM(KEY_NPAGE));
1586  #endif
1587  #ifdef KEY_PPAGE
1588      rb_curses_define_const(KEY_PPAGE);
1589      rb_define_const(mKey, "PPAGE", INT2NUM(KEY_PPAGE));
1590  #endif
1591  #ifdef KEY_STAB
1592      rb_curses_define_const(KEY_STAB);
1593      rb_define_const(mKey, "STAB", INT2NUM(KEY_STAB));
1594  #endif
1595  #ifdef KEY_CTAB
1596      rb_curses_define_const(KEY_CTAB);
1597      rb_define_const(mKey, "CTAB", INT2NUM(KEY_CTAB));
1598  #endif
1599  #ifdef KEY_CATAB
1600      rb_curses_define_const(KEY_CATAB);
1601      rb_define_const(mKey, "CATAB", INT2NUM(KEY_CATAB));
1602  #endif
1603  #ifdef KEY_ENTER
1604      rb_curses_define_const(KEY_ENTER);
1605      rb_define_const(mKey, "ENTER", INT2NUM(KEY_ENTER));
1606  #endif
1607  #ifdef KEY_SRESET
1608      rb_curses_define_const(KEY_SRESET);
1609      rb_define_const(mKey, "SRESET", INT2NUM(KEY_SRESET));
1610  #endif
1611  #ifdef KEY_RESET
1612      rb_curses_define_const(KEY_RESET);
1613      rb_define_const(mKey, "RESET", INT2NUM(KEY_RESET));
1614  #endif
1615  #ifdef KEY_PRINT
1616      rb_curses_define_const(KEY_PRINT);
1617      rb_define_const(mKey, "PRINT", INT2NUM(KEY_PRINT));
1618  #endif
1619  #ifdef KEY_LL
1620      rb_curses_define_const(KEY_LL);
1621      rb_define_const(mKey, "LL", INT2NUM(KEY_LL));
1622  #endif
1623  #ifdef KEY_A1
1624      rb_curses_define_const(KEY_A1);
1625      rb_define_const(mKey, "A1", INT2NUM(KEY_A1));
1626  #endif
1627  #ifdef KEY_A3
1628      rb_curses_define_const(KEY_A3);
1629      rb_define_const(mKey, "A3", INT2NUM(KEY_A3));
1630  #endif
1631  #ifdef KEY_B2
1632      rb_curses_define_const(KEY_B2);
1633      rb_define_const(mKey, "B2", INT2NUM(KEY_B2));
1634  #endif
1635  #ifdef KEY_C1
1636      rb_curses_define_const(KEY_C1);
1637      rb_define_const(mKey, "C1", INT2NUM(KEY_C1));
1638  #endif
1639  #ifdef KEY_C3
1640      rb_curses_define_const(KEY_C3);
1641      rb_define_const(mKey, "C3", INT2NUM(KEY_C3));
1642  #endif
1643  #ifdef KEY_BTAB
1644      rb_curses_define_const(KEY_BTAB);
1645      rb_define_const(mKey, "BTAB", INT2NUM(KEY_BTAB));
1646  #endif
1647  #ifdef KEY_BEG
1648      rb_curses_define_const(KEY_BEG);
1649      rb_define_const(mKey, "BEG", INT2NUM(KEY_BEG));
1650  #endif
1651  #ifdef KEY_CANCEL
1652      rb_curses_define_const(KEY_CANCEL);
1653      rb_define_const(mKey, "CANCEL", INT2NUM(KEY_CANCEL));
1654  #endif
1655  #ifdef KEY_CLOSE
1656      rb_curses_define_const(KEY_CLOSE);
1657      rb_define_const(mKey, "CLOSE", INT2NUM(KEY_CLOSE));
1658  #endif
1659  #ifdef KEY_COMMAND
1660      rb_curses_define_const(KEY_COMMAND);
1661      rb_define_const(mKey, "COMMAND", INT2NUM(KEY_COMMAND));
1662  #endif
1663  #ifdef KEY_COPY
1664      rb_curses_define_const(KEY_COPY);
1665      rb_define_const(mKey, "COPY", INT2NUM(KEY_COPY));
1666  #endif
1667  #ifdef KEY_CREATE
1668      rb_curses_define_const(KEY_CREATE);
1669      rb_define_const(mKey, "CREATE", INT2NUM(KEY_CREATE));
1670  #endif
1671  #ifdef KEY_END
1672      rb_curses_define_const(KEY_END);
1673      rb_define_const(mKey, "END", INT2NUM(KEY_END));
1674  #endif
1675  #ifdef KEY_EXIT
1676      rb_curses_define_const(KEY_EXIT);
1677      rb_define_const(mKey, "EXIT", INT2NUM(KEY_EXIT));
1678  #endif
1679  #ifdef KEY_FIND
1680      rb_curses_define_const(KEY_FIND);
1681      rb_define_const(mKey, "FIND", INT2NUM(KEY_FIND));
1682  #endif
1683  #ifdef KEY_HELP
1684      rb_curses_define_const(KEY_HELP);
1685      rb_define_const(mKey, "HELP", INT2NUM(KEY_HELP));
1686  #endif
1687  #ifdef KEY_MARK
1688      rb_curses_define_const(KEY_MARK);
1689      rb_define_const(mKey, "MARK", INT2NUM(KEY_MARK));
1690  #endif
1691  #ifdef KEY_MESSAGE
1692      rb_curses_define_const(KEY_MESSAGE);
1693      rb_define_const(mKey, "MESSAGE", INT2NUM(KEY_MESSAGE));
1694  #endif
1695  #ifdef KEY_MOVE
1696      rb_curses_define_const(KEY_MOVE);
1697      rb_define_const(mKey, "MOVE", INT2NUM(KEY_MOVE));
1698  #endif
1699  #ifdef KEY_NEXT
1700      rb_curses_define_const(KEY_NEXT);
1701      rb_define_const(mKey, "NEXT", INT2NUM(KEY_NEXT));
1702  #endif
1703  #ifdef KEY_OPEN
1704      rb_curses_define_const(KEY_OPEN);
1705      rb_define_const(mKey, "OPEN", INT2NUM(KEY_OPEN));
1706  #endif
1707  #ifdef KEY_OPTIONS
1708      rb_curses_define_const(KEY_OPTIONS);
1709      rb_define_const(mKey, "OPTIONS", INT2NUM(KEY_OPTIONS));
1710  #endif
1711  #ifdef KEY_PREVIOUS
1712      rb_curses_define_const(KEY_PREVIOUS);
1713      rb_define_const(mKey, "PREVIOUS", INT2NUM(KEY_PREVIOUS));
1714  #endif
1715  #ifdef KEY_REDO
1716      rb_curses_define_const(KEY_REDO);
1717      rb_define_const(mKey, "REDO", INT2NUM(KEY_REDO));
1718  #endif
1719  #ifdef KEY_REFERENCE
1720      rb_curses_define_const(KEY_REFERENCE);
1721      rb_define_const(mKey, "REFERENCE", INT2NUM(KEY_REFERENCE));
1722  #endif
1723  #ifdef KEY_REFRESH
1724      rb_curses_define_const(KEY_REFRESH);
1725      rb_define_const(mKey, "REFRESH", INT2NUM(KEY_REFRESH));
1726  #endif
1727  #ifdef KEY_REPLACE
1728      rb_curses_define_const(KEY_REPLACE);
1729      rb_define_const(mKey, "REPLACE", INT2NUM(KEY_REPLACE));
1730  #endif
1731  #ifdef KEY_RESTART
1732      rb_curses_define_const(KEY_RESTART);
1733      rb_define_const(mKey, "RESTART", INT2NUM(KEY_RESTART));
1734  #endif
1735  #ifdef KEY_RESUME
1736      rb_curses_define_const(KEY_RESUME);
1737      rb_define_const(mKey, "RESUME", INT2NUM(KEY_RESUME));
1738  #endif
1739  #ifdef KEY_SAVE
1740      rb_curses_define_const(KEY_SAVE);
1741      rb_define_const(mKey, "SAVE", INT2NUM(KEY_SAVE));
1742  #endif
1743  #ifdef KEY_SBEG
1744      rb_curses_define_const(KEY_SBEG);
1745      rb_define_const(mKey, "SBEG", INT2NUM(KEY_SBEG));
1746  #endif
1747  #ifdef KEY_SCANCEL
1748      rb_curses_define_const(KEY_SCANCEL);
1749      rb_define_const(mKey, "SCANCEL", INT2NUM(KEY_SCANCEL));
1750  #endif
1751  #ifdef KEY_SCOMMAND
1752      rb_curses_define_const(KEY_SCOMMAND);
1753      rb_define_const(mKey, "SCOMMAND", INT2NUM(KEY_SCOMMAND));
1754  #endif
1755  #ifdef KEY_SCOPY
1756      rb_curses_define_const(KEY_SCOPY);
1757      rb_define_const(mKey, "SCOPY", INT2NUM(KEY_SCOPY));
1758  #endif
1759  #ifdef KEY_SCREATE
1760      rb_curses_define_const(KEY_SCREATE);
1761      rb_define_const(mKey, "SCREATE", INT2NUM(KEY_SCREATE));
1762  #endif
1763  #ifdef KEY_SDC
1764      rb_curses_define_const(KEY_SDC);
1765      rb_define_const(mKey, "SDC", INT2NUM(KEY_SDC));
1766  #endif
1767  #ifdef KEY_SDL
1768      rb_curses_define_const(KEY_SDL);
1769      rb_define_const(mKey, "SDL", INT2NUM(KEY_SDL));
1770  #endif
1771  #ifdef KEY_SELECT
1772      rb_curses_define_const(KEY_SELECT);
1773      rb_define_const(mKey, "SELECT", INT2NUM(KEY_SELECT));
1774  #endif
1775  #ifdef KEY_SEND
1776      rb_curses_define_const(KEY_SEND);
1777      rb_define_const(mKey, "SEND", INT2NUM(KEY_SEND));
1778  #endif
1779  #ifdef KEY_SEOL
1780      rb_curses_define_const(KEY_SEOL);
1781      rb_define_const(mKey, "SEOL", INT2NUM(KEY_SEOL));
1782  #endif
1783  #ifdef KEY_SEXIT
1784      rb_curses_define_const(KEY_SEXIT);
1785      rb_define_const(mKey, "SEXIT", INT2NUM(KEY_SEXIT));
1786  #endif
1787  #ifdef KEY_SFIND
1788      rb_curses_define_const(KEY_SFIND);
1789      rb_define_const(mKey, "SFIND", INT2NUM(KEY_SFIND));
1790  #endif
1791  #ifdef KEY_SHELP
1792      rb_curses_define_const(KEY_SHELP);
1793      rb_define_const(mKey, "SHELP", INT2NUM(KEY_SHELP));
1794  #endif
1795  #ifdef KEY_SHOME
1796      rb_curses_define_const(KEY_SHOME);
1797      rb_define_const(mKey, "SHOME", INT2NUM(KEY_SHOME));
1798  #endif
1799  #ifdef KEY_SIC
1800      rb_curses_define_const(KEY_SIC);
1801      rb_define_const(mKey, "SIC", INT2NUM(KEY_SIC));
1802  #endif
1803  #ifdef KEY_SLEFT
1804      rb_curses_define_const(KEY_SLEFT);
1805      rb_define_const(mKey, "SLEFT", INT2NUM(KEY_SLEFT));
1806  #endif
1807  #ifdef KEY_SMESSAGE
1808      rb_curses_define_const(KEY_SMESSAGE);
1809      rb_define_const(mKey, "SMESSAGE", INT2NUM(KEY_SMESSAGE));
1810  #endif
1811  #ifdef KEY_SMOVE
1812      rb_curses_define_const(KEY_SMOVE);
1813      rb_define_const(mKey, "SMOVE", INT2NUM(KEY_SMOVE));
1814  #endif
1815  #ifdef KEY_SNEXT
1816      rb_curses_define_const(KEY_SNEXT);
1817      rb_define_const(mKey, "SNEXT", INT2NUM(KEY_SNEXT));
1818  #endif
1819  #ifdef KEY_SOPTIONS
1820      rb_curses_define_const(KEY_SOPTIONS);
1821      rb_define_const(mKey, "SOPTIONS", INT2NUM(KEY_SOPTIONS));
1822  #endif
1823  #ifdef KEY_SPREVIOUS
1824      rb_curses_define_const(KEY_SPREVIOUS);
1825      rb_define_const(mKey, "SPREVIOUS", INT2NUM(KEY_SPREVIOUS));
1826  #endif
1827  #ifdef KEY_SPRINT
1828      rb_curses_define_const(KEY_SPRINT);
1829      rb_define_const(mKey, "SPRINT", INT2NUM(KEY_SPRINT));
1830  #endif
1831  #ifdef KEY_SREDO
1832      rb_curses_define_const(KEY_SREDO);
1833      rb_define_const(mKey, "SREDO", INT2NUM(KEY_SREDO));
1834  #endif
1835  #ifdef KEY_SREPLACE
1836      rb_curses_define_const(KEY_SREPLACE);
1837      rb_define_const(mKey, "SREPLACE", INT2NUM(KEY_SREPLACE));
1838  #endif
1839  #ifdef KEY_SRIGHT
1840      rb_curses_define_const(KEY_SRIGHT);
1841      rb_define_const(mKey, "SRIGHT", INT2NUM(KEY_SRIGHT));
1842  #endif
1843  #ifdef KEY_SRSUME
1844      rb_curses_define_const(KEY_SRSUME);
1845      rb_define_const(mKey, "SRSUME", INT2NUM(KEY_SRSUME));
1846  #endif
1847  #ifdef KEY_SSAVE
1848      rb_curses_define_const(KEY_SSAVE);
1849      rb_define_const(mKey, "SSAVE", INT2NUM(KEY_SSAVE));
1850  #endif
1851  #ifdef KEY_SSUSPEND
1852      rb_curses_define_const(KEY_SSUSPEND);
1853      rb_define_const(mKey, "SSUSPEND", INT2NUM(KEY_SSUSPEND));
1854  #endif
1855  #ifdef KEY_SUNDO
1856      rb_curses_define_const(KEY_SUNDO);
1857      rb_define_const(mKey, "SUNDO", INT2NUM(KEY_SUNDO));
1858  #endif
1859  #ifdef KEY_SUSPEND
1860      rb_curses_define_const(KEY_SUSPEND);
1861      rb_define_const(mKey, "SUSPEND", INT2NUM(KEY_SUSPEND));
1862  #endif
1863  #ifdef KEY_UNDO
1864      rb_curses_define_const(KEY_UNDO);
1865      rb_define_const(mKey, "UNDO", INT2NUM(KEY_UNDO));
1866  #endif
1867  #ifdef KEY_RESIZE
1868      rb_curses_define_const(KEY_RESIZE);
1869      rb_define_const(mKey, "RESIZE", INT2NUM(KEY_RESIZE));
1870  #endif
1871  #ifdef KEY_MAX
1872      rb_curses_define_const(KEY_MAX);
1873      rb_define_const(mKey, "MAX", INT2NUM(KEY_MAX));
1874  #endif
1875      {
1876        int c;
1877        char name[] = "KEY_CTRL_x";
1878        for( c = 'A'; c <= 'Z'; c++ ){
1879          sprintf(name, "KEY_CTRL_%c", c);
1880          rb_define_const(mCurses, name, INT2FIX(c - 'A' + 1));
1881        };
1882      }
1883  #undef rb_curses_define_const
1884  
1885      rb_set_end_proc(curses_finalize, 0);
1886  }