OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <base/logging.h> |
| 6 #include <base/scoped_ptr.h> |
| 7 #include <dlfcn.h> |
| 8 #include <glib-object.h> |
| 9 |
| 10 #include <iostream> // NOLINT |
| 11 |
| 12 #include "chromeos_cros_api.h" // NOLINT |
| 13 #include "chromeos_language.h" |
| 14 #include "monitor_utils.h" //NOLINT |
| 15 |
| 16 // \file This is a simple console application which checks whether the cros |
| 17 // library (chromeos_language.cc) can monitor IME/XKB status changes or not. |
| 18 |
| 19 // How to use this tool: |
| 20 // 1. Set up your IBus daemon using ibus-setup command. Add at least one IME. |
| 21 // 2. Start the candidate_window for ChromeOS. |
| 22 // 3. Start this tool ***in your gnome-terminal***. You need to have X desktop. |
| 23 // 4. Verify that all IME languages and XKB layouts are displayed. |
| 24 // 5. Focus another X application, then focus back the gnome-terminal in order |
| 25 // to make candidate_window send "FocusIn" signal to this process. Please |
| 26 // note that Callback::Run() is called upon "FocusIn" and "StateChanged" |
| 27 // signals from candidate_window. |
| 28 // 6. Verify that this tool automatically exits in a second or so. |
| 29 |
| 30 namespace { |
| 31 |
| 32 const size_t kTestCount = 5; |
| 33 chromeos::LanguageStatusConnection* global_connection = NULL; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 // Callback is an example object which can be passed to MonitorLanguageStatus. |
| 38 class Callback { |
| 39 public: |
| 40 // You can store whatever state is needed in the function object. |
| 41 explicit Callback(GMainLoop* loop) |
| 42 : count_(0), loop_(loop) { |
| 43 } |
| 44 |
| 45 static void Run(void* object, const chromeos::InputLanguage& language) { |
| 46 Callback* self = static_cast<Callback*>(object); |
| 47 |
| 48 ++self->count_; |
| 49 if (self->count_ == kTestCount) { |
| 50 std::cout << "*** Done ***" << std::endl; |
| 51 ::g_main_loop_quit(self->loop_); |
| 52 } else { |
| 53 // Change the current IME engine or XKB layout by calling |
| 54 // ChromeOSChangeLanguage() function in libcros. |
| 55 // |
| 56 // 1. Calls the ChromeOSChangeLanguage() function. |
| 57 // 2. The function calls "SetEngine" method (if language->category is |
| 58 // IME) or "Disable" method (if the category is XKB) in ibus-daemon. |
| 59 // 3. ibus-daemon changes its state and calls "StateChanged" method in |
| 60 // candidate_window. |
| 61 // 4. candidate_window sends "StateChanged" signal to this process. |
| 62 // 5. Since |self| is registered as a monitor function, libcros calls |
| 63 // Callback::Run() function again. |
| 64 // |
| 65 // As a result, "SetEngine" method and "Disable" method in ibus-daemon |
| 66 // are called in turn rapidly. |
| 67 if (language.category == chromeos::LANGUAGE_CATEGORY_XKB) { |
| 68 // This triggers the Run() function to be called again |
| 69 // (see the comment above). |
| 70 chromeos::ChangeLanguage(global_connection, |
| 71 chromeos::LANGUAGE_CATEGORY_IME, |
| 72 self->ime_id().c_str()); |
| 73 } else { |
| 74 // Ditto. |
| 75 chromeos::ChangeLanguage(global_connection, |
| 76 chromeos::LANGUAGE_CATEGORY_XKB, |
| 77 self->xkb_id().c_str()); |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 std::string xkb_id() const { |
| 83 return xkb_id_; |
| 84 } |
| 85 void set_xkb_id(const std::string& id) { |
| 86 xkb_id_ = id; |
| 87 } |
| 88 std::string ime_id() const { |
| 89 return ime_id_; |
| 90 } |
| 91 void set_ime_id(const std::string& id) { |
| 92 ime_id_ = id; |
| 93 } |
| 94 |
| 95 private: |
| 96 int count_; |
| 97 GMainLoop* loop_; |
| 98 std::string xkb_id_; |
| 99 std::string ime_id_; |
| 100 }; |
| 101 |
| 102 int main(int argc, const char** argv) { |
| 103 // Initialize the g_type systems an g_main event loop, normally this would be |
| 104 // done by chrome. |
| 105 ::g_type_init(); |
| 106 GMainLoop* loop = ::g_main_loop_new(NULL, false); |
| 107 DCHECK(LoadCrosLibrary(argv)) << "Failed to load cros.so"; |
| 108 |
| 109 Callback callback(loop); |
| 110 global_connection |
| 111 = chromeos::MonitorLanguageStatus(&Callback::Run, &callback); |
| 112 DCHECK(global_connection) << "MonitorLanguageStatus() failed. " |
| 113 << "candidate_window is not running?"; |
| 114 |
| 115 const scoped_ptr<chromeos::InputLanguageList> engines( |
| 116 chromeos::GetLanguages(global_connection)); |
| 117 DCHECK(engines.get()) << "GetLanguages() failed"; |
| 118 |
| 119 std::cout << "Available IMEs and XKB layouts:" << std::endl; |
| 120 for (size_t i = 0; i < engines->size(); ++i) { |
| 121 const chromeos::InputLanguage &engine = engines->at(i); |
| 122 std::cout << "* " << engine.display_name << std::endl; |
| 123 // Remember (at least) one XKB id and one IME id. |
| 124 if (engine.category == chromeos::LANGUAGE_CATEGORY_XKB) { |
| 125 callback.set_xkb_id(engine.id); |
| 126 } else { |
| 127 callback.set_ime_id(engine.id); |
| 128 } |
| 129 } |
| 130 |
| 131 ::g_main_loop_run(loop); |
| 132 chromeos::DisconnectLanguageStatus(global_connection); |
| 133 ::g_main_loop_unref(loop); |
| 134 |
| 135 return 0; |
| 136 } |
OLD | NEW |