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 Aug 23, 2020
1 parent 2858e9b commit 9f72c80
Show file tree
Hide file tree
Showing 3 changed files with 39 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 size_t 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);
}

size_t tb_poll_fds(int *fds, size_t fds_size)
{
if (fds != NULL) {
if (fds_size >= 1) {
fds[0] = inout;
}
if (fds_size >= 2) {
fds[1] = winch_fds[0];
}
}
return 2;
}

int tb_width(void)
{
return termw;
Expand Down
10 changes: 10 additions & 0 deletions src/termbox.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

/* for shared objects */
#if __GNUC__ >= 4
Expand Down Expand Up @@ -309,6 +310,15 @@ SO_IMPORT int tb_peek_event(struct tb_event *event, int timeout);
*/
SO_IMPORT int tb_poll_event(struct tb_event *event);


/* Obtains a list of internal file descriptors which can be watched for events.
* This can be used to integrate termbox into fd-based event loops.
* If fds is not NULL, up to fds_size file descriptors are stored there.
* Returns the number of available file descriptors, which
* may be more or less than fds_size.
*/
SO_IMPORT size_t tb_poll_fds(int *fds, size_t fds_size);

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

0 comments on commit 9f72c80

Please sign in to comment.