| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <assert.h> | 5 #include <assert.h> |
| 6 #include <glib.h> | 6 #include <glib.h> |
| 7 #include <ibus.h> | 7 #include <ibus.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const gchar kDummySection[] = "aaa/bbb"; | 14 const gchar kDummySection[] = "aaa/bbb"; |
| 15 const gchar kDummyConfigName[] = "ccc"; | 15 const gchar kDummyConfigName[] = "ccc"; |
| 16 | 16 |
| 17 const gboolean kDummyValueBoolean = TRUE; | 17 const gboolean kDummyValueBoolean = TRUE; |
| 18 const gint kDummyValueInt = 12345; | 18 const gint kDummyValueInt = 12345; |
| 19 const gdouble kDummyValueDouble = 2345.5432; | 19 const gdouble kDummyValueDouble = 2345.5432; |
| 20 const gchar kDummyValueString[] = "dummy value"; | 20 const gchar kDummyValueString[] = "dummy value"; |
| 21 | 21 |
| 22 const size_t kArraySize = 3; | 22 const size_t kArraySize = 3; |
| 23 const gboolean kDummyValueBooleanArray[kArraySize] = { FALSE, TRUE, FALSE }; | 23 const gboolean kDummyValueBooleanArray[kArraySize] = { FALSE, TRUE, FALSE }; |
| 24 const gint kDummyValueIntArray[kArraySize] = { 123, 234, 345 }; | 24 const gint kDummyValueIntArray[kArraySize] = { 123, 234, 345 }; |
| 25 const gdouble kDummyValueDoubleArray[kArraySize] = { 111.22, 333.44, 555.66 }; | 25 const gdouble kDummyValueDoubleArray[kArraySize] = { 111.22, 333.44, 555.66 }; |
| 26 const gchar* kDummyValueStringArray[kArraySize] = { | 26 const gchar* kDummyValueStringArray[kArraySize] = { |
| 27 "DUMMY_VALUE 1", "DUMMY_VALUE 2", "DUMMY_VALUE 3", | 27 "DUMMY_VALUE 1", "DUMMY_VALUE 2", "DUMMY_VALUE 3", |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 const char kGeneralSectionName[] = "general"; |
| 31 const char kPreloadEnginesConfigName[] = "preload_engines"; |
| 32 |
| 30 // Converts |list_type_string| into its element type (e.g. "int_list" to "int"). | 33 // Converts |list_type_string| into its element type (e.g. "int_list" to "int"). |
| 31 std::string GetElementType(const std::string& list_type_string) { | 34 std::string GetElementType(const std::string& list_type_string) { |
| 32 const std::string suffix = "_list"; | 35 const std::string suffix = "_list"; |
| 33 if (list_type_string.length() > suffix.length()) { | 36 if (list_type_string.length() > suffix.length()) { |
| 34 return list_type_string.substr( | 37 return list_type_string.substr( |
| 35 0, list_type_string.length() - suffix.length()); | 38 0, list_type_string.length() - suffix.length()); |
| 36 } | 39 } |
| 37 return list_type_string; | 40 return list_type_string; |
| 38 } | 41 } |
| 39 | 42 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 g_value_set_int(&tmp, kDummyValueIntArray[i]); | 100 g_value_set_int(&tmp, kDummyValueIntArray[i]); |
| 98 } else if (element_gtype == G_TYPE_DOUBLE) { | 101 } else if (element_gtype == G_TYPE_DOUBLE) { |
| 99 g_value_set_double(&tmp, kDummyValueDoubleArray[i]); | 102 g_value_set_double(&tmp, kDummyValueDoubleArray[i]); |
| 100 } else if (element_gtype == G_TYPE_STRING) { | 103 } else if (element_gtype == G_TYPE_STRING) { |
| 101 g_value_set_string(&tmp, kDummyValueStringArray[i]); | 104 g_value_set_string(&tmp, kDummyValueStringArray[i]); |
| 102 } | 105 } |
| 103 g_value_array_append(array, &tmp); | 106 g_value_array_append(array, &tmp); |
| 104 } | 107 } |
| 105 | 108 |
| 106 g_value_take_boxed(&gvalue, array); | 109 g_value_take_boxed(&gvalue, array); |
| 107 } | 110 } |
| 108 | 111 |
| 109 if (ibus_config_set_value( | 112 if (ibus_config_set_value( |
| 110 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { | 113 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
| 111 printf("OK\n"); | 114 printf("OK\n"); |
| 112 } else { | 115 } else { |
| 113 printf("FAIL\n"); | 116 printf("FAIL\n"); |
| 114 } | 117 } |
| 115 } | 118 } |
| 116 | 119 |
| 117 // Gets a dummy value from ibus config service. This function checks if the | 120 // Gets a dummy value from ibus config service. This function checks if the |
| 118 // dummy value is |type_string| type. | 121 // dummy value is |type_string| type. |
| 119 void GetConfigAndPrintResult( | 122 void GetConfigAndPrintResult( |
| 120 IBusConfig* ibus_config, const std::string& type_string) { | 123 IBusConfig* ibus_config, const std::string& type_string) { |
| 121 GValue gvalue = {0}; | 124 GValue gvalue = {0}; |
| 122 if (!ibus_config_get_value( | 125 if (!ibus_config_get_value( |
| 123 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { | 126 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
| 124 printf("FAIL (not found)\n"); | 127 printf("FAIL (not found)\n"); |
| 125 return; | 128 return; |
| 126 } | 129 } |
| 127 | 130 |
| 128 const GType gtype = GetGValueTypeFromStringOrDie(type_string); | 131 const GType gtype = GetGValueTypeFromStringOrDie(type_string); |
| 129 if (G_VALUE_TYPE(&gvalue) != gtype) { | 132 if (G_VALUE_TYPE(&gvalue) != gtype) { |
| 130 printf("FAIL (type mismatch)\n"); | 133 printf("FAIL (type mismatch)\n"); |
| 131 return; | 134 return; |
| 132 } | 135 } |
| 133 | 136 |
| 134 if (gtype== G_TYPE_BOOLEAN) { | 137 if (gtype== G_TYPE_BOOLEAN) { |
| 135 if (g_value_get_boolean(&gvalue) != kDummyValueBoolean) { | 138 if (g_value_get_boolean(&gvalue) != kDummyValueBoolean) { |
| 136 printf("FAIL (value mismatch)\n"); | 139 printf("FAIL (value mismatch)\n"); |
| 137 return; | 140 return; |
| 138 } | 141 } |
| 139 } else if (gtype == G_TYPE_INT) { | 142 } else if (gtype == G_TYPE_INT) { |
| 140 if (g_value_get_int(&gvalue) != kDummyValueInt) { | 143 if (g_value_get_int(&gvalue) != kDummyValueInt) { |
| 141 printf("FAIL (value mismatch)\n"); | 144 printf("FAIL (value mismatch)\n"); |
| 142 return; | 145 return; |
| 143 } | 146 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 if (!match) { | 196 if (!match) { |
| 194 printf("FAIL (value mismatch)\n"); | 197 printf("FAIL (value mismatch)\n"); |
| 195 return; | 198 return; |
| 196 } | 199 } |
| 197 } | 200 } |
| 198 } | 201 } |
| 199 | 202 |
| 200 printf("OK\n"); | 203 printf("OK\n"); |
| 201 } | 204 } |
| 202 | 205 |
| 206 // Prints out the array held in gvalue. It is assumed that the array contains |
| 207 // G_TYPE_STRING values. |
| 208 // On success, returns true |
| 209 // On failure, prints out "FAIL (error message)" and returns false |
| 210 bool PrintArray(GValue* gvalue) { |
| 211 GValueArray* array = |
| 212 reinterpret_cast<GValueArray*>(g_value_get_boxed(gvalue)); |
| 213 for (guint i = 0; array && (i < array->n_values); ++i) { |
| 214 const GType element_type = G_VALUE_TYPE(&(array->values[i])); |
| 215 if (element_type != G_TYPE_STRING) { |
| 216 printf("FAIL (Array element type is not STRING)\n"); |
| 217 return false; |
| 218 } |
| 219 const char* value = g_value_get_string(&(array->values[i])); |
| 220 if (!value) { |
| 221 printf("FAIL (Array element type is NULL)\n"); |
| 222 return false; |
| 223 } |
| 224 printf("%s\n", value); |
| 225 } |
| 226 return true; |
| 227 } |
| 228 |
| 229 // Print out the list of unused config variables from ibus. |
| 230 // On failure, prints out "FAIL (error message)" instead. |
| 231 void PrintUnused(IBusConfig* ibus_config) { |
| 232 GValue unread = {0}; |
| 233 GValue unwritten = {0}; |
| 234 if (!ibus_config_get_unused(ibus_config, &unread, &unwritten)) { |
| 235 printf("FAIL (get_unused failed)\n"); |
| 236 return; |
| 237 } |
| 238 |
| 239 if (G_VALUE_TYPE(&unread) != G_TYPE_VALUE_ARRAY) { |
| 240 printf("FAIL (unread is not an array)\n"); |
| 241 return; |
| 242 } |
| 243 |
| 244 if (G_VALUE_TYPE(&unwritten) != G_TYPE_VALUE_ARRAY) { |
| 245 printf("FAIL (unwritten is not an array)\n"); |
| 246 return; |
| 247 } |
| 248 |
| 249 printf("Unread:\n"); |
| 250 if (!PrintArray(&unread)) { |
| 251 g_value_unset(&unread); |
| 252 g_value_unset(&unwritten); |
| 253 return; |
| 254 } |
| 255 |
| 256 printf("Unwritten:\n"); |
| 257 if (!PrintArray(&unwritten)) { |
| 258 g_value_unset(&unread); |
| 259 g_value_unset(&unwritten); |
| 260 return; |
| 261 } |
| 262 |
| 263 g_value_unset(&unread); |
| 264 g_value_unset(&unwritten); |
| 265 return; |
| 266 } |
| 267 |
| 268 // Set the preload engines to those named in the array |engines| of size |
| 269 // |num_engines| and prints the result. |
| 270 // |
| 271 // Note that this only fails if it can't set the config value; it does not check |
| 272 // that the names of the engines are valid. |
| 273 void PreloadEnginesAndPrintResult(IBusConfig* ibus_config, int num_engines, |
| 274 char** engines) { |
| 275 GValue gvalue = {0}; |
| 276 g_value_init(&gvalue, G_TYPE_VALUE_ARRAY); |
| 277 GValueArray* array = g_value_array_new(num_engines); |
| 278 for (int i = 0; i < num_engines; ++i) { |
| 279 GValue array_element = {0}; |
| 280 g_value_init(&array_element, G_TYPE_STRING); |
| 281 g_value_set_string(&array_element, engines[i]); |
| 282 g_value_array_append(array, &array_element); |
| 283 } |
| 284 g_value_take_boxed(&gvalue, array); |
| 285 |
| 286 if (ibus_config_set_value(ibus_config, kGeneralSectionName, |
| 287 kPreloadEnginesConfigName, &gvalue)) { |
| 288 printf("OK\n"); |
| 289 } else { |
| 290 printf("FAIL\n"); |
| 291 } |
| 292 |
| 293 g_value_unset(&gvalue); |
| 294 } |
| 295 |
| 296 // Sets |engine_name| as the active IME engine. |
| 297 void ActivateEngineAndPrintResult(IBusBus* ibus, const char* engine_name) { |
| 298 if (!ibus_bus_set_global_engine(ibus, engine_name)) { |
| 299 printf("FAIL (could not start engine)\n"); |
| 300 } else { |
| 301 printf("OK\n"); |
| 302 } |
| 303 } |
| 304 |
| 203 // Prints the names of the given engines. Takes the ownership of |engines|. | 305 // Prints the names of the given engines. Takes the ownership of |engines|. |
| 204 void PrintEngineNames(GList* engines) { | 306 void PrintEngineNames(GList* engines) { |
| 205 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { | 307 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { |
| 206 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); | 308 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); |
| 207 assert(engine_desc); | 309 assert(engine_desc); |
| 208 printf("%s\n", engine_desc->name); | 310 printf("%s\n", engine_desc->name); |
| 209 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); | 311 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); |
| 210 } | 312 } |
| 211 g_list_free(engines); | 313 g_list_free(engines); |
| 212 } | 314 } |
| 213 | 315 |
| 214 void PrintUsage(const char* argv0) { | 316 void PrintUsage(const char* argv0) { |
| 215 printf("Usage: %s COMMAND\n", argv0); | 317 printf("Usage: %s COMMAND\n", argv0); |
| 216 printf("check_reachable Check if ibus-daemon is reachable\n"); | 318 printf("check_reachable Check if ibus-daemon is reachable\n"); |
| 217 printf("list_engines List engine names (all engines)\n"); | 319 printf("list_engines List engine names (all engines)\n"); |
| 218 printf("list_active_engines List active engine names\n"); | 320 printf("list_active_engines List active engine names\n"); |
| 219 // TODO(yusukes): Add 2 parameters, config_key and config_value, to | 321 // TODO(yusukes): Add 2 parameters, config_key and config_value, to |
| 220 // set_config and get_config commands. | 322 // set_config and get_config commands. |
| 221 printf("set_config (boolean|int|double|string|\n" | 323 printf("set_config (boolean|int|double|string|\n" |
| 222 " boolean_list|int_list|double_list|string_list)\n" | 324 " boolean_list|int_list|double_list|string_list)\n" |
| 223 " Set a dummy value to ibus config service\n"); | 325 " Set a dummy value to ibus config service\n"); |
| 224 printf("get_config (boolean|int|double|string\n" | 326 printf("get_config (boolean|int|double|string\n" |
| 225 " boolean_list|int_list|double_list|string_list)\n" | 327 " boolean_list|int_list|double_list|string_list)\n" |
| 226 " Get a dummy value from ibus config service\n"); | 328 " Get a dummy value from ibus config service\n"); |
| 227 // TODO(yusukes): Add config_key parameter to unset_config. | 329 // TODO(yusukes): Add config_key parameter to unset_config. |
| 228 printf("unset_config Unset a dummy value from ibus config service\n"); | 330 printf("unset_config Unset a dummy value from ibus config service\n"); |
| 331 printf("get_unused List all keys that never were used.\n"); |
| 332 printf("preload_engines Preload the listed engines.\n"); |
| 333 printf("activate_engine Activate the specified engine.\n"); |
| 229 } | 334 } |
| 230 | 335 |
| 231 } // namespace | 336 } // namespace |
| 232 | 337 |
| 233 int main(int argc, char **argv) { | 338 int main(int argc, char **argv) { |
| 234 if (argc == 1) { | 339 if (argc == 1) { |
| 235 PrintUsage(argv[0]); | 340 PrintUsage(argv[0]); |
| 236 return 1; | 341 return 1; |
| 237 } | 342 } |
| 238 | 343 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 268 } | 373 } |
| 269 SetConfigAndPrintResult(ibus_config, argv[2]); | 374 SetConfigAndPrintResult(ibus_config, argv[2]); |
| 270 } else if (command == "get_config") { | 375 } else if (command == "get_config") { |
| 271 if (argc != 3) { | 376 if (argc != 3) { |
| 272 PrintUsage(argv[0]); | 377 PrintUsage(argv[0]); |
| 273 return 1; | 378 return 1; |
| 274 } | 379 } |
| 275 GetConfigAndPrintResult(ibus_config, argv[2]); | 380 GetConfigAndPrintResult(ibus_config, argv[2]); |
| 276 } else if (command == "unset_config") { | 381 } else if (command == "unset_config") { |
| 277 UnsetConfigAndPrintResult(ibus_config); | 382 UnsetConfigAndPrintResult(ibus_config); |
| 383 } else if (command == "get_unused") { |
| 384 PrintUnused(ibus_config); |
| 385 } else if (command == "preload_engines") { |
| 386 if (argc < 3) { |
| 387 PrintUsage(argv[0]); |
| 388 return 1; |
| 389 } |
| 390 PreloadEnginesAndPrintResult(ibus_config, argc-2, &(argv[2])); |
| 391 } else if (command == "activate_engine") { |
| 392 if (argc != 3) { |
| 393 PrintUsage(argv[0]); |
| 394 return 1; |
| 395 } |
| 396 ActivateEngineAndPrintResult(ibus, argv[2]); |
| 397 } else { |
| 398 PrintUsage(argv[0]); |
| 399 return 1; |
| 278 } | 400 } |
| 279 | 401 |
| 280 return 0; | 402 return 0; |
| 281 } | 403 } |
| OLD | NEW |