rb_termbox 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,226 @@
1
+ #pragma once
2
+
3
+ #include <stdbool.h>
4
+ #include <stdint.h>
5
+
6
+ // for shared objects
7
+ #if __GNUC__ >= 4
8
+ #define SO_IMPORT __attribute__((visibility("default")))
9
+ #else
10
+ #define SO_IMPORT
11
+ #endif
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ // Key constants. See also struct tb_event's key field.
18
+
19
+ // These are a safe subset of terminfo keys, which exist on all popular
20
+ // terminals. Termbox uses only them to stay truly portable.
21
+ #define TB_KEY_F1 (0xFFFF-0)
22
+ #define TB_KEY_F2 (0xFFFF-1)
23
+ #define TB_KEY_F3 (0xFFFF-2)
24
+ #define TB_KEY_F4 (0xFFFF-3)
25
+ #define TB_KEY_F5 (0xFFFF-4)
26
+ #define TB_KEY_F6 (0xFFFF-5)
27
+ #define TB_KEY_F7 (0xFFFF-6)
28
+ #define TB_KEY_F8 (0xFFFF-7)
29
+ #define TB_KEY_F9 (0xFFFF-8)
30
+ #define TB_KEY_F10 (0xFFFF-9)
31
+ #define TB_KEY_F11 (0xFFFF-10)
32
+ #define TB_KEY_F12 (0xFFFF-11)
33
+ #define TB_KEY_INSERT (0xFFFF-12)
34
+ #define TB_KEY_DELETE (0xFFFF-13)
35
+ #define TB_KEY_HOME (0xFFFF-14)
36
+ #define TB_KEY_END (0xFFFF-15)
37
+ #define TB_KEY_PGUP (0xFFFF-16)
38
+ #define TB_KEY_PGDN (0xFFFF-17)
39
+ #define TB_KEY_ARROW_UP (0xFFFF-18)
40
+ #define TB_KEY_ARROW_DOWN (0xFFFF-19)
41
+ #define TB_KEY_ARROW_LEFT (0xFFFF-20)
42
+ #define TB_KEY_ARROW_RIGHT (0xFFFF-21)
43
+
44
+ // These are all ASCII code points below SPACE character and a BACKSPACE key.
45
+ #define TB_KEY_CTRL_TILDE 0x00
46
+ #define TB_KEY_CTRL_2 0x00 // clash with 'CTRL_TILDE'
47
+ #define TB_KEY_CTRL_A 0x01
48
+ #define TB_KEY_CTRL_B 0x02
49
+ #define TB_KEY_CTRL_C 0x03
50
+ #define TB_KEY_CTRL_D 0x04
51
+ #define TB_KEY_CTRL_E 0x05
52
+ #define TB_KEY_CTRL_F 0x06
53
+ #define TB_KEY_CTRL_G 0x07
54
+ #define TB_KEY_BACKSPACE 0x08
55
+ #define TB_KEY_CTRL_H 0x08 // clash with 'CTRL_BACKSPACE'
56
+ #define TB_KEY_TAB 0x09
57
+ #define TB_KEY_CTRL_I 0x09 // clash with 'TAB'
58
+ #define TB_KEY_CTRL_J 0x0A
59
+ #define TB_KEY_CTRL_K 0x0B
60
+ #define TB_KEY_CTRL_L 0x0C
61
+ #define TB_KEY_ENTER 0x0D
62
+ #define TB_KEY_CTRL_M 0x0D // clash with 'ENTER'
63
+ #define TB_KEY_CTRL_N 0x0E
64
+ #define TB_KEY_CTRL_O 0x0F
65
+ #define TB_KEY_CTRL_P 0x10
66
+ #define TB_KEY_CTRL_Q 0x11
67
+ #define TB_KEY_CTRL_R 0x12
68
+ #define TB_KEY_CTRL_S 0x13
69
+ #define TB_KEY_CTRL_T 0x14
70
+ #define TB_KEY_CTRL_U 0x15
71
+ #define TB_KEY_CTRL_V 0x16
72
+ #define TB_KEY_CTRL_W 0x17
73
+ #define TB_KEY_CTRL_X 0x18
74
+ #define TB_KEY_CTRL_Y 0x19
75
+ #define TB_KEY_CTRL_Z 0x1A
76
+ #define TB_KEY_ESC 0x1B
77
+ #define TB_KEY_CTRL_LSQ_BRACKET 0x1B // clash with 'ESC'
78
+ #define TB_KEY_CTRL_3 0x1B // clash with 'ESC'
79
+ #define TB_KEY_CTRL_4 0x1C
80
+ #define TB_KEY_CTRL_BACKSLASH 0x1C // clash with 'CTRL_4'
81
+ #define TB_KEY_CTRL_5 0x1D
82
+ #define TB_KEY_CTRL_RSQ_BRACKET 0x1D // clash with 'CTRL_5'
83
+ #define TB_KEY_CTRL_6 0x1E
84
+ #define TB_KEY_CTRL_7 0x1F
85
+ #define TB_KEY_CTRL_SLASH 0x1F // clash with 'CTRL_7'
86
+ #define TB_KEY_CTRL_UNDERSCORE 0x1F // clash with 'CTRL_7'
87
+ #define TB_KEY_SPACE 0x20
88
+ #define TB_KEY_BACKSPACE2 0x7F
89
+ #define TB_KEY_CTRL_8 0x7F // clash with 'DELETE'
90
+
91
+ // These are non-existing ones.
92
+ //
93
+ // #define TB_KEY_CTRL_1 clash with '1'
94
+ // #define TB_KEY_CTRL_9 clash with '9'
95
+ // #define TB_KEY_CTRL_0 clash with '0'
96
+
97
+ // Currently there is only one modificator. See also struct tb_event's mod
98
+ // field.
99
+ #define TB_MOD_ALT 0x01
100
+
101
+ // Colors (see struct tb_cell's fg and bg fields).
102
+ #define TB_DEFAULT 0x00
103
+ #define TB_BLACK 0x01
104
+ #define TB_RED 0x02
105
+ #define TB_GREEN 0x03
106
+ #define TB_YELLOW 0x04
107
+ #define TB_BLUE 0x05
108
+ #define TB_MAGENTA 0x06
109
+ #define TB_CYAN 0x07
110
+ #define TB_WHITE 0x08
111
+
112
+ // Attributes, it is possible to use multiple attributes by combining them
113
+ // using bitwise OR ('|'). Although, colors cannot be combined. But you can
114
+ // combine attributes and a single color. See also struct tb_cell's fg and bg
115
+ // fields.
116
+ #define TB_BOLD 0x10
117
+ #define TB_UNDERLINE 0x20
118
+ #define TB_REVERSE 0x40
119
+
120
+ // A cell, single conceptual entity on the terminal screen. The terminal screen
121
+ // is basically a 2d array of cells. It has the following fields:
122
+ // - 'ch' is a unicode character
123
+ // - 'fg' foreground color and attributes
124
+ // - 'bg' background color and attributes
125
+ struct tb_cell {
126
+ uint32_t ch;
127
+ uint16_t fg;
128
+ uint16_t bg;
129
+ };
130
+
131
+ #define TB_EVENT_KEY 1
132
+ #define TB_EVENT_RESIZE 2
133
+
134
+ // This struct represents a termbox event. The 'mod', 'key' and 'ch' fields are
135
+ // valid if 'type' is TB_EVENT_KEY. The 'w' and 'h' fields are valid if 'type'
136
+ // is TB_EVENT_RESIZE. See also
137
+ struct tb_event {
138
+ uint8_t type;
139
+ uint8_t mod;
140
+ uint16_t key;
141
+ uint32_t ch;
142
+ int32_t w;
143
+ int32_t h;
144
+ };
145
+
146
+ // Error codes returned by tb_init(). All of them are self-explanatory, except
147
+ // the pipe trap error. Termbox uses unix pipes in order to deliever a message
148
+ // from a signal handler (SIGWINCH) to the main event reading loop. Honestly in
149
+ // most cases you should just check the returned code as < 0.
150
+ #define TB_EUNSUPPORTED_TERMINAL -1
151
+ #define TB_EFAILED_TO_OPEN_TTY -2
152
+ #define TB_EPIPE_TRAP_ERROR -3
153
+
154
+ // Initializes the termbox library. This function should be called before any
155
+ // other functions. After successful initialization, the library must be
156
+ // finalized using the tb_shutdown() function.
157
+ SO_IMPORT int tb_init(void);
158
+ SO_IMPORT void tb_shutdown(void);
159
+
160
+ // Returns the size of the internal back buffer (which is the same as
161
+ // terminal's window size in characters). The internal buffer can be resized
162
+ // after tb_clear() or tb_present() function calls.
163
+ SO_IMPORT int tb_width(void);
164
+ SO_IMPORT int tb_height(void);
165
+
166
+ // Clears the internal back buffer using TB_DEFAULT color or the
167
+ // color/attributes set by tb_set_clear_attributes() function.
168
+ SO_IMPORT void tb_clear(void);
169
+ SO_IMPORT void tb_set_clear_attributes(uint16_t fg, uint16_t bg);
170
+
171
+ // Syncronizes the internal back buffer with the terminal.
172
+ SO_IMPORT void tb_present(void);
173
+
174
+ #define TB_HIDE_CURSOR -1
175
+
176
+ // Sets the position of the cursor. Upper-left character is (0, 0). If you pass
177
+ // TB_HIDE_CURSOR as both coordinates, then the cursor will be hidden. Cursor
178
+ // is hidden by default.
179
+ SO_IMPORT void tb_set_cursor(int cx, int cy);
180
+
181
+ // Changes cell's parameters in the internal back buffer at the specified
182
+ // position.
183
+ SO_IMPORT void tb_put_cell(int x, int y, const struct tb_cell *cell);
184
+ SO_IMPORT void tb_change_cell(int x, int y, uint32_t ch, uint16_t fg, uint16_t bg);
185
+
186
+ // Copies the buffer from 'cells' at the specified position, assuming the
187
+ // buffer is a two-dimensional array of size ('w' x 'h'), represented as a
188
+ // one-dimensional buffer containing lines of cells starting from the top.
189
+ SO_IMPORT void tb_blit(int x, int y, int w, int h, const struct tb_cell *cells);
190
+
191
+ #define TB_INPUT_CURRENT 0
192
+ #define TB_INPUT_ESC 1
193
+ #define TB_INPUT_ALT 2
194
+
195
+ // Sets the termbox input mode. Termbox has two input modes:
196
+ // 1. Esc input mode.
197
+ // When ESC sequence is in the buffer and it doesn't match any known
198
+ // ESC sequence => ESC means TB_KEY_ESC.
199
+ // 2. Alt input mode.
200
+ // When ESC sequence is in the buffer and it doesn't match any known
201
+ // sequence => ESC enables TB_MOD_ALT modifier for the next keyboard event.
202
+ //
203
+ // If 'mode' is TB_INPUT_CURRENT, it returns the current input mode.
204
+ SO_IMPORT int tb_select_input_mode(int mode);
205
+
206
+
207
+ // Wait for an event up to 'timeout' milliseconds and fill the 'event'
208
+ // structure with it, when the event is available. Returns the type of the
209
+ // event (one of TB_EVENT_* constants) or -1 if there was an error or 0 in case
210
+ // there were no event during 'timeout' period.
211
+ SO_IMPORT int tb_peek_event(struct tb_event *event, int timeout);
212
+
213
+ // Wait for an event forever and fill the 'event' structure with it, when the
214
+ // event is available. Returns the type of the event (one of TB_EVENT_*
215
+ // constants) or -1 if there was an error.
216
+ SO_IMPORT int tb_poll_event(struct tb_event *event);
217
+
218
+ // Utility utf8 functions.
219
+ #define TB_EOF -1
220
+ SO_IMPORT int tb_utf8_char_length(char c);
221
+ SO_IMPORT int tb_utf8_char_to_unicode(uint32_t *out, const char *c);
222
+ SO_IMPORT int tb_utf8_unicode_to_char(char *out, uint32_t c);
223
+
224
+ #ifdef __cplusplus
225
+ }
226
+ #endif
@@ -0,0 +1,79 @@
1
+ #include "termbox.h"
2
+
3
+ static const unsigned char utf8_length[256] = {
4
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
5
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
6
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
7
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
8
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
9
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
10
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
11
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
12
+ };
13
+
14
+ static const unsigned char utf8_mask[6] = {
15
+ 0x7F,
16
+ 0x1F,
17
+ 0x0F,
18
+ 0x07,
19
+ 0x03,
20
+ 0x01
21
+ };
22
+
23
+ int tb_utf8_char_length(char c)
24
+ {
25
+ return utf8_length[(unsigned char)c];
26
+ }
27
+
28
+ int tb_utf8_char_to_unicode(uint32_t *out, const char *c)
29
+ {
30
+ if (*c == 0)
31
+ return TB_EOF;
32
+
33
+ int i;
34
+ unsigned char len = tb_utf8_char_length(*c);
35
+ unsigned char mask = utf8_mask[len-1];
36
+ uint32_t result = c[0] & mask;
37
+ for (i = 1; i < len; ++i) {
38
+ result <<= 6;
39
+ result |= c[i] & 0x3f;
40
+ }
41
+
42
+ *out = result;
43
+ return (int)len;
44
+ }
45
+
46
+ int tb_utf8_unicode_to_char(char *out, uint32_t c)
47
+ {
48
+ int len = 0;
49
+ int first;
50
+ int i;
51
+
52
+ if (c < 0x80) {
53
+ first = 0;
54
+ len = 1;
55
+ } else if (c < 0x800) {
56
+ first = 0xc0;
57
+ len = 2;
58
+ } else if (c < 0x10000) {
59
+ first = 0xe0;
60
+ len = 3;
61
+ } else if (c < 0x200000) {
62
+ first = 0xf0;
63
+ len = 4;
64
+ } else if (c < 0x4000000) {
65
+ first = 0xf8;
66
+ len = 5;
67
+ } else {
68
+ first = 0xfc;
69
+ len = 6;
70
+ }
71
+
72
+ for (i = len - 1; i > 0; --i) {
73
+ out[i] = (c & 0x3f) | 0x80;
74
+ c >>= 6;
75
+ }
76
+ out[0] = c | first;
77
+
78
+ return len;
79
+ }
@@ -27,7 +27,7 @@ module Termbox
27
27
  @library_path = path
