Commit 88883384efe7d45355b85d670d5ac397510ca139

Authored by perry.werneck@gmail.com
1 parent 8223b6f7

Implementando callbacks para ativação de temporizadores através da API android

android/jni/main.cpp
@@ -27,6 +27,9 @@ @@ -27,6 +27,9 @@
27 */ 27 */
28 28
29 #include "globals.h" 29 #include "globals.h"
  30 + #include <stdio.h>
  31 + #include <string.h>
  32 + #include <lib3270/config.h>
30 #include <lib3270/popup.h> 33 #include <lib3270/popup.h>
31 #include <lib3270/internals.h> 34 #include <lib3270/internals.h>
32 35
@@ -126,22 +129,24 @@ static int write_buffer(H3270 *session, unsigned const char *buf, int len) @@ -126,22 +129,24 @@ static int write_buffer(H3270 *session, unsigned const char *buf, int len)
126 return rc; 129 return rc;
127 } 130 }
128 131
129 -static void * add_timeout(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session)) 132 +static void * add_timer(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session))
130 { 133 {
  134 + TIMER * timer = NULL;
  135 +
131 if(session->widget) 136 if(session->widget)
132 { 137 {
133 JNIEnv * env = ((INFO *) session->widget)->env; 138 JNIEnv * env = ((INFO *) session->widget)->env;
134 jobject obj = ((INFO *) session->widget)->obj; 139 jobject obj = ((INFO *) session->widget)->obj;
135 jclass cls = env->GetObjectClass(obj); 140 jclass cls = env->GetObjectClass(obj);
136 jmethodID mid = env->GetMethodID(cls, "newTimer", "(JI)V"); 141 jmethodID mid = env->GetMethodID(cls, "newTimer", "(JI)V");
137 - TIMER * timer = (TIMER *) lib3270_malloc(sizeof(TIMER));  
138 142
  143 + timer = (TIMER *) lib3270_malloc(sizeof(TIMER));
139 timer->sz = sizeof(timer); 144 timer->sz = sizeof(timer);
140 timer->enabled = true; 145 timer->enabled = true;
141 timer->session = session; 146 timer->session = session;
142 timer->proc = proc; 147 timer->proc = proc;
143 148
144 - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Timer ID %08lx created",(unsigned long) timer); 149 + trace("Timer %08lx created",(unsigned long) timer);
145 150
146 env->CallVoidMethod(obj,mid, (jlong) timer, (jint) interval_ms); 151 env->CallVoidMethod(obj,mid, (jlong) timer, (jint) interval_ms);
147 152
@@ -151,19 +156,22 @@ static void * add_timeout(unsigned long interval_ms, H3270 *session, void (*proc @@ -151,19 +156,22 @@ static void * add_timeout(unsigned long interval_ms, H3270 *session, void (*proc
151 __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Can set timer, no jni env for active session"); 156 __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Can set timer, no jni env for active session");
152 } 157 }
153 158
154 - 159 + return timer;
155 } 160 }
156 161
157 -static void remove_timeout(void *timer) 162 +static void remove_timer(void *id)
158 { 163 {
159 - if(timer == NULL || ((TIMER *) timer)->sz != sizeof(TIMER)) 164 + TIMER *timer = (TIMER *) id;
  165 +
  166 + if(timer == NULL)
160 { 167 {
161 __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Invalid timer ID %08lx",(unsigned long) timer); 168 __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Invalid timer ID %08lx",(unsigned long) timer);
162 return; 169 return;
163 } 170 }
164 171
165 - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Removing timeout %d",(int) timer);  
166 - ((TIMER *) timer)->enabled = false; 172 + __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Disabling timer %08lx",(unsigned long) timer);
  173 +
  174 + timer->enabled = false;
167 175
168 } 176 }
169 177
@@ -177,30 +185,71 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_timerFinish(JNIEnv *env, jo @@ -177,30 +185,71 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_timerFinish(JNIEnv *env, jo
177 return; 185 return;
178 } 186 }
179 187
180 - if(timer->sz != sizeof(TIMER))  
181 - {  
182 - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME,  
183 - "Invalid timer ID %08lx in %s (sz=%d, expected=%d)",(unsigned long) timer,  
184 - __FUNCTION__,timer->sz,sizeof(timer));  
185 - return;  
186 - }  
187 -  
188 if(timer->enabled) 188 if(timer->enabled)
  189 + {
  190 + __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Running timer %08lx",(unsigned long) timer);
189 timer->proc(timer->session); 191 timer->proc(timer->session);
  192 + }
190 193
191 lib3270_free(timer); 194 lib3270_free(timer);
192 } 195 }
193 196
  197 +#ifdef X3270_TRACE
  198 +static void tracehandler(H3270 *session, const char *fmt, va_list args)
  199 +{
  200 + static char * buffer = NULL;
  201 + static int szBuffer = 0;
194 202
  203 + char temp[4096];
  204 + size_t sz;
  205 + char * src;
  206 + char * dst;
  207 +
  208 + if(!szBuffer)
  209 + {
  210 + buffer = (char *) lib3270_malloc(szBuffer = 4096);
  211 + *buffer = 0;
  212 + }
  213 +
  214 + sz = vsnprintf(temp,4095,fmt,args);
  215 +
  216 + if( (sz+strlen(buffer)) > szBuffer)
  217 + {
  218 + szBuffer += (sz+strlen(buffer)+1);
  219 + buffer = (char *) lib3270_realloc(buffer,szBuffer);
  220 + }
  221 +
  222 + dst = buffer+strlen(buffer);
  223 + for(src = temp;*src;src++)
  224 + {
  225 + if(*src == '\n')
  226 + {
  227 + *dst = 0;
  228 + __android_log_print(ANDROID_LOG_DEBUG, PACKAGE_NAME, "%s", buffer);
  229 + dst = buffer;
  230 + }
  231 + else
  232 + {
  233 + *(dst++) = *src;
  234 + }
  235 + }
  236 +
  237 + *dst = 0;
  238 +}
  239 +#endif // X3270_TRACE
