github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/android/game-activity/include/game-text-input/gametextinput.h (about)

     1  /*
     2   * Copyright (C) 2021 The Android Open Source Project
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  /**
    18   * @defgroup game_text_input Game Text Input
    19   * The interface to use GameTextInput.
    20   * @{
    21   */
    22  
    23  #pragma once
    24  
    25  #include <android/rect.h>
    26  #include <jni.h>
    27  #include <stdint.h>
    28  
    29  #include "gamecommon.h"
    30  
    31  #ifdef __cplusplus
    32  extern "C" {
    33  #endif
    34  
    35  /**
    36   * This struct holds a span within a region of text from start (inclusive) to
    37   * end (exclusive). An empty span or cursor position is specified with
    38   * start==end. An undefined span is specified with start = end = SPAN_UNDEFINED.
    39   */
    40  typedef struct GameTextInputSpan {
    41      /** The start of the region (inclusive). */
    42      int32_t start;
    43      /** The end of the region (exclusive). */
    44      int32_t end;
    45  } GameTextInputSpan;
    46  
    47  /**
    48   * Values with special meaning in a GameTextInputSpan.
    49   */
    50  enum GameTextInputSpanFlag { SPAN_UNDEFINED = -1 };
    51  
    52  /**
    53   * This struct holds the state of an editable section of text.
    54   * The text can have a selection and a composing region defined on it.
    55   * A composing region is used by IMEs that allow input using multiple steps to
    56   * compose a glyph or word. Use functions GameTextInput_getState and
    57   * GameTextInput_setState to read and modify the state that an IME is editing.
    58   */
    59  typedef struct GameTextInputState {
    60      /**
    61       * Text owned by the state, as a modified UTF-8 string. Null-terminated.
    62       * https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
    63       */
    64      const char *text_UTF8;
    65      /**
    66       * Length in bytes of text_UTF8, *not* including the null at end.
    67       */
    68      int32_t text_length;
    69      /**
    70       * A selection defined on the text.
    71       */
    72      GameTextInputSpan selection;
    73      /**
    74       * A composing region defined on the text.
    75       */
    76      GameTextInputSpan composingRegion;
    77  } GameTextInputState;
    78  
    79  /**
    80   * A callback called by GameTextInput_getState.
    81   * @param context User-defined context.
    82   * @param state State, owned by the library, that will be valid for the duration
    83   * of the callback.
    84   */
    85  typedef void (*GameTextInputGetStateCallback)(
    86      void *context, const struct GameTextInputState *state);
    87  
    88  /**
    89   * Opaque handle to the GameTextInput API.
    90   */
    91  typedef struct GameTextInput GameTextInput;
    92  
    93  /**
    94   * Initialize the GameTextInput library.
    95   * If called twice without GameTextInput_destroy being called, the same pointer
    96   * will be returned and a warning will be issued.
    97   * @param env A JNI env valid on the calling thread.
    98   * @param max_string_size The maximum length of a string that can be edited. If
    99   * zero, the maximum defaults to 65536 bytes. A buffer of this size is allocated
   100   * at initialization.
   101   * @return A handle to the library.
   102   */
   103  GameTextInput *GameTextInput_init(JNIEnv *env, uint32_t max_string_size);
   104  
   105  /**
   106   * When using GameTextInput, you need to create a gametextinput.InputConnection
   107   * on the Java side and pass it using this function to the library, unless using
   108   * GameActivity in which case this will be done for you. See the GameActivity
   109   * source code or GameTextInput samples for examples of usage.
   110   * @param input A valid GameTextInput library handle.
   111   * @param inputConnection A gametextinput.InputConnection object.
   112   */
   113  void GameTextInput_setInputConnection(GameTextInput *input,
   114                                        jobject inputConnection);
   115  
   116  /**
   117   * Unless using GameActivity, it is required to call this function from your
   118   * Java gametextinput.Listener.stateChanged method to convert eventState and
   119   * trigger any event callbacks. When using GameActivity, this does not need to
   120   * be called as event processing is handled by the Activity.
   121   * @param input A valid GameTextInput library handle.
   122   * @param eventState A Java gametextinput.State object.
   123   */
   124  void GameTextInput_processEvent(GameTextInput *input, jobject eventState);
   125  
   126  /**
   127   * Free any resources owned by the GameTextInput library.
   128   * Any subsequent calls to the library will fail until GameTextInput_init is
   129   * called again.
   130   * @param input A valid GameTextInput library handle.
   131   */
   132  void GameTextInput_destroy(GameTextInput *input);
   133  
   134  /**
   135   * Flags to be passed to GameTextInput_showIme.
   136   */
   137  enum ShowImeFlags {
   138      SHOW_IME_UNDEFINED = 0,  // Default value.
   139      SHOW_IMPLICIT =
   140          1,  // Indicates that the user has forced the input method open so it
   141              // should not be closed until they explicitly do so.
   142      SHOW_FORCED = 2  // Indicates that this is an implicit request to show the
   143                       // input window, not as the result of a direct request by
   144                       // the user. The window may not be shown in this case.
   145  };
   146  
   147  /**
   148   * Show the IME. Calls InputMethodManager.showSoftInput().
   149   * @param input A valid GameTextInput library handle.
   150   * @param flags Defined in ShowImeFlags above. For more information see:
   151   * https://developer.android.com/reference/android/view/inputmethod/InputMethodManager
   152   */
   153  void GameTextInput_showIme(GameTextInput *input, uint32_t flags);
   154  
   155  /**
   156   * Flags to be passed to GameTextInput_hideIme.
   157   */
   158  enum HideImeFlags {
   159      HIDE_IME_UNDEFINED = 0,  // Default value.
   160      HIDE_IMPLICIT_ONLY =
   161          1,  // Indicates that the soft input window should only be hidden if it
   162              // was not explicitly shown by the user.
   163      HIDE_NOT_ALWAYS =
   164          2,  // Indicates that the soft input window should normally be hidden,
   165              // unless it was originally shown with SHOW_FORCED.
   166  };
   167  
   168  /**
   169   * Show the IME. Calls InputMethodManager.hideSoftInputFromWindow().
   170   * @param input A valid GameTextInput library handle.
   171   * @param flags Defined in HideImeFlags above. For more information see:
   172   * https://developer.android.com/reference/android/view/inputmethod/InputMethodManager
   173   */
   174  void GameTextInput_hideIme(GameTextInput *input, uint32_t flags);
   175  
   176  /**
   177   * Call a callback with the current GameTextInput state, which may have been
   178   * modified by changes in the IME and calls to GameTextInput_setState. We use a
   179   * callback rather than returning the state in order to simplify ownership of
   180   * text_UTF8 strings. These strings are only valid during the calling of the
   181   * callback.
   182   * @param input A valid GameTextInput library handle.
   183   * @param callback A function that will be called with valid state.
   184   * @param context Context used by the callback.
   185   */
   186  void GameTextInput_getState(GameTextInput *input,
   187                              GameTextInputGetStateCallback callback,
   188                              void *context);
   189  
   190  /**
   191   * Set the current GameTextInput state. This state is reflected to any active
   192   * IME.
   193   * @param input A valid GameTextInput library handle.
   194   * @param state The state to set. Ownership is maintained by the caller and must
   195   * remain valid for the duration of the call.
   196   */
   197  void GameTextInput_setState(GameTextInput *input,
   198                              const GameTextInputState *state);
   199  
   200  /**
   201   * Type of the callback needed by GameTextInput_setEventCallback that will be
   202   * called every time the IME state changes.
   203   * @param context User-defined context set in GameTextInput_setEventCallback.
   204   * @param current_state Current IME state, owned by the library and valid during
   205   * the callback.
   206   */
   207  typedef void (*GameTextInputEventCallback)(
   208      void *context, const GameTextInputState *current_state);
   209  
   210  /**
   211   * Optionally set a callback to be called whenever the IME state changes.
   212   * Not necessary if you are using GameActivity, which handles these callbacks
   213   * for you.
   214   * @param input A valid GameTextInput library handle.
   215   * @param callback Called by the library when the IME state changes.
   216   * @param context Context passed as first argument to the callback.
   217   */
   218  void GameTextInput_setEventCallback(GameTextInput *input,
   219                                      GameTextInputEventCallback callback,
   220                                      void *context);
   221  
   222  /**
   223   * Type of the callback needed by GameTextInput_setImeInsetsCallback that will
   224   * be called every time the IME window insets change.
   225   * @param context User-defined context set in
   226   * GameTextInput_setImeWIndowInsetsCallback.
   227   * @param current_insets Current IME insets, owned by the library and valid
   228   * during the callback.
   229   */
   230  typedef void (*GameTextInputImeInsetsCallback)(void *context,
   231                                                 const ARect *current_insets);
   232  
   233  /**
   234   * Optionally set a callback to be called whenever the IME insets change.
   235   * Not necessary if you are using GameActivity, which handles these callbacks
   236   * for you.
   237   * @param input A valid GameTextInput library handle.
   238   * @param callback Called by the library when the IME insets change.
   239   * @param context Context passed as first argument to the callback.
   240   */
   241  void GameTextInput_setImeInsetsCallback(GameTextInput *input,
   242                                          GameTextInputImeInsetsCallback callback,
   243                                          void *context);
   244  
   245  /**
   246   * Get the current window insets for the IME.
   247   * @param input A valid GameTextInput library handle.
   248   * @param insets Filled with the current insets by this function.
   249   */
   250  void GameTextInput_getImeInsets(const GameTextInput *input, ARect *insets);
   251  
   252  /**
   253   * Unless using GameActivity, it is required to call this function from your
   254   * Java gametextinput.Listener.onImeInsetsChanged method to
   255   * trigger any event callbacks. When using GameActivity, this does not need to
   256   * be called as insets processing is handled by the Activity.
   257   * @param input A valid GameTextInput library handle.
   258   * @param eventState A Java gametextinput.State object.
   259   */
   260  void GameTextInput_processImeInsets(GameTextInput *input, const ARect *insets);
   261  
   262  /**
   263   * Convert a GameTextInputState struct to a Java gametextinput.State object.
   264   * Don't forget to delete the returned Java local ref when you're done.
   265   * @param input A valid GameTextInput library handle.
   266   * @param state Input state to convert.
   267   * @return A Java object of class gametextinput.State. The caller is required to
   268   * delete this local reference.
   269   */
   270  jobject GameTextInputState_toJava(const GameTextInput *input,
   271                                    const GameTextInputState *state);
   272  
   273  /**
   274   * Convert from a Java gametextinput.State object into a C GameTextInputState
   275   * struct.
   276   * @param input A valid GameTextInput library handle.
   277   * @param state A Java gametextinput.State object.
   278   * @param callback A function called with the C struct, valid for the duration
   279   * of the call.
   280   * @param context Context passed to the callback.
   281   */
   282  void GameTextInputState_fromJava(const GameTextInput *input, jobject state,
   283                                   GameTextInputGetStateCallback callback,
   284                                   void *context);
   285  
   286  #ifdef __cplusplus
   287  }
   288  #endif
   289  
   290  /** @} */