root/src/haiku_io.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. haiku_io_init
  2. haiku_len
  3. haiku_read_size
  4. haiku_read
  5. haiku_read_with_timeout
  6. haiku_write
  7. haiku_write_without_signal
  8. haiku_io_init_in_app_thread

     1 /* Haiku window system support.
     2    Copyright (C) 2021-2023 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 /* The port used to send popup menu messages from the application
    40    thread to Emacs.  */
    41 port_id port_popup_menu_to_emacs;
    42 
    43 /* The port used to send replies to the application after a session
    44    management event.  */
    45 port_id port_emacs_to_session_manager;
    46 
    47 void
    48 haiku_io_init (void)
    49 {
    50   port_application_to_emacs = create_port (PORT_CAP, "application emacs port");
    51   port_emacs_to_session_manager = create_port (1, "session manager port");
    52 }
    53 
    54 static ssize_t
    55 haiku_len (enum haiku_event_type type)
    56 {
    57   switch (type)
    58     {
    59     case QUIT_REQUESTED:
    60       return sizeof (struct haiku_quit_requested_event);
    61     case FRAME_RESIZED:
    62       return sizeof (struct haiku_resize_event);
    63     case FRAME_EXPOSED:
    64       return sizeof (struct haiku_expose_event);
    65     case KEY_DOWN:
    66     case KEY_UP:
    67       return sizeof (struct haiku_key_event);
    68     case ACTIVATION:
    69       return sizeof (struct haiku_activation_event);
    70     case MOUSE_MOTION:
    71       return sizeof (struct haiku_mouse_motion_event);
    72     case BUTTON_DOWN:
    73     case BUTTON_UP:
    74       return sizeof (struct haiku_button_event);
    75     case ICONIFICATION:
    76       return sizeof (struct haiku_iconification_event);
    77     case MOVE_EVENT:
    78       return sizeof (struct haiku_move_event);
    79     case SCROLL_BAR_VALUE_EVENT:
    80       return sizeof (struct haiku_scroll_bar_value_event);
    81     case SCROLL_BAR_DRAG_EVENT:
    82       return sizeof (struct haiku_scroll_bar_drag_event);
    83     case WHEEL_MOVE_EVENT:
    84       return sizeof (struct haiku_wheel_move_event);
    85     case MENU_BAR_RESIZE:
    86       return sizeof (struct haiku_menu_bar_resize_event);
    87     case MENU_BAR_CLICK:
    88       return sizeof (struct haiku_menu_bar_click_event);
    89     case MENU_BAR_OPEN:
    90     case MENU_BAR_CLOSE:
    91       return sizeof (struct haiku_menu_bar_state_event);
    92     case MENU_BAR_SELECT_EVENT:
    93       return sizeof (struct haiku_menu_bar_select_event);
    94     case MENU_BAR_HELP_EVENT:
    95       return sizeof (struct haiku_menu_bar_help_event);
    96     case ZOOM_EVENT:
    97       return sizeof (struct haiku_zoom_event);
    98     case DRAG_AND_DROP_EVENT:
    99       return sizeof (struct haiku_drag_and_drop_event);
   100     case APP_QUIT_REQUESTED_EVENT:
   101       return sizeof (struct haiku_app_quit_requested_event);
   102     case DUMMY_EVENT:
   103       return sizeof (struct haiku_dummy_event);
   104     case MENU_BAR_LEFT:
   105       return sizeof (struct haiku_menu_bar_left_event);
   106     case SCROLL_BAR_PART_EVENT:
   107       return sizeof (struct haiku_scroll_bar_part_event);
   108     case SCREEN_CHANGED_EVENT:
   109       return sizeof (struct haiku_screen_changed_event);
   110     case CLIPBOARD_CHANGED_EVENT:
   111       return sizeof (struct haiku_clipboard_changed_event);
   112     case FONT_CHANGE_EVENT:
   113       return sizeof (struct haiku_font_change_event);
   114     case NOTIFICATION_CLICK_EVENT:
   115       return sizeof (struct haiku_notification_click_event);
   116     }
   117 
   118   emacs_abort ();
   119 }
   120 
   121 /* Read the size of the next message into len, returning -1 if the
   122    query fails or there is no next message.  */
   123 void
   124 haiku_read_size (ssize_t *len, bool popup_menu_p)
   125 {
   126   port_id from = (popup_menu_p
   127                   ? port_popup_menu_to_emacs
   128                   : port_application_to_emacs);
   129   ssize_t size;
   130 
   131   size = port_buffer_size_etc (from, B_TIMEOUT, 0);
   132 
   133   if (size < B_OK)
   134     *len = -1;
   135   else
   136     *len = size;
   137 }
   138 
   139 /* Read the next message into BUF, putting its type into TYPE,
   140    assuming the message is at most LEN long.  Return 0 if successful
   141    and -1 if the read fails.  */
   142 int
   143 haiku_read (enum haiku_event_type *type, void *buf, ssize_t len)
   144 {
   145   int32 typ;
   146   port_id from = port_application_to_emacs;
   147 
   148   if (read_port (from, &typ, buf, len) < B_OK)
   149     return -1;
   150 
   151   *type = (enum haiku_event_type) typ;
   152   eassert (len >= haiku_len (typ));
   153   return 0;
   154 }
   155 
   156 /* The same as haiku_read, but time out after TIMEOUT microseconds.
   157    POPUP_MENU_P means to read from the popup menu port instead.
   158    Input is blocked when an attempt to read is in progress.  */
   159 int
   160 haiku_read_with_timeout (enum haiku_event_type *type, void *buf, ssize_t len,
   161                          bigtime_t timeout, bool popup_menu_p)
   162 {
   163   int32 typ;
   164   port_id from = (popup_menu_p
   165                   ? port_popup_menu_to_emacs
   166                   : port_application_to_emacs);
   167 
   168   block_input ();
   169   if (read_port_etc (from, &typ, buf, len,
   170                      B_TIMEOUT, (bigtime_t) timeout) < B_OK)
   171     {
   172       unblock_input ();
   173       return -1;
   174     }
   175   unblock_input ();
   176   *type = (enum haiku_event_type) typ;
   177   eassert (len >= haiku_len (typ));
   178   return 0;
   179 }
   180 
   181 /* Write a message with type TYPE into BUF.  */
   182 int
   183 haiku_write (enum haiku_event_type type, void *buf)
   184 {
   185   port_id to = port_application_to_emacs;
   186 
   187   if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
   188     return -1;
   189 
   190   kill (getpid (), SIGPOLL);
   191 
   192   return 0;
   193 }
   194 
   195 int
   196 haiku_write_without_signal (enum haiku_event_type type, void *buf,
   197                             bool popup_menu_p)
   198 {
   199   port_id to = (popup_menu_p
   200                 ? port_popup_menu_to_emacs
   201                 : port_application_to_emacs);
   202 
   203   if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
   204     return -1;
   205 
   206   return 0;
   207 }
   208 
   209 void
   210 haiku_io_init_in_app_thread (void)
   211 {
   212   sigset_t set;
   213   sigfillset (&set);
   214 
   215   if (pthread_sigmask (SIG_BLOCK, &set, NULL))
   216     perror ("pthread_sigmask");
   217 }

/* [<][>][^][v][top][bottom][index][help] */