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     }
   115 
   116   emacs_abort ();
   117 }
   118 
   119 /* Read the size of the next message into len, returning -1 if the
   120    query fails or there is no next message.  */
   121 void
   122 haiku_read_size (ssize_t *len, bool popup_menu_p)
   123 {
   124   port_id from = (popup_menu_p
   125                   ? port_popup_menu_to_emacs
   126                   : port_application_to_emacs);
   127   ssize_t size;
   128 
   129   size = port_buffer_size_etc (from, B_TIMEOUT, 0);
   130 
   131   if (size < B_OK)
   132     *len = -1;
   133   else
   134     *len = size;
   135 }
   136 
   137 /* Read the next message into BUF, putting its type into TYPE,
   138    assuming the message is at most LEN long.  Return 0 if successful
   139    and -1 if the read fails.  */
   140 int
   141 haiku_read (enum haiku_event_type *type, void *buf, ssize_t len)
   142 {
   143   int32 typ;
   144   port_id from = port_application_to_emacs;
   145 
   146   if (read_port (from, &typ, buf, len) < B_OK)
   147     return -1;
   148 
   149   *type = (enum haiku_event_type) typ;
   150   eassert (len >= haiku_len (typ));
   151   return 0;
   152 }
   153 
   154 /* The same as haiku_read, but time out after TIMEOUT microseconds.
   155    POPUP_MENU_P means to read from the popup menu port instead.
   156    Input is blocked when an attempt to read is in progress.  */
   157 int
   158 haiku_read_with_timeout (enum haiku_event_type *type, void *buf, ssize_t len,
   159                          bigtime_t timeout, bool popup_menu_p)
   160 {
   161   int32 typ;
   162   port_id from = (popup_menu_p
   163                   ? port_popup_menu_to_emacs
   164                   : port_application_to_emacs);
   165 
   166   block_input ();
   167   if (read_port_etc (from, &typ, buf, len,
   168                      B_TIMEOUT, (bigtime_t) timeout) < B_OK)
   169     {
   170       unblock_input ();
   171       return -1;
   172     }
   173   unblock_input ();
   174   *type = (enum haiku_event_type) typ;
   175   eassert (len >= haiku_len (typ));
   176   return 0;
   177 }
   178 
   179 /* Write a message with type TYPE into BUF.  */
   180 int
   181 haiku_write (enum haiku_event_type type, void *buf)
   182 {
   183   port_id to = port_application_to_emacs;
   184 
   185   if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
   186     return -1;
   187 
   188   kill (getpid (), SIGPOLL);
   189 
   190   return 0;
   191 }
   192 
   193 int
   194 haiku_write_without_signal (enum haiku_event_type type, void *buf,
   195                             bool popup_menu_p)
   196 {
   197   port_id to = (popup_menu_p
   198                 ? port_popup_menu_to_emacs
   199                 : port_application_to_emacs);
   200 
   201   if (write_port (to, (int32_t) type, buf, haiku_len (type)) < B_OK)
   202     return -1;
   203 
   204   return 0;
   205 }
   206 
   207 void
   208 haiku_io_init_in_app_thread (void)
   209 {
   210   sigset_t set;
   211   sigfillset (&set);
   212 
   213   if (pthread_sigmask (SIG_BLOCK, &set, NULL))
   214     perror ("pthread_sigmask");
   215 }

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