You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # event event lister for libcaca
  5. # Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
  6. #
  7. # This file is a Python port of "examples/event.c"
  8. # which is:
  9. # Copyright (c) 2004-2010 Sam Hocevar <sam@hocevar.net>
  10. # All Rights Reserverd
  11. #
  12. # This library is free software. It comes without any warranty, to
  13. # the extent permitted by applicable law. You can redistribute it
  14. # and/or modify it under the terms of the Do What the Fuck You Want
  15. # to Public License, Version 2, as published by Sam Hocevar. See
  16. # http://www.wtfpl.net/ for more details.
  17. #
  18. import sys
  19. import caca
  20. from caca.canvas import Canvas, CanvasError
  21. from caca.display import Display, DisplayError, Event
  22. def print_event(cv, x, y, event):
  23. """ Print event name.
  24. :cv: libcaca canvas
  25. :x: X coordinate
  26. :y: Y coordinate
  27. :event: event to display
  28. """
  29. if event.get_type() == caca.EVENT_NONE:
  30. cv.put_str(x, y, "CACA_EVENT_NONE")
  31. elif event.get_type() == caca.EVENT_KEY_PRESS:
  32. character = event.get_key_ch()
  33. if character > 0x1f and character < 0x80:
  34. cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, chr(character)))
  35. else:
  36. cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, '?'))
  37. elif event.get_type() == caca.EVENT_KEY_RELEASE:
  38. character = event.get_key_ch()
  39. if character > 0x1f and character < 0x80:
  40. cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, chr(character)))
  41. else:
  42. cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, '?'))
  43. elif event.get_type() == caca.EVENT_MOUSE_MOTION:
  44. cv.put_str(x, y, "CACA_EVENT_MOUSE_MOTION %u %u" % (event.get_mouse_x(),
  45. event.get_mouse_y()))
  46. elif event.get_type() == caca.EVENT_MOUSE_PRESS:
  47. cv.put_str(x, y, "CACA_EVENT_MOUSE_PRESS %u" % (event.get_mouse_button()))
  48. elif event.get_type() == caca.EVENT_MOUSE_RELEASE:
  49. cv.put_str(x, y, "CACA_EVENT_MOUSE_RELEASE %u" % (event.get_mouse_button()))
  50. elif event.get_type() == caca.EVENT_RESIZE:
  51. cv.put_str(x, y, "CACA_EVENT_RESIZE %u %u" % (event.get_resize_width(),
  52. event.get_resize_height()))
  53. elif event.get_type() == caca.EVENT_QUIT:
  54. cv.put_str(x, y, "CACA_EVENT_QUIT")
  55. else:
  56. cv.put_str(x, y, "CACA_EVENT_UNKNOWN")
  57. def main():
  58. """ Main function. """
  59. events = []
  60. quit = 0
  61. quit_string = ["", "q", "qu", "qui", "quit"]
  62. try:
  63. cv = Canvas(80, 24)
  64. dp = Display(cv)
  65. except (CanvasError, DisplayError) as err:
  66. sys.stderr.write("%s\n" % err)
  67. sys.exit(127)
  68. h = cv.get_height() - 1
  69. cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
  70. cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
  71. cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
  72. cv.put_str(0, h, "Type \"quit\" to exit")
  73. dp.refresh()
  74. while quit < 4:
  75. ev = Event()
  76. if dp.get_event(caca.EVENT_ANY, ev, -1):
  77. if ev.get_type() == caca.EVENT_KEY_PRESS:
  78. key = ev.get_key_ch()
  79. if key == ord('u') and quit == 1:
  80. quit += 1
  81. elif key == ord('i') and quit == 2:
  82. quit += 1
  83. elif key == ord('t') and quit == 3:
  84. quit += 1
  85. elif key == ord('q'):
  86. quit = 1
  87. else:
  88. quit = 0
  89. events.append(ev)
  90. cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
  91. cv.clear()
  92. #print current event
  93. cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
  94. cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
  95. if events:
  96. print_event(cv, 0, 0, events[-1])
  97. cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
  98. cv.put_str(0, h, "Type \"quit\" to exit: %s" % quit_string[quit])
  99. #print previous events
  100. cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
  101. counts = list(range(0, len(events)-1))
  102. counts.reverse()
  103. if len(events) > 1:
  104. j = 0
  105. for i in counts:
  106. if j < h - 1 and events[i].get_type():
  107. print_event(cv, 0, ((len(events) - 1) - i), events[i])
  108. j += 1
  109. dp.refresh()
  110. if __name__ == "__main__":
  111. main()