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.

org_zoy_caca_Display.c 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * libcaca Java bindings for libcaca
  3. * Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What The Fuck You Want
  8. * To Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. #include "caca_java_common.h"
  12. #include "org_zoy_caca_Display.h"
  13. #include <stdlib.h>
  14. #include "caca.h"
  15. #define DISPLAY_CREATION_ERROR "Cannot create display"
  16. JNIEXPORT jlong JNICALL
  17. Java_org_zoy_caca_Display_createDisplayAndCanvas(JNIEnv *env, jclass cls)
  18. {
  19. caca_display_t *display = caca_create_display(NULL);
  20. if(display == NULL) {
  21. THROW_EX(DISPLAY_CREATION_ERROR);
  22. return 0;
  23. }
  24. return (jlong)display;
  25. }
  26. JNIEXPORT jlong JNICALL
  27. Java_org_zoy_caca_Display_createDisplay(JNIEnv *env, jclass cls, jlong canvas_ptr)
  28. {
  29. caca_display_t *display = caca_create_display((caca_canvas_t *)canvas_ptr);
  30. if(display == NULL) {
  31. THROW_EX(DISPLAY_CREATION_ERROR);
  32. return 0;
  33. }
  34. return (jlong)display;
  35. }
  36. JNIEXPORT jlong JNICALL
  37. Java_org_zoy_caca_Display_createDisplayAndCanvasWithDriver(JNIEnv *env, jclass cls,
  38. jstring driver_name)
  39. {
  40. const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
  41. caca_display_t *ret = caca_create_display_with_driver(NULL, driver_name_chars);
  42. (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
  43. if(ret == NULL) {
  44. THROW_EX(DISPLAY_CREATION_ERROR);
  45. return 0;
  46. }
  47. return (jlong)ret;
  48. }
  49. JNIEXPORT jlong JNICALL
  50. Java_org_zoy_caca_Display_createDisplayWithDriver(JNIEnv *env, jclass cls,
  51. jlong canvas_ptr, jstring driver_name)
  52. {
  53. const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
  54. caca_display_t *ret = caca_create_display_with_driver((caca_canvas_t *)canvas_ptr, driver_name_chars);
  55. (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
  56. if(ret == NULL) {
  57. THROW_EX(DISPLAY_CREATION_ERROR);
  58. return 0;
  59. }
  60. return (jlong)ret;
  61. }
  62. JNIEXPORT jobjectArray JNICALL
  63. Java_org_zoy_caca_Display_getDisplayDriverList(JNIEnv *env, jclass cls)
  64. {
  65. const char *const *drivers = caca_get_display_driver_list();
  66. return caca_java_to_string_array(env, drivers);
  67. }
  68. JNIEXPORT void JNICALL
  69. Java_org_zoy_caca_Display_setDisplayDriver(JNIEnv *env, jclass cls, jlong ptr, jstring driver_name)
  70. {
  71. const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
  72. caca_set_display_driver((caca_display_t *)ptr, driver_name_chars);
  73. (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
  74. }
  75. JNIEXPORT jstring JNICALL
  76. Java_org_zoy_caca_Display_getDisplayDriver(JNIEnv *env, jclass cls, jlong ptr)
  77. {
  78. return (*env)->NewStringUTF(env, caca_get_display_driver((caca_display_t *)ptr));
  79. }
  80. JNIEXPORT jlong JNICALL
  81. Java_org_zoy_caca_Display_getDisplayCanvas(JNIEnv *env, jclass cls, jlong ptr)
  82. {
  83. return (jlong)caca_get_canvas((caca_display_t *)ptr);
  84. }
  85. JNIEXPORT void JNICALL
  86. Java_org_zoy_caca_Display_displayRefresh(JNIEnv *env, jclass cls, jlong ptr)
  87. {
  88. caca_refresh_display((caca_display_t *)ptr);
  89. }
  90. JNIEXPORT void JNICALL
  91. Java_org_zoy_caca_Display_setDisplayTime(JNIEnv *env, jclass cls, jlong ptr, jint time)
  92. {
  93. caca_set_display_time((caca_display_t *)ptr, time);
  94. }
  95. JNIEXPORT jint JNICALL
  96. Java_org_zoy_caca_Display_getDisplayTime(JNIEnv *env, jclass cls, jlong ptr)
  97. {
  98. return caca_get_display_time((caca_display_t *)ptr);
  99. }
  100. JNIEXPORT jint JNICALL
  101. Java_org_zoy_caca_Display_getDisplayWidth(JNIEnv *env, jclass cls, jlong ptr)
  102. {
  103. return caca_get_display_width((caca_display_t *)ptr);
  104. }
  105. JNIEXPORT jint JNICALL
  106. Java_org_zoy_caca_Display_getDisplayHeight(JNIEnv *env, jclass cls, jlong ptr)
  107. {
  108. return caca_get_display_height((caca_display_t *)ptr);
  109. }
  110. JNIEXPORT void JNICALL
  111. Java_org_zoy_caca_Display_setDisplayTitle(JNIEnv *env, jclass cls, jlong ptr, jstring title)
  112. {
  113. const char *title_chars = (*env)->GetStringUTFChars(env, title, 0);
  114. caca_set_display_title((caca_display_t *)ptr, title_chars);
  115. (*env)->ReleaseStringUTFChars(env, title, title_chars);
  116. }
  117. JNIEXPORT void JNICALL
  118. Java_org_zoy_caca_Display_setDisplayMouse(JNIEnv *env, jclass cls, jlong ptr, jboolean st)
  119. {
  120. caca_set_mouse((caca_display_t *)ptr, st);
  121. }
  122. JNIEXPORT void JNICALL
  123. Java_org_zoy_caca_Display_setDisplayCursor(JNIEnv *env, jclass cls, jlong ptr, jboolean st)
  124. {
  125. caca_set_cursor((caca_display_t *)ptr, st);
  126. }
  127. JNIEXPORT jlong JNICALL
  128. Java_org_zoy_caca_Display_getDisplayEvent(JNIEnv *env, jclass cls, jlong ptr, jint mask, jint timeout)
  129. {
  130. caca_event_t *ev = malloc(sizeof(caca_event_t));
  131. if (caca_get_event((caca_display_t *)ptr, mask, ev, timeout))
  132. {
  133. return (jlong) ev;
  134. }
  135. free(ev);
  136. (*env)->ThrowNew(env, (*env)->FindClass(env, "org/zoy/caca/TimeoutException"), "No event received");
  137. return -1;
  138. }
  139. JNIEXPORT jint JNICALL
  140. Java_org_zoy_caca_Display_getDisplayMouseX(JNIEnv *env, jclass cls, jlong ptr)
  141. {
  142. return caca_get_mouse_x((caca_display_t *)ptr);
  143. }
  144. JNIEXPORT jint JNICALL
  145. Java_org_zoy_caca_Display_getDisplayMouseY(JNIEnv *env, jclass cls, jlong ptr)
  146. {
  147. return caca_get_mouse_y((caca_display_t *)ptr);
  148. }
  149. JNIEXPORT void JNICALL
  150. Java_org_zoy_caca_Display_freeDisplay(JNIEnv *env, jclass cls, jlong ptr)
  151. {
  152. caca_free_display((caca_display_t *)ptr);
  153. }