* doc/HELPER-PROTOCOL - Add switcher_start and switcher_quit. * helper/im-switcher-gtk.c - (parse_helper_str): Call parse_helper_str_switcher() to parse "switcher_{start,quit}". - (parse_helper_str_switcher): New function. - (main): Check if another uim-im-switcher exists by trying to contact one. If one exists, this process exits. * qt4/switcher/qt4.cpp - (UimImSwitcher::UimImSwitcher): Check if another uim-im-switcher exists by trying to contact one. If one exists, this process exits. - (UimImSwitcher::slotStdinActivated): Call parseHelperStrSwitcher() to parse "switcher_{start,quit}". - (UimImSwitcher::parseHelperStrSwitcher): New function. * qt4/switcher/qt4.h - (UimImSwitcher): Add parseHelperStrSwitcher(). --- doc/HELPER-PROTOCOL | 26 +++++++++++++++++++++++++- helper/im-switcher-gtk.c | 29 ++++++++++++++++++++++++++++- qt4/switcher/qt4.cpp | 26 +++++++++++++++++++++++++- qt4/switcher/qt4.h | 2 +- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/doc/HELPER-PROTOCOL b/doc/HELPER-PROTOCOL index 2787ce6..5b3f553 100644 --- a/doc/HELPER-PROTOCOL +++ b/doc/HELPER-PROTOCOL @@ -91,7 +91,9 @@ protocol within uim. im_change_this_application_only | prop_update_custom | custom_reload_notify | - commit_string) "\n" + commit_string | + switcher_start | + switcher_quit) "\n" charset_specifier = "charset=" charset "\n" charset = "UTF-8" | "EUC-JP" | "GB18030" | @@ -324,6 +326,28 @@ protocol within uim. commit_string = "commit_string\n" charset_specifier str_to_commit "\n" str_to_commit = /^[^\n]+$/ + - switcher_start + + This message notifies that a new uim-im-switcher is started. + When an existing old uim-im-switcher receives switcher_start, + the existing uim-im-switcher must send switcher_quit + to quit newly started uim-im-switcher. + + See also switcher_quit. + + switcher_start = "switcher_start\n" + + - switcher_quit + + This message requests newly started uim-im-switcher to quit. + switcher_id is an identification number tied to uim-im-switcher + which sent this message. All uim-im-switcher which don't have this number + must quit immediately after received this message. + + See also switcher_start. + + switcher_quit = "switcher_quit=" switcher_id "\n" + switcher_id = [1-9][0-9]* Local Variables: mode: indented-text diff --git a/helper/im-switcher-gtk.c b/helper/im-switcher-gtk.c index af09ae2..cebfc68 100644 --- a/helper/im-switcher-gtk.c +++ b/helper/im-switcher-gtk.c @@ -35,11 +35,12 @@ #include +#include #include #include #include #include -#include +#include #include #include @@ -50,12 +51,15 @@ static unsigned int read_tag; static int uim_fd; /* file descriptor to connect helper message bus */ static gchar *im_list_str_old; /* To compare new im_list_str */ static GtkWidget *switcher_tree_view; +static int switcher_id = -1; static gboolean reload_im_list(GtkWindow *window, gpointer user_data); static void parse_helper_str(const char *sent_str); static void +parse_helper_str_switcher(const char *switcher_str_new); +static void parse_helper_str_im_list(const char *im_list_str_new); static void check_helper_connection(void); @@ -430,6 +434,8 @@ parse_helper_str(const char *sent_str) { if (g_str_has_prefix(sent_str, "im_list") == TRUE) { parse_helper_str_im_list(sent_str); + } else if (g_str_has_prefix(sent_str, "switcher_") == TRUE) { + parse_helper_str_switcher(sent_str); } } @@ -443,6 +449,24 @@ get_text(const char *str) } static void +parse_helper_str_switcher(const char *switcher_str_new) +{ + if (g_str_has_prefix(switcher_str_new, "switcher_start\n") == TRUE) { + GString *msg = g_string_new(""); + srand(time(0)); + switcher_id = rand(); + g_string_printf(msg, "switcher_quit=%d\n", switcher_id); + uim_helper_send_message(uim_fd, msg->str); + g_string_free(msg, TRUE); + } else if (g_str_has_prefix(switcher_str_new, "switcher_quit") == TRUE) { + gchar **lines = g_strsplit(switcher_str_new, "=", 2); + if (lines[1] != NULL && atoi(lines[1]) != switcher_id) + gtk_main_quit(); + g_strfreev(lines); + } +} + +static void parse_helper_str_im_list(const char *im_list_str_new) { gchar **lines; @@ -556,6 +580,9 @@ main(int argc, char *argv[]) uim_fd = -1; check_helper_connection(); + /* to check if another uim-im-switcher exists */ + uim_helper_send_message( uim_fd, "switcher_start\n" ); + /* To load input method list */ uim_helper_send_message(uim_fd, "im_list_get\n"); diff --git a/qt4/switcher/qt4.cpp b/qt4/switcher/qt4.cpp index f29b42a..96a10ed 100644 --- a/qt4/switcher/qt4.cpp +++ b/qt4/switcher/qt4.cpp @@ -48,14 +48,16 @@ #include #include -#include #include +#include +#include #include "qtgettext.h" static const int NAME_COLUMN = 0; static int uim_fd; +static int switcher_id = -1; static QSocketNotifier *notifier = 0; int main( int argc, char **argv ) @@ -85,6 +87,9 @@ UimImSwitcher::UimImSwitcher( QWidget *parent ) uim_fd = -1; checkHelperConnection(); + /* to check if another uim-im-switcher exists */ + uim_helper_send_message( uim_fd, "switcher_start\n" ); + /* to load input method list */ uim_helper_send_message( uim_fd, "im_list_get\n" ); @@ -244,6 +249,8 @@ void UimImSwitcher::slotStdinActivated() reloadImList(); else if ( msg.startsWith( QLatin1String( "im_list" ) ) ) parseHelperStrImList( msg ); + else if ( msg.startsWith( QLatin1String( "switcher_" ) ) ) + parseHelperStrSwitcher( msg ); } } @@ -306,3 +313,20 @@ void UimImSwitcher::reloadImList() /* send request to get im list */ uim_helper_send_message( uim_fd, "im_list_get\n" ); } + +void UimImSwitcher::parseHelperStrSwitcher( const QString &message ) +{ + if ( message.startsWith( QLatin1String( "switcher_start\n" ) ) ) + { + srand( time( 0 ) ); + switcher_id = rand(); + uim_helper_send_message( uim_fd, QString( "switcher_quit=%1\n" ) + .arg( switcher_id ).toAscii().data() ); + } + else if ( message.startsWith( QLatin1String( "switcher_quit" ) ) ) + { + const QStringList msgs = message.split( '=', QString::SkipEmptyParts ); + if ( msgs.count() > 1 && msgs[ 1 ].toInt() != switcher_id ) + QApplication::instance()->quit(); + } +} diff --git a/qt4/switcher/qt4.h b/qt4/switcher/qt4.h index 0a40703..e77f8c6 100644 --- a/qt4/switcher/qt4.h +++ b/qt4/switcher/qt4.h @@ -62,8 +62,8 @@ protected: static void helper_disconnect_cb(); void parseHelperStrImList( const QString &message ); - void reloadImList(); + void parseHelperStrSwitcher( const QString &message ); protected slots: void slotStdinActivated(); -- 1.6.4.2