28
28
  end
29
29
 
30
- @library_path
30
+ @library_path || "libtermbox"
31
31
  end
32
32
 
33
33
  def initialize_library path=nil
@@ -1,12 +1,13 @@
1
1
  module Termbox
2
2
  Colors = {
3
- :black => 0x00,
4
- :red => 0x01,
5
- :green => 0x02,
6
- :yellow => 0x03,
7
- :blue => 0x04,
8
- :magenta => 0x05,
9
- :cyan => 0x06,
10
- :white => 0x07
3
+ :default => 0x00,
4
+ :black => 0x01,
5
+ :red => 0x02,
6
+ :green => 0x03,
7
+ :yellow => 0x04,
8
+ :blue => 0x05,
9
+ :magenta => 0x06,
10
+ :cyan => 0x07,
11
+ :white => 0x08
11
12
  }
12
13
  end
@@ -70,7 +70,7 @@ module Termbox
70
70
  "[" => 0x1B,
71
71
  "]" => 0x1D,
72
72
  "_" => 0x1F, # same as few others
73
- " " => 0x20, # same as few others
73
+ " " => 0x20 # same as few others
74
74
  },
75
75
 
76
76
  "BACKSPACE" => 0x08,
@@ -80,6 +80,7 @@ module Termbox
80
80
  "RETURN" => 0x0D,
