Skip to content

Instantly share code, notes, and snippets.

@pztrick
Created October 27, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pztrick/028ecf0df7ad68d0173d2ec7bd6b84d8 to your computer and use it in GitHub Desktop.
Save pztrick/028ecf0df7ad68d0173d2ec7bd6b84d8 to your computer and use it in GitHub Desktop.
uim-switch-im
/*
Copyright (c) 2004-2016 uim Project https://github.com/uim/uim
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of authors nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <gtk/gtk.h>
// #include <locale.h>
// #include <string.h>
// #include <stdlib.h>
// #include <stdio.h>
#include <errno.h>
#include <uim/uim.h>
#include <uim/uim-helper.h>
#include <uim/uim-custom.h>
// #include <uim/uim-scm.h>
// #include "gettext.h"
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 void parse_helper_str(const char *sent_str);
static void parse_helper_str_im_list(const char *im_list_str_new);
static void check_helper_connection(void);
static void
send_message_im_change(const gchar *type, gchar *im_name)
{
GString *msg = g_string_new(type);
// gchar *im_name = "direct";
// if (im_name == NULL) {
// g_string_free(msg, TRUE);
// return; /* Or should pop-up alert window here? */
// }
check_helper_connection(); /* ensuring connected to message bus */
g_string_append(msg, im_name);
g_string_append(msg, "\n");
// g_free(im_name);
uim_helper_send_message(uim_fd, msg->str);
g_string_free(msg, TRUE);
}
static void parse_arg(int argc, char *argv[])
{
/* Doing nothing yet. */
}
static void parse_helper_str(const char *sent_str)
{
if (g_str_has_prefix(sent_str, "im_list") == TRUE) {
parse_helper_str_im_list(sent_str);
}
}
static void parse_helper_str_im_list(const char *im_list_str_new)
{
gchar **lines;
int i = 2;
gchar **info;
printf("inside parse");
if (im_list_str_old && strcmp(im_list_str_new, im_list_str_old) == 0) {
return; /* No need to update */
}
im_list_str_old = g_strdup(im_list_str_new);
}
static void helper_disconnect_cb(void)
{
uim_fd = -1;
g_source_remove(read_tag);
}
static gboolean fd_read_cb(GIOChannel *channel, GIOCondition c, gpointer p)
{
char *tmp;
int fd = g_io_channel_unix_get_fd(channel);
uim_helper_read_proc(fd);
while ((tmp = uim_helper_get_message())) {
parse_helper_str(tmp);
g_free(tmp);
tmp = NULL;
}
return TRUE;
}
static void check_helper_connection(void)
{
if (uim_fd < 0) {
uim_fd = uim_helper_init_client_fd(helper_disconnect_cb);
if (uim_fd > 0) {
GIOChannel *channel;
channel = g_io_channel_unix_new(uim_fd);
read_tag = g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, fd_read_cb, NULL);
g_io_channel_unref(channel);
}
}
}
int main(int argc, char *argv[])
{
parse_arg(argc, argv);
if(!argv[1]){
fprintf(stderr, "Invalid argument.\n");
}else{
fprintf(stdout, "Using argument: %s\n", argv[1]);
}
gchar *im_name = argv[1];
/* connect to uim helper message bus */
uim_fd = -1;
check_helper_connection();
/* To check if another uim-im-switcher exists */
uim_helper_send_message(uim_fd, "im_switcher_start\n");
/* To load input method list */
uim_helper_send_message(uim_fd, "im_list_get\n");
// py pyunihan pinyin-big5 anthy anthy-utf8 byeoru
send_message_im_change("im_change_whole_desktop\n", im_name);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment