emacs/java/org/gnu/emacs/EmacsContextMenu.java
Po Lu a158c1d5b9 Update Android port
* configure.ac (HAVE_TEXT_CONVERSION): Define on Android.
* doc/emacs/input.texi (On-Screen Keyboards): Document ``text
conversion'' slightly.
* doc/lispref/commands.texi (Misc Events): Document new
`text-conversion' event.
* java/org/gnu/emacs/EmacsContextMenu.java (display): Use
`syncRunnable'.
* java/org/gnu/emacs/EmacsDialog.java (display): Likewise.
* java/org/gnu/emacs/EmacsEditable.java: Delete file.
* java/org/gnu/emacs/EmacsInputConnection.java
(EmacsInputConnection): Reimplement from scratch.
* java/org/gnu/emacs/EmacsNative.java (EmacsNative): Add new
functions.
* java/org/gnu/emacs/EmacsService.java (EmacsService, getEmacsView)
(getLocationOnScreen, sync, getClipboardManager, restartEmacs):
Use syncRunnable.
(syncRunnable): New function.
(updateIC, resetIC): New functions.

* java/org/gnu/emacs/EmacsView.java (EmacsView): New field
`inputConnection' and `icMode'.
(onCreateInputConnection): Update accordingly.
(setICMode, getICMode): New functions.

* lisp/bindings.el (global-map): Ignore text conversion events.
* src/alloc.c (mark_frame): Mark text conversion data.
* src/android.c (struct android_emacs_service): New fields
`update_ic' and `reset_ic'.
(event_serial): Export.
(android_query_sem): New function.
(android_init_events): Initialize new semaphore.
(android_write_event): Export.
(android_select): Check for UI thread code.
(setEmacsParams, android_init_emacs_service): Initialize new
methods.
(android_check_query, android_begin_query, android_end_query)
(android_run_in_emacs_thread):
(android_update_ic, android_reset_ic): New functions for
managing synchronous queries from one thread to another.

* src/android.h: Export new functions.
* src/androidgui.h (enum android_event_type): Add input method
events.
(enum android_ime_operation, struct android_ime_event)
(union android_event, enum android_ic_mode): New structs and
enums.

* src/androidterm.c (android_window_to_frame): Allow DPYINFO to
be NULL.
(android_decode_utf16, android_handle_ime_event)
(handle_one_android_event, android_sync_edit)
(android_copy_java_string, beginBatchEdit, endBatchEdit)
(commitCompletion, deleteSurroundingText, finishComposingText)
(getSelectedtext, getTextAfterCursor, getTextBeforeCursor)
(setComposingText, setComposingRegion, setSelection, getSelection)
(performEditorAction, getExtractedText): New functions.
(struct android_conversion_query_context):
(android_perform_conversion_query):
(android_text_to_string):
(struct android_get_selection_context):
(android_get_selection):
(struct android_get_extracted_text_context):
(android_get_extracted_text):
(struct android_extracted_text_request_class):
(struct android_extracted_text_class):
(android_update_selection):
(android_reset_conversion):
(android_set_point):
(android_compose_region_changed):
(android_notify_conversion):
(text_conversion_interface): New functions and structures.
(android_term_init): Initialize text conversion.

* src/coding.c (syms_of_coding): Define Qutf_16le on Android.
* src/frame.c (make_frame): Clear conversion data.
(delete_frame): Reset conversion state.

* src/frame.h (enum text_conversion_operation)
(struct text_conversion_action, struct text_conversion_state)
(GCALIGNED_STRUCT): Update structures.
* src/keyboard.c (read_char, readable_events, kbd_buffer_get_event)
(syms_of_keyboard): Handle text conversion events.
* src/lisp.h:
* src/process.c: Fix includes.

* src/textconv.c (enum textconv_batch_edit_flags, textconv_query)
(reset_frame_state, detect_conversion_events)
(restore_selected_window, really_commit_text)
(really_finish_composing_text, really_set_composing_text)
(really_set_composing_region, really_delete_surrounding_text)
(really_set_point, complete_edit)
(handle_pending_conversion_events_1)
(handle_pending_conversion_events, start_batch_edit)
(end_batch_edit, commit_text, finish_composing_text)
(set_composing_text, set_composing_region, textconv_set_point)
(delete_surrounding_text, get_extracted_text)
(report_selected_window_change, report_point_change)
(register_texconv_interface): New functions.

* src/textconv.h (struct textconv_interface)
(TEXTCONV_SKIP_CONVERSION_REGION): Update prototype.
* src/xdisp.c (mark_window_display_accurate_1):
* src/xfns.c (xic_string_conversion_callback):
* src/xterm.c (init_xterm): Adjust accordingly.
2023-02-15 12:23:03 +08:00

304 lines
7.4 KiB
Java

