/kivy/input/providers/wm_common.py

https://github.com/akshayaurora/kivy · Python · 162 lines · 133 code · 22 blank · 7 comment · 9 complexity · 5cea9e12a86c5b92ce94076ac309affb MD5 · raw file

  1. '''
  2. Common definitions for a Windows provider
  3. =========================================
  4. This file provides common definitions for constants used by WM_Touch / WM_Pen.
  5. '''
  6. import os
  7. WM_MOUSEFIRST = 512
  8. WM_MOUSEMOVE = 512
  9. WM_LBUTTONDOWN = 513
  10. WM_LBUTTONUP = 514
  11. WM_LBUTTONDBLCLK = 515
  12. WM_RBUTTONDOWN = 516
  13. WM_RBUTTONUP = 517
  14. WM_RBUTTONDBLCLK = 518
  15. WM_MBUTTONDOWN = 519
  16. WM_MBUTTONUP = 520
  17. WM_MBUTTONDBLCLK = 521
  18. WM_MOUSEWHEEL = 522
  19. WM_MOUSELAST = 522
  20. WM_DPICHANGED = 736
  21. WM_GETDPISCALEDSIZE = 740
  22. WM_NCCALCSIZE = 131
  23. WM_TOUCH = 576
  24. TOUCHEVENTF_MOVE = 1
  25. TOUCHEVENTF_DOWN = 2
  26. TOUCHEVENTF_UP = 4
  27. PEN_OR_TOUCH_SIGNATURE = 0xFF515700
  28. PEN_OR_TOUCH_MASK = 0xFFFFFF00
  29. PEN_EVENT_TOUCH_MASK = 0x80
  30. SM_CYCAPTION = 4
  31. WM_TABLET_QUERYSYSTEMGESTURE = 0x000002CC
  32. TABLET_DISABLE_PRESSANDHOLD = 0x00000001
  33. TABLET_DISABLE_PENTAPFEEDBACK = 0x00000008
  34. TABLET_DISABLE_PENBARRELFEEDBACK = 0x00000010
  35. TABLET_DISABLE_TOUCHUIFORCEON = 0x00000100
  36. TABLET_DISABLE_TOUCHUIFORCEOFF = 0x00000200
  37. TABLET_DISABLE_TOUCHSWITCH = 0x00008000
  38. TABLET_DISABLE_FLICKS = 0x00010000
  39. TABLET_ENABLE_FLICKSONCONTEXT = 0x00020000
  40. TABLET_ENABLE_FLICKLEARNINGMODE = 0x00040000
  41. TABLET_DISABLE_SMOOTHSCROLLING = 0x00080000
  42. TABLET_DISABLE_FLICKFALLBACKKEYS = 0x00100000
  43. GWL_WNDPROC = -4
  44. QUERYSYSTEMGESTURE_WNDPROC = (
  45. TABLET_DISABLE_PRESSANDHOLD |
  46. TABLET_DISABLE_PENTAPFEEDBACK |
  47. TABLET_DISABLE_PENBARRELFEEDBACK |
  48. TABLET_DISABLE_SMOOTHSCROLLING |
  49. TABLET_DISABLE_FLICKFALLBACKKEYS |
  50. TABLET_DISABLE_TOUCHSWITCH |
  51. TABLET_DISABLE_FLICKS)
  52. if 'KIVY_DOC' not in os.environ:
  53. from ctypes.wintypes import (ULONG, HANDLE, DWORD, LONG, UINT,
  54. WPARAM, LPARAM, BOOL, HWND, POINT,
  55. RECT as RECT_BASE)
  56. from ctypes import (windll, WINFUNCTYPE, POINTER,
  57. c_int, c_longlong, c_void_p, Structure,
  58. sizeof, byref, cast)
  59. class RECT(RECT_BASE):
  60. x = property(lambda self: self.left)
  61. y = property(lambda self: self.top)
  62. w = property(lambda self: self.right - self.left)
  63. h = property(lambda self: self.bottom - self.top)
  64. # check availability of RegisterTouchWindow
  65. if not hasattr(windll.user32, 'RegisterTouchWindow'):
  66. raise Exception('Unsupported Window version')
  67. LRESULT = LPARAM
  68. WNDPROC = WINFUNCTYPE(LRESULT, HWND, UINT, WPARAM, LPARAM)
  69. class TOUCHINPUT(Structure):
  70. _fields_ = [
  71. ('x', LONG),
  72. ('y', LONG),
  73. ('pSource', HANDLE),
  74. ('id', DWORD),
  75. ('flags', DWORD),
  76. ('mask', DWORD),
  77. ('time', DWORD),
  78. ('extraInfo', POINTER(ULONG)),
  79. ('size_x', DWORD),
  80. ('size_y', DWORD)]
  81. def size(self):
  82. return (self.size_x, self.size_y)
  83. def screen_x(self):
  84. return self.x / 100.0
  85. def screen_y(self):
  86. return self.y / 100.0
  87. def _event_type(self):
  88. if self.flags & TOUCHEVENTF_MOVE:
  89. return 'update'
  90. if self.flags & TOUCHEVENTF_DOWN:
  91. return 'begin'
  92. if self.flags & TOUCHEVENTF_UP:
  93. return 'end'
  94. event_type = property(_event_type)
  95. def SetWindowLong_WndProc_wrapper_generator(func):
  96. def _closure(hWnd, wndProc):
  97. oldAddr = func(hWnd, GWL_WNDPROC, cast(wndProc, c_void_p).value)
  98. return cast(c_void_p(oldAddr), WNDPROC)
  99. return _closure
  100. try:
  101. LONG_PTR = c_longlong
  102. windll.user32.SetWindowLongPtrW.restype = LONG_PTR
  103. windll.user32.SetWindowLongPtrW.argtypes = [HWND, c_int, LONG_PTR]
  104. SetWindowLong_WndProc_wrapper = \
  105. SetWindowLong_WndProc_wrapper_generator(
  106. windll.user32.SetWindowLongPtrW)
  107. except AttributeError:
  108. windll.user32.SetWindowLongW.restype = LONG
  109. windll.user32.SetWindowLongW.argtypes = [HWND, c_int, LONG]
  110. SetWindowLong_WndProc_wrapper = \
  111. SetWindowLong_WndProc_wrapper_generator(
  112. windll.user32.SetWindowLongW)
  113. windll.user32.GetMessageExtraInfo.restype = LPARAM
  114. windll.user32.GetMessageExtraInfo.argtypes = []
  115. windll.user32.GetClientRect.restype = BOOL
  116. windll.user32.GetClientRect.argtypes = [HANDLE, POINTER(RECT_BASE)]
  117. windll.user32.GetWindowRect.restype = BOOL
  118. windll.user32.GetWindowRect.argtypes = [HANDLE, POINTER(RECT_BASE)]
  119. windll.user32.CallWindowProcW.restype = LRESULT
  120. windll.user32.CallWindowProcW.argtypes = [WNDPROC, HWND, UINT, WPARAM,
  121. LPARAM]
  122. windll.user32.GetActiveWindow.restype = HWND
  123. windll.user32.GetActiveWindow.argtypes = []
  124. windll.user32.RegisterTouchWindow.restype = BOOL
  125. windll.user32.RegisterTouchWindow.argtypes = [HWND, ULONG]
  126. windll.user32.UnregisterTouchWindow.restype = BOOL
  127. windll.user32.UnregisterTouchWindow.argtypes = [HWND]
  128. windll.user32.GetTouchInputInfo.restype = BOOL
  129. windll.user32.GetTouchInputInfo.argtypes = [HANDLE, UINT,
  130. POINTER(TOUCHINPUT), c_int]
  131. windll.user32.GetSystemMetrics.restype = c_int
  132. windll.user32.GetSystemMetrics.argtypes = [c_int]
  133. windll.user32.ClientToScreen.restype = BOOL
  134. windll.user32.ClientToScreen.argtypes = [HWND, POINTER(POINT)]
  135. try:
  136. windll.user32.GetDpiForWindow.restype = UINT
  137. windll.user32.GetDpiForWindow.argtypes = [HWND]
  138. except AttributeError:
  139. pass