Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ui/events/keycodes/keyboard_code_conversion_x.cc

Issue 336403005: Use XInput2 events for keyboard events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments (derat), the sequel. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "ui/events/keycodes/keyboard_code_conversion_x.h" 5 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
6 6
7 #define XK_3270 // for XK_3270_BackTab 7 #define XK_3270 // for XK_3270_BackTab
8 #include <X11/keysym.h> 8 #include <X11/XF86keysym.h>
9 #include <X11/Xlib.h> 9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h> 10 #include <X11/Xutil.h>
11 #include <X11/XF86keysym.h> 11 #include <X11/extensions/XInput2.h>
12 #include <X11/keysym.h>
12 13
13 #include "base/basictypes.h" 14 #include "base/basictypes.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
16 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "ui/events/keycodes/dom4/keycode_converter.h" 19 #include "ui/events/keycodes/dom4/keycode_converter.h"
19 20
20 namespace ui { 21 namespace ui {
21 22
22 // Get an ui::KeyboardCode from an X keyevent 23 // Get an ui::KeyboardCode from an X keyevent
23 KeyboardCode KeyboardCodeFromXKeyEvent(XEvent* xev) { 24 KeyboardCode KeyboardCodeFromXKeyEvent(XEvent* xev) {
25 XEvent xkeyevent;
26 XKeyEvent* xkey = NULL;
27 if (xev->type == GenericEvent) {
28 // Convert the XI2 key event into a core key event so that we can
29 // continue to use XLookupString() until crbug.com/367732 is complete.
30 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent);
31 xkey = &xkeyevent.xkey;
32 } else {
33 xkey = &xev->xkey;
34 }
24 // XLookupKeysym does not take into consideration the state of the lock/shift 35 // XLookupKeysym does not take into consideration the state of the lock/shift
25 // etc. keys. So it is necessary to use XLookupString instead. 36 // etc. keys. So it is necessary to use XLookupString instead.
26 KeySym keysym; 37 KeySym keysym;
27 XLookupString(&xev->xkey, NULL, 0, &keysym, NULL); 38 XLookupString(xkey, NULL, 0, &keysym, NULL);
28 KeyboardCode keycode = KeyboardCodeFromXKeysym(keysym); 39 KeyboardCode keycode = KeyboardCodeFromXKeysym(keysym);
29 if (keycode == VKEY_UNKNOWN) { 40 if (keycode == VKEY_UNKNOWN) {
30 keysym = DefaultXKeysymFromHardwareKeycode(xev->xkey.keycode); 41 keysym = DefaultXKeysymFromHardwareKeycode(xkey->keycode);
31 keycode = KeyboardCodeFromXKeysym(keysym); 42 keycode = KeyboardCodeFromXKeysym(keysym);
32 } 43 }
33 44
34 return keycode; 45 return keycode;
35 } 46 }
36 47
37 KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym) { 48 KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym) {
38 // TODO(sad): Have |keysym| go through the X map list? 49 // TODO(sad): Have |keysym| go through the X map list?
39 50
40 switch (keysym) { 51 switch (keysym) {
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 case XF86XK_KbdBrightnessUp: 432 case XF86XK_KbdBrightnessUp:
422 return VKEY_KBD_BRIGHTNESS_UP; 433 return VKEY_KBD_BRIGHTNESS_UP;
423 434
424 // TODO(sad): some keycodes are still missing. 435 // TODO(sad): some keycodes are still missing.
425 } 436 }
426 DLOG(WARNING) << "Unknown keysym: " << base::StringPrintf("0x%x", keysym); 437 DLOG(WARNING) << "Unknown keysym: " << base::StringPrintf("0x%x", keysym);
427 return VKEY_UNKNOWN; 438 return VKEY_UNKNOWN;
428 } 439 }
429 440
430 const char* CodeFromXEvent(XEvent* xev) { 441 const char* CodeFromXEvent(XEvent* xev) {
431 return KeycodeConverter::GetInstance()->NativeKeycodeToCode( 442 int keycode = (xev->type == GenericEvent)
432 xev->xkey.keycode); 443 ? static_cast<XIDeviceEvent*>(xev->xcookie.data)->detail
444 : xev->xkey.keycode;
445 return KeycodeConverter::GetInstance()->NativeKeycodeToCode(keycode);
433 } 446 }
434 447
435 uint16 GetCharacterFromXEvent(XEvent* xev) { 448 uint16 GetCharacterFromXEvent(XEvent* xev) {
449 XEvent xkeyevent;
450 XKeyEvent* xkey = NULL;
436 char buf[6]; 451 char buf[6];
437 int bytes_written = XLookupString(&xev->xkey, buf, 6, NULL, NULL); 452 if (xev->type == GenericEvent) {
453 // Convert the XI2 key event into a core key event so that we can
454 // continue to use XLookupString() until crbug.com/367732 is complete.
455 InitXKeyEventFromXIDeviceEvent(*xev, &xkeyevent);
456 xkey = &xkeyevent.xkey;
457 } else {
458 xkey = &xev->xkey;
459 }
460 int bytes_written = XLookupString(xkey, buf, 6, NULL, NULL);
438 DCHECK_LE(bytes_written, 6); 461 DCHECK_LE(bytes_written, 6);
439 462
440 if (bytes_written <= 0) 463 if (bytes_written <= 0)
441 return 0; 464 return 0;
442 const base::string16& result = base::WideToUTF16( 465 const base::string16& result = base::WideToUTF16(
443 base::SysNativeMBToWide(base::StringPiece(buf, bytes_written))); 466 base::SysNativeMBToWide(base::StringPiece(buf, bytes_written)));
444 return result.length() == 1 ? result[0] : 0; 467 return result.length() == 1 ? result[0] : 0;
445 } 468 }
446 469
447 unsigned int DefaultXKeysymFromHardwareKeycode(unsigned int hardware_code) { 470 unsigned int DefaultXKeysymFromHardwareKeycode(unsigned int hardware_code) {
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 return XF86XK_KbdBrightnessDown; 892 return XF86XK_KbdBrightnessDown;
870 case VKEY_KBD_BRIGHTNESS_UP: 893 case VKEY_KBD_BRIGHTNESS_UP:
871 return XF86XK_KbdBrightnessUp; 894 return XF86XK_KbdBrightnessUp;
872 895
873 default: 896 default:
874 LOG(WARNING) << "Unknown keycode:" << keycode; 897 LOG(WARNING) << "Unknown keycode:" << keycode;
875 return 0; 898 return 0;
876 } 899 }
877 } 900 }
878 901
902 void InitXKeyEventFromXIDeviceEvent(const XEvent& src, XEvent* xkeyevent) {
903 DCHECK(src.type == GenericEvent);
904 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(src.xcookie.data);
905 switch (xievent->evtype) {
906 case XI_KeyPress:
sadrul 2014/06/26 12:34:20 should be 2 more indent
kpschoedel 2014/06/26 17:24:10 Done.
907 xkeyevent->type = KeyPress;
908 break;
909 case XI_KeyRelease:
910 xkeyevent->type = KeyRelease;
911 break;
912 default:
913 NOTREACHED();
914 }
915 xkeyevent->xkey.serial = xievent->serial;
916 xkeyevent->xkey.send_event = xievent->send_event;
917 xkeyevent->xkey.display = xievent->display;
918 xkeyevent->xkey.window = xievent->event;
919 xkeyevent->xkey.root = xievent->root;
920 xkeyevent->xkey.subwindow = xievent->child;
921 xkeyevent->xkey.time = xievent->time;
922 xkeyevent->xkey.x = xievent->event_x;
923 xkeyevent->xkey.y = xievent->event_y;
924 xkeyevent->xkey.x_root = xievent->root_x;
925 xkeyevent->xkey.y_root = xievent->root_y;
926 xkeyevent->xkey.state = xievent->mods.effective;
927 xkeyevent->xkey.keycode = xievent->detail;
928 xkeyevent->xkey.same_screen = 1;
929 }
930
879 } // namespace ui 931 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698