summaryrefslogtreecommitdiffstats
path: root/src/os_beos.c
blob: ebb448829b18440483a778b6a4dc6cb7fed2e556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* vi:set ts=8 sts=4 sw=4 noet:
 *
 * VIM - Vi IMproved	by Bram Moolenaar
 *		 BeBox port Copyright 1997 by Olaf Seibert.
 *
 * Do ":help uganda"  in Vim to read copying and usage conditions.
 * Do ":help credits" in Vim to see a list of people who contributed.
 * See README.txt for an overview of the Vim source code.
 */
/*
 * os_beos.c  Additional stuff for BeOS (rest is in os_unix.c)
 */

#include <float.h>
#include <termios.h>
#ifndef PROTO
# include <kernel/OS.h>
#endif

#include "vim.h"

#if USE_THREAD_FOR_INPUT_WITH_TIMEOUT

#ifdef PROTO	    /* making prototypes on Unix */
#define sem_id int
#define thread_id int
#endif

char_u charbuf;
signed char charcount;
sem_id character_present;
sem_id character_wanted;
thread_id read_thread_id;

#define TRY_ABORT	0	/* This code does not work so turn it off. */

#if TRY_ABORT
    static void
mostly_ignore(int sig)
{
}
#endif

    static long
read_thread(void *dummy)
{
    signal(SIGINT, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
#if TRY_ABORT
    signal(SIGUSR1, mostly_ignore);
#endif

    for (;;) {
	if (acquire_sem(character_wanted) != B_NO_ERROR)
	    break;
	charcount = read(read_cmd_fd, &charbuf, 1);
	release_sem(character_present);
    }

    return 0;
}

    void
beos_cleanup_read_thread(void)
{
    if (character_present > 0)
	delete_sem(character_present);
    character_present = 0;
    if (read_thread_id > 0)
	kill_thread(read_thread_id);
    read_thread_id = 0;
}

#endif

/*
 * select() emulation. Hopefully, in DR9 there will be something
 * useful supplied by the system. ... Alas, not. Not in AAPR, nor
 * in PR or even PR2... R3 then maybe? I don't think so!
 */

    int
beos_select(int nbits,
       struct fd_set *rbits,
       struct fd_set *wbits,
       struct fd_set *ebits,
       struct timeval *timeout)
{
    bigtime_t tmo;

    if (nbits == 0) {
	/* select is purely being used for delay */
	snooze(timeout->tv_sec * 1e6 + timeout->tv_usec);
	return 0;
    }
#if 0
    /*
     * This does not seem to work either. Reads here are not supposed to
     * block indefinitely, yet they do. This is most annoying.
     */
    if (FD_ISSET(0, rbits)) {
	char cbuf[1];
	int count;
	struct termios told;
	struct termios tnew;
	tcgetattr(0, &told);
	tnew = told;
	tnew.c_lflag &= ~ICANON;
	tnew.c_cc[VMIN] = 0;
	tnew.c_cc[VTIME] = timeout->tv_sec * 10 + timeout->tv_usec / 100000;
	tcsetattr(0, TCSANOW, &tnew);

	count = read(0, &cbuf, sizeof(cbuf));
	tcsetattr(0, TCSANOW, &told);
	if (count > 0) {
	    add_to_input_buf(&cbuf[0], count);
	    return 1;
	}
	return 0;
    }
#endif
#if USE_THREAD_FOR_INPUT_WITH_TIMEOUT
    /*
     * Check if the operation is really on stdin...
     */
    if (FD_ISSET(read_cmd_fd, rbits))
    {
	int acquired;

	/*
	 * Is this the first time through?
	 * Then start up the thread and initialise the semaphores.
	 */
	if (character_present == 0) {
	    character_present = create_sem(0, "vim character_present");
	    character_wanted = create_sem(1, "vim character_wanted");
	    read_thread_id = spawn_thread(read_thread, "vim async read",
		    B_NORMAL_PRIORITY, NULL);
	    atexit(beos_cleanup_read_thread);
	    resume_thread(read_thread_id);
	}

	/* timeout == NULL means "indefinitely" */
	if (timeout) {
	    tmo = timeout->tv_sec * 1e6 + timeout->tv_usec;
	    /* 0 means "don't wait, which is impossible to do exactly. */
	    if (tmo == 0)
		tmo = 1.0;
	}
#if TRY_ABORT
	release_sem(character_wanted);
#endif
	if (timeout)
	    acquired = acquire_sem_etc(character_present, 1, B_TIMEOUT, tmo);
	else
	    acquired = acquire_sem(character_present);
	if (acquired == B_NO_ERROR) {
	    if (charcount > 0) {
		add_to_input_buf(&charbuf, 1);
#if !TRY_ABORT
		release_sem(character_wanted);
#endif

		return 1;
	    } else {
#if !TRY_ABORT
		release_sem(character_wanted);
#endif

		return 0;
	    }
	}
#if TRY_ABORT
	else {
	    /*
	     * Timeout occurred. Break the read() call by sending
	     * a signal. Problem: it may be just read()ing it now.
	     * Therefore we still have to finish the handshake with
	     * the thread and maybe remember the character.
	     */
	    kill(read_thread_id, SIGUSR1);
	    /*
	     *	If some other error occurred, don't hang now.
	     * (We will most likely hang later anyway...)
	     */
	    if (acquired == B_TIMED_OUT)
		acquire_sem(character_present);
	    if (charcount > 0) {
		add_to_input_buf(&charbuf, 1);
		return 1;
	    }
	    return 0;
	}
#endif
    }
#endif

    return 0;
}