/* Communication module for Android terminals. -*- c-file-style: "GNU" -*-
Copyright (C) 2023 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
package org.gnu.emacs;
import java.util.List;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.SubMenu;
import android.util.Log;
import android.widget.PopupMenu;
/* Context menu implementation. This object is built from JNI and
describes a menu hiearchy. Then, `inflate' can turn it into an
Android menu, which can be turned into a popup (or other kind of)
menu. */
public class EmacsContextMenu
{
private static final String TAG = "EmacsContextMenu";
/* Whether or not an item was selected. */
public static boolean itemAlreadySelected;
/* Whether or not a submenu was selected. */
public static boolean wasSubmenuSelected;
private class Item implements MenuItem.OnMenuItemClickListener
{
public int itemID;
public String itemName;
public EmacsContextMenu subMenu;
public boolean isEnabled, isCheckable, isChecked;
@Override
public boolean
onMenuItemClick (MenuItem item)
{
Log.d (TAG, "onMenuItemClick: " + itemName + " (" + itemID + ")");
if (subMenu != null)
{
/* After opening a submenu within a submenu, Android will
send onContextMenuClosed for a ContextMenuBuilder. This
will normally confuse Emacs into thinking that the
context menu has been dismissed. Wrong!
Setting this flag makes EmacsActivity to only handle
SubMenuBuilder being closed, which always means the menu
has actually been dismissed. */
wasSubmenuSelected = true;
return false;
}
/* Send a context menu event. */
EmacsNative.sendContextMenu ((short) 0, itemID);
/* Say that an item has already been selected. */
itemAlreadySelected = true;
return true;
}
};
public List<Item> menuItems;
public String title;
private EmacsContextMenu parent;
/* Create a context menu with no items inside and the title TITLE,
which may be NULL. */
public static EmacsContextMenu
createContextMenu (String title)
{
EmacsContextMenu menu;
menu = new EmacsContextMenu ();
menu.menuItems = new ArrayList<Item> ();
menu.title = title;
return menu;
}
/* Add a normal menu item to the context menu with the id ITEMID and
the name ITEMNAME. Enable it if ISENABLED, else keep it
disabled.
If this is not a submenu and ISCHECKABLE is set, make the item
checkable. Likewise, if ISCHECKED is set, make the item
checked. */
public void
addItem (int itemID, String itemName, boolean isEnabled,
boolean isCheckable, boolean isChecked)
{
Item item;
item = new Item ();
item.itemID = itemID;
item.itemName = itemName;
item.isEnabled = isEnabled;
item.isCheckable = isCheckable;
item.isChecked = isChecked;
menuItems.add (item);
}
/* Create a disabled menu item with the name ITEMNAME. */
public void
addPane (String itemName)
{
Item item;
item = new Item ();
item.itemName = itemName;
menuItems.add (item);
}
/* Add a submenu to the context menu with the specified title and
item name. */
public EmacsContextMenu
addSubmenu (String itemName, String title)
{
EmacsContextMenu submenu;
Item item;
item = new Item ();
item.itemID = 0;
item.itemName = itemName;
item.subMenu = createContextMenu (title);
item.subMenu.parent = this;
menuItems.add (item);
return item.subMenu;
}
/* Add the contents of this menu to MENU. */
private void
inflateMenuItems (Menu menu)
{
Intent intent;
MenuItem menuItem;
SubMenu submenu;
for (Item item : menuItems)
{
if (item.subMenu != null)
{
try
{
/* This is a submenu. On versions of Android which
support doing so, create the submenu and add the
contents of the menu to it. */
submenu = menu.addSubMenu (item.itemName);
item.subMenu.inflateMenuItems (submenu);
}
catch (UnsupportedOperationException exception)
{
/* This version of Android has a restriction
preventing submenus from being added to submenus.
Inflate everything into the parent menu
instead. */
item.subMenu.inflateMenuItems (menu);
continue;
}
/* This is still needed to set wasSubmenuSelected. */
menuItem = submenu.getItem ();
menuItem.setOnMenuItemClickListener (item);
}
else
{
menuItem = menu.add (item.itemName);
menuItem.setOnMenuItemClickListener (item);
/* If the item ID is zero, then disable the item. */
if (item.itemID == 0 || !item.isEnabled)
menuItem.setEnabled (false);
/* Now make the menu item display a checkmark as
appropriate. */
if (item.isCheckable)
menuItem.setCheckable (true);
if (item.isChecked)
menuItem.setChecked (true);
}
}
}
/* Enter the items in this context menu to MENU. Create each menu
item with an Intent containing a Bundle, where the key
"emacs:menu_item_hi" maps to the high 16 bits of the
corresponding item ID, and the key "emacs:menu_item_low" maps to
the low 16 bits of the item ID. */
public void
expandTo (Menu menu)
{
inflateMenuItems (menu);
}
/* Return the parent or NULL. */
public EmacsContextMenu
parent ()
{
return this.parent;
}
/* Like display, but does the actual work and runs in the main
thread. */
private boolean
display1 (EmacsWindow window, int xPosition, int yPosition)
{
/* Set this flag to false. It is used to decide whether or not to
send 0 in response to the context menu being closed. */
itemAlreadySelected = false;
/* No submenu has been selected yet. */
wasSubmenuSelected = false;
return window.view.popupMenu (this, xPosition, yPosition);
}
/* Display this context menu on WINDOW, at xPosition and
yPosition. */
public boolean
display (final EmacsWindow window, final int xPosition,
final int yPosition)
{
Runnable runnable;
final Holder<Boolean> rc;
rc = new Holder<Boolean> ();
runnable = new Runnable () {
@Override
public void
run ()
{
synchronized (this)
{
rc.thing = display1 (window, xPosition, yPosition);
notify ();
}
}
};
EmacsService.syncRunnable (runnable);
return rc.thing;
}
/* Dismiss this context menu. WINDOW is the window where the
context menu is being displayed. */
public void
dismiss (final EmacsWindow window)
{
Runnable runnable;
EmacsService.SERVICE.runOnUiThread (new Runnable () {
@Override
public void
run ()
{
window.view.cancelPopupMenu ();
itemAlreadySelected = false;
}
});
}
};