Skip to content

Commit

Permalink
Add tb_poll_fds() to obtain file descriptors for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyp committed Sep 29, 2018
1 parent d63b83a commit f8b97a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/python/termboxmodule.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This is a binding module for termbox library.
"""

from libc.stdlib cimport malloc, free

include "termboxconfig.pyx"

## No unichr in python3, use chr instead.
Expand Down Expand Up @@ -36,6 +38,7 @@ cdef extern from "../termbox.h":
int tb_select_output_mode(int mode)
int tb_peek_event(tb_event *event, int timeout) nogil
int tb_poll_event(tb_event *event) nogil
int tb_poll_fds(int *fds, int fds_size)

class TermboxException(Exception):
def __init__(self, msg):
Expand Down Expand Up @@ -284,3 +287,16 @@ cdef class Termbox:
else:
uch = None
return (e.type, uch, e.key, e.mod, e.w, e.h, e.x, e.y)

def poll_fds(self):
"""Returns a list of file descriptors that can be polled to see when an event has arrived
"""
cdef int fds_count;
cdef int *fds;
try:
fds_count = tb_poll_fds(NULL, 0)
fds = <int *> malloc(fds_count * sizeof(int))
tb_poll_fds(fds, fds_count)
return [fd for fd in fds[:fds_count]]
finally:
free(fds)
13 changes: 13 additions & 0 deletions src/termbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ int tb_peek_event(struct tb_event *event, int timeout)
return wait_fill_event(event, &tv);
}

int tb_poll_fds(int *fds, int fds_size)
{
if (fds_size) {
*fds++ = inout;
fds_size--;
}
if (fds_size--) {
*fds++ = winch_fds[0];
fds_size--;
}
return 2;
}

int tb_width(void)
{
return termw;
Expand Down
8 changes: 8 additions & 0 deletions src/termbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ SO_IMPORT int tb_peek_event(struct tb_event *event, int timeout);
*/
SO_IMPORT int tb_poll_event(struct tb_event *event);


/* Writes a list of (up to fds_size) file descriptors into the memory at fds,
* which can be watched for events. This can be used to integrate termbox into
* fd-based event loops. Returns the number of relevant file descriptors, which
* may be more or less than fds_size.
*/
SO_IMPORT int tb_poll_fds(int *fds, int fds_size);

/* Utility utf8 functions. */
#define TB_EOF -1
SO_IMPORT int tb_utf8_char_length(char c);
Expand Down

0 comments on commit f8b97a6

Please sign in to comment.