| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium 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 "chrome/browser/chromeos/input_method/xkeyboard.h" | 5 #include "chrome/browser/chromeos/input_method/xkeyboard.h" |
| 6 | 6 |
| 7 #include <queue> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include <X11/XKBlib.h> | 10 #include <X11/XKBlib.h> |
| 10 #include <X11/Xlib.h> | 11 #include <X11/Xlib.h> |
| 11 #include <glib.h> | 12 #include <glib.h> |
| 12 #include <stdlib.h> | 13 #include <stdlib.h> |
| 13 #include <string.h> | 14 #include <string.h> |
| 14 | 15 |
| 15 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 18 #include "base/process_util.h" | 19 #include "base/process_util.h" |
| 19 #include "chrome/browser/chromeos/cros/cros_library.h" | 20 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 20 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 21 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 22 #include "content/browser/browser_thread.h" |
| 21 | 23 |
| 22 namespace chromeos { | 24 namespace chromeos { |
| 23 namespace input_method { | 25 namespace input_method { |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // The default keyboard layout name in the xorg config file. | 28 // The default keyboard layout name in the xorg config file. |
| 27 const char kDefaultLayoutName[] = "us"; | 29 const char kDefaultLayoutName[] = "us"; |
| 28 // The command we use to set the current XKB layout and modifier key mapping. | 30 // The command we use to set the current XKB layout and modifier key mapping. |
| 29 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | 31 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) |
| 30 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap"; | 32 const char kSetxkbmapCommand[] = "/usr/bin/setxkbmap"; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 // This function is used by SetLayout() and RemapModifierKeys(). Calls | 223 // This function is used by SetLayout() and RemapModifierKeys(). Calls |
| 222 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. | 224 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. |
| 223 bool SetLayoutInternal(const std::string& layout_name, | 225 bool SetLayoutInternal(const std::string& layout_name, |
| 224 const ModifierMap& modifier_map, | 226 const ModifierMap& modifier_map, |
| 225 bool force) { | 227 bool force) { |
| 226 if (!CrosLibrary::Get()->EnsureLoaded()) { | 228 if (!CrosLibrary::Get()->EnsureLoaded()) { |
| 227 // We should not try to change a layout inside ui_tests. | 229 // We should not try to change a layout inside ui_tests. |
| 228 return false; | 230 return false; |
| 229 } | 231 } |
| 230 | 232 |
| 231 const std::string layouts_to_set = CreateFullXkbLayoutName( | 233 const std::string layout_to_set = CreateFullXkbLayoutName( |
| 232 layout_name, modifier_map); | 234 layout_name, modifier_map); |
| 233 if (layouts_to_set.empty()) { | 235 if (layout_to_set.empty()) { |
| 234 return false; | 236 return false; |
| 235 } | 237 } |
| 236 | 238 |
| 237 if (!current_layout_name_.empty()) { | 239 if (!current_layout_name_.empty()) { |
| 238 const std::string current_layout = CreateFullXkbLayoutName( | 240 const std::string current_layout = CreateFullXkbLayoutName( |
| 239 current_layout_name_, current_modifier_map_); | 241 current_layout_name_, current_modifier_map_); |
| 240 if (!force && (current_layout == layouts_to_set)) { | 242 if (!force && (current_layout == layout_to_set)) { |
| 241 DLOG(INFO) << "The requested layout is already set: " << layouts_to_set; | 243 DLOG(INFO) << "The requested layout is already set: " << layout_to_set; |
| 242 return true; | 244 return true; |
| 243 } | 245 } |
| 244 } | 246 } |
| 245 | 247 |
| 246 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. | 248 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. |
| 247 if (!ContainsModifierKeyAsReplacement( | 249 if (!ContainsModifierKeyAsReplacement( |
| 248 modifier_map, kCapsLockKey)) { | 250 modifier_map, kCapsLockKey)) { |
| 249 SetCapsLockEnabled(false); | 251 SetCapsLockEnabled(false); |
| 250 } | 252 } |
| 251 | 253 |
| 252 // TODO(yusukes): Revert to VLOG(1) when crosbug.com/15851 is resolved. | 254 // TODO(yusukes): Revert to VLOG(1) when crosbug.com/15851 is resolved. |
| 253 LOG(WARNING) << (force ? "Reapply" : "Set") | 255 LOG(WARNING) << (force ? "Reapply" : "Set") |
| 254 << " layout: " << layouts_to_set; | 256 << " layout: " << layout_to_set; |
| 255 | 257 |
| 256 ExecuteSetLayoutCommand(layouts_to_set); | 258 const bool start_execution = execute_queue_.empty(); |
| 259 // If no setxkbmap command is in flight (i.e. start_execution is true), |
| 260 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand(). |
| 261 // If one or more setxkbmap commands are already in flight, just push the |
| 262 // layout name to the queue. setxkbmap command for the layout will be called |
| 263 // via OnSetLayoutFinish() callback later. |
| 264 execute_queue_.push(layout_to_set); |
| 265 if (start_execution) { |
| 266 MaybeExecuteSetLayoutCommand(); |
| 267 } |
| 257 return true; | 268 return true; |
| 258 } | 269 } |
| 259 | 270 |
| 260 // Executes 'setxkbmap -layout ...' command asynchronously. | 271 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name |
| 272 // in the |execute_queue_|. Do nothing if the queue is empty. |
| 261 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | 273 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) |
| 262 void ExecuteSetLayoutCommand(const std::string& layouts_to_set) { | 274 void MaybeExecuteSetLayoutCommand() { |
| 275 if (execute_queue_.empty()) { |
| 276 return; |
| 277 } |
| 278 const std::string layout_to_set = execute_queue_.front(); |
| 279 |
| 263 std::vector<std::string> argv; | 280 std::vector<std::string> argv; |
| 264 base::file_handle_mapping_vector fds_to_remap; | 281 base::file_handle_mapping_vector fds_to_remap; |
| 265 base::ProcessHandle handle = base::kNullProcessHandle; | 282 base::ProcessHandle handle = base::kNullProcessHandle; |
| 266 | 283 |
| 267 argv.push_back(kSetxkbmapCommand); | 284 argv.push_back(kSetxkbmapCommand); |
| 268 argv.push_back("-layout"); | 285 argv.push_back("-layout"); |
| 269 argv.push_back(layouts_to_set); | 286 argv.push_back(layout_to_set); |
| 287 argv.push_back("-synch"); |
| 270 const bool result = base::LaunchApp(argv, | 288 const bool result = base::LaunchApp(argv, |
| 271 fds_to_remap, // No remapping. | 289 fds_to_remap, // No remapping. |
| 272 false, // Don't wait. | 290 false, // Don't wait. |
| 273 &handle); | 291 &handle); |
| 274 if (!result) { | 292 if (!result) { |
| 275 LOG(ERROR) << "Failed to execute setxkbmap: " << layouts_to_set; | 293 LOG(ERROR) << "Failed to execute setxkbmap: " << layout_to_set; |
| 294 execute_queue_ = std::queue<std::string>(); // clear the queue. |
| 276 return; | 295 return; |
| 277 } | 296 } |
| 278 | 297 |
| 279 // g_child_watch_add is necessary to prevent the process from becoming a | 298 // g_child_watch_add is necessary to prevent the process from becoming a |
| 280 // zombie. | 299 // zombie. |
| 281 const base::ProcessId pid = base::GetProcId(handle); | 300 const base::ProcessId pid = base::GetProcId(handle); |
| 282 g_child_watch_add(pid, | 301 g_child_watch_add(pid, |
| 283 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), | 302 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), |
| 284 this); | 303 this); |
| 304 VLOG(1) << "ExecuteSetLayoutCommand: " << layout_to_set << ": pid=" << pid; |
| 285 } | 305 } |
| 286 | 306 |
| 287 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { | 307 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { |
| 288 DLOG(INFO) << "OnSetLayoutFinish: pid=" << pid; | 308 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 309 VLOG(1) << "OnSetLayoutFinish: pid=" << pid; |
| 310 if (self->execute_queue_.empty()) { |
| 311 LOG(ERROR) << "OnSetLayoutFinish: execute_queue_ is empty. " |
| 312 << "base::LaunchApp failed? pid=" << pid; |
| 313 return; |
| 314 } |
| 315 self->execute_queue_.pop(); |
| 316 self->MaybeExecuteSetLayoutCommand(); |
| 289 } | 317 } |
| 290 | 318 |
| 291 // The XKB layout name which we set last time like "us" and "us(dvorak)". | 319 // The XKB layout name which we set last time like "us" and "us(dvorak)". |
| 292 std::string current_layout_name_; | 320 std::string current_layout_name_; |
| 293 // The mapping of modifier keys we set last time. | 321 // The mapping of modifier keys we set last time. |
| 294 ModifierMap current_modifier_map_; | 322 ModifierMap current_modifier_map_; |
| 323 // A queue for executing setxkbmap one by one. |
| 324 std::queue<std::string> execute_queue_; |
| 295 | 325 |
| 296 DISALLOW_COPY_AND_ASSIGN(XKeyboard); | 326 DISALLOW_COPY_AND_ASSIGN(XKeyboard); |
| 297 }; | 327 }; |
| 298 | 328 |
| 299 } // namespace | 329 } // namespace |
| 300 | 330 |
| 301 std::string CreateFullXkbLayoutName(const std::string& layout_name, | 331 std::string CreateFullXkbLayoutName(const std::string& layout_name, |
| 302 const ModifierMap& modifier_map) { | 332 const ModifierMap& modifier_map) { |
| 303 static const char kValidLayoutNameCharacters[] = | 333 static const char kValidLayoutNameCharacters[] = |
| 304 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; | 334 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 bool SetAutoRepeatEnabled(bool enabled) { | 448 bool SetAutoRepeatEnabled(bool enabled) { |
| 419 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); | 449 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); |
| 420 } | 450 } |
| 421 | 451 |
| 422 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { | 452 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { |
| 423 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); | 453 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); |
| 424 } | 454 } |
| 425 | 455 |
| 426 } // namespace input_method | 456 } // namespace input_method |
| 427 } // namespace chromeos | 457 } // namespace chromeos |
| OLD | NEW |