Chromium Code Reviews| 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 execute_queue_.push(layout_to_set); | |
| 260 if (start_execution) { | |
|
satorux1
2011/06/07 05:41:29
I thought we could move the queue management logic
Yusuke Sato
2011/06/07 05:51:43
I think it will not work if SetLayout() is rapidly
satorux1
2011/06/07 05:59:13
Thank you for the explanation. The code wasn't obv
Yusuke Sato
2011/06/07 06:53:28
Done.
| |
| 261 // No setxkbmap command is in flight. Start the first one. | |
| 262 MaybeExecuteSetLayoutCommand(); | |
| 263 } | |
| 257 return true; | 264 return true; |
| 258 } | 265 } |
| 259 | 266 |
| 260 // Executes 'setxkbmap -layout ...' command asynchronously. | 267 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name |
| 268 // in the |execute_queue_|. Do nothing if the queue is empty. | |
| 261 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | 269 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) |
| 262 void ExecuteSetLayoutCommand(const std::string& layouts_to_set) { | 270 void MaybeExecuteSetLayoutCommand() { |
| 271 if (execute_queue_.empty()) { | |
| 272 return; | |
| 273 } | |
| 274 const std::string layout_to_set = execute_queue_.front(); | |
| 275 | |
| 263 std::vector<std::string> argv; | 276 std::vector<std::string> argv; |
| 264 base::file_handle_mapping_vector fds_to_remap; | 277 base::file_handle_mapping_vector fds_to_remap; |
| 265 base::ProcessHandle handle = base::kNullProcessHandle; | 278 base::ProcessHandle handle = base::kNullProcessHandle; |
| 266 | 279 |
| 267 argv.push_back(kSetxkbmapCommand); | 280 argv.push_back(kSetxkbmapCommand); |
| 268 argv.push_back("-layout"); | 281 argv.push_back("-layout"); |
| 269 argv.push_back(layouts_to_set); | 282 argv.push_back(layout_to_set); |
| 283 argv.push_back("-synch"); | |
| 270 const bool result = base::LaunchApp(argv, | 284 const bool result = base::LaunchApp(argv, |
| 271 fds_to_remap, // No remapping. | 285 fds_to_remap, // No remapping. |
| 272 false, // Don't wait. | 286 false, // Don't wait. |
| 273 &handle); | 287 &handle); |
| 274 if (!result) { | 288 if (!result) { |
| 275 LOG(ERROR) << "Failed to execute setxkbmap: " << layouts_to_set; | 289 LOG(ERROR) << "Failed to execute setxkbmap: " << layout_to_set; |
| 290 execute_queue_ = std::queue<std::string>(); // clear the queue. | |
| 276 return; | 291 return; |
| 277 } | 292 } |
| 278 | 293 |
| 279 // g_child_watch_add is necessary to prevent the process from becoming a | 294 // g_child_watch_add is necessary to prevent the process from becoming a |
| 280 // zombie. | 295 // zombie. |
| 281 const base::ProcessId pid = base::GetProcId(handle); | 296 const base::ProcessId pid = base::GetProcId(handle); |
| 282 g_child_watch_add(pid, | 297 g_child_watch_add(pid, |
| 283 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), | 298 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), |
| 284 this); | 299 this); |
| 300 VLOG(1) << "ExecuteSetLayoutCommand: " << layout_to_set << ": pid=" << pid; | |
| 285 } | 301 } |
| 286 | 302 |
| 287 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { | 303 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { |
| 288 DLOG(INFO) << "OnSetLayoutFinish: pid=" << pid; | 304 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 305 VLOG(1) << "OnSetLayoutFinish: pid=" << pid; | |
| 306 if (self->execute_queue_.empty()) { | |
| 307 LOG(ERROR) << "OnSetLayoutFinish: execute_queue_ is empty. " | |
| 308 << "base::LaunchApp failed? pid=" << pid; | |
| 309 return; | |
| 310 } | |
| 311 self->execute_queue_.pop(); | |
| 312 self->MaybeExecuteSetLayoutCommand(); | |
| 289 } | 313 } |
| 290 | 314 |
| 291 // The XKB layout name which we set last time like "us" and "us(dvorak)". | 315 // The XKB layout name which we set last time like "us" and "us(dvorak)". |
| 292 std::string current_layout_name_; | 316 std::string current_layout_name_; |
| 293 // The mapping of modifier keys we set last time. | 317 // The mapping of modifier keys we set last time. |
| 294 ModifierMap current_modifier_map_; | 318 ModifierMap current_modifier_map_; |
| 319 // A queue for executing setxkbmap one by one. | |
| 320 std::queue<std::string> execute_queue_; | |
| 295 | 321 |
| 296 DISALLOW_COPY_AND_ASSIGN(XKeyboard); | 322 DISALLOW_COPY_AND_ASSIGN(XKeyboard); |
| 297 }; | 323 }; |
| 298 | 324 |
| 299 } // namespace | 325 } // namespace |
| 300 | 326 |
| 301 std::string CreateFullXkbLayoutName(const std::string& layout_name, | 327 std::string CreateFullXkbLayoutName(const std::string& layout_name, |
| 302 const ModifierMap& modifier_map) { | 328 const ModifierMap& modifier_map) { |
| 303 static const char kValidLayoutNameCharacters[] = | 329 static const char kValidLayoutNameCharacters[] = |
| 304 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; | 330 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 bool SetAutoRepeatEnabled(bool enabled) { | 444 bool SetAutoRepeatEnabled(bool enabled) { |
| 419 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); | 445 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); |
| 420 } | 446 } |
| 421 | 447 |
| 422 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { | 448 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { |
| 423 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); | 449 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); |
| 424 } | 450 } |
| 425 | 451 |
| 426 } // namespace input_method | 452 } // namespace input_method |
| 427 } // namespace chromeos | 453 } // namespace chromeos |
| OLD | NEW |