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.
 
 
 
 
 
 

171 lines
4.3 KiB

  1. /**
  2. * libcaca Java bindings for libcaca
  3. * Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
  4. *
  5. * $Id$
  6. *
  7. * This library is free software. It comes without any warranty, to
  8. * the extent permitted by applicable law. You can redistribute it
  9. * and/or modify it under the terms of the Do What The Fuck You Want
  10. * To Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. package org.zoy.caca;
  14. public class Display extends NativeObject {
  15. static {
  16. Caca.load();
  17. }
  18. private static native long createDisplayAndCanvas();
  19. private static native long createDisplay(long canvasPtr);
  20. private static native long createDisplayAndCanvasWithDriver(String driver);
  21. private static native long createDisplayWithDriver(long canvasPtr, String driver);
  22. // Keep a reference to the canvas so that it does not get collected before this.
  23. private Canvas canvas;
  24. public Display() {
  25. ptr = createDisplayAndCanvas();
  26. long canvasPtr = getDisplayCanvas(ptr);
  27. // Create a managed canvas
  28. canvas = new Canvas(canvasPtr);
  29. }
  30. public Display(Canvas canvas) {
  31. this.canvas = canvas;
  32. ptr = createDisplay(canvas.ptr);
  33. }
  34. public Display(String driver) {
  35. ptr = createDisplayAndCanvasWithDriver(driver);
  36. long canvasPtr = getDisplayCanvas(ptr);
  37. // Create a managed canvas
  38. canvas = new Canvas(canvasPtr);
  39. }
  40. public Display(Canvas canvas, Driver driver) {
  41. ptr = createDisplayWithDriver(canvas.ptr, driver.code);
  42. }
  43. public static class Driver extends CacaObject {
  44. public Driver(String code, String desc) {
  45. super(code, desc);
  46. }
  47. public static Driver forCode(String code) {
  48. for (Driver driver : getDriverList()) {
  49. if (driver.code.equals(code)) {
  50. return driver;
  51. }
  52. }
  53. return null;
  54. }
  55. }
  56. private static native String[] getDisplayDriverList();
  57. public static Driver[] getDriverList() {
  58. String[] tmp = getDisplayDriverList();
  59. Driver[] drivers = new Driver[tmp.length / 2];
  60. for (int i = 0; 2*i < tmp.length; i++) {
  61. drivers[i] = new Driver(tmp[2*i], tmp[2*i+1]);
  62. }
  63. return drivers;
  64. }
  65. private static native void setDisplayDriver(long displayPtr, String driver);
  66. public void setDriver(Driver driver) {
  67. setDisplayDriver(ptr, driver.code);
  68. }
  69. private static native String getDisplayDriver(long displayPtr);
  70. public Driver getDriver() {
  71. return Driver.forCode(getDisplayDriver(ptr));
  72. }
  73. private static native long getDisplayCanvas(long displayPtr);
  74. public Canvas getCanvas() {
  75. return canvas;
  76. }
  77. private static native void displayRefresh(long displayPtr);
  78. public void refresh() {
  79. displayRefresh(ptr);
  80. }
  81. private static native void setDisplayTime(long displayPtr, int time);
  82. public void setTime(int time) {
  83. setDisplayTime(ptr, time);
  84. }
  85. private static native int getDisplayTime(long displayPtr);
  86. public int getTime() {
  87. return getDisplayTime(ptr);
  88. }
  89. private static native int getDisplayWidth(long displayPtr);
  90. public int getWidth() {
  91. return getDisplayWidth(ptr);
  92. }
  93. private static native int getDisplayHeight(long displayPtr);
  94. public int getHeight() {
  95. return getDisplayHeight(ptr);
  96. }
  97. private static native void setDisplayTitle(long displayPtr, String title);
  98. public void setTitle(String title) {
  99. setDisplayTitle(ptr, title);
  100. }
  101. private static native void setDisplayMouse(long displayPtr, boolean status);
  102. public void setMouse(boolean status) {
  103. setDisplayMouse(ptr, status);
  104. }
  105. private static native void setDisplayCursor(long displayPtr, boolean status);
  106. public void setCursor(boolean status) {
  107. setDisplayCursor(ptr, status);
  108. }
  109. private static native long getDisplayEvent(long displayPtr, int eventMask, int timeout);
  110. public Event getEvent(Event.Type type, int timeout) {
  111. return new Event(getDisplayEvent(ptr, type.code, timeout));
  112. }
  113. private static native int getDisplayMouseX(long eventPtr);
  114. public int getMouseX() {
  115. return getDisplayMouseX(ptr);
  116. }
  117. private static native int getDisplayMouseY(long eventPtr);
  118. public int getMouseY() {
  119. return getDisplayMouseY(ptr);
  120. }
  121. private static native void freeDisplay(long displayPtr);
  122. @Override
  123. public void finalize() throws Throwable {
  124. freeDisplay(ptr);
  125. super.finalize();
  126. }
  127. }