| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <cstring> | 5 #include <cstring> |
| 6 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
| 9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
| 10 #include <X11/Xutil.h> | 10 #include <X11/Xutil.h> |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 // We don't bother setting fields that the event code doesn't use, such as | 40 // We don't bother setting fields that the event code doesn't use, such as |
| 41 // x_root/y_root and window/root/subwindow. | 41 // x_root/y_root and window/root/subwindow. |
| 42 XButtonEvent* button_event = &(event->xbutton); | 42 XButtonEvent* button_event = &(event->xbutton); |
| 43 button_event->type = is_press ? ButtonPress : ButtonRelease; | 43 button_event->type = is_press ? ButtonPress : ButtonRelease; |
| 44 button_event->x = location.x(); | 44 button_event->x = location.x(); |
| 45 button_event->y = location.y(); | 45 button_event->y = location.y(); |
| 46 button_event->button = button; | 46 button_event->button = button; |
| 47 button_event->state = state; | 47 button_event->state = state; |
| 48 } | 48 } |
| 49 | 49 |
| 50 #if !defined(OS_CHROMEOS) |
| 50 // Initializes the passed-in Xlib event. | 51 // Initializes the passed-in Xlib event. |
| 51 void InitKeyEvent(Display* display, | 52 void InitKeyEvent(Display* display, |
| 52 XEvent* event, | 53 XEvent* event, |
| 53 bool is_press, | 54 bool is_press, |
| 54 int keycode, | 55 int keycode, |
| 55 int state) { | 56 int state) { |
| 56 memset(event, 0, sizeof(*event)); | 57 memset(event, 0, sizeof(*event)); |
| 57 | 58 |
| 58 // We don't bother setting fields that the event code doesn't use, such as | 59 // We don't bother setting fields that the event code doesn't use, such as |
| 59 // x_root/y_root and window/root/subwindow. | 60 // x_root/y_root and window/root/subwindow. |
| 60 XKeyEvent* key_event = &(event->xkey); | 61 XKeyEvent* key_event = &(event->xkey); |
| 61 key_event->display = display; | 62 key_event->display = display; |
| 62 key_event->type = is_press ? KeyPress : KeyRelease; | 63 key_event->type = is_press ? KeyPress : KeyRelease; |
| 63 key_event->keycode = keycode; | 64 key_event->keycode = keycode; |
| 64 key_event->state = state; | 65 key_event->state = state; |
| 65 } | 66 } |
| 66 | 67 #endif |
| 67 // Returns true if the keysym maps to a KeyEvent with the EF_FUNCTION_KEY | |
| 68 // flag set, or the keysym maps to a zero key code. | |
| 69 bool HasFunctionKeyFlagSetIfSupported(Display* display, int x_keysym) { | |
| 70 XEvent event; | |
| 71 int x_keycode = XKeysymToKeycode(display, x_keysym); | |
| 72 // Exclude keysyms for which the server has no corresponding keycode. | |
| 73 if (x_keycode) { | |
| 74 InitKeyEvent(display, &event, true, x_keycode, 0); | |
| 75 ui::KeyEvent ui_key_event(&event); | |
| 76 return (ui_key_event.flags() & ui::EF_FUNCTION_KEY); | |
| 77 } | |
| 78 return true; | |
| 79 } | |
| 80 | 68 |
| 81 } // namespace | 69 } // namespace |
| 82 | 70 |
| 83 class EventsXTest : public testing::Test { | 71 class EventsXTest : public testing::Test { |
| 84 public: | 72 public: |
| 85 EventsXTest() {} | 73 EventsXTest() {} |
| 86 ~EventsXTest() override {} | 74 ~EventsXTest() override {} |
| 87 | 75 |
| 88 void SetUp() override { | 76 void SetUp() override { |
| 89 DeviceDataManagerX11::CreateInstance(); | 77 DeviceDataManagerX11::CreateInstance(); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 ui::ScopedXI2Event xrelease0; | 358 ui::ScopedXI2Event xrelease0; |
| 371 xrelease0.InitTouchEvent(0, XI_TouchEnd, 0, gfx::Point(10, 10), valuators); | 359 xrelease0.InitTouchEvent(0, XI_TouchEnd, 0, gfx::Point(10, 10), valuators); |
| 372 ui::TouchEvent urelease0(xrelease0); | 360 ui::TouchEvent urelease0(xrelease0); |
| 373 { | 361 { |
| 374 // When the copy is destructed it should not attempt to remove the mapping. | 362 // When the copy is destructed it should not attempt to remove the mapping. |
| 375 // Exiting this scope should not cause a crash. | 363 // Exiting this scope should not cause a crash. |
| 376 ui::TouchEvent copy = urelease0; | 364 ui::TouchEvent copy = urelease0; |
| 377 } | 365 } |
| 378 } | 366 } |
| 379 | 367 |
| 380 TEST_F(EventsXTest, NumpadKeyEvents) { | |
| 381 XEvent event; | |
| 382 Display* display = gfx::GetXDisplay(); | |
| 383 | |
| 384 struct { | |
| 385 bool is_numpad_key; | |
| 386 int x_keysym; | |
| 387 } keys[] = { | |
| 388 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional | |
| 389 // keysymdef.h numbering. | |
| 390 { true, XK_KP_Space }, | |
| 391 { true, XK_KP_Equal }, | |
| 392 // Other numpad keysyms. (This is actually exhaustive in the current list.) | |
| 393 { true, XK_KP_Tab }, | |
| 394 { true, XK_KP_Enter }, | |
| 395 { true, XK_KP_F1 }, | |
| 396 { true, XK_KP_F2 }, | |
| 397 { true, XK_KP_F3 }, | |
| 398 { true, XK_KP_F4 }, | |
| 399 { true, XK_KP_Home }, | |
| 400 { true, XK_KP_Left }, | |
| 401 { true, XK_KP_Up }, | |
| 402 { true, XK_KP_Right }, | |
| 403 { true, XK_KP_Down }, | |
| 404 { true, XK_KP_Prior }, | |
| 405 { true, XK_KP_Page_Up }, | |
| 406 { true, XK_KP_Next }, | |
| 407 { true, XK_KP_Page_Down }, | |
| 408 { true, XK_KP_End }, | |
| 409 { true, XK_KP_Begin }, | |
| 410 { true, XK_KP_Insert }, | |
| 411 { true, XK_KP_Delete }, | |
| 412 { true, XK_KP_Multiply }, | |
| 413 { true, XK_KP_Add }, | |
| 414 { true, XK_KP_Separator }, | |
| 415 { true, XK_KP_Subtract }, | |
| 416 { true, XK_KP_Decimal }, | |
| 417 { true, XK_KP_Divide }, | |
| 418 { true, XK_KP_0 }, | |
| 419 { true, XK_KP_1 }, | |
| 420 { true, XK_KP_2 }, | |
| 421 { true, XK_KP_3 }, | |
| 422 { true, XK_KP_4 }, | |
| 423 { true, XK_KP_5 }, | |
| 424 { true, XK_KP_6 }, | |
| 425 { true, XK_KP_7 }, | |
| 426 { true, XK_KP_8 }, | |
| 427 { true, XK_KP_9 }, | |
| 428 // Largest keysym preceding XK_KP_Space. | |
| 429 { false, XK_Num_Lock }, | |
| 430 // Smallest keysym following XK_KP_Equal. | |
| 431 { false, XK_F1 }, | |
| 432 // Non-numpad analogues of numpad keysyms. | |
| 433 { false, XK_Tab }, | |
| 434 { false, XK_Return }, | |
| 435 { false, XK_F1 }, | |
| 436 { false, XK_F2 }, | |
| 437 { false, XK_F3 }, | |
| 438 { false, XK_F4 }, | |
| 439 { false, XK_Home }, | |
| 440 { false, XK_Left }, | |
| 441 { false, XK_Up }, | |
| 442 { false, XK_Right }, | |
| 443 { false, XK_Down }, | |
| 444 { false, XK_Prior }, | |
| 445 { false, XK_Page_Up }, | |
| 446 { false, XK_Next }, | |
| 447 { false, XK_Page_Down }, | |
| 448 { false, XK_End }, | |
| 449 { false, XK_Insert }, | |
| 450 { false, XK_Delete }, | |
| 451 { false, XK_multiply }, | |
| 452 { false, XK_plus }, | |
| 453 { false, XK_minus }, | |
| 454 { false, XK_period }, | |
| 455 { false, XK_slash }, | |
| 456 { false, XK_0 }, | |
| 457 { false, XK_1 }, | |
| 458 { false, XK_2 }, | |
| 459 { false, XK_3 }, | |
| 460 { false, XK_4 }, | |
| 461 { false, XK_5 }, | |
| 462 { false, XK_6 }, | |
| 463 { false, XK_7 }, | |
| 464 { false, XK_8 }, | |
| 465 { false, XK_9 }, | |
| 466 // Miscellaneous other keysyms. | |
| 467 { false, XK_BackSpace }, | |
| 468 { false, XK_Scroll_Lock }, | |
| 469 { false, XK_Multi_key }, | |
| 470 { false, XK_Select }, | |
| 471 { false, XK_Num_Lock }, | |
| 472 { false, XK_Shift_L }, | |
| 473 { false, XK_space }, | |
| 474 { false, XK_A }, | |
| 475 }; | |
| 476 | |
| 477 for (size_t k = 0; k < arraysize(keys); ++k) { | |
| 478 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym); | |
| 479 // Exclude keysyms for which the server has no corresponding keycode. | |
| 480 if (x_keycode) { | |
| 481 InitKeyEvent(display, &event, true, x_keycode, 0); | |
| 482 // int keysym = XLookupKeysym(&event.xkey, 0); | |
| 483 // if (keysym) { | |
| 484 ui::KeyEvent ui_key_event(&event); | |
| 485 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0, | |
| 486 ui_key_event.flags() & ui::EF_NUMPAD_KEY); | |
| 487 } | |
| 488 } | |
| 489 } | |
| 490 | |
| 491 TEST_F(EventsXTest, FunctionKeyEvents) { | |
| 492 Display* display = gfx::GetXDisplay(); | |
| 493 | |
| 494 // Min function key code minus 1. | |
| 495 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F1 - 1)); | |
| 496 // All function keys. | |
| 497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F1)); | |
| 498 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F2)); | |
| 499 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F3)); | |
| 500 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F4)); | |
| 501 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F5)); | |
| 502 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F6)); | |
| 503 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F7)); | |
| 504 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F8)); | |
| 505 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F9)); | |
| 506 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F10)); | |
| 507 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F11)); | |
| 508 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F12)); | |
| 509 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F13)); | |
| 510 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F14)); | |
| 511 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F15)); | |
| 512 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F16)); | |
| 513 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F17)); | |
| 514 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F18)); | |
| 515 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F19)); | |
| 516 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F20)); | |
| 517 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F21)); | |
| 518 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F22)); | |
| 519 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F23)); | |
| 520 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F24)); | |
| 521 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F25)); | |
| 522 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F26)); | |
| 523 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F27)); | |
| 524 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F28)); | |
| 525 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F29)); | |
| 526 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F30)); | |
| 527 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F31)); | |
| 528 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F32)); | |
| 529 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F33)); | |
| 530 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F34)); | |
| 531 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F35)); | |
| 532 // Max function key code plus 1. | |
| 533 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F35 + 1)); | |
| 534 } | |
| 535 | |
| 536 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but | 368 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but |
| 537 // that an exception list of keys can still be processed. | 369 // that an exception list of keys can still be processed. |
| 538 TEST_F(EventsXTest, DisableKeyboard) { | 370 TEST_F(EventsXTest, DisableKeyboard) { |
| 539 DeviceDataManagerX11* device_data_manager = | 371 DeviceDataManagerX11* device_data_manager = |
| 540 static_cast<DeviceDataManagerX11*>( | 372 static_cast<DeviceDataManagerX11*>( |
| 541 DeviceDataManager::GetInstance()); | 373 DeviceDataManager::GetInstance()); |
| 542 int blocked_device_id = 1; | 374 int blocked_device_id = 1; |
| 543 int other_device_id = 2; | 375 int other_device_id = 2; |
| 544 int master_device_id = 3; | 376 int master_device_id = 3; |
| 545 device_data_manager->DisableDevice(blocked_device_id); | 377 device_data_manager->DisableDevice(blocked_device_id); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 KeyEventTestApi test_event(&key_event); | 487 KeyEventTestApi test_event(&key_event); |
| 656 test_event.set_is_char(true); | 488 test_event.set_is_char(true); |
| 657 } | 489 } |
| 658 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); | 490 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); |
| 659 } | 491 } |
| 660 } | 492 } |
| 661 } | 493 } |
| 662 #endif | 494 #endif |
| 663 | 495 |
| 664 } // namespace ui | 496 } // namespace ui |
| OLD | NEW |