81
81
  "ESC" => 0x1B,
82
82
  "ESCAPE" => 0x1B,
83
+ "SPACE" => 0x20,
83
84
  }
84
85
 
85
86
  def lookup_key string
@@ -1,3 +1,3 @@
1
1
  module Termbox
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -9,17 +9,16 @@ Gem::Specification.new do |s|
9
9
  s.version = Termbox::VERSION
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = ["James Cook"]
12
- s.email = ["jamecook@gmail.com"]
13
- s.summary = %q{Ruby binding to Termbox, a ncurses alternative.}
14
- #s.description = %q{}
15
-
16
- s.required_rubygems_version = ">= 1.3.6"
17
- s.rubyforge_project = "rb_termbox"
12
+ s.email = ["jcook.rubyist@gmail.com"]
13
+ s.summary = s.description = %q{Ruby binding to Termbox, a ncurses alternative.}
14
+ s.homepage = "http://github.com/jamescook/rb_termbox"
15
+ s.license = "MIT"
18
16
 
19
17
  s.add_dependency "ffi"
20
18
  s.add_development_dependency "rspec"
21
19
 
22
20
  s.files = `git ls-files`.split("\n")
23
21
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
- s.require_paths = ["lib"]
22
+ s.require_paths = ["lib", "."]
23
+ s.extensions = %w[ext/termbox/extconf.rb]
25
24
  end
