Skip to content

Commit bbe734b

Browse files
committed
Initial Commit
Signed-off-by: Derek Buitenhuis <[email protected]>
0 parents  commit bbe734b

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
termbox-go C bindings
2+
=====================
3+
4+
Go-to-C bindings for [termbox-go](https://github.com/nsf/termbox-go) for use with
5+
[BXD](https://github.com/dwbuiten/bxd). Please read the comment in `termbox-c.go`
6+
for build instructions.
7+
8+
A build for Windows, build with Go 1.7rc3, can be downloaded from the Releases
9+
section.

termbox-c.go

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* ISC License (ISC)
3+
*
4+
* Copyright (c) 2016, Derek Buitenhuis <derek.buitenhuis at gmail dot com>
5+
*
6+
* Permission to use, copy, modify, and/or distribute this software for any
7+
* purpose with or without fee is hereby granted, provided that the above
8+
* copyright notice and this permission notice appear in all copies.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
/*
20+
* This file contans minimal Go-to-C bindings for use termbox-go that emulate
21+
* just enough of the termbox C API to allow BXD to link and run. The goal
22+
* is Windows support for C codebases using termbox.
23+
*
24+
* To build and install:
25+
* go get github.com/nsf/termbox-go
26+
* go build -buildmode=c-archive -o termbox.a termbox-c.go
27+
* cp -f termbox.h $(PREFIX)/include/termbox.h
28+
* cp -f termbox.a $(PREFIX)/lib/libtermbox.a
29+
*/
30+
31+
package main
32+
33+
/*
34+
#include <stdint.h>
35+
#include <stdlib.h>
36+
#include <string.h>
37+
38+
#define TB_BLACK 1
39+
#define TB_WHITE 8
40+
#define TB_BOLD 0x0100
41+
#define TB_UNDERLINE 0x0200
42+
#define TB_EVENT_KEY 1
43+
#define TB_EVENT_RESIZE 2
44+
#define TB_KEY_ARROW_UP (0xFFFF-18)
45+
#define TB_KEY_ARROW_DOWN (0xFFFF-19)
46+
#define TB_KEY_SPACE 0x20
47+
48+
struct tb_cell {
49+
uint32_t ch;
50+
uint16_t fg;
51+
uint16_t bg;
52+
};
53+
54+
struct tb_event {
55+
uint8_t type;
56+
uint8_t mod;
57+
uint16_t key;
58+
uint32_t ch;
59+
int32_t w;
60+
int32_t h;
61+
int32_t x;
62+
int32_t y;
63+
};
64+
*/
65+
import "C"
66+
67+
import (
68+
termbox "github.com/nsf/termbox-go"
69+
"unsafe"
70+
)
71+
72+
var cellBuffer *C.struct_tb_cell
73+
var cellSize int
74+
var attrs = map[C.uint16_t]termbox.Attribute{
75+
0: termbox.ColorDefault,
76+
1: termbox.ColorBlack,
77+
8: termbox.ColorWhite,
78+
}
79+
var cattrs = map[termbox.Attribute]C.uint16_t{
80+
termbox.ColorDefault: 0,
81+
termbox.ColorBlack: 1,
82+
termbox.ColorWhite: 2,
83+
}
84+
85+
//export tb_init
86+
func tb_init() C.int {
87+
cellBuffer = nil
88+
cellSize = 0
89+
90+
err := termbox.Init()
91+
if err != nil {
92+
return -1
93+
}
94+
return 0
95+
}
96+
97+
//export tb_shutdown
98+
func tb_shutdown() {
99+
termbox.Close()
100+
}
101+
102+
//export tb_width
103+
func tb_width() C.int {
104+
w, _ := termbox.Size()
105+
return C.int(w)
106+
}
107+
108+
//export tb_height
109+
func tb_height() C.int {
110+
_, h := termbox.Size()
111+
return C.int(h)
112+
}
113+
114+
//export tb_clear
115+
func tb_clear() {
116+
if cellBuffer != nil && cellSize != 0 {
117+
C.memset(unsafe.Pointer(cellBuffer), 0, C.size_t(cellSize))
118+
}
119+
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
120+
}
121+
122+
//export tb_present
123+
func tb_present() {
124+
w, h := termbox.Size()
125+
buf := termbox.CellBuffer()
126+
if cellSize == w*h {
127+
for i := 0; i < w*h; i++ {
128+
c_cell := (*C.struct_tb_cell)(unsafe.Pointer(uintptr(unsafe.Pointer(cellBuffer)) + uintptr(C.sizeof_struct_tb_cell*i)))
129+
buf[i].Fg = termbox.Attribute((*c_cell).fg)
130+
buf[i].Bg = termbox.Attribute((*c_cell).bg)
131+
buf[i].Ch = rune(uint32((*c_cell).ch))
132+
}
133+
}
134+
termbox.Flush()
135+
}
136+
137+
//export tb_cell_buffer
138+
func tb_cell_buffer() *C.struct_tb_cell {
139+
w, h := termbox.Size()
140+
buf := termbox.CellBuffer()
141+
if cellSize != len(buf) {
142+
if cellBuffer != nil {
143+
C.free(unsafe.Pointer(cellBuffer))
144+
}
145+
cellBuffer = (*C.struct_tb_cell)(C.malloc(C.sizeof_struct_tb_cell * C.size_t(w*h)))
146+
cellSize = w * h
147+
}
148+
for i := 0; i < w*h; i++ {
149+
c_cell := (*C.struct_tb_cell)(unsafe.Pointer(uintptr(unsafe.Pointer(cellBuffer)) + uintptr(C.sizeof_struct_tb_cell*i)))
150+
(*c_cell).fg = C.uint16_t(buf[i].Fg)
151+
(*c_cell).bg = C.uint16_t(buf[i].Bg)
152+
(*c_cell).ch = C.uint32_t(uint32(buf[i].Ch))
153+
}
154+
return cellBuffer
155+
}
156+
157+
//export tb_poll_event
158+
func tb_poll_event(ev *C.struct_tb_event) C.int {
159+
event := termbox.PollEvent()
160+
161+
if event.Type == termbox.EventKey {
162+
(*ev)._type = C.TB_EVENT_KEY
163+
} else if event.Type == termbox.EventResize {
164+
(*ev)._type = C.TB_EVENT_RESIZE
165+
return 1
166+
} else {
167+
(*ev)._type = 3
168+
return 1
169+
}
170+
171+
if event.Ch == 0 {
172+
if event.Key == termbox.KeySpace {
173+
(*ev).key = C.TB_KEY_SPACE
174+
} else if event.Key == termbox.KeyArrowUp {
175+
(*ev).key = C.TB_KEY_ARROW_UP
176+
} else if event.Key == termbox.KeyArrowDown {
177+
(*ev).key = C.TB_KEY_ARROW_DOWN
178+
} else {
179+
(*ev).key = 0
180+
}
181+
} else {
182+
(*ev).key = 0
183+
(*ev).ch = C.uint32_t(uint32(event.Ch))
184+
}
185+
186+
return 1
187+
}
188+
189+
func main() {
190+
}

0 commit comments

Comments
 (0)