├── .gitignore ├── LICENSE ├── LICENSE.Apache ├── Makefile ├── README.md ├── c_src ├── Makefile ├── encurses.c └── encurses.h ├── include └── encurses.hrl ├── rebar.config ├── rebar.lock ├── rebar3 ├── run.escript ├── src ├── encurses.app.src ├── encurses.erl ├── encurses_demo.erl └── paths.erl └── test └── nif_path_test.erl /.gitignore: -------------------------------------------------------------------------------- 1 | priv/ 2 | c_src/*.o 3 | ebin/ 4 | _build/ 5 | .rebar 6 | *.swp 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ============================================================================ 2 | Encurses 0.4.2 3 | 4 | Copyright © 2012 Jeff Zellner . All Rights Reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. The name of the author may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | ============================================================================ 28 | 29 | include/encurses.hrl Copyright 2010 Erlang Solutions Ltd. 30 | -------------------------------------------------------------------------------- /LICENSE.Apache: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: compile dialyzer 2 | 3 | compile: 4 | @./rebar3 compile 5 | 6 | dialyzer: 7 | @./rebar3 dialyzer 8 | 9 | clean: 10 | @./rebar3 clean 11 | $(if $(wildcard _build/), rm -rf _build/) 12 | $(if $(wildcard ebin/), rm -rf ebin/) 13 | $(if $(wildcard priv/), rm -rf priv/) 14 | 15 | run: compile 16 | @./run.escript 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | encurses 2 | ===== 3 | An ncurses library for Erlang. 4 | 5 | Inspired by and based on cecho, written by Mazen Harake (http://github.com/mazenharake/cecho). 6 | 7 | Requirements 8 | ===== 9 | Erlang R14A+ is recommended. 10 | 11 | SMP Erlang required. 12 | 13 | -------------------------------------------------------------------------------- /c_src/Makefile: -------------------------------------------------------------------------------- 1 | # Based on c_src.mk from erlang.mk by Loic Hoguin 2 | 3 | CURDIR := $(shell pwd) 4 | BASEDIR := $(abspath $(CURDIR)/..) 5 | 6 | PROJECT ?= $(notdir $(BASEDIR)) 7 | PROJECT := $(strip $(PROJECT)) 8 | 9 | ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).") 10 | ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).") 11 | ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).") 12 | 13 | C_SRC_DIR = $(CURDIR) 14 | C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so 15 | 16 | # System type and C compiler/flags. 17 | 18 | UNAME_SYS := $(shell uname -s) 19 | ifeq ($(UNAME_SYS), Darwin) 20 | CC ?= cc 21 | CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes 22 | CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall 23 | LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress 24 | else ifeq ($(UNAME_SYS), FreeBSD) 25 | CC ?= cc 26 | CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes 27 | CXXFLAGS ?= -O3 -finline-functions -Wall 28 | else ifeq ($(UNAME_SYS), Linux) 29 | CC ?= gcc 30 | CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes 31 | CXXFLAGS ?= -O3 -finline-functions -Wall 32 | endif 33 | 34 | CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) 35 | CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) 36 | 37 | LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei -lncurses 38 | LDFLAGS += -shared 39 | 40 | # Verbosity. 41 | 42 | c_verbose_0 = @echo " C " $(?F); 43 | c_verbose = $(c_verbose_$(V)) 44 | 45 | cpp_verbose_0 = @echo " CPP " $(?F); 46 | cpp_verbose = $(cpp_verbose_$(V)) 47 | 48 | link_verbose_0 = @echo " LD " $(@F); 49 | link_verbose = $(link_verbose_$(V)) 50 | 51 | SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \)) 52 | OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) 53 | 54 | COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c 55 | COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c 56 | 57 | $(C_SRC_OUTPUT): $(OBJECTS) 58 | @mkdir -p $(BASEDIR)/priv/ 59 | $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) 60 | 61 | %.o: %.c 62 | $(COMPILE_C) $(OUTPUT_OPTION) $< 63 | 64 | %.o: %.cc 65 | $(COMPILE_CPP) $(OUTPUT_OPTION) $< 66 | 67 | %.o: %.C 68 | $(COMPILE_CPP) $(OUTPUT_OPTION) $< 69 | 70 | %.o: %.cpp 71 | $(COMPILE_CPP) $(OUTPUT_OPTION) $< 72 | 73 | clean: 74 | @rm -f $(C_SRC_OUTPUT) $(OBJECTS) 75 | -------------------------------------------------------------------------------- /c_src/encurses.c: -------------------------------------------------------------------------------- 1 | // ============================================================================ 2 | // Encurses 0.4.2 3 | // 4 | // Copyright © 2012 Jeff Zellner . All Rights Reserved. 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 2. Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 3. The name of the author may not be used to endorse or promote products 15 | // derived from this software without specific prior written permission. 16 | // 17 | // THIS SOFTWARE IS PROVIDED BY AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | // ============================================================================ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "erl_nif.h" 35 | #include "encurses.h" 36 | 37 | static ErlNifMutex* g_lock = NULL; 38 | 39 | typedef struct 40 | { 41 | ErlNifPid* pid; 42 | long winslot; 43 | } qitem_payload_t; 44 | 45 | 46 | typedef struct _qitem_t 47 | { 48 | struct _qitem_t* next; 49 | qitem_payload_t* payload; 50 | } qitem_t; 51 | 52 | typedef struct 53 | { 54 | ErlNifMutex* lock; 55 | ErlNifCond* cond; 56 | qitem_t* head; 57 | qitem_t* tail; 58 | } queue_t; 59 | 60 | typedef struct 61 | { 62 | ErlNifThreadOpts* opts; 63 | ErlNifTid qthread; 64 | queue_t* queue; 65 | ERL_NIF_TERM atom_ok; 66 | } state_t; 67 | 68 | queue_t* 69 | queue_create() 70 | { 71 | queue_t* ret; 72 | 73 | ret = (queue_t*) enif_alloc(sizeof(queue_t)); 74 | if(ret == NULL) return NULL; 75 | 76 | ret->lock = NULL; 77 | ret->cond = NULL; 78 | ret->head = NULL; 79 | ret->tail = NULL; 80 | 81 | ret->lock = enif_mutex_create("q_lock"); 82 | if(ret->lock == NULL) goto error; 83 | 84 | ret->cond = enif_cond_create("q_cond"); 85 | if(ret->cond == NULL) goto error; 86 | 87 | return ret; 88 | 89 | error: 90 | if(ret->lock != NULL) enif_mutex_destroy(ret->lock); 91 | if(ret->cond != NULL) enif_cond_destroy(ret->cond); 92 | if(ret != NULL) enif_free(ret); 93 | return NULL; 94 | } 95 | 96 | void 97 | queue_destroy(queue_t* queue) 98 | { 99 | ErlNifMutex* lock; 100 | ErlNifCond* cond; 101 | 102 | enif_mutex_lock(queue->lock); 103 | assert(queue->head == NULL && "Destroying a non-empty queue."); 104 | assert(queue->tail == NULL && "Destroying a queue in an invalid state."); 105 | lock = queue->lock; 106 | cond = queue->cond; 107 | queue->lock = NULL; 108 | queue->cond = NULL; 109 | enif_mutex_unlock(lock); 110 | 111 | enif_cond_destroy(cond); 112 | enif_mutex_destroy(lock); 113 | enif_free(queue); 114 | } 115 | 116 | int 117 | queue_push(queue_t* queue, qitem_payload_t* payload) 118 | { 119 | qitem_t* item = (qitem_t*) enif_alloc(sizeof(qitem_t)); 120 | if(item == NULL) return 0; 121 | 122 | item->payload = payload; 123 | item->next = NULL; 124 | 125 | enif_mutex_lock(queue->lock); 126 | 127 | if(queue->tail != NULL) 128 | { 129 | queue->tail->next = item; 130 | } 131 | 132 | queue->tail = item; 133 | 134 | if(queue->head == NULL) 135 | { 136 | queue->head = queue->tail; 137 | } 138 | 139 | enif_cond_signal(queue->cond); 140 | enif_mutex_unlock(queue->lock); 141 | 142 | return 1; 143 | } 144 | 145 | qitem_payload_t* 146 | queue_pop(queue_t* queue) 147 | { 148 | qitem_t* item; 149 | qitem_payload_t* ret = NULL; 150 | 151 | enif_mutex_lock(queue->lock); 152 | 153 | while(queue->head == NULL) 154 | { 155 | enif_cond_wait(queue->cond, queue->lock); 156 | } 157 | 158 | item = queue->head; 159 | queue->head = item->next; 160 | item->next = NULL; 161 | 162 | if(queue->head == NULL) 163 | { 164 | queue->tail = NULL; 165 | } 166 | 167 | enif_mutex_unlock(queue->lock); 168 | 169 | ret = item->payload; 170 | enif_free(item); 171 | 172 | return ret; 173 | } 174 | 175 | /** implementations **/ 176 | 177 | /* NIF management */ 178 | 179 | static int 180 | load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) 181 | { 182 | g_lock = enif_mutex_create("g_lock"); 183 | 184 | int i; 185 | for(i=0;i<_MAXWINDOWS;i++){ 186 | slots[i] = NULL; 187 | } 188 | 189 | state_t* state = (state_t*) enif_alloc(sizeof(state_t)); 190 | if(state == NULL) return -1; 191 | 192 | state->queue = queue_create(); 193 | if(state->queue == NULL) goto error; 194 | 195 | state->opts = enif_thread_opts_create("thread_opts"); 196 | if(enif_thread_create("", &(state->qthread), thr_main, state, state->opts 197 | ) != 0) 198 | { 199 | goto error; 200 | } 201 | 202 | state->atom_ok = enif_make_atom(env, "ok"); 203 | 204 | *priv = (void*) state; 205 | 206 | return 0; 207 | 208 | error: 209 | if(g_lock != NULL) enif_mutex_destroy(g_lock); 210 | if(state->queue != NULL) queue_destroy(state->queue); 211 | enif_free(state->queue); 212 | return -1; 213 | } 214 | 215 | static void 216 | unload(ErlNifEnv* env, void* priv) 217 | { 218 | state_t* state = (state_t*) priv; 219 | void* resp; 220 | 221 | if(g_lock != NULL) enif_mutex_destroy(g_lock); 222 | 223 | queue_push(state->queue, NULL); 224 | enif_thread_join(state->qthread, &resp); 225 | queue_destroy(state->queue); 226 | 227 | enif_thread_opts_destroy(state->opts); 228 | enif_free(state); 229 | } 230 | 231 | 232 | /* internal helper functions */ 233 | 234 | static int 235 | find_free_window_slot() 236 | { 237 | int i; 238 | for(i=1;i<_MAXWINDOWS;i++){ 239 | if(slots[i] == NULL) 240 | return i; 241 | } 242 | return -1; 243 | } 244 | 245 | static ERL_NIF_TERM 246 | done(ErlNifEnv* env, int code) 247 | { 248 | if(code == OK) 249 | { 250 | return enif_make_atom(env, "ok"); 251 | } 252 | else 253 | { 254 | return enif_make_tuple2(env, 255 | enif_make_atom(env, "error"), 256 | enif_make_int(env, code)); 257 | } 258 | } 259 | 260 | static ERL_NIF_TERM 261 | boolean(ErlNifEnv* env, int value) 262 | { 263 | if(value == TRUE) 264 | { 265 | return enif_make_atom(env, "true"); 266 | } 267 | else 268 | { 269 | return enif_make_atom(env, "false"); 270 | } 271 | } 272 | 273 | // refresh 274 | 275 | static ERL_NIF_TERM 276 | e_refresh(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 277 | { 278 | enif_mutex_lock(g_lock); 279 | int code = refresh(); 280 | enif_mutex_unlock(g_lock); 281 | return done(env, code); 282 | } 283 | 284 | static ERL_NIF_TERM 285 | e_wrefresh(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 286 | { 287 | long win; 288 | enif_get_long(env, argv[0], &win); 289 | 290 | enif_mutex_lock(g_lock); 291 | int code = wrefresh(slots[win]); 292 | enif_mutex_unlock(g_lock); 293 | 294 | return done(env, code); 295 | } 296 | 297 | // newwin 298 | 299 | static ERL_NIF_TERM 300 | e_newwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 301 | { 302 | enif_mutex_lock(g_lock); 303 | int slot = find_free_window_slot(); 304 | if(slot >= 1) { 305 | int width, height, startx, starty; 306 | enif_get_int(env, argv[0], &width); 307 | enif_get_int(env, argv[1], &height); 308 | enif_get_int(env, argv[2], &startx); 309 | enif_get_int(env, argv[3], &starty); 310 | slots[slot] = newwin(height, width, starty, startx); 311 | enif_mutex_unlock(g_lock); 312 | return enif_make_long(env, slot); 313 | } else { 314 | enif_mutex_unlock(g_lock); 315 | return done(env, FALSE); 316 | } 317 | } 318 | 319 | // delwin 320 | 321 | static ERL_NIF_TERM 322 | e_delwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 323 | { 324 | long slot; 325 | enif_get_long(env, argv[0], &slot); 326 | enif_mutex_lock(g_lock); 327 | if(slot == 0) 328 | { 329 | enif_mutex_unlock(g_lock); 330 | return done(env, FALSE); 331 | } 332 | else if(slots[slot] == NULL) 333 | { 334 | enif_mutex_unlock(g_lock); 335 | return done(env, FALSE); 336 | } 337 | else 338 | { 339 | delwin(slots[slot]); 340 | slots[slot] = NULL; 341 | enif_mutex_unlock(g_lock); 342 | return done(env, OK); 343 | } 344 | } 345 | 346 | // endwin 347 | 348 | static ERL_NIF_TERM 349 | e_endwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 350 | { 351 | enif_mutex_lock(g_lock); 352 | int code = endwin(); 353 | enif_mutex_unlock(g_lock); 354 | return done(env, code); 355 | } 356 | 357 | // initscr 358 | 359 | static ERL_NIF_TERM 360 | e_initscr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 361 | { 362 | enif_mutex_lock(g_lock); 363 | slots[0] = (WINDOW *)initscr(); 364 | enif_mutex_unlock(g_lock); 365 | return enif_make_long(env, 0); 366 | } 367 | 368 | // cbreak 369 | 370 | static ERL_NIF_TERM 371 | e_cbreak(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 372 | { 373 | enif_mutex_lock(g_lock); 374 | int code = cbreak(); 375 | enif_mutex_unlock(g_lock); 376 | return done(env, code); 377 | } 378 | 379 | // nocbreak 380 | 381 | static ERL_NIF_TERM 382 | e_nocbreak(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 383 | { 384 | enif_mutex_lock(g_lock); 385 | int code = nocbreak(); 386 | enif_mutex_unlock(g_lock); 387 | return done(env, code); 388 | } 389 | 390 | // echo 391 | 392 | static ERL_NIF_TERM 393 | e_echo(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 394 | { 395 | enif_mutex_lock(g_lock); 396 | int code = echo(); 397 | enif_mutex_unlock(g_lock); 398 | return done(env, code); 399 | } 400 | 401 | // noecho 402 | 403 | static ERL_NIF_TERM 404 | e_noecho(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 405 | { 406 | enif_mutex_lock(g_lock); 407 | int code = noecho(); 408 | enif_mutex_unlock(g_lock); 409 | return done(env, code); 410 | } 411 | 412 | // erase 413 | 414 | static ERL_NIF_TERM 415 | int_erase(ErlNifEnv* env, WINDOW *win) 416 | { 417 | enif_mutex_lock(g_lock); 418 | int code = werase(win); 419 | enif_mutex_unlock(g_lock); 420 | return done(env, code); 421 | } 422 | 423 | static ERL_NIF_TERM 424 | e_erase(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 425 | { 426 | return int_erase(env, slots[0]); 427 | } 428 | 429 | static ERL_NIF_TERM 430 | e_werase(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 431 | { 432 | long win; 433 | enif_get_long(env, argv[0], &win); 434 | return int_erase(env, slots[win]); 435 | } 436 | 437 | // addch 438 | 439 | static ERL_NIF_TERM 440 | e_addch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 441 | { 442 | long ch; 443 | enif_get_long(env, argv[0], &ch); 444 | enif_mutex_lock(g_lock); 445 | int code = addch((chtype)ch); 446 | enif_mutex_unlock(g_lock); 447 | return done(env, code); 448 | } 449 | 450 | static ERL_NIF_TERM 451 | e_waddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 452 | { 453 | long win, ch; 454 | enif_get_long(env, argv[0], &win); 455 | enif_get_long(env, argv[1], &ch); 456 | enif_mutex_lock(g_lock); 457 | int code = waddch(slots[win], (chtype)ch); 458 | enif_mutex_unlock(g_lock); 459 | return done(env, code); 460 | } 461 | 462 | static ERL_NIF_TERM 463 | e_mvaddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 464 | { 465 | long x, y, ch; 466 | enif_get_long(env, argv[0], &x); 467 | enif_get_long(env, argv[1], &y); 468 | enif_get_long(env, argv[2], &ch); 469 | enif_mutex_lock(g_lock); 470 | int code = mvaddch((int)y, (int)x, (chtype)ch); 471 | enif_mutex_unlock(g_lock); 472 | return done(env, code); 473 | } 474 | 475 | static ERL_NIF_TERM 476 | e_mvwaddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 477 | { 478 | long win, x, y, ch; 479 | enif_get_long(env, argv[0], &win); 480 | enif_get_long(env, argv[1], &x); 481 | enif_get_long(env, argv[2], &y); 482 | enif_get_long(env, argv[3], &ch); 483 | enif_mutex_lock(g_lock); 484 | int code = mvwaddch(slots[win], (int)y, (int)x, (chtype)ch); 485 | enif_mutex_unlock(g_lock); 486 | return done(env, code); 487 | } 488 | 489 | // addstr 490 | 491 | static ERL_NIF_TERM 492 | e_addstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 493 | { 494 | long length; 495 | enif_get_long(env, argv[0], &length); 496 | char buff[length]; 497 | enif_get_string(env, argv[1], buff, length+1, ERL_NIF_LATIN1); 498 | enif_mutex_lock(g_lock); 499 | int code = addnstr(buff, length); 500 | enif_mutex_unlock(g_lock); 501 | return done(env, code); 502 | } 503 | 504 | static ERL_NIF_TERM 505 | e_waddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 506 | { 507 | int length; 508 | enif_get_int(env, argv[1], &length); 509 | 510 | char buff[length]; 511 | enif_get_string(env, argv[2], buff, length+1, ERL_NIF_LATIN1); 512 | 513 | int win; 514 | enif_get_int(env, argv[0], &win); 515 | 516 | enif_mutex_lock(g_lock); 517 | int code = waddnstr(slots[win], buff, length); 518 | enif_mutex_unlock(g_lock); 519 | 520 | return done(env, code); 521 | } 522 | 523 | static ERL_NIF_TERM 524 | e_mvaddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 525 | { 526 | int length; 527 | enif_get_int(env, argv[2], &length); 528 | 529 | char buff[length]; 530 | enif_get_string(env, argv[3], buff, length+1, ERL_NIF_LATIN1); 531 | 532 | int x, y; 533 | enif_get_int(env, argv[0], &x); 534 | enif_get_int(env, argv[1], &y); 535 | 536 | enif_mutex_lock(g_lock); 537 | int code = mvaddnstr(y, x, buff, length); 538 | enif_mutex_unlock(g_lock); 539 | 540 | return done(env, code); 541 | } 542 | 543 | static ERL_NIF_TERM 544 | e_mvwaddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 545 | { 546 | int length; 547 | enif_get_int(env, argv[3], &length); 548 | 549 | char buff[length]; 550 | enif_get_string(env, argv[4], buff, length+1, ERL_NIF_LATIN1); 551 | 552 | int win, x, y; 553 | enif_get_int(env, argv[0], &win); 554 | enif_get_int(env, argv[1], &x); 555 | enif_get_int(env, argv[2], &y); 556 | 557 | enif_mutex_lock(g_lock); 558 | int code = mvwaddnstr(slots[win], y, x, buff, length); 559 | enif_mutex_unlock(g_lock); 560 | 561 | return done(env, code); 562 | } 563 | 564 | // move 565 | 566 | static ERL_NIF_TERM 567 | e_move(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 568 | { 569 | int x, y; 570 | enif_get_int(env, argv[0], &x); 571 | enif_get_int(env, argv[1], &y); 572 | enif_mutex_lock(g_lock); 573 | int code = move(y,x); 574 | enif_mutex_unlock(g_lock); 575 | return done(env, code); 576 | } 577 | 578 | static ERL_NIF_TERM 579 | e_wmove(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 580 | { 581 | int win, x, y; 582 | enif_get_int(env, argv[0], &win); 583 | enif_get_int(env, argv[1], &x); 584 | enif_get_int(env, argv[2], &y); 585 | enif_mutex_lock(g_lock); 586 | int code = wmove(slots[win], y,x); 587 | enif_mutex_unlock(g_lock); 588 | return done(env, code); 589 | } 590 | 591 | // getxy 592 | 593 | static ERL_NIF_TERM 594 | int_getxy(ErlNifEnv* env, WINDOW *win) 595 | { 596 | long x, y; 597 | enif_mutex_lock(g_lock); 598 | getyx(win, y, x); 599 | enif_mutex_unlock(g_lock); 600 | return enif_make_tuple2(env, 601 | enif_make_int(env, (int) x), 602 | enif_make_int(env, (int) y)); 603 | } 604 | 605 | static ERL_NIF_TERM 606 | e_getxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 607 | { 608 | return int_getxy(env, slots[0]); 609 | } 610 | 611 | static ERL_NIF_TERM 612 | e_wgetxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 613 | { 614 | long win; 615 | enif_get_long(env, argv[0], &win); 616 | return int_getxy(env, slots[win]); 617 | } 618 | 619 | // getmaxxy 620 | 621 | static ERL_NIF_TERM 622 | int_getmaxxy(ErlNifEnv* env, WINDOW *win) 623 | { 624 | long x, y; 625 | enif_mutex_lock(g_lock); 626 | getmaxyx(win, y, x); 627 | enif_mutex_unlock(g_lock); 628 | return enif_make_tuple2(env, 629 | enif_make_int(env, (int) x), 630 | enif_make_int(env, (int) y)); 631 | } 632 | 633 | static ERL_NIF_TERM 634 | e_getmaxxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 635 | { 636 | return int_getmaxxy(env, slots[0]); 637 | } 638 | 639 | static ERL_NIF_TERM 640 | e_wgetmaxxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 641 | { 642 | long win; 643 | enif_get_long(env, argv[0], &win); 644 | return int_getmaxxy(env, slots[win]); 645 | } 646 | 647 | // curs_set 648 | 649 | static ERL_NIF_TERM 650 | e_curs_set(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 651 | { 652 | long flag; 653 | enif_get_long(env, argv[0], &flag); 654 | enif_mutex_lock(g_lock); 655 | int code = curs_set((int)flag); 656 | enif_mutex_unlock(g_lock); 657 | return done(env, code); 658 | } 659 | 660 | // has_colors 661 | 662 | static ERL_NIF_TERM 663 | e_has_colors(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 664 | { 665 | enif_mutex_lock(g_lock); 666 | int code = has_colors(); 667 | enif_mutex_unlock(g_lock); 668 | return boolean(env, code); 669 | } 670 | 671 | // start_colors 672 | 673 | static ERL_NIF_TERM 674 | e_start_color(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 675 | { 676 | enif_mutex_lock(g_lock); 677 | int code = start_color(); 678 | enif_mutex_unlock(g_lock); 679 | return done(env, code); 680 | } 681 | 682 | // init_pair 683 | 684 | static ERL_NIF_TERM 685 | e_init_pair(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 686 | { 687 | long n, fcolor, bcolor; 688 | enif_get_long(env, argv[0], &n); 689 | enif_get_long(env, argv[1], &fcolor); 690 | enif_get_long(env, argv[2], &bcolor); 691 | 692 | enif_mutex_lock(g_lock); 693 | int code = init_pair((int)n, (int)fcolor, (int)bcolor); 694 | enif_mutex_unlock(g_lock); 695 | return done(env, code); 696 | } 697 | 698 | // attron/attroff 699 | 700 | static ERL_NIF_TERM 701 | e_attron(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 702 | { 703 | long attrs; 704 | enif_get_long(env, argv[0], &attrs); 705 | enif_mutex_lock(g_lock); 706 | int code = wattron(slots[0], (int)attrs); 707 | enif_mutex_unlock(g_lock); 708 | return done(env, code); 709 | } 710 | 711 | static ERL_NIF_TERM 712 | e_wattron(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 713 | { 714 | long win, attrs; 715 | enif_get_long(env, argv[0], &win); 716 | enif_get_long(env, argv[1], &attrs); 717 | enif_mutex_lock(g_lock); 718 | int code = wattron(slots[win], (int)attrs); 719 | enif_mutex_unlock(g_lock); 720 | return done(env, code); 721 | } 722 | 723 | static ERL_NIF_TERM 724 | e_attroff(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 725 | { 726 | long attrs; 727 | enif_get_long(env, argv[0], &attrs); 728 | enif_mutex_lock(g_lock); 729 | int code = wattroff(slots[0], (int)attrs); 730 | enif_mutex_unlock(g_lock); 731 | return done(env, code); 732 | } 733 | 734 | static ERL_NIF_TERM 735 | e_wattroff(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 736 | { 737 | long win, attrs; 738 | enif_get_long(env, argv[0], &win); 739 | enif_get_long(env, argv[1], &attrs); 740 | enif_mutex_lock(g_lock); 741 | int code = wattroff(slots[win], (int)attrs); 742 | enif_mutex_unlock(g_lock); 743 | return done(env, code); 744 | } 745 | 746 | // nl/nonl 747 | 748 | static ERL_NIF_TERM 749 | e_nl(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 750 | { 751 | enif_mutex_lock(g_lock); 752 | int code = nl(); 753 | enif_mutex_unlock(g_lock); 754 | return done(env, code); 755 | } 756 | 757 | static ERL_NIF_TERM 758 | e_nonl(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 759 | { 760 | enif_mutex_lock(g_lock); 761 | int code = nonl(); 762 | enif_mutex_unlock(g_lock); 763 | return done(env, code); 764 | } 765 | 766 | // scrollok 767 | 768 | static ERL_NIF_TERM 769 | e_scrollok(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 770 | { 771 | long win, flag; 772 | enif_get_long(env, argv[0], &win); 773 | enif_get_long(env, argv[1], &flag); 774 | enif_mutex_lock(g_lock); 775 | int code = scrollok(slots[win], (int)flag); 776 | enif_mutex_unlock(g_lock); 777 | return done(env, code); 778 | } 779 | 780 | // vline/hline 781 | 782 | static ERL_NIF_TERM 783 | e_hline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 784 | { 785 | long ch, max; 786 | enif_get_long(env, argv[0], &ch); 787 | enif_get_long(env, argv[1], &max); 788 | enif_mutex_lock(g_lock); 789 | int code = whline(slots[0], (chtype)ch, (int)max); 790 | enif_mutex_unlock(g_lock); 791 | return done(env, code); 792 | } 793 | 794 | static ERL_NIF_TERM 795 | e_whline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 796 | { 797 | long win, ch, max; 798 | enif_get_long(env, argv[0], &win); 799 | enif_get_long(env, argv[1], &ch); 800 | enif_get_long(env, argv[2], &max); 801 | enif_mutex_lock(g_lock); 802 | int code = whline(slots[win], (chtype)ch, (int)max); 803 | enif_mutex_unlock(g_lock); 804 | return done(env, code); 805 | } 806 | 807 | static ERL_NIF_TERM 808 | e_vline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 809 | { 810 | long ch, max; 811 | enif_get_long(env, argv[0], &ch); 812 | enif_get_long(env, argv[1], &max); 813 | enif_mutex_lock(g_lock); 814 | int code = wvline(slots[0], (chtype)ch, (int)max); 815 | enif_mutex_unlock(g_lock); 816 | return done(env, code); 817 | } 818 | 819 | static ERL_NIF_TERM 820 | e_wvline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 821 | { 822 | long win, ch, max; 823 | enif_get_long(env, argv[0], &win); 824 | enif_get_long(env, argv[1], &ch); 825 | enif_get_long(env, argv[2], &max); 826 | enif_mutex_lock(g_lock); 827 | int code = wvline(slots[win], (chtype)ch, (int)max); 828 | enif_mutex_unlock(g_lock); 829 | return done(env, code); 830 | } 831 | 832 | // border 833 | 834 | static ERL_NIF_TERM 835 | e_border(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 836 | { 837 | long ls, rs, ts, bs, tl, tr, bl, br; 838 | enif_get_long(env, argv[0], &ls); 839 | enif_get_long(env, argv[1], &rs); 840 | enif_get_long(env, argv[2], &ts); 841 | enif_get_long(env, argv[3], &bs); 842 | enif_get_long(env, argv[4], &tl); 843 | enif_get_long(env, argv[5], &tr); 844 | enif_get_long(env, argv[6], &bl); 845 | enif_get_long(env, argv[7], &br); 846 | enif_mutex_lock(g_lock); 847 | int code = wborder(slots[0], (chtype)ls, (chtype)rs, (chtype)ts, 848 | (chtype)bs, (chtype)tl, (chtype)tr, (chtype)bl, (chtype)br); 849 | enif_mutex_unlock(g_lock); 850 | return done(env, code); 851 | } 852 | 853 | static ERL_NIF_TERM 854 | e_wborder(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 855 | { 856 | long win, ls, rs, ts, bs, tl, tr, bl, br; 857 | enif_get_long(env, argv[0], &win); 858 | enif_get_long(env, argv[1], &ls); 859 | enif_get_long(env, argv[2], &rs); 860 | enif_get_long(env, argv[3], &ts); 861 | enif_get_long(env, argv[4], &bs); 862 | enif_get_long(env, argv[5], &tl); 863 | enif_get_long(env, argv[6], &tr); 864 | enif_get_long(env, argv[7], &bl); 865 | enif_get_long(env, argv[8], &br); 866 | enif_mutex_lock(g_lock); 867 | int code = wborder(slots[win], (chtype)ls, (chtype)rs, (chtype)ts, 868 | (chtype)bs, (chtype)tl, (chtype)tr, (chtype)bl, (chtype)br); 869 | enif_mutex_unlock(g_lock); 870 | return done(env, code); 871 | } 872 | 873 | // box 874 | 875 | static ERL_NIF_TERM 876 | e_box(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 877 | { 878 | long win, hch, vch; 879 | enif_get_long(env, argv[0], &win); 880 | enif_get_long(env, argv[1], &hch); 881 | enif_get_long(env, argv[2], &vch); 882 | enif_mutex_lock(g_lock); 883 | int code = box(slots[win], (chtype)vch, (chtype)hch); 884 | enif_mutex_unlock(g_lock); 885 | return done(env, code); 886 | } 887 | 888 | // keypad 889 | 890 | static ERL_NIF_TERM 891 | e_keypad(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 892 | { 893 | long win, flag; 894 | enif_get_long(env, argv[0], &win); 895 | enif_get_long(env, argv[1], &flag); 896 | enif_mutex_lock(g_lock); 897 | int code = keypad(slots[win], (int)flag); 898 | enif_mutex_unlock(g_lock); 899 | return done(env, code); 900 | } 901 | 902 | // timeout 903 | 904 | static ERL_NIF_TERM 905 | e_timeout(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 906 | { 907 | int delay; 908 | enif_get_int(env, argv[0], &delay); 909 | enif_mutex_lock(g_lock); 910 | timeout(delay); 911 | enif_mutex_unlock(g_lock); 912 | return done(env, 0); 913 | } 914 | 915 | static ERL_NIF_TERM 916 | e_wtimeout(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 917 | { 918 | long win; 919 | int delay; 920 | enif_get_long(env, argv[0], &win); 921 | enif_get_int(env, argv[1], &delay); 922 | enif_mutex_lock(g_lock); 923 | wtimeout(slots[win], delay); 924 | enif_mutex_unlock(g_lock); 925 | return done(env, 0); 926 | } 927 | 928 | // wgetch 929 | 930 | static ERL_NIF_TERM 931 | e_wgetch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) 932 | { 933 | state_t* state = (state_t*) enif_priv_data(env); 934 | qitem_payload_t* payload = 935 | (qitem_payload_t*) enif_alloc(sizeof(qitem_payload_t)); 936 | ErlNifPid* pid = (ErlNifPid*) enif_alloc(sizeof(ErlNifPid)); 937 | 938 | if(!enif_get_local_pid(env, argv[0], pid)) 939 | { 940 | return enif_make_badarg(env); 941 | } 942 | 943 | long win; 944 | enif_get_long(env, argv[1], &win); 945 | 946 | payload->winslot = win; 947 | payload->pid = pid; 948 | queue_push(state->queue, payload); 949 | 950 | return state->atom_ok; 951 | } 952 | 953 | static void* 954 | thr_main(void* obj) 955 | { 956 | state_t* state = (state_t*) obj; 957 | ErlNifEnv* env = enif_alloc_env(); 958 | qitem_payload_t* payload; 959 | ERL_NIF_TERM msg; 960 | 961 | while((payload = queue_pop(state->queue)) != NULL) 962 | { 963 | WINDOW* win = slots[payload->winslot]; 964 | msg = enif_make_int(env, wgetch(win)); 965 | enif_send(NULL, payload->pid, env, msg); 966 | enif_free(payload->pid); 967 | enif_free(payload); 968 | enif_clear_env(env); 969 | } 970 | 971 | return NULL; 972 | } 973 | 974 | /* NIF map */ 975 | 976 | static ErlNifFunc nif_funcs[] = 977 | { 978 | {"e_refresh", 0, e_refresh}, 979 | {"e_wrefresh", 1, e_wrefresh}, 980 | 981 | {"e_newwin", 4, e_newwin}, 982 | {"e_delwin", 1, e_delwin}, 983 | {"e_endwin", 0, e_endwin}, 984 | 985 | {"e_initscr", 0, e_initscr}, 986 | 987 | {"e_cbreak", 0, e_cbreak}, 988 | {"e_nocbreak", 0, e_nocbreak}, 989 | 990 | {"e_echo", 0, e_echo}, 991 | {"e_noecho", 0, e_noecho}, 992 | 993 | {"e_erase", 0, e_erase}, 994 | {"e_werase", 1, e_werase}, 995 | 996 | {"e_addch", 1, e_addch}, 997 | {"e_waddch", 2, e_waddch}, 998 | {"e_mvaddch", 3, e_mvaddch}, 999 | {"e_mvwaddch", 4, e_mvwaddch}, 1000 | 1001 | {"e_addstr", 2, e_addstr}, 1002 | {"e_waddstr", 3, e_waddstr}, 1003 | {"e_mvaddstr", 4, e_mvaddstr}, 1004 | {"e_mvwaddstr", 5, e_mvwaddstr}, 1005 | 1006 | {"e_move", 2, e_move}, 1007 | {"e_wmove", 3, e_wmove}, 1008 | 1009 | {"e_getxy", 0, e_getxy}, 1010 | {"e_wgetxy", 1, e_wgetxy}, 1011 | {"e_getmaxxy", 0, e_getmaxxy}, 1012 | {"e_wgetmaxxy", 1, e_wgetmaxxy}, 1013 | 1014 | {"e_curs_set", 1, e_curs_set}, 1015 | 1016 | {"e_has_colors", 0, e_has_colors}, 1017 | {"e_start_color", 0, e_start_color}, 1018 | 1019 | {"e_init_pair", 3, e_init_pair}, 1020 | 1021 | {"e_attron", 1, e_attron}, 1022 | {"e_wattron", 2, e_wattron}, 1023 | {"e_attroff", 1, e_attroff}, 1024 | {"e_wattroff", 2, e_wattroff}, 1025 | 1026 | {"e_nl", 0, e_nl}, 1027 | {"e_nonl", 0, e_nonl}, 1028 | 1029 | {"e_scrollok", 2, e_scrollok}, 1030 | 1031 | {"e_hline", 2, e_hline}, 1032 | {"e_whline", 3, e_whline}, 1033 | {"e_vline", 2, e_vline}, 1034 | {"e_wvline", 3, e_wvline}, 1035 | 1036 | {"e_border", 8, e_border}, 1037 | {"e_wborder", 9, e_wborder}, 1038 | 1039 | {"e_box", 3, e_box}, 1040 | 1041 | {"e_keypad", 2, e_keypad}, 1042 | {"e_wgetch", 2, e_wgetch}, 1043 | 1044 | {"e_timeout", 1, e_timeout}, 1045 | {"e_wtimeout", 2, e_wtimeout} 1046 | }; 1047 | 1048 | ERL_NIF_INIT(encurses, nif_funcs, load, NULL, NULL, unload) 1049 | -------------------------------------------------------------------------------- /c_src/encurses.h: -------------------------------------------------------------------------------- 1 | // ============================================================================ 2 | // Encurses 0.4.2 3 | // 4 | // Copyright © 2012 Jeff Zellner . All Rights Reserved. 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 2. Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 3. The name of the author may not be used to endorse or promote products 15 | // derived from this software without specific prior written permission. 16 | // 17 | // THIS SOFTWARE IS PROVIDED BY AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | // ============================================================================ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "erl_nif.h" 34 | 35 | #define _MAXWINDOWS 256 36 | 37 | static WINDOW *slots[_MAXWINDOWS+1]; 38 | 39 | /** function prototypes **/ 40 | 41 | /* NIF management */ 42 | 43 | static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_data); 44 | static void unload(ErlNifEnv* env, void* priv); 45 | 46 | /* internal helpers */ 47 | 48 | static void *thr_main(void *arg); 49 | static int find_free_window_slot(); 50 | static ERL_NIF_TERM done(ErlNifEnv* env, int code); 51 | static ERL_NIF_TERM boolean(ErlNifEnv* env, int value); 52 | 53 | /* curses helpers */ 54 | 55 | static ERL_NIF_TERM int_erase(ErlNifEnv* env, WINDOW *win); 56 | static ERL_NIF_TERM int_getxy(ErlNifEnv* env, WINDOW *win); 57 | static ERL_NIF_TERM int_getmaxxy(ErlNifEnv* env, WINDOW *win); 58 | 59 | /* more or less curses interface */ 60 | 61 | static ERL_NIF_TERM e_refresh(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 62 | static ERL_NIF_TERM e_wrefresh(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 63 | 64 | static ERL_NIF_TERM e_newwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 65 | static ERL_NIF_TERM e_delwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 66 | static ERL_NIF_TERM e_endwin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 67 | 68 | static ERL_NIF_TERM e_initscr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 69 | 70 | static ERL_NIF_TERM e_cbreak(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 71 | static ERL_NIF_TERM e_nocbreak(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 72 | 73 | static ERL_NIF_TERM e_echo(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 74 | static ERL_NIF_TERM e_noecho(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 75 | 76 | static ERL_NIF_TERM e_erase(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 77 | static ERL_NIF_TERM e_werase(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 78 | 79 | static ERL_NIF_TERM e_addch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 80 | static ERL_NIF_TERM e_waddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 81 | static ERL_NIF_TERM e_mvaddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 82 | static ERL_NIF_TERM e_mvwaddch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 83 | 84 | static ERL_NIF_TERM e_addstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 85 | static ERL_NIF_TERM e_waddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 86 | static ERL_NIF_TERM e_mvaddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 87 | static ERL_NIF_TERM e_mvwaddstr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 88 | 89 | static ERL_NIF_TERM e_move(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 90 | static ERL_NIF_TERM e_wmove(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 91 | 92 | static ERL_NIF_TERM e_getxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 93 | static ERL_NIF_TERM e_wgetxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 94 | static ERL_NIF_TERM e_getmaxxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 95 | static ERL_NIF_TERM e_wgetmaxxy(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 96 | 97 | static ERL_NIF_TERM e_curs_set(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 98 | 99 | static ERL_NIF_TERM e_has_colors(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 100 | static ERL_NIF_TERM e_start_color(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 101 | 102 | static ERL_NIF_TERM e_init_pair(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 103 | 104 | static ERL_NIF_TERM e_attron(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 105 | static ERL_NIF_TERM e_wattron(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 106 | static ERL_NIF_TERM e_attroff(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 107 | static ERL_NIF_TERM e_wattroff(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 108 | 109 | static ERL_NIF_TERM e_nl(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 110 | static ERL_NIF_TERM e_nonl(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 111 | 112 | static ERL_NIF_TERM e_scrollok(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 113 | 114 | static ERL_NIF_TERM e_hline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 115 | static ERL_NIF_TERM e_whline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 116 | static ERL_NIF_TERM e_vline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 117 | static ERL_NIF_TERM e_wvline(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 118 | 119 | static ERL_NIF_TERM e_border(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 120 | static ERL_NIF_TERM e_wborder(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 121 | 122 | static ERL_NIF_TERM e_box(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 123 | 124 | static ERL_NIF_TERM e_keypad(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 125 | 126 | static ERL_NIF_TERM e_wgetch(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); 127 | 128 | -------------------------------------------------------------------------------- /include/encurses.hrl: -------------------------------------------------------------------------------- 1 | %%============================================================================== 2 | %% Copyright 2010 Erlang Solutions Ltd. 3 | %% 4 | %% Licensed under the Apache License, Version 2.0 (the "License"); 5 | %% you may not use this file except in compliance with the License. 6 | %% You may obtain a copy of the License at 7 | %% 8 | %% http://www.apache.org/licenses/LICENSE-2.0 9 | %% 10 | %% Unless required by applicable law or agreed to in writing, software 11 | %% distributed under the License is distributed on an "AS IS" BASIS, 12 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | %% See the License for the specific language governing permissions and 14 | %% limitations under the License. 15 | %%============================================================================== 16 | 17 | -define(TRUE, 1). 18 | -define(FALSE, 0). 19 | 20 | -define(CURS_INVISIBLE, 0). 21 | -define(CURS_NORMAL, 1). 22 | -define(CURS_VERY_VISIBLE, 2). 23 | 24 | -define(COLOR_BLACK, 0). 25 | -define(COLOR_RED, 1). 26 | -define(COLOR_GREEN, 2). 27 | -define(COLOR_YELLOW, 3). 28 | -define(COLOR_BLUE, 4). 29 | -define(COLOR_MAGENTA, 5). 30 | -define(COLOR_CYAN, 6). 31 | -define(COLOR_WHITE, 7). 32 | 33 | -define(A_NORMAL, 0). 34 | -define(COLOR_PAIR(C), (C bsl 8)). 35 | -define(A_BOLD, (1 bsl (8 + 13))). 36 | -define(A_UNDERLINE, (1 bsl (8 + 9))). 37 | -define(A_REVERSE, (1 bsl (8 + 10))). 38 | -define(A_BLINK, (1 bsl (8 + 11))). 39 | 40 | -define(STDSCR, 0). 41 | 42 | -define(ACS_DIAMOND, 4194400). 43 | -define(ACS_CKBOARD, 4194401). 44 | -define(ACS_DEGREE, 4194406). 45 | -define(ACS_PLMINUS, 4194407). 46 | -define(ACS_BOARD, 4194408). 47 | -define(ACS_LANTERN, 4194409). 48 | -define(ACS_LRCORNER, 4194410). 49 | -define(ACS_URCORNER, 4194411). 50 | -define(ACS_ULCORNER, 4194412). 51 | -define(ACS_LLCORNER, 4194413). 52 | -define(ACS_PLUS, 4194414). 53 | -define(ACS_S1, 4194415). 54 | -define(ACS_S3, 4194416). 55 | -define(ACS_HLINE, 4194417). 56 | -define(ACS_S7, 4194418). 57 | -define(ACS_S9, 4194419). 58 | -define(ACS_LTEE, 4194420). 59 | -define(ACS_RTEE, 4194421). 60 | -define(ACS_BTEE, 4194422). 61 | -define(ACS_TTEE, 4194423). 62 | -define(ACS_VLINE, 4194424). 63 | -define(ACS_LEQUAL, 4194425). 64 | -define(ACS_GEQUAL, 4194426). 65 | -define(ACS_PI, 4194427). 66 | -define(ACS_NEQUAL, 4194428). 67 | -define(ACS_STERLING, 4194429). 68 | -define(ACS_BULLET, 4194430). 69 | -define(ACS_RARROW, 4194347). 70 | -define(ACS_LARROW, 4194348). 71 | -define(ACS_UARROW, 4194349). 72 | -define(ACS_DARROW, 4194350). 73 | -define(ACS_BLOCK, 4194352). 74 | 75 | -define(KEY_TAB, 9). 76 | -define(KEY_ESC, 27). 77 | 78 | 79 | -define(KEY_DOWN, 258). 80 | -define(KEY_UP, 259). 81 | -define(KEY_LEFT, 260). 82 | -define(KEY_RIGHT, 261). 83 | -define(KEY_HOME, 262). 84 | -define(KEY_F(N), 264+N). 85 | -define(KEY_DEL, 330). 86 | -define(KEY_INS, 331). 87 | -define(KEY_PGDOWN, 338). 88 | -define(KEY_PGUP, 339). 89 | -define(KEY_END, 360). 90 | 91 | 92 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | 2 | {erl_opts, [debug_info]}. 3 | {deps, []}. 4 | 5 | {pre_hooks, 6 | [{"(linux|darwin|solaris)", compile, "make -C c_src"}, 7 | {"(freebsd)", compile, "make -C c_src"}]}. 8 | {post_hooks, 9 | [{"(linux|darwin|solaris)", clean, "make -C c_src clean"}, 10 | {"(freebsd)", clean, "make -C c_src clean"}]}. 11 | 12 | -------------------------------------------------------------------------------- /rebar.lock: -------------------------------------------------------------------------------- 1 | []. 2 | -------------------------------------------------------------------------------- /rebar3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofuture/encurses/188aed75b0f69cb71b99ea245e26ec5e808cb668/rebar3 -------------------------------------------------------------------------------- /run.escript: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env escript 2 | %%! -noinput -pa ./ebin -pa _build/default/lib/encurses/ebin +A 10 3 | -include_lib("include/encurses.hrl"). 4 | main(_) -> encurses_demo:go(). 5 | -------------------------------------------------------------------------------- /src/encurses.app.src: -------------------------------------------------------------------------------- 1 | {application, encurses, 2 | [ 3 | {description, ""}, 4 | {vsn, "0.4.2"}, 5 | {registered, []}, 6 | {applications, [ 7 | kernel, 8 | stdlib 9 | ]}, 10 | {mod, { encurses_app, []}}, 11 | {env, []} 12 | ]}. 13 | -------------------------------------------------------------------------------- /src/encurses.erl: -------------------------------------------------------------------------------- 1 | %% ============================================================================ 2 | %% Encurses 0.4.2 3 | %% 4 | %% Copyright © 2012 Jeff Zellner . All Rights Reserved. 5 | %% 6 | %% Redistribution and use in source and binary forms, with or without 7 | %% modification, are permitted provided that the following conditions are met: 8 | %% 9 | %% 1. Redistributions of source code must retain the above copyright notice, 10 | %% this list of conditions and the following disclaimer. 11 | %% 2. Redistributions in binary form must reproduce the above copyright 12 | %% notice, this list of conditions and the following disclaimer in the 13 | %% documentation and/or other materials provided with the distribution. 14 | %% 3. The name of the author may not be used to endorse or promote products 15 | %% derived from this software without specific prior written permission. 16 | %% 17 | %% THIS SOFTWARE IS PROVIDED BY AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | %% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | %% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | %% EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | %% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 | %% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 | %% OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | %% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 | %% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | %% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | %% ============================================================================ 28 | 29 | -module(encurses). 30 | 31 | -author("Jeff Zellner "). 32 | 33 | -on_load(load_nif/0). 34 | 35 | -export([ 36 | addch/1, 37 | addstr/1, 38 | attroff/1, 39 | attroff/2, 40 | attron/1, 41 | attron/2, 42 | border/8, 43 | border/9, 44 | box/3, 45 | cbreak/0, 46 | curs_set/1, 47 | delwin/1, 48 | echo/0, 49 | endwin/0, 50 | erase/0, 51 | erase/1, 52 | getch/0, 53 | getch/1, 54 | getxy/0, 55 | getxy/1, 56 | getmaxxy/0, 57 | getmaxxy/1, 58 | hline/2, 59 | hline/3, 60 | has_colors/0, 61 | init_pair/3, 62 | initscr/0, 63 | keypad/1, 64 | keypad/2, 65 | move/2, 66 | move/3, 67 | mvaddch/3, 68 | mvaddstr/3, 69 | mvwaddch/4, 70 | mvwaddstr/4, 71 | newwin/4, 72 | nl/0, 73 | nocbreak/0, 74 | noecho/0, 75 | nonl/0, 76 | refresh/0, 77 | refresh/1, 78 | scrollok/2, 79 | start_color/0, 80 | timeout/1, 81 | timeout/2, 82 | vline/2, 83 | vline/3, 84 | waddch/2, 85 | waddstr/2 86 | ]). 87 | 88 | %% ============================================================================= 89 | %% NIF Loading 90 | %% ============================================================================= 91 | 92 | load_nif() -> 93 | Dir = paths:nif_dir(), 94 | erlang:load_nif(Dir ++ "/encurses", 0). 95 | 96 | %% ============================================================================= 97 | %% Application API 98 | %% ============================================================================= 99 | 100 | %% refresh 101 | 102 | refresh() -> 103 | e_refresh(). 104 | 105 | refresh(Win) when is_integer(Win) -> 106 | e_wrefresh(Win). 107 | 108 | %% window management 109 | 110 | newwin(Width, Height, StartX, StartY) when is_integer(Width) andalso 111 | is_integer(Height) andalso is_integer(StartX) andalso 112 | is_integer(StartY) -> 113 | e_newwin(Width, Height, StartX, StartY). 114 | 115 | delwin(Win) when is_integer(Win) -> 116 | e_delwin(Win). 117 | 118 | endwin() -> 119 | e_endwin(). 120 | 121 | %% initscr 122 | 123 | initscr() -> 124 | e_initscr(). 125 | 126 | %% cbreak 127 | 128 | cbreak() -> 129 | e_cbreak(). 130 | 131 | nocbreak() -> 132 | e_nocbreak(). 133 | 134 | %% echo 135 | 136 | echo() -> 137 | e_echo(). 138 | 139 | noecho() -> 140 | e_noecho(). 141 | 142 | %% erase 143 | 144 | erase() -> 145 | e_erase(). 146 | 147 | erase(Win) when is_integer(Win) -> 148 | e_werase(Win). 149 | 150 | %% addch 151 | 152 | addch(Char) when is_integer(Char) -> 153 | e_addch(Char). 154 | 155 | waddch(Win, Char) when is_integer(Win) andalso is_integer(Char) -> 156 | e_waddch(Win, Char). 157 | 158 | mvaddch(X, Y, Char) when is_integer(X) andalso is_integer(Y) andalso 159 | is_integer(Char) -> 160 | e_mvaddch(X, Y, Char). 161 | 162 | mvwaddch(Win, X, Y, Char) when is_integer(Win) andalso is_integer(X) andalso 163 | is_integer(Y) andalso is_integer(Char) -> 164 | e_mvwaddch(Win, X, Y, Char). 165 | 166 | %% addstr 167 | 168 | addstr(String) when is_list(String) -> 169 | Str = lists:flatten(String), 170 | e_addstr(erlang:iolist_size(Str), Str). 171 | 172 | waddstr(Win, String) when is_integer(Win) andalso is_list(String) -> 173 | Str = lists:flatten(String), 174 | e_waddstr(Win, erlang:iolist_size(Str), Str). 175 | 176 | mvaddstr(X, Y, String) when is_integer(X) andalso is_integer(Y) andalso 177 | is_list(String) -> 178 | Str = lists:flatten(String), 179 | e_mvaddstr(X, Y, erlang:iolist_size(Str), Str). 180 | 181 | mvwaddstr(Win, X, Y, String) when is_integer(Win) andalso is_integer(X) andalso 182 | is_integer(Y) andalso is_list(String) -> 183 | Str = lists:flatten(String), 184 | e_mvwaddstr(Win, X, Y, erlang:iolist_size(Str), Str). 185 | 186 | %% move 187 | 188 | move(X, Y) when is_integer(X) andalso is_integer(Y) -> 189 | e_move(X, Y). 190 | 191 | move(Win, X, Y) when is_integer(Win) andalso is_integer(X) andalso 192 | is_integer(Y) -> 193 | e_wmove(Win, X, Y). 194 | 195 | %% get x and y 196 | 197 | getxy() -> 198 | e_getxy(). 199 | 200 | getxy(Win) when is_integer(Win) -> 201 | e_wgetxy(Win). 202 | 203 | getmaxxy() -> 204 | e_getmaxxy(). 205 | 206 | getmaxxy(Win) when is_integer(Win) -> 207 | e_wgetmaxxy(Win). 208 | 209 | %% curs_set 210 | 211 | curs_set(Flag) when is_integer(Flag) -> 212 | e_curs_set(Flag). 213 | 214 | %% has_colors 215 | 216 | has_colors() -> 217 | e_has_colors(). 218 | 219 | %% start_color 220 | 221 | start_color() -> 222 | e_start_color(). 223 | 224 | %% init_pair 225 | 226 | init_pair(N, FColor, BColor) when is_integer(N) andalso is_integer(FColor) 227 | andalso is_integer(BColor) -> 228 | e_init_pair(N, FColor, BColor). 229 | 230 | %% attron/attroff 231 | 232 | attron(Mask) when is_integer(Mask) -> 233 | e_attron(Mask). 234 | 235 | attron(Win, Mask) when is_integer(Win) andalso is_integer(Mask) -> 236 | e_wattron(Win, Mask). 237 | 238 | attroff(Mask) when is_integer(Mask) -> 239 | e_attroff(Mask). 240 | 241 | attroff(Win, Mask) when is_integer(Win) andalso is_integer(Mask) -> 242 | e_wattroff(Win, Mask). 243 | 244 | %% nl/nonl 245 | 246 | nl() -> 247 | e_nl(). 248 | 249 | nonl() -> 250 | e_nonl(). 251 | 252 | %% scrollok 253 | 254 | scrollok(Win, Flag) when is_integer(Win) andalso is_boolean(Flag) -> 255 | case Flag of 256 | true -> e_scrollok(Win, 1); 257 | false -> e_scrollok(Win, 0) 258 | end. 259 | 260 | %% hline/vline 261 | 262 | hline(Char, MaxN) when is_integer(Char) andalso is_integer(MaxN) -> 263 | e_hline(Char, MaxN). 264 | 265 | hline(Win, Char, MaxN) when is_integer(Win) andalso is_integer(Char) 266 | andalso is_integer(MaxN) -> 267 | e_whline(Win, Char, MaxN). 268 | 269 | vline(Char, MaxN) when is_integer(Char) andalso is_integer(MaxN) -> 270 | e_vline(Char, MaxN). 271 | 272 | vline(Win, Char, MaxN) when is_integer(Win) andalso is_integer(Char) 273 | andalso is_integer(MaxN) -> 274 | e_wvline(Win, Char, MaxN). 275 | 276 | %% border 277 | 278 | border(Ls, Rs, Ts, Bs, TLs, TRs, BLs, BRs) 279 | when is_integer(Ls) andalso is_integer(Rs) andalso 280 | is_integer(Ts) andalso is_integer(Bs) andalso is_integer(TLs) andalso 281 | is_integer(TRs) andalso is_integer(BLs) andalso is_integer(BRs) -> 282 | e_border(Ls, Rs, Ts, Bs, TLs, TRs, BLs, BRs). 283 | 284 | border(Win, Ls, Rs, Ts, Bs, TLs, TRs, BLs, BRs) 285 | when is_integer(Win) andalso is_integer(Ls) andalso 286 | is_integer(Rs) andalso is_integer(Ts) andalso is_integer(Bs) andalso 287 | is_integer(TLs) andalso is_integer(TRs) andalso is_integer(BLs) andalso 288 | is_integer(BRs) -> 289 | e_wborder(Win, Ls, Rs, Ts, Bs, TLs, TRs, BLs, BRs). 290 | 291 | %% box 292 | 293 | box(Win, Horz, Vert) when is_integer(Win) andalso is_integer(Horz) andalso 294 | is_integer(Vert) -> 295 | e_box(Win, Horz, Vert). 296 | 297 | %% keypad 298 | 299 | keypad(Flag) when is_boolean(Flag) -> 300 | keypad(0, Flag). 301 | 302 | keypad(Win, Flag) when is_integer(Win) and is_boolean(Flag) -> 303 | case Flag of 304 | true -> e_keypad(Win, 1); 305 | false -> e_keypad(Win, 0) 306 | end. 307 | 308 | %% timeout 309 | 310 | timeout(Delay) when is_integer(Delay) -> 311 | e_timeout(Delay). 312 | 313 | timeout(Win, Delay) when is_integer(Win) and is_integer(Delay) -> 314 | e_wtimeout(Win, Delay). 315 | 316 | %% getch 317 | 318 | getch() -> 319 | getch(0). 320 | 321 | getch(Win) -> 322 | e_wgetch(self(), Win), 323 | receive 324 | Char -> Char 325 | end. 326 | 327 | %% ============================================================================= 328 | %% Internal functions 329 | %% ============================================================================= 330 | 331 | e_refresh() -> 332 | erlang:nif_error(not_initialized, []). 333 | 334 | e_wrefresh(_Win) -> 335 | erlang:nif_error(not_initialized, []). 336 | 337 | e_newwin(_Width, _Height, _StartX, _StartY) -> 338 | erlang:nif_error(not_initialized, []). 339 | 340 | e_delwin(_Win) -> 341 | erlang:nif_error(not_initialized, []). 342 | 343 | e_endwin() -> 344 | erlang:nif_error(not_initialized, []). 345 | 346 | e_initscr() -> 347 | erlang:nif_error(not_initialized, []). 348 | 349 | e_cbreak() -> 350 | erlang:nif_error(not_initialized, []). 351 | 352 | e_nocbreak() -> 353 | erlang:nif_error(not_initialized, []). 354 | 355 | e_echo() -> 356 | erlang:nif_error(not_initialized, []). 357 | 358 | e_noecho() -> 359 | erlang:nif_error(not_initialized, []). 360 | 361 | e_erase() -> 362 | erlang:nif_error(not_initialized, []). 363 | 364 | e_werase(_Win) -> 365 | erlang:nif_error(not_initialized, []). 366 | 367 | e_addch(_Char) -> 368 | erlang:nif_error(not_initialized, []). 369 | 370 | e_waddch(_Win, _Char) -> 371 | erlang:nif_error(not_initialized, []). 372 | 373 | e_mvaddch(_X, _Y, _Char) -> 374 | erlang:nif_error(not_initialized, []). 375 | 376 | e_mvwaddch(_Win, _X, _Y, _Char) -> 377 | erlang:nif_error(not_initialized, []). 378 | 379 | e_addstr(_StrLen, _String) -> 380 | erlang:nif_error(not_initialized, []). 381 | 382 | e_waddstr(_Win, _StrLen, _String) -> 383 | erlang:nif_error(not_initialized, []). 384 | 385 | e_mvaddstr(_X, _Y, _StrLen, _String) -> 386 | erlang:nif_error(not_initialized, []). 387 | 388 | e_mvwaddstr(_Win, _X, _Y, _StrLen, _String) -> 389 | erlang:nif_error(not_initialized, []). 390 | 391 | e_move(_X, _Y) -> 392 | erlang:nif_error(not_initialized, []). 393 | 394 | e_wmove(_Win, _X, _Y) -> 395 | erlang:nif_error(not_initialized, []). 396 | 397 | e_getxy() -> 398 | erlang:nif_error(not_initialized, []). 399 | 400 | e_wgetxy(_Win) -> 401 | erlang:nif_error(not_initialized, []). 402 | 403 | e_getmaxxy() -> 404 | erlang:nif_error(not_initialized, []). 405 | 406 | e_wgetmaxxy(_Win) -> 407 | erlang:nif_error(not_initialized, []). 408 | 409 | e_curs_set(_Flag) -> 410 | erlang:nif_error(not_initialized, []). 411 | 412 | e_has_colors() -> 413 | erlang:nif_error(not_initialized, []). 414 | 415 | e_start_color() -> 416 | erlang:nif_error(not_initialized, []). 417 | 418 | e_init_pair(_N, _FColor, _BColor) -> 419 | erlang:nif_error(not_initialized, []). 420 | 421 | e_attron(_Mask) -> 422 | erlang:nif_error(not_initialized, []). 423 | 424 | e_wattron(_Win, _Mask) -> 425 | erlang:nif_error(not_initialized, []). 426 | 427 | e_attroff(_Mask) -> 428 | erlang:nif_error(not_initialized, []). 429 | 430 | e_wattroff(_Win, _Mask) -> 431 | erlang:nif_error(not_initialized, []). 432 | 433 | e_nl() -> 434 | erlang:nif_error(not_initialized, []). 435 | 436 | e_nonl() -> 437 | erlang:nif_error(not_initialized, []). 438 | 439 | e_scrollok(_Win, _Flag) -> 440 | erlang:nif_error(not_initialized, []). 441 | 442 | e_hline(_Char, _MaxN) -> 443 | erlang:nif_error(not_initialized, []). 444 | 445 | e_whline(_Win, _Char, _MaxN) -> 446 | erlang:nif_error(not_initialized, []). 447 | 448 | e_vline(_Char, _MaxN) -> 449 | erlang:nif_error(not_initialized, []). 450 | 451 | e_wvline(_Win, _Char, _MaxN) -> 452 | erlang:nif_error(not_initialized, []). 453 | 454 | e_border(_Ls, _Rs, _Ts, _Bs, _TLs, _TRs, _BLs, _BRs) -> 455 | erlang:nif_error(not_initialized, []). 456 | 457 | e_wborder(_Win, _Ls, _Rs, _Ts, _Bs, _TLs, _TRs, _BLs, _BRs) -> 458 | erlang:nif_error(not_initialized, []). 459 | 460 | e_box(_Win, _Horz, _Vert) -> 461 | erlang:nif_error(not_initialized, []). 462 | 463 | e_keypad(_Win, _Flag) -> 464 | erlang:nif_error(not_initialized, []). 465 | 466 | e_timeout(_Delay) -> 467 | erlang:nif_error(not_initialized, []). 468 | 469 | e_wtimeout(_Win, _Delay) -> 470 | erlang:nif_error(not_initialized, []). 471 | 472 | e_wgetch(_Pid, _Win) -> 473 | erlang:nif_error(not_initialized, []). 474 | -------------------------------------------------------------------------------- /src/encurses_demo.erl: -------------------------------------------------------------------------------- 1 | %% ============================================================================ 2 | %% Encurses 0.4.2 3 | %% 4 | %% Copyright © 2012 Jeff Zellner . All Rights Reserved. 5 | %% 6 | %% Redistribution and use in source and binary forms, with or without 7 | %% modification, are permitted provided that the following conditions are met: 8 | %% 9 | %% 1. Redistributions of source code must retain the above copyright notice, 10 | %% this list of conditions and the following disclaimer. 11 | %% 2. Redistributions in binary form must reproduce the above copyright 12 | %% notice, this list of conditions and the following disclaimer in the 13 | %% documentation and/or other materials provided with the distribution. 14 | %% 3. The name of the author may not be used to endorse or promote products 15 | %% derived from this software without specific prior written permission. 16 | %% 17 | %% THIS SOFTWARE IS PROVIDED BY AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 18 | %% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | %% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | %% EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | %% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 | %% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 | %% OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | %% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 | %% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | %% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | %% ============================================================================ 28 | 29 | -module(encurses_demo). 30 | -include("encurses.hrl"). 31 | -compile(export_all). 32 | 33 | go() -> 34 | encurses:initscr(), 35 | encurses:keypad(0, true), 36 | encurses:curs_set(?CURS_INVISIBLE), 37 | encurses:erase(), 38 | encurses:refresh(), 39 | 40 | {Mx,My} = encurses:getmaxxy(), 41 | Str = "Hello World! - encurses!", 42 | StrLen = length(Str), 43 | Timer = erlang:start_timer(10000, main, times_up), 44 | 45 | spawn(?MODULE, keyloop, []), 46 | spawn(?MODULE, other_win, []), 47 | spawn(?MODULE, bounce_text, [Str, StrLen, 0, 0, 1, 1]), 48 | spawn(?MODULE, bounce_timer, [Timer, Mx div 2, My div 2, -1, 1]), 49 | register(main, self()), 50 | receive 51 | {timeout, _Timer, times_up} -> 52 | encurses:erase(), 53 | encurses:refresh(), 54 | encurses:endwin() 55 | end. 56 | 57 | bounce_timer(Timer, PrevX, PrevY, DirX, DirY) -> 58 | Str = io_lib:format("~p", [erlang:read_timer(Timer)]), 59 | encurses:move(PrevX, PrevY), 60 | encurses:hline($\s, 4), 61 | {NewX, NewY, NewDirX, NewDirY} = calc_new_pos(4, PrevX, PrevY, DirX, DirY), 62 | encurses:mvaddstr(NewX, NewY, Str), 63 | encurses:refresh(), 64 | timer:sleep(20), 65 | bounce_timer(Timer, NewX, NewY, NewDirX, NewDirY). 66 | 67 | bounce_text(Str, StrLen, PrevX, PrevY, DirX, DirY) -> 68 | encurses:move(PrevX, PrevY), 69 | encurses:hline($\s, length(Str)), 70 | {NewX, NewY, NewDirX, NewDirY} = calc_new_pos(StrLen, PrevX, PrevY, DirX, DirY), 71 | encurses:mvaddstr(NewX, NewY, Str), 72 | encurses:refresh(), 73 | timer:sleep(100), 74 | bounce_text(Str, StrLen, NewX, NewY, NewDirX, NewDirY). 75 | 76 | calc_new_pos(Len, Px, Py, Dx, Dy) -> 77 | {Mx, My} = encurses:getmaxxy(), 78 | {NewPy, NewDy} = 79 | if 80 | (Py+(Dy) >= My) orelse (Py+(Dy) < 0) -> 81 | {Py+(Dy*-1), Dy*-1}; 82 | true -> 83 | {Py+(Dy), Dy} 84 | end, 85 | {NewPx, NewDx} = 86 | if 87 | (Px+(Dx)+Len >= Mx) orelse (Px+(Dx) < 0) -> 88 | {Px+(Dx*-1), Dx*-1}; 89 | true -> 90 | {Px+(Dx), Dx} 91 | end, 92 | {NewPx, NewPy, NewDx, NewDy}. 93 | 94 | 95 | other_win() -> 96 | Win = encurses:newwin(5,5,5,5), 97 | encurses:border(Win, $|, $|, $-, $-, $+, $+, $+, $+), 98 | encurses:mvwaddstr(Win, 1, 1, "sup"), 99 | encurses:mvwaddstr(Win, 1, 2, "sup"), 100 | encurses:mvwaddstr(Win, 1, 3, "sup"), 101 | encurses:refresh(Win), 102 | timer:sleep(100), 103 | other_win(Win). 104 | 105 | other_win(Win) -> 106 | encurses:delwin(Win), 107 | other_win(). 108 | 109 | refresh(Win) -> 110 | encurses:refresh(Win), 111 | timer:sleep(100), 112 | refresh(Win). 113 | 114 | keyloop() -> 115 | encurses:noecho(), 116 | Ch = encurses:getch(), 117 | encurses:mvaddstr(10,10," "), 118 | encurses:mvaddstr(10,10,io_lib:format("~p",[parse_direction(Ch)])), 119 | keyloop(). 120 | 121 | parse_direction(Char) -> 122 | case Char of 123 | 262 -> kp_nw; 124 | $7 -> kp_nw; 125 | 259 -> kp_n; 126 | $8 -> kp_n; 127 | 339 -> kp_ne; 128 | $9 -> kp_ne; 129 | 260 -> kp_w; 130 | $4 -> kp_w; 131 | 350 -> kp_center; 132 | $5 -> kp_center; 133 | 261 -> kp_e; 134 | $6 -> kp_e; 135 | 360 -> kp_sw; 136 | $1 -> kp_sw; 137 | 258 -> kp_s; 138 | $2 -> kp_s; 139 | 338 -> kp_se; 140 | $3 -> kp_se; 141 | $q -> main ! {timeout, bla, times_up}; 142 | Other -> Other 143 | end. 144 | -------------------------------------------------------------------------------- /src/paths.erl: -------------------------------------------------------------------------------- 1 | -module(paths). 2 | -export([nif_dir/0, nif_dir/2]). 3 | 4 | nif_dir() -> 5 | Cpd = code:priv_dir(encurses), 6 | MoCpd = filename:dirname(code:which(?MODULE)) ++ "/../priv", 7 | nif_dir(Cpd, MoCpd). 8 | 9 | 10 | nif_dir({error, _}, ModulePrivDir) -> handle_escript_format(ModulePrivDir); 11 | nif_dir(Dir, _) -> handle_escript_format(Dir). 12 | 13 | handle_escript_format(Dir) -> 14 | Handled = re:replace(Dir, 15 | "(_build/[^/]+)/bin/([^/]+)/encurses/priv", 16 | "\\1/lib/encurses/priv"), 17 | binary_to_list(iolist_to_binary(Handled)). 18 | -------------------------------------------------------------------------------- /test/nif_path_test.erl: -------------------------------------------------------------------------------- 1 | -module(nif_path_test). 2 | -include_lib("eunit/include/eunit.hrl"). 3 | 4 | 'use the module priv directory when code priv isnt known_test'() -> 5 | NifDir = paths:nif_dir({error, bad_name}, "module dir"), 6 | ?assertEqual("module dir", NifDir). 7 | 8 | 'use the code priv directory_test'() -> 9 | NifDir = paths:nif_dir("./priv", "module dir"), 10 | ?assertEqual("./priv", NifDir). 11 | 12 | 'find the lib dir when in a bin escript directory_test'() -> 13 | NifDir = paths:nif_dir("/dir/_build/default/bin/app/encurses/priv", "module dir"), 14 | ?assertEqual("/dir/_build/default/lib/encurses/priv", NifDir). 15 | 16 | 'find the lib dir when in a bin escript directory when code priv was not found_test'() -> 17 | NifDir = paths:nif_dir({error, bad_name}, "/dir/_build/default/bin/app/encurses/priv"), 18 | ?assertEqual("/dir/_build/default/lib/encurses/priv", NifDir). 19 | --------------------------------------------------------------------------------