1 /* Haiku window system support.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20
21 #include <signal.h>
22 #include <stdio.h>
23 #include <pthread.h>
24 #include <unistd.h>
25
26 #include <OS.h>
27
28 #include "haiku_support.h"
29 #include "lisp.h"
30 #include "haikuterm.h"
31 #include "blockinput.h"
32
33 #define PORT_CAP 1200
34
35 /* The port used to send messages from the application thread to
36 Emacs. */
37 port_id port_application_to_emacs;
38
39 void
haiku_io_init(void)40 haiku_io_init (void)
41 {
42 port_application_to_emacs = create_port (PORT_CAP, "application emacs port");
43 }
44
45 static ssize_t
haiku_len(enum haiku_event_type type)46 haiku_len (enum haiku_event_type type)
47 {
48 switch (type)
49 {
50 case QUIT_REQUESTED:
51 return sizeof (struct haiku_quit_requested_event);
52 case FRAME_RESIZED:
53 return sizeof (struct haiku_resize_event);
54 case FRAME_EXPOSED:
55 return sizeof (struct haiku_expose_event);
56 case KEY_DOWN:
57 case KEY_UP:
58 return sizeof (struct haiku_key_event);
59 case ACTIVATION:
60 return sizeof (struct haiku_activation_event);
61 case MOUSE_MOTION:
62 return sizeof (struct haiku_mouse_motion_event);
63 case BUTTON_DOWN:
64 case BUTTON_UP:
65 return sizeof (struct haiku_button_event);
66 case ICONIFICATION:
67 return sizeof (struct haiku_iconification_event);
68 case MOVE_EVENT:
69 return sizeof (struct haiku_move_event);
70 case SCROLL_BAR_VALUE_EVENT:
71 return sizeof (struct haiku_scroll_bar_value_event);
72 case SCROLL_BAR_DRAG_EVENT:
73 return sizeof (struct haiku_scroll_bar_drag_event);
74 case WHEEL_MOVE_EVENT:
75 return sizeof (struct haiku_wheel_move_event);
76 case MENU_BAR_RESIZE:
77 return sizeof (struct haiku_menu_bar_resize_event);
78 case MENU_BAR_OPEN:
79 case MENU_BAR_CLOSE:
80 return sizeof (struct haiku_menu_bar_state_event);
81 case MENU_BAR_SELECT_EVENT:
82 return sizeof (struct haiku_menu_bar_select_event);
83 case FILE_PANEL_EVENT:
84 return sizeof (struct haiku_file_panel_event);
85 case MENU_BAR_HELP_EVENT:
86 return sizeof (struct haiku_menu_bar_help_event);
87 case ZOOM_EVENT:
88 return sizeof (struct haiku_zoom_event);
89 case REFS_EVENT:
90 return sizeof (struct haiku_refs_event);
91 case APP_QUIT_REQUESTED_EVENT:
92 return sizeof (struct haiku_app_quit_requested_event);
93 }
94
95 emacs_abort ();
96 }
97
98 /* Read the size of the next message into len, returning -1 if the
99 query fails or there is no next message. */
100 void
haiku_read_size(ssize_t * len)101 haiku_read_size (ssize_t *len)
102 {
103 port_id from = port_application_to_emacs;
104 ssize_t size;
105
106 size = port_buffer_size_etc (from, B_TIMEOUT, 0);
107
108 if (size < B_OK)
109 *len = -1;
110 else
111 *len = size;
112 }
113
114 /* Read the next message into BUF, putting its type into TYPE,
115 assuming the message is at most LEN long. Return 0 if successful
116 and -1 if the read fails. */
117 int
haiku_read(enum haiku_event_type * type,void * buf,ssize_t len)118 haiku_read (enum haiku_event_type *type, void *buf, ssize_t len)
119 {
120 int32 typ;
121 port_id from = port_application_to_emacs;
122
123 if (read_port (from, &typ, buf, len) < B_OK)
124 return -1;
125
126 *type = (enum haiku_event_type) typ;
127 eassert (len >= haiku_len (typ));
128 return 0;
129 }
130
131 /* The same as haiku_read, but time out after TIMEOUT microseconds.
132 Input is blocked when an attempt to read is in progress. */
133 int
haiku_read_with_timeout(enum haiku_event_type * type,void * buf,ssize_t len,time_t timeout)134 haiku_read_with_timeout (enum haiku_event_type *type, void *buf, ssize_t len,
135 time_t timeout)
136 {
137 int32 typ;
138 port_id from = port_application_to_emacs;
139
140 block_input ();
141 if (read_port_etc (from, &typ, buf, len,
142 B_TIMEOUT, (bigtime_t) timeout) < B_OK)
143 {
144 unblock_input ();
145 return -1;
146 }
147 unblock_input ();
148 *type = (enum haiku_event_type) typ;
149 eassert (len >= haiku_len (typ));
150 return 0;
151 }
152
153 /* Write a message with type TYPE into BUF. */
154 int
haiku_write(enum haiku_event_type type,void * buf)155 haiku_write (enum haiku_event_type type, void *buf)
156 {
157 port_id to = port_application_to_emacs;
158
159 if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
160 return -1;
161
162 kill (getpid (), SIGPOLL);
163
164 return 0;
165 }
166
167 int
haiku_write_without_signal(enum haiku_event_type type,void * buf)168 haiku_write_without_signal (enum haiku_event_type type, void *buf)
169 {
170 port_id to = port_application_to_emacs;
171
172 if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
173 return -1;
174
175 return 0;
176 }
177
178 void
haiku_io_init_in_app_thread(void)179 haiku_io_init_in_app_thread (void)
180 {
181 sigset_t set;
182 sigfillset (&set);
183
184 if (pthread_sigmask (SIG_BLOCK, &set, NULL))
185 perror ("pthread_sigmask");
186 }
187
188 /* Record an unwind protect from C++ code. */
189 void
record_c_unwind_protect_from_cxx(void (* fn)(void *),void * r)190 record_c_unwind_protect_from_cxx (void (*fn) (void *), void *r)
191 {
192 record_unwind_protect_ptr (fn, r);
193 }
194
195 /* SPECPDL_IDX that is safe from C++ code. */
196 ptrdiff_t
c_specpdl_idx_from_cxx(void)197 c_specpdl_idx_from_cxx (void)
198 {
199 return SPECPDL_INDEX ();
200 }
201
202 /* unbind_to (IDX, Qnil), but safe from C++ code. */
203 void
c_unbind_to_nil_from_cxx(ptrdiff_t idx)204 c_unbind_to_nil_from_cxx (ptrdiff_t idx)
205 {
206 unbind_to (idx, Qnil);
207 }
208