195 240
196 JNIEXPORT jint JNICALL Java_br_com_bb_pw3270_lib3270_init(JNIEnv *env, jclass obj) 241 JNIEXPORT jint JNICALL Java_br_com_bb_pw3270_lib3270_init(JNIEnv *env, jclass obj)
197 { 242 {
198 H3270 * session = lib3270_session_new(""); 243 H3270 * session = lib3270_session_new("");
199 244
200 - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Initializing session %p",session); 245 + __android_log_print(ANDROID_LOG_DEBUG, PACKAGE_NAME, "Initializing session %p",session);
  246 +
  247 +#ifdef X3270_TRACE
  248 + lib3270_set_trace_handler(tracehandler);
  249 +#endif // X3270_TRACE
201 250
202 lib3270_set_popup_handler(popuphandler); 251 lib3270_set_popup_handler(popuphandler);
203 - lib3270_register_time_handlers(add_timeout,remove_timeout); 252 + lib3270_register_time_handlers(add_timer,remove_timer);
204 253
205 session->write = write_buffer; 254 session->write = write_buffer;
206 session->changed = changed; 255 session->changed = changed;
@@ -264,6 +313,8 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_procRecvdata(JNIEnv *env, j @@ -264,6 +313,8 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_procRecvdata(JNIEnv *env, j
264 313
265 lib3270_data_recv(session, (size_t) sz, netrbuf); 314 lib3270_data_recv(session, (size_t) sz, netrbuf);
266 315
  316 + trace("Liberando %d bytes",(size_t) sz);
  317 +
267 env->ReleaseByteArrayElements(buffer, (signed char *) netrbuf, 0); 318 env->ReleaseByteArrayElements(buffer, (signed char *) netrbuf, 0);
268 319
269 session_release(); 320 session_release();
android/lib3270NDK.cbp
@@ -36,6 +36,7 @@ @@ -36,6 +36,7 @@
36 <Unit filename="../src/include/lib3270/actions.h" /> 36 <Unit filename="../src/include/lib3270/actions.h" />
37 <Unit filename="../src/include/lib3270/html.h" /> 37 <Unit filename="../src/include/lib3270/html.h" />
38 <Unit filename="../src/include/lib3270/internals.h" /> 38 <Unit filename="../src/include/lib3270/internals.h" />
  39 + <Unit filename="../src/include/lib3270/log.h" />
39 <Unit filename="../src/include/lib3270/session.h" /> 40 <Unit filename="../src/include/lib3270/session.h" />
40 <Unit filename="../src/lib3270/html.c"> 41 <Unit filename="../src/lib3270/html.c">
41 <Option compilerVar="CC" /> 42 <Option compilerVar="CC" />
@@ -50,6 +51,9 @@ @@ -50,6 +51,9 @@
50 <Unit filename="../src/lib3270/telnet.c"> 51 <Unit filename="../src/lib3270/telnet.c">
51 <Option compilerVar="CC" /> 52 <Option compilerVar="CC" />
52 </Unit> 53 </Unit>
  54 + <Unit filename="../src/lib3270/trace_ds.c">
  55 + <Option compilerVar="CC" />
  56 + </Unit>
53 <Unit filename="Makefile" /> 57 <Unit filename="Makefile" />
54 <Unit filename="jni/Android.mk" /> 58 <Unit filename="jni/Android.mk" />
55 <Unit filename="jni/actions.cpp" /> 59 <Unit filename="jni/actions.cpp" />
android/res/layout/main.xml
@@ -1,32 +0,0 @@ @@ -1,32 +0,0 @@
1 -<?xml version="1.0" encoding="utf-8"?>  
2 -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3 - android:layout_width="fill_parent"  
4 - android:layout_height="fill_parent"  
5 - android:orientation="vertical" >  
6 -  
7 - <LinearLayout  
8 - android:layout_width="match_parent"  
9 - android:layout_height="wrap_content" >  
10 -  
11 - <EditText  
12 - android:id="@+id/hostname"  
13 - android:layout_width="0dip"  
14 - android:layout_height="match_parent"  
15 - android:layout_weight="1"  
16 - android:ems="10" />  
17 -  
18 - <Button  
19 - android:id="@+id/connect"  
20 - style="?android:attr/buttonStyleSmall"  
21 - android:layout_width="80dp"  
22 - android:layout_height="match_parent"  
23 - android:text="@string/connect" />  
24 -  
25 - </LinearLayout>  
26 -  
27 - <WebView  
28 - android:id="@+id/view"  
29 - android:layout_width="match_parent"  
30 - android:layout_height="match_parent" />  
31 -  
32 -</LinearLayout>  
33 \ No newline at end of file 0 \ No newline at end of file
android/src/br/com/bb/pw3270/PW3270Activity.java
@@ -13,7 +13,7 @@ import android.webkit.WebView; @@ -13,7 +13,7 @@ import android.webkit.WebView;
13 import android.webkit.WebViewClient; 13 import android.webkit.WebViewClient;
14 import android.webkit.WebResourceResponse; 14 import android.webkit.WebResourceResponse;
15 import android.webkit.WebChromeClient; 15 import android.webkit.WebChromeClient;
16 -import java.io.InputStream; 16 +// import java.io.InputStream;
17 17
18 // import android.app.Dialog; 18 // import android.app.Dialog;
19 19
android/src/br/com/bb/pw3270/lib3270.java
@@ -10,7 +10,7 @@ import java.net.Socket; @@ -10,7 +10,7 @@ import java.net.Socket;
10 import javax.net.ssl.SSLSocketFactory; 10 import javax.net.ssl.SSLSocketFactory;
11 import java.io.DataInputStream; 11 import java.io.DataInputStream;
12 import java.io.DataOutputStream; 12 import java.io.DataOutputStream;
13 -import java.util.Vector; 13 +// import java.util.Vector;
14 14
15 public class lib3270 15 public class lib3270
16 { 16 {
src/include/lib3270/config.h.in
@@ -54,10 +54,11 @@ @@ -54,10 +54,11 @@
54 54
55 #ifndef ANDROID 55 #ifndef ANDROID
56 #undef HAVE_LIBSSL 56 #undef HAVE_LIBSSL
57 - #define X3270_TRACE  
58 #define X3270_FT 57 #define X3270_FT
59 #endif // ANDROID 58 #endif // ANDROID
60 59
  60 + #define X3270_TRACE
  61 +
61 /* #define X3270_PRINTER */ 62 /* #define X3270_PRINTER */
62 63
63 #undef HAVE_IGEMAC 64 #undef HAVE_IGEMAC
src/lib3270/iocalls.c
@@ -533,12 +533,16 @@ static void internal_ring_bell(H3270 *session) @@ -533,12 +533,16 @@ static void internal_ring_bell(H3270 *session)
533 533
534 void * AddTimeOut(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session)) 534 void * AddTimeOut(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session))
535 { 535 {
  536 + void *timer;
536 CHECK_SESSION_HANDLE(session); 537 CHECK_SESSION_HANDLE(session);
537 - return add_timeout(interval_ms,session,proc); 538 + timer = add_timeout(interval_ms,session,proc);
  539 + trace("Timeout %p created with %ld ms",timer,interval_ms);
  540 + return timer;
538 } 541 }
539 542
540 void RemoveTimeOut(void * timer) 543 void RemoveTimeOut(void * timer)
541 { 544 {
  545 + trace("Removing timeout %p",timer);
542 return remove_timeout(timer); 546 return remove_timeout(timer);
543 } 547 }
544 548
src/lib3270/sources.mak
@@ -27,12 +27,12 @@ @@ -27,12 +27,12 @@
27 # Terminal only sources 27 # Terminal only sources
28 TERMINAL_SOURCES = bounds.c ctlr.c util.c toggles.c screen.c selection.c kybd.c telnet.c \ 28 TERMINAL_SOURCES = bounds.c ctlr.c util.c toggles.c screen.c selection.c kybd.c telnet.c \
29 host.c sf.c ansi.c resolver.c tables.c utf8.c charset.c \ 29 host.c sf.c ansi.c resolver.c tables.c utf8.c charset.c \
30 - version.c session.c state.c html.c 30 + version.c session.c state.c html.c trace_ds.c see.c
31 31
32 # Network I/O Sources 32 # Network I/O Sources
33 NETWORK_SOURCES = iocalls.c proxy.c 33 NETWORK_SOURCES = iocalls.c proxy.c
34 34
35 # Full library sources 35 # Full library sources
36 SOURCES = $(TERMINAL_SOURCES) $(NETWORK_SOURCES) ft.c ft_cut.c ft_dft.c glue.c resources.c \ 36 SOURCES = $(TERMINAL_SOURCES) $(NETWORK_SOURCES) ft.c ft_cut.c ft_dft.c glue.c resources.c \
37 - rpq.c see.c trace_ds.c paste.c macros.c fallbacks.c log.c 37 + rpq.c paste.c macros.c fallbacks.c log.c
38 38