@@ -0,0 +1,37 @@
1
+ load File.join(File.expand_path("."), "/lib/termbox.rb")
2
+
3
+ class AllColors
4
+ def self.run
5
+ Termbox.termbox_library_path File.expand_path("./libtermbox.so")
6
+ Termbox.initialize_library
7
+ start = Termbox.tb_init
8
+ if start
9
+ Termbox.tb_select_input_mode 1 # TB_INPUT_ESC
10
+ ev = Termbox::Event.new
11
+ Termbox.tb_clear
12
+ draw_colors
13
+ Termbox.tb_present
14
+
15
+ while Termbox.tb_poll_event(ev) >= 0 do
16
+ case ev[:type]
17
+ when 1 #TB_EVENT_KEY
18
+ if ev[:key] == 0x1B # ESC
19
+ Termbox.tb_shutdown
20
+ exit
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.draw_colors
28
+ (0..255).to_a.each do |i|
29
+ Termbox.tb_change_cell(i,0, 0x20, 0x00, i);
30
+ end
31
+ end
32
+ end
33
+
34
+ if __FILE__ == $0
35
+ AllColors.run
36
+ end
37
+
@@ -6,7 +6,7 @@ class Keyboard
6
6
  def self.run
7
7
 
8
8
  # Tell termbox where your shared lib is
