main.vala 3.4 KB
Newer Older
1 2 3 4
/* vim:set et sts=4 sw=4:
 *
 * ibus - The Input Bus
 *
5 6
 * Copyright (c) 2011-2013 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (c) 2015 Takao Fujiwara <takao.fujiwara1@gmail.com>
7 8 9 10
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12 13 14
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
17 18
 *
 * You should have received a copy of the GNU Lesser General Public
19 20 21
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
22 23 24 25 26 27 28 29
 */

using IBus;

class DummyEngine : IBus.EngineSimple {
}

public int main(string[] args) {
30
    Intl.setlocale(LocaleCategory.ALL, "");
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    IBus.init();

    IBus.Bus bus = new IBus.Bus();
    if (!bus.is_connected()) {
        warning("ibus-daemon does not exist.");
        return 1;
    }

    bus.disconnected.connect((bus) => {
        debug("bus disconnected");
        IBus.quit();
    });

    IBus.Factory factory = new IBus.Factory(bus.get_connection());
    
    int id = 0;

    factory.create_engine.connect((factory, name) => {
49 50 51
        if (!name.has_prefix("xkb:"))
            return null;

52 53 54 55 56 57
        const string path = "/org/freedesktop/IBus/engine/simple/%d";

        IBus.Engine engine = new IBus.Engine.with_type(
            typeof(IBus.EngineSimple), name,
            path.printf(++id), bus.get_connection());

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        /* Use Idle.add() to reduce the lag caused by file io */
        GLib.Idle.add(() => {
            /* I think "c" + "'" is c with acute U+0107 and
             * "c" + "," is c with cedilla U+00E7 and they are
             * visually comprehensible. But pt-br people need
             * "c" + "'" is c with cedilla and I think the
             * cedilla_compose_seqs is needed for the specific keyboards
             * or regions.
             * X11 uses compose by locale:
             * In /usr/share/X11/locale/en_US.UTF-8/Compose ,
             * <Multi_key> <apostrophe> <c> : U0107
             */
            IBus.EngineSimple? simple = (IBus.EngineSimple ?) engine; 
            simple.add_table_by_locale(null);

            string user_file = null;

            var home = GLib.Environment.get_home_dir();
            if (home != null) {
                user_file = home + "/.XCompose";
                if (GLib.FileUtils.test(user_file, GLib.FileTest.EXISTS))
                    simple.add_compose_file(user_file);
            }

            user_file = GLib.Environment.get_variable("XCOMPOSEFILE");
            if (user_file != null) {
                if (GLib.FileUtils.test(user_file, GLib.FileTest.EXISTS))
                    simple.add_compose_file(user_file);
            }

            return false;
        });
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

        return engine;
    });

    uint flags = 
        IBus.BusNameFlag.REPLACE_EXISTING |
        IBus.BusNameFlag.ALLOW_REPLACEMENT;
    uint retval = bus.request_name("org.freedesktop.IBus.Simple", flags);

    if (retval == 0) {
        warning("Registry bus name org.freedesktop.IBus.Simple failed!");
        return 1;
    }

    IBus.main();

    return 0;
}