source: libcaca/trunk/java/org_zoy_caca_Display.c @ 4320

Last change on this file since 4320 was 4197, checked in by Sam Hocevar, 10 years ago

Java bindings, courtesy of Adrien Grand <jpountz@dinauz.org>.

File size: 5.4 KB
Line 
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
14#include "caca_java_common.h"
15#include "org_zoy_caca_Display.h"
16
17#include <stdlib.h>
18#include "caca.h"
19
20#define DISPLAY_CREATION_ERROR "Cannot create display"
21
22JNIEXPORT jlong JNICALL
23Java_org_zoy_caca_Display_createDisplayAndCanvas(JNIEnv *env, jclass cls)
24{
25  caca_display_t *display = caca_create_display(NULL);
26
27  if(display == NULL) {
28    THROW_EX(DISPLAY_CREATION_ERROR);
29    return 0;
30  }
31
32  return (jlong)display;
33}
34
35JNIEXPORT jlong JNICALL
36Java_org_zoy_caca_Display_createDisplay(JNIEnv *env, jclass cls, jlong canvas_ptr)
37{
38  caca_display_t *display = caca_create_display((caca_canvas_t *)canvas_ptr);
39
40  if(display == NULL) {
41    THROW_EX(DISPLAY_CREATION_ERROR);
42    return 0;
43  }
44
45  return (jlong)display;
46}
47
48JNIEXPORT jlong JNICALL
49Java_org_zoy_caca_Display_createDisplayAndCanvasWithDriver(JNIEnv *env, jclass cls,
50                                                           jstring driver_name)
51{
52  const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
53  caca_display_t *ret = caca_create_display_with_driver(NULL, driver_name_chars);
54  (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
55
56  if(ret == NULL) {
57    THROW_EX(DISPLAY_CREATION_ERROR);
58    return 0;
59  }
60
61  return (jlong)ret;
62}
63
64JNIEXPORT jlong JNICALL
65Java_org_zoy_caca_Display_createDisplayWithDriver(JNIEnv *env, jclass cls,
66                                                  jlong canvas_ptr, jstring driver_name)
67{
68  const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
69  caca_display_t *ret = caca_create_display_with_driver((caca_canvas_t *)canvas_ptr, driver_name_chars);
70  (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
71
72  if(ret == NULL) {
73    THROW_EX(DISPLAY_CREATION_ERROR);
74    return 0;
75  }
76
77  return (jlong)ret;
78}
79
80JNIEXPORT jobjectArray JNICALL
81Java_org_zoy_caca_Display_getDisplayDriverList(JNIEnv *env, jclass cls)
82{
83  const char *const *drivers = caca_get_display_driver_list();
84  return caca_java_to_string_array(env, drivers);
85}
86
87JNIEXPORT void JNICALL
88Java_org_zoy_caca_Display_setDisplayDriver(JNIEnv *env, jclass cls, jlong ptr, jstring driver_name)
89{
90  const char *driver_name_chars = (*env)->GetStringUTFChars(env, driver_name, 0);
91  caca_set_display_driver((caca_display_t *)ptr, driver_name_chars);
92  (*env)->ReleaseStringUTFChars(env, driver_name, driver_name_chars);
93}
94
95JNIEXPORT jstring JNICALL
96Java_org_zoy_caca_Display_getDisplayDriver(JNIEnv *env, jclass cls, jlong ptr)
97{
98  return (*env)->NewStringUTF(env, caca_get_display_driver((caca_display_t *)ptr));
99}
100
101JNIEXPORT jlong JNICALL
102Java_org_zoy_caca_Display_getDisplayCanvas(JNIEnv *env, jclass cls, jlong ptr)
103{
104  return (jlong)caca_get_canvas((caca_display_t *)ptr);
105}
106
107JNIEXPORT void JNICALL
108Java_org_zoy_caca_Display_displayRefresh(JNIEnv *env, jclass cls, jlong ptr)
109{
110  caca_refresh_display((caca_display_t *)ptr);
111}
112
113JNIEXPORT void JNICALL
114Java_org_zoy_caca_Display_setDisplayTime(JNIEnv *env, jclass cls, jlong ptr, jint time)
115{
116  caca_set_display_time((caca_display_t *)ptr, time);
117}
118
119JNIEXPORT jint JNICALL
120Java_org_zoy_caca_Display_getDisplayTime(JNIEnv *env, jclass cls, jlong ptr)
121{
122  return caca_get_display_time((caca_display_t *)ptr);
123}
124
125JNIEXPORT jint JNICALL
126Java_org_zoy_caca_Display_getDisplayWidth(JNIEnv *env, jclass cls, jlong ptr)
127{
128  return caca_get_display_width((caca_display_t *)ptr);
129}
130
131JNIEXPORT jint JNICALL
132Java_org_zoy_caca_Display_getDisplayHeight(JNIEnv *env, jclass cls, jlong ptr)
133{
134  return caca_get_display_height((caca_display_t *)ptr);
135}
136
137JNIEXPORT void JNICALL
138Java_org_zoy_caca_Display_setDisplayTitle(JNIEnv *env, jclass cls, jlong ptr, jstring title)
139{
140  const char *title_chars = (*env)->GetStringUTFChars(env, title, 0);
141  caca_set_display_title((caca_display_t *)ptr, title_chars);
142  (*env)->ReleaseStringUTFChars(env, title, title_chars);
143}
144
145JNIEXPORT void JNICALL
146Java_org_zoy_caca_Display_setDisplayMouse(JNIEnv *env, jclass cls, jlong ptr, jboolean st)
147{
148  caca_set_mouse((caca_display_t *)ptr, st);
149}
150
151JNIEXPORT void JNICALL
152Java_org_zoy_caca_Display_setDisplayCursor(JNIEnv *env, jclass cls, jlong ptr, jboolean st)
153{
154  caca_set_cursor((caca_display_t *)ptr, st);
155}
156
157JNIEXPORT jlong JNICALL
158Java_org_zoy_caca_Display_getDisplayEvent(JNIEnv *env, jclass cls, jlong ptr, jint mask, jint timeout)
159{
160  caca_event_t *ev = malloc(sizeof(caca_event_t));
161  if (caca_get_event((caca_display_t *)ptr, mask, ev, timeout))
162  {
163    return (jlong) ev;
164  }
165  free(ev);
166  (*env)->ThrowNew(env, (*env)->FindClass(env, "org/zoy/caca/TimeoutException"), "No event received");
167  return -1;
168}
169
170JNIEXPORT jint JNICALL
171Java_org_zoy_caca_Display_getDisplayMouseX(JNIEnv *env, jclass cls, jlong ptr)
172{
173  return caca_get_mouse_x((caca_display_t *)ptr);
174}
175
176JNIEXPORT jint JNICALL
177Java_org_zoy_caca_Display_getDisplayMouseY(JNIEnv *env, jclass cls, jlong ptr)
178{
179  return caca_get_mouse_y((caca_display_t *)ptr);
180}
181
182JNIEXPORT void JNICALL
183Java_org_zoy_caca_Display_freeDisplay(JNIEnv *env, jclass cls, jlong ptr)
184{
185  caca_free_display((caca_display_t *)ptr);
186}
187
Note: See TracBrowser for help on using the repository browser.