annotate src/os_beos.c @ 0:76efa0be13f1

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