9
- Termbox.termbox_library_path File.join(File.expand_path("~/"), "work", "termbox_src", "termbox.dylib")
9
+ Termbox.termbox_library_path "termbox"
10
10
 
11
11
  # Call initialize_library once the above is set, this links us to the library C functions
12
12
  Termbox.initialize_library
@@ -49,28 +49,28 @@ class Keyboard
49
49
  end
50
50
 
51
51
  def self.draw
52
- Termbox.tb_change_cell(0, 0, 0x250C, Termbox::Colors[:white], Termbox::Colors[:black])
53
- Termbox.tb_change_cell(79, 0, 0x2510, Termbox::Colors[:white], Termbox::Colors[:black])
54
- Termbox.tb_change_cell(0, 23, 0x2514, Termbox::Colors[:white], Termbox::Colors[:black])
55
- Termbox.tb_change_cell(79, 23, 0x2518, Termbox::Colors[:white], Termbox::Colors[:black])
52
+ Termbox.tb_change_cell(0, 0, 0x250C, Termbox::Colors[:white], Termbox::Colors[:default])
53
+ Termbox.tb_change_cell(79, 0, 0x2510, Termbox::Colors[:white], Termbox::Colors[:default])
54
+ Termbox.tb_change_cell(0, 23, 0x2514, Termbox::Colors[:white], Termbox::Colors[:default])
55
+ Termbox.tb_change_cell(79, 23, 0x2518, Termbox::Colors[:white], Termbox::Colors[:default])
56
56
 
