| 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 207 |
| 206 // This function is used by SetLayout() and RemapModifierKeys(). Calls | 208 // This function is used by SetLayout() and RemapModifierKeys(). Calls |
| 207 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. | 209 // setxkbmap command if needed, and updates the last_full_layout_name_ cache. |
| 208 bool SetLayoutInternal(const std::string& layout_name, | 210 bool SetLayoutInternal(const std::string& layout_name, |
| 209 const ModifierMap& modifier_map) { | 211 const ModifierMap& modifier_map) { |
| 210 if (!CrosLibrary::Get()->EnsureLoaded()) { | 212 if (!CrosLibrary::Get()->EnsureLoaded()) { |
| 211 // We should not try to change a layout inside ui_tests. | 213 // We should not try to change a layout inside ui_tests. |
| 212 return false; | 214 return false; |
| 213 } | 215 } |
| 214 | 216 |
| 215 const std::string layouts_to_set = CreateFullXkbLayoutName( | 217 const std::string layout_to_set = CreateFullXkbLayoutName( |
| 216 layout_name, modifier_map); | 218 layout_name, modifier_map); |
| 217 if (layouts_to_set.empty()) { | 219 if (layout_to_set.empty()) { |
| 218 return false; | 220 return false; |
| 219 } | 221 } |
| 220 | 222 |
| 221 if (!current_layout_name_.empty()) { | 223 if (!current_layout_name_.empty()) { |
| 222 const std::string current_layout = CreateFullXkbLayoutName( | 224 const std::string current_layout = CreateFullXkbLayoutName( |
| 223 current_layout_name_, current_modifier_map_); | 225 current_layout_name_, current_modifier_map_); |
| 224 if (current_layout == layouts_to_set) { | 226 if (current_layout == layout_to_set) { |
| 225 DLOG(INFO) << "The requested layout is already set: " << layouts_to_set; | 227 DLOG(INFO) << "The requested layout is already set: " << layout_to_set; |
| 226 return true; | 228 return true; |
| 227 } | 229 } |
| 228 } | 230 } |
| 229 | 231 |
| 230 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. | 232 // Turn off caps lock if there is no kCapsLockKey in the remapped keys. |
| 231 if (!ContainsModifierKeyAsReplacement( | 233 if (!ContainsModifierKeyAsReplacement( |
| 232 modifier_map, kCapsLockKey)) { | 234 modifier_map, kCapsLockKey)) { |
| 233 SetCapsLockEnabled(false); | 235 SetCapsLockEnabled(false); |
| 234 } | 236 } |
| 235 | 237 |
| 236 ExecuteSetLayoutCommand(layouts_to_set); | 238 // TODO(yusukes): Revert to VLOG(1) when crosbug.com/15851 is resolved. |
| 239 LOG(WARNING) << "Set layout: " << layout_to_set; |
| 240 |
| 241 const bool start_execution = execute_queue_.empty(); |
| 242 // If no setxkbmap command is in flight (i.e. start_execution is true), |
| 243 // start the first one by explicitly calling MaybeExecuteSetLayoutCommand(). |
| 244 // If one or more setxkbmap commands are already in flight, just push the |
| 245 // layout name to the queue. setxkbmap command for the layout will be called |
| 246 // via OnSetLayoutFinish() callback later. |
| 247 execute_queue_.push(layout_to_set); |
| 248 if (start_execution) { |
| 249 MaybeExecuteSetLayoutCommand(); |
| 250 } |
| 237 return true; | 251 return true; |
| 238 } | 252 } |
| 239 | 253 |
| 240 // Executes 'setxkbmap -layout ...' command asynchronously. | 254 // Executes 'setxkbmap -layout ...' command asynchronously using a layout name |
| 255 // in the |execute_queue_|. Do nothing if the queue is empty. |
| 241 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) | 256 // TODO(yusukes): Use libxkbfile.so instead of the command (crosbug.com/13105) |
| 242 void ExecuteSetLayoutCommand(const std::string& layouts_to_set) { | 257 void MaybeExecuteSetLayoutCommand() { |
| 258 if (execute_queue_.empty()) { |
| 259 return; |
| 260 } |
| 261 const std::string layout_to_set = execute_queue_.front(); |
| 262 |
| 243 std::vector<std::string> argv; | 263 std::vector<std::string> argv; |
| 244 base::file_handle_mapping_vector fds_to_remap; | 264 base::file_handle_mapping_vector fds_to_remap; |
| 245 base::ProcessHandle handle = base::kNullProcessHandle; | 265 base::ProcessHandle handle = base::kNullProcessHandle; |
| 246 | 266 |
| 247 argv.push_back(kSetxkbmapCommand); | 267 argv.push_back(kSetxkbmapCommand); |
| 248 argv.push_back("-layout"); | 268 argv.push_back("-layout"); |
| 249 argv.push_back(layouts_to_set); | 269 argv.push_back(layout_to_set); |
| 270 argv.push_back("-synch"); |
| 250 const bool result = base::LaunchApp(argv, | 271 const bool result = base::LaunchApp(argv, |
| 251 fds_to_remap, // No remapping. | 272 fds_to_remap, // No remapping. |
| 252 false, // Don't wait. | 273 false, // Don't wait. |
| 253 &handle); | 274 &handle); |
| 254 if (!result) { | 275 if (!result) { |
| 255 LOG(ERROR) << "Failed to execute setxkbmap: " << layouts_to_set; | 276 LOG(ERROR) << "Failed to execute setxkbmap: " << layout_to_set; |
| 277 execute_queue_ = std::queue<std::string>(); // clear the queue. |
| 256 return; | 278 return; |
| 257 } | 279 } |
| 258 | 280 |
| 259 // g_child_watch_add is necessary to prevent the process from becoming a | 281 // g_child_watch_add is necessary to prevent the process from becoming a |
| 260 // zombie. | 282 // zombie. |
| 261 const base::ProcessId pid = base::GetProcId(handle); | 283 const base::ProcessId pid = base::GetProcId(handle); |
| 262 g_child_watch_add(pid, | 284 g_child_watch_add(pid, |
| 263 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), | 285 reinterpret_cast<GChildWatchFunc>(OnSetLayoutFinish), |
| 264 this); | 286 this); |
| 287 VLOG(1) << "ExecuteSetLayoutCommand: " << layout_to_set << ": pid=" << pid; |
| 265 } | 288 } |
| 266 | 289 |
| 267 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { | 290 static void OnSetLayoutFinish(GPid pid, gint status, XKeyboard* self) { |
| 268 DLOG(INFO) << "OnSetLayoutFinish: pid=" << pid; | 291 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 292 VLOG(1) << "OnSetLayoutFinish: pid=" << pid; |
| 293 if (self->execute_queue_.empty()) { |
| 294 LOG(ERROR) << "OnSetLayoutFinish: execute_queue_ is empty. " |
| 295 << "base::LaunchApp failed? pid=" << pid; |
| 296 return; |
| 297 } |
| 298 self->execute_queue_.pop(); |
| 299 self->MaybeExecuteSetLayoutCommand(); |
| 269 } | 300 } |
| 270 | 301 |
| 271 // The XKB layout name which we set last time like "us" and "us(dvorak)". | 302 // The XKB layout name which we set last time like "us" and "us(dvorak)". |
| 272 std::string current_layout_name_; | 303 std::string current_layout_name_; |
| 273 // The mapping of modifier keys we set last time. | 304 // The mapping of modifier keys we set last time. |
| 274 ModifierMap current_modifier_map_; | 305 ModifierMap current_modifier_map_; |
| 306 // A queue for executing setxkbmap one by one. |
| 307 std::queue<std::string> execute_queue_; |
| 275 | 308 |
| 276 DISALLOW_COPY_AND_ASSIGN(XKeyboard); | 309 DISALLOW_COPY_AND_ASSIGN(XKeyboard); |
| 277 }; | 310 }; |
| 278 | 311 |
| 279 } // namespace | 312 } // namespace |
| 280 | 313 |
| 281 std::string CreateFullXkbLayoutName(const std::string& layout_name, | 314 std::string CreateFullXkbLayoutName(const std::string& layout_name, |
| 282 const ModifierMap& modifier_map) { | 315 const ModifierMap& modifier_map) { |
| 283 static const char kValidLayoutNameCharacters[] = | 316 static const char kValidLayoutNameCharacters[] = |
| 284 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; | 317 "abcdefghijklmnopqrstuvwxyz0123456789()-_"; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 bool SetAutoRepeatEnabled(bool enabled) { | 427 bool SetAutoRepeatEnabled(bool enabled) { |
| 395 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); | 428 return XKeyboard::GetInstance()->SetAutoRepeatEnabled(enabled); |
| 396 } | 429 } |
| 397 | 430 |
| 398 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { | 431 bool SetAutoRepeatRate(const AutoRepeatRate& rate) { |
| 399 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); | 432 return XKeyboard::GetInstance()->SetAutoRepeatRate(rate); |
| 400 } | 433 } |
| 401 | 434 |
| 402 } // namespace input_method | 435 } // namespace input_method |
| 403 } // namespace chromeos | 436 } // namespace chromeos |
| OLD | NEW |