ffi-ncurses 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,215 @@
1
+ module FFI
2
+ module NCurses
3
+ module Darwin
4
+ NCURSES_SIZE_T = :short
5
+ NCURSES_ATTR_T = :int
6
+ NCURSES_COLOR_T = :short
7
+ NCURSES_CH_T = :short
8
+
9
+ CHTYPE = :ulong
10
+ CCHARW_MAX = 5
11
+
12
+ BOOLEAN = :uchar # sizeof(bool) == 1
13
+
14
+ WCHAR_T = :ushort
15
+
16
+ # class CCharT < FFI::Struct
17
+ # layout \
18
+ # :attr, NCURSES_ATTR_T,
19
+ # :chars, [WCHAR_T, CCHARW_MAX]
20
+ # end
21
+
22
+ class PDat < FFI::Struct
23
+ layout \
24
+ :_pad_y, NCURSES_SIZE_T,
25
+ :_pad_x, NCURSES_SIZE_T,
26
+ :_pad_top, NCURSES_SIZE_T,
27
+ :_pad_left, NCURSES_SIZE_T,
28
+ :_pad_bottom, NCURSES_SIZE_T,
29
+ :_pad_right, NCURSES_SIZE_T
30
+ end
31
+
32
+ class WinSt < FFI::Struct
33
+ layout \
34
+ :_cury, NCURSES_SIZE_T,
35
+ :_curx, NCURSES_SIZE_T,
36
+ :_maxy, NCURSES_SIZE_T,
37
+ :_maxx, NCURSES_SIZE_T,
38
+ :_begy, NCURSES_SIZE_T,
39
+ :_begx, NCURSES_SIZE_T,
40
+ :_flags, :short,
41
+ :_attrs, NCURSES_ATTR_T,
42
+ :_bkgd, CHTYPE,
43
+ :_notimeout, BOOLEAN,
44
+ :_clear, BOOLEAN,
45
+ :_leaveok, BOOLEAN,
46
+ :_scroll, BOOLEAN,
47
+ :_idlok, BOOLEAN,
48
+ :_idcok, BOOLEAN,
49
+ :_immed, BOOLEAN,
50
+ :_sync, BOOLEAN,
51
+ :_use_keypad, BOOLEAN,
52
+ :_delay, :int,
53
+
54
+ # struct ldat *_line
55
+ :_line, :pointer,
56
+
57
+ :_regtop, NCURSES_SIZE_T,
58
+ :_regbottom, NCURSES_SIZE_T,
59
+
60
+ # why x,y when everything else is y,x?
61
+ :_parx, :int,
62
+ :_pary, :int,
63
+ :_parent, :pointer # WINDOW
64
+
65
+ # don't know how to include nested Struct yet
66
+ # :_pad, PDat,
67
+
68
+ # :_yoffset, NCURSES_SIZE_T
69
+ # :_bkgrnd, CCharT
70
+ end
71
+
72
+ # translated from Mac OSX 10.4 ('Tiger') /usr/include/ncurses.h
73
+ def getattrs(win)
74
+ win_st = WinSt.new(win)
75
+ win ? win_st[:_attrs] : A_NORMAL
76
+ end
77
+ def getcurx(win)
78
+ win_st = WinSt.new(win)
79
+ win ? win_st[:_curx] : ERR
80
+ end
81
+ def getcury(win)
82
+ win_st = WinSt.new(win)
83
+ win ? win_st[:_cury] : ERR
84
+ end
85
+ def getbegx(win)
86
+ win_st = WinSt.new(win)
87
+ win ? win_st[:_begx] : ERR
88
+ end
89
+ def getbegy(win)
90
+ win_st = WinSt.new(win)
91
+ win ? win_st[:_begy] : ERR
92
+ end
93
+ def getmaxx(win)
94
+ win_st = WinSt.new(win)
95
+ win ? win_st[:_maxx] + 1 : ERR
96
+ end
97
+ def getmaxy(win)
98
+ win_st = WinSt.new(win)
99
+ win ? win_st[:_maxy] + 1 : ERR
100
+ end
101
+ def getparx(win)
102
+ win_st = WinSt.new(win)
103
+ win ? win_st[:_parx] : ERR
104
+ end
105
+ def getpary(win)
106
+ win_st = WinSt.new(win)
107
+ win ? win_st[:_pary] : ERR
108
+ end
109
+
110
+ def _win(win, member)
111
+ win && WinSt.new(win)[member]
112
+ end
113
+ private :_win
114
+
115
+ # extensions - not in X/Open
116
+ def is_cleared(win)
117
+ _win(win, :_clear)
118
+ end
119
+ def is_idcok(win)
120
+ _win(win, :_idcok)
121
+ end
122
+ def is_idlok(win)
123
+ _win(:win, :_idlok)
124
+ end
125
+ def is_immedok(win)
126
+ _win(win, :_immed)
127
+ end
128
+ def is_keypad(win)
129
+ _win(win, :_use_keypad)
130
+ end
131
+ def is_leaveok(win)
132
+ _win(win, :_leaveok) || ERR
133
+ end
134
+ def is_nodelay(win)
135
+ _win(win, :_delay) == 0 ? FFI::NCurses::TRUE : FFI::NCurses::FALSE
136
+ end
137
+ def is_notimeout(win)
138
+ _win(win, :_notimeout)
139
+ end
140
+ def is_scrollok(win)
141
+ _win(win, :_scroll)
142
+ end
143
+ def is_syncok(win)
144
+ _win(win, :_sync)
145
+ end
146
+ def wgetparent(win)
147
+ _win(win, :_parent)
148
+ end
149
+ def wgetscrreg(win, t, b)
150
+ # ((win) ? (*(t) = (win)->_regtop, *(b) = (win)->_regbottom, OK) : ERR)
151
+ # not entirely satisfactory - no error return
152
+ # should I raise an exception?
153
+ if win
154
+ win_st = WinSt.new(win)
155
+ [win_st[:_regtop], win_st[:_regbottom]]
156
+ else
157
+ #raise ArgumentError, "win is nil"
158
+ nil
159
+ end
160
+ end
161
+ end
162
+
163
+ # struct _win_st
164
+ # {
165
+ # NCURSES_SIZE_T _cury, _curx; /* current cursor position */
166
+
167
+ # /* window location and size */
168
+ # NCURSES_SIZE_T _maxy, _maxx; /* maximums of x and y, NOT window size */
169
+ # NCURSES_SIZE_T _begy, _begx; /* screen coords of upper-left-hand corner */
170
+
171
+ # short _flags; /* window state flags */
172
+
173
+ # /* attribute tracking */
174
+ # attr_t _attrs; /* current attribute for non-space character */
175
+ # chtype _bkgd; /* current background char/attribute pair */
176
+
177
+ # /* option values set by user */
178
+ # bool _notimeout; /* no time out on function-key entry? */
179
+ # bool _clear; /* consider all data in the window invalid? */
180
+ # bool _leaveok; /* OK to not reset cursor on exit? */
181
+ # bool _scroll; /* OK to scroll this window? */
182
+ # bool _idlok; /* OK to use insert/delete line? */
183
+ # bool _idcok; /* OK to use insert/delete char? */
184
+ # bool _immed; /* window in immed mode? (not yet used) */
185
+ # bool _sync; /* window in sync mode? */
186
+ # bool _use_keypad; /* process function keys into KEY_ symbols? */
187
+ # int _delay; /* 0 = nodelay, <0 = blocking, >0 = delay */
188
+
189
+ # struct ldat *_line; /* the actual line data */
190
+
191
+ # /* global screen state */
192
+ # NCURSES_SIZE_T _regtop; /* top line of scrolling region */
193
+ # NCURSES_SIZE_T _regbottom; /* bottom line of scrolling region */
194
+
195
+ # /* these are used only if this is a sub-window */
196
+ # int _parx; /* x coordinate of this window in parent */
197
+ # int _pary; /* y coordinate of this window in parent */
198
+ # WINDOW *_parent; /* pointer to parent if a sub-window */
199
+
200
+ # /* these are used only if this is a pad */
201
+ # struct pdat
202
+ # {
203
+ # NCURSES_SIZE_T _pad_y, _pad_x;
204
+ # NCURSES_SIZE_T _pad_top, _pad_left;
205
+ # NCURSES_SIZE_T _pad_bottom, _pad_right;
206
+ # } _pad;
207
+
208
+ # NCURSES_SIZE_T _yoffset; /* real begy is _begy + _yoffset */
209
+
210
+ # #ifdef _XOPEN_SOURCE_EXTENDED
211
+ # cchar_t _bkgrnd; /* current background char/attribute pair */
212
+ # #endif
213
+ # };
214
+ end
215
+ end
@@ -0,0 +1,106 @@
1
+ module NCurses
2
+ KEY_CODE_YES = 0400 # A wchar_t contains a key code
3
+ KEY_MIN = 0401 # Minimum curses key
4
+ KEY_BREAK = 0401 # Break key (unreliable)
5
+ KEY_SRESET = 0530 # Soft (partial) reset (unreliable)
6
+ KEY_RESET = 0531 # Reset or hard reset (unreliable)
7
+
8
+ KEY_DOWN = 0402 # down-arrow key
9
+ KEY_UP = 0403 # up-arrow key
10
+ KEY_LEFT = 0404 # left-arrow key
11
+ KEY_RIGHT = 0405 # right-arrow key
12
+ KEY_HOME = 0406 # home key
13
+ KEY_BACKSPACE = 0407 # backspace key
14
+ KEY_F0 = 0410 # Function keys. Space for 64
15
+
16
+ # Value of function key n
17
+ def KEY_F(n)
18
+ (KEY_F0+(n))
19
+ end
20
+
21
+ KEY_DL = 0510 # delete-line key
22
+ KEY_IL = 0511 # insert-line key
23
+ KEY_DC = 0512 # delete-character key
24
+ KEY_IC = 0513 # insert-character key
25
+ KEY_EIC = 0514 # sent by rmir or smir in insert mode
26
+ KEY_CLEAR = 0515 # clear-screen or erase key
27
+ KEY_EOS = 0516 # clear-to-end-of-screen key
28
+ KEY_EOL = 0517 # clear-to-end-of-line key
29
+ KEY_SF = 0520 # scroll-forward key
30
+ KEY_SR = 0521 # scroll-backward key
31
+ KEY_NPAGE = 0522 # next-page key
32
+ KEY_PPAGE = 0523 # previous-page key
33
+ KEY_STAB = 0524 # set-tab key
34
+ KEY_CTAB = 0525 # clear-tab key
35
+ KEY_CATAB = 0526 # clear-all-tabs key
36
+ KEY_ENTER = 0527 # enter/send key
37
+ KEY_PRINT = 0532 # print key
38
+ KEY_LL = 0533 # lower-left key (home down)
39
+ KEY_A1 = 0534 # upper left of keypad
40
+ KEY_A3 = 0535 # upper right of keypad
41
+ KEY_B2 = 0536 # center of keypad
42
+ KEY_C1 = 0537 # lower left of keypad
43
+ KEY_C3 = 0540 # lower right of keypad
44
+ KEY_BTAB = 0541 # back-tab key
45
+ KEY_BEG = 0542 # begin key
46
+ KEY_CANCEL = 0543 # cancel key
47
+ KEY_CLOSE = 0544 # close key
48
+ KEY_COMMAND = 0545 # command key
49
+ KEY_COPY = 0546 # copy key
50
+ KEY_CREATE = 0547 # create key
51
+ KEY_END = 0550 # end key
52
+ KEY_EXIT = 0551 # exit key
53
+ KEY_FIND = 0552 # find key
54
+ KEY_HELP = 0553 # help key
55
+ KEY_MARK = 0554 # mark key
56
+ KEY_MESSAGE = 0555 # message key
57
+ KEY_MOVE = 0556 # move key
58
+ KEY_NEXT = 0557 # next key
59
+ KEY_OPEN = 0560 # open key
60
+ KEY_OPTIONS = 0561 # options key
61
+ KEY_PREVIOUS = 0562 # previous key
62
+ KEY_REDO = 0563 # redo key
63
+ KEY_REFERENCE = 0564 # reference key
64
+ KEY_REFRESH = 0565 # refresh key
65
+ KEY_REPLACE = 0566 # replace key
66
+ KEY_RESTART = 0567 # restart key
67
+ KEY_RESUME = 0570 # resume key
68
+ KEY_SAVE = 0571 # save key
69
+ KEY_SBEG = 0572 # shifted begin key
70
+ KEY_SCANCEL = 0573 # shifted cancel key
71
+ KEY_SCOMMAND = 0574 # shifted command key
72
+ KEY_SCOPY = 0575 # shifted copy key
73
+ KEY_SCREATE = 0576 # shifted create key
74
+ KEY_SDC = 0577 # shifted delete-character key
75
+ KEY_SDL = 0600 # shifted delete-line key
76
+ KEY_SELECT = 0601 # select key
77
+ KEY_SEND = 0602 # shifted end key
78
+ KEY_SEOL = 0603 # shifted clear-to-end-of-line key
79
+ KEY_SEXIT = 0604 # shifted exit key
80
+ KEY_SFIND = 0605 # shifted find key
81
+ KEY_SHELP = 0606 # shifted help key
82
+ KEY_SHOME = 0607 # shifted home key
83
+ KEY_SIC = 0610 # shifted insert-character key
84
+ KEY_SLEFT = 0611 # shifted left-arrow key
85
+ KEY_SMESSAGE = 0612 # shifted message key
86
+ KEY_SMOVE = 0613 # shifted move key
87
+ KEY_SNEXT = 0614 # shifted next key
88
+ KEY_SOPTIONS = 0615 # shifted options key
89
+ KEY_SPREVIOUS = 0616 # shifted previous key
90
+ KEY_SPRINT = 0617 # shifted print key
91
+ KEY_SREDO = 0620 # shifted redo key
92
+ KEY_SREPLACE = 0621 # shifted replace key
93
+ KEY_SRIGHT = 0622 # shifted right-arrow key
94
+ KEY_SRSUME = 0623 # shifted resume key
95
+ KEY_SSAVE = 0624 # shifted save key
96
+ KEY_SSUSPEND = 0625 # shifted suspend key
97
+ KEY_SUNDO = 0626 # shifted undo key
98
+ KEY_SUSPEND = 0627 # suspend key
99
+ KEY_UNDO = 0630 # undo key
100
+ KEY_MOUSE = 0631 # Mouse event has occurred
101
+ KEY_RESIZE = 0632 # Terminal resize event
102
+ KEY_EVENT = 0633 # We were interrupted by an event
103
+
104
+ KEY_MAX = 0777 # Maximum key value is 0633
105
+
106
+ end
@@ -0,0 +1,146 @@
1
+ require 'ffi-ncurses'
2
+ module FFI
3
+ module NCurses
4
+
5
+ NCURSES_MOUSE_VERSION = 1
6
+
7
+ # mouse interface
8
+
9
+ if NCURSES_MOUSE_VERSION > 1
10
+ def self.NCURSES_MOUSE_MASK(b,m)
11
+ ((m) << (((b) - 1) * 5))
12
+ end
13
+ else
14
+ def self.NCURSES_MOUSE_MASK(b,m)
15
+ ((m) << (((b) - 1) * 6))
16
+ end
17
+ end
18
+
19
+ NCURSES_BUTTON_RELEASED = 001
20
+ NCURSES_BUTTON_PRESSED = 002
21
+ NCURSES_BUTTON_CLICKED = 004
22
+ NCURSES_DOUBLE_CLICKED = 010
23
+ NCURSES_TRIPLE_CLICKED = 020
24
+ NCURSES_RESERVED_EVENT = 040
25
+
26
+ # event masks
27
+ BUTTON1_RELEASED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_RELEASED)
28
+ BUTTON1_PRESSED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_PRESSED)
29
+ BUTTON1_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_CLICKED)
30
+ BUTTON1_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_DOUBLE_CLICKED)
31
+ BUTTON1_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_TRIPLE_CLICKED)
32
+
33
+ BUTTON2_RELEASED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_RELEASED)
34
+ BUTTON2_PRESSED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_PRESSED)
35
+ BUTTON2_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_CLICKED)
36
+ BUTTON2_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_DOUBLE_CLICKED)
37
+ BUTTON2_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_TRIPLE_CLICKED)
38
+
39
+ BUTTON3_RELEASED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_RELEASED)
40
+ BUTTON3_PRESSED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_PRESSED)
41
+ BUTTON3_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_CLICKED)
42
+ BUTTON3_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_DOUBLE_CLICKED)
43
+ BUTTON3_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_TRIPLE_CLICKED)
44
+
45
+ BUTTON4_RELEASED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_RELEASED)
46
+ BUTTON4_PRESSED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_PRESSED)
47
+ BUTTON4_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_CLICKED)
48
+ BUTTON4_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_DOUBLE_CLICKED)
49
+ BUTTON4_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_TRIPLE_CLICKED)
50
+
51
+ #
52
+ # In 32 bits the version-1 scheme does not provide enough space for a 5th
53
+ # button, unless we choose to change the ABI by omitting the reserved-events.
54
+ #
55
+
56
+ if NCURSES_MOUSE_VERSION > 1
57
+
58
+ BUTTON5_RELEASED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_RELEASED)
59
+ BUTTON5_PRESSED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_PRESSED)
60
+ BUTTON5_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_CLICKED)
61
+ BUTTON5_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_DOUBLE_CLICKED)
62
+ BUTTON5_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_TRIPLE_CLICKED)
63
+
64
+ BUTTON_CTRL = NCURSES_MOUSE_MASK(6, 0001)
65
+ BUTTON_SHIFT = NCURSES_MOUSE_MASK(6, 0002)
66
+ BUTTON_ALT = NCURSES_MOUSE_MASK(6, 0004)
67
+ REPORT_MOUSE_POSITION = NCURSES_MOUSE_MASK(6, 0010)
68
+
69
+ else
70
+
71
+ BUTTON1_RESERVED_EVENT = NCURSES_MOUSE_MASK(1, NCURSES_RESERVED_EVENT)
72
+ BUTTON2_RESERVED_EVENT = NCURSES_MOUSE_MASK(2, NCURSES_RESERVED_EVENT)
73
+ BUTTON3_RESERVED_EVENT = NCURSES_MOUSE_MASK(3, NCURSES_RESERVED_EVENT)
74
+ BUTTON4_RESERVED_EVENT = NCURSES_MOUSE_MASK(4, NCURSES_RESERVED_EVENT)
75
+
76
+ BUTTON_CTRL = NCURSES_MOUSE_MASK(5, 0001)
77
+ BUTTON_SHIFT = NCURSES_MOUSE_MASK(5, 0002)
78
+ BUTTON_ALT = NCURSES_MOUSE_MASK(5, 0004)
79
+ REPORT_MOUSE_POSITION = NCURSES_MOUSE_MASK(5, 0010)
80
+
81
+ end
82
+
83
+ ALL_MOUSE_EVENTS = (REPORT_MOUSE_POSITION - 1)
84
+
85
+ class << self
86
+ # macros to extract single event-bits from masks
87
+ def BUTTON_RELEASE(e, x)
88
+ ((e) & (001 << (6 * ((x) - 1))))
89
+ end
90
+ def BUTTON_PRESS(e, x)
91
+ ((e) & (002 << (6 * ((x) - 1))))
92
+ end
93
+ def BUTTON_CLICK(e, x)
94
+ ((e) & (004 << (6 * ((x) - 1))))
95
+ end
96
+ def BUTTON_DOUBLE_CLICK(e, x)
97
+ ((e) & (010 << (6 * ((x) - 1))))
98
+ end
99
+ def BUTTON_TRIPLE_CLICK(e, x)
100
+ ((e) & (020 << (6 * ((x) - 1))))
101
+ end
102
+ def BUTTON_RESERVED_EVENT(e, x)
103
+ ((e) & (040 << (6 * ((x) - 1))))
104
+ end
105
+
106
+ # if NCURSES_MOUSE_VERSION > 1
107
+ # def NCURSES_MOUSE_MASK(b,m)
108
+ # ((m) << (((b) - 1) * 5))
109
+ # end
110
+ # else
111
+ # def NCURSES_MOUSE_MASK(b,m)
112
+ # ((m) << (((b) - 1) * 6))
113
+ # end
114
+ # end
115
+ end
116
+
117
+ # def mouse_trafo(y, x, to_screen)
118
+ # end
119
+
120
+ KEY_MOUSE = 0631 # Mouse event has occurred
121
+
122
+ #FFI.add_typedef :ulong, :mmask_t
123
+ class MEVENT < FFI::Struct
124
+ layout :id, :short,
125
+ :x, :int,
126
+ :y, :int,
127
+ :z, :int,
128
+ # :bstate, :mmask_t
129
+ :bstate, :ulong
130
+ end
131
+
132
+ functions =
133
+ [
134
+ [:getmouse, [MEVENT], :int],
135
+ [:ungetmouse, [MEVENT], :int],
136
+ ]
137
+
138
+ functions.each do |function|
139
+ begin
140
+ attach_function(*function)
141
+ rescue Object => e
142
+ (@unattached_functions ||= []) << function
143
+ end
144
+ end
145
+ end
146
+ end