Merged from the latest developing branch.
[MacVim/KaoriYa.git] / src / os_beos.c
blob10135aa3b28ab1c718070e7d1ccdae3f4637c211
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * BeBox port Copyright 1997 by Olaf Seibert.
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
11 * os_beos.c Additional stuff for BeOS (rest is in os_unix.c)
14 #include <float.h>
15 #include <termios.h>
16 #include <kernel/OS.h>
17 #include "vim.h"
19 #if USE_THREAD_FOR_INPUT_WITH_TIMEOUT
21 #ifdef PROTO /* making prototypes on Unix */
22 #define sem_id int
23 #define thread_id int
24 #endif
26 char_u charbuf;
27 signed char charcount;
28 sem_id character_present;
29 sem_id character_wanted;
30 thread_id read_thread_id;
32 #define TRY_ABORT 0 /* This code does not work so turn it off. */
34 #if TRY_ABORT
35 static void
36 mostly_ignore(int sig)
39 #endif
41 static long
42 read_thread(void *dummy)
44 signal(SIGINT, SIG_IGN);
45 signal(SIGQUIT, SIG_IGN);
46 #if TRY_ABORT
47 signal(SIGUSR1, mostly_ignore);
48 #endif
50 for (;;) {
51 if (acquire_sem(character_wanted) != B_NO_ERROR)
52 break;
53 charcount = read(read_cmd_fd, &charbuf, 1);
54 release_sem(character_present);
57 return 0;
60 void
61 beos_cleanup_read_thread(void)
63 if (character_present > 0)
64 delete_sem(character_present);
65 character_present = 0;
66 if (read_thread_id > 0)
67 kill_thread(read_thread_id);
68 read_thread_id = 0;
71 #endif
74 * select() emulation. Hopefully, in DR9 there will be something
75 * useful supplied by the system. ... Alas, not. Not in AAPR, nor
76 * in PR or even PR2... R3 then maybe? I don't think so!
79 int
80 beos_select(int nbits,
81 struct fd_set *rbits,
82 struct fd_set *wbits,
83 struct fd_set *ebits,
84 struct timeval *timeout)
86 bigtime_t tmo;
88 if (nbits == 0) {
89 /* select is purely being used for delay */
90 snooze(timeout->tv_sec * 1e6 + timeout->tv_usec);
91 return 0;
93 #if 0
95 * This does not seem to work either. Reads here are not supposed to
96 * block indefinitely, yet they do. This is most annoying.
98 if (FD_ISSET(0, rbits)) {
99 char cbuf[1];
100 int count;
101 struct termios told;
102 struct termios tnew;
103 tcgetattr(0, &told);
104 tnew = told;
105 tnew.c_lflag &= ~ICANON;
106 tnew.c_cc[VMIN] = 0;
107 tnew.c_cc[VTIME] = timeout->tv_sec * 10 + timeout->tv_usec / 100000;
108 tcsetattr(0, TCSANOW, &tnew);
110 count = read(0, &cbuf, sizeof(cbuf));
111 tcsetattr(0, TCSANOW, &told);
112 if (count > 0) {
113 add_to_input_buf(&cbuf[0], count);
114 return 1;
116 return 0;
118 #endif
119 #if USE_THREAD_FOR_INPUT_WITH_TIMEOUT
121 * Check if the operation is really on stdin...
123 if (FD_ISSET(read_cmd_fd, rbits))
125 int acquired;
128 * Is this the first time through?
129 * Then start up the thread and initialise the semaphores.
131 if (character_present == 0) {
132 character_present = create_sem(0, "vim character_present");
133 character_wanted = create_sem(1, "vim character_wanted");
134 read_thread_id = spawn_thread(read_thread, "vim async read",
135 B_NORMAL_PRIORITY, NULL);
136 atexit(beos_cleanup_read_thread);
137 resume_thread(read_thread_id);
140 /* timeout == NULL means "indefinitely" */
141 if (timeout) {
142 tmo = timeout->tv_sec * 1e6 + timeout->tv_usec;
143 /* 0 means "don't wait, which is impossible to do exactly. */
144 if (tmo == 0)
145 tmo = 1.0;
147 #if TRY_ABORT
148 release_sem(character_wanted);
149 #endif
150 if (timeout)
151 acquired = acquire_sem_etc(character_present, 1, B_TIMEOUT, tmo);
152 else
153 acquired = acquire_sem(character_present);
154 if (acquired == B_NO_ERROR) {
155 if (charcount > 0) {
156 add_to_input_buf(&charbuf, 1);
157 #if !TRY_ABORT
158 release_sem(character_wanted);
159 #endif
161 return 1;
162 } else {
163 #if !TRY_ABORT
164 release_sem(character_wanted);
165 #endif
167 return 0;
170 #if TRY_ABORT
171 else {
173 * Timeout occurred. Break the read() call by sending
174 * a signal. Problem: it may be just read()ing it now.
175 * Therefore we still have to finish the handshake with
176 * the thread and maybe remember the character.
178 kill(read_thread_id, SIGUSR1);
180 * If some other error occurred, don't hang now.
181 * (We will most likely hang later anyway...)
183 if (acquired == B_TIMED_OUT)
184 acquire_sem(character_present);
185 if (charcount > 0) {
186 add_to_input_buf(&charbuf, 1);
187 return 1;
189 return 0;
191 #endif
193 #endif
195 return 0;