57
57
 
58
58
  1.upto(79) do |i|
59
- Termbox.tb_change_cell(i, 0, 0x2500, Termbox::Colors[:white], Termbox::Colors[:black])
60
- Termbox.tb_change_cell(i, 23, 0x2500, Termbox::Colors[:white], Termbox::Colors[:black])
61
- Termbox.tb_change_cell(i, 17, 0x2500, Termbox::Colors[:white], Termbox::Colors[:black])
62
- Termbox.tb_change_cell(i, 4, 0x2500, Termbox::Colors[:white], Termbox::Colors[:black])
59
+ Termbox.tb_change_cell(i, 0, 0x2500, Termbox::Colors[:white], Termbox::Colors[:default])
60
+ Termbox.tb_change_cell(i, 23, 0x2500, Termbox::Colors[:white], Termbox::Colors[:default])
61
+ Termbox.tb_change_cell(i, 17, 0x2500, Termbox::Colors[:white], Termbox::Colors[:default])
62
+ Termbox.tb_change_cell(i, 4, 0x2500, Termbox::Colors[:white], Termbox::Colors[:default])
63
63
  end
64
64
 
65
65
  1.upto(23) do |i|
66
- Termbox.tb_change_cell(0, i, 0x2502, Termbox::Colors[:white], Termbox::Colors[:black])
67
- Termbox.tb_change_cell(79, i, 0x2502, Termbox::Colors[:white], Termbox::Colors[:black])
66
+ Termbox.tb_change_cell(0, i, 0x2502, Termbox::Colors[:white], Termbox::Colors[:default])
67
+ Termbox.tb_change_cell(79, i, 0x2502, Termbox::Colors[:white], Termbox::Colors[:default])
68
68
  end
69
69
 
70
- Termbox.tb_change_cell(0, 17, 0x251C, Termbox::Colors[:white], Termbox::Colors[:black])
71
- Termbox.tb_change_cell(79, 17, 0x2524, Termbox::Colors[:white], Termbox::Colors[:black])
72
- Termbox.tb_change_cell(0, 4, 0x251C, Termbox::Colors[:white], Termbox::Colors[:black])
73
- Termbox.tb_change_cell(79, 4, 0x2524, Termbox::Colors[:white], Termbox::Colors[:black])
70
+ Termbox.tb_change_cell(0, 17, 0x251C, Termbox::Colors[:white], Termbox::Colors[:default])
71
+ Termbox.tb_change_cell(79, 17, 0x2524, Termbox::Colors[:white], Termbox::Colors[:default])
72
+ Termbox.tb_change_cell(0, 4, 0x251C, Termbox::Colors[:white], Termbox::Colors[:default])
73
+ Termbox.tb_change_cell(79, 4, 0x2524, Termbox::Colors[:white], Termbox::Colors[:default])
74
74
 
75
75
  5.upto(17) do |i|
76
76
  Termbox.tb_change_cell(1, i, 0x2588, Termbox::Colors[:yellow], Termbox::Colors[:yellow])