Commit b2a6f0d263792e07e0f61353225b9cf8153c4ecc

Authored by perry.werneck@gmail.com
1 parent 7b076f4e

Plugin remotectl passa a mudar o id da sessao de acordo com o nome da pipe (:A p…

…ara a primeira instancia, :B para a segunda, ate :Z para a ultima)
src/include/pw3270.h
... ... @@ -61,6 +61,9 @@
61 61 LIB3270_EXPORT void pw3270_save_window_size(GtkWidget *widget, const gchar *name);
62 62 LIB3270_EXPORT void pw3270_restore_window(GtkWidget *widget, const gchar *name);
63 63  
  64 + LIB3270_EXPORT const gchar * pw3270_get_session_name(GtkWidget *widget);
  65 + LIB3270_EXPORT void pw3270_set_session_name(GtkWidget *widget, const gchar *name);
  66 +
64 67 #ifdef HAVE_GTKMAC
65 68 #include <gtk-mac-bundle.h>
66 69 LIB3270_EXPORT GtkMacBundle * pw3270_get_bundle(void);
... ...
src/lib3270/screen.c
... ... @@ -418,8 +418,11 @@ int cursor_move(H3270 *h, int baddr)
418 418 if(ret == baddr)
419 419 return ret;
420 420  
421   - h->cursor_addr = baddr;
422   - h->update_cursor(h,(unsigned short) (baddr/h->cols),(unsigned short) (baddr%h->cols),h->text[baddr].chr,h->text[baddr].attr);
  421 + if(baddr >= 0)
  422 + {
  423 + h->cursor_addr = baddr;
  424 + h->update_cursor(h,(unsigned short) (baddr/h->cols),(unsigned short) (baddr%h->cols),h->text[baddr].chr,h->text[baddr].attr);
  425 + }
423 426  
424 427 return ret;
425 428 }
... ...
src/plugins/remotectl/hllapi.c
... ... @@ -35,38 +35,27 @@
35 35 #include <stdio.h>
36 36 #include <lib3270/log.h>
37 37  
  38 +/*--[ Globals ]--------------------------------------------------------------------------------------*/
  39 +
  40 + static char *session_name = NULL;
  41 +
38 42 /*--[ Implement ]------------------------------------------------------------------------------------*/
39 43  
40   - LIB3270_EXPORT int hllapi(unsigned long func, char *string, unsigned short length, unsigned short *rc)
  44 + static int run_query(unsigned long func, char *string, unsigned short length, unsigned short *rc)
41 45 {
42 46 int result = -1;
43 47  
44 48 #ifdef WIN32
45   -
46   - static DWORD dwMode = PIPE_READMODE_MESSAGE;
47   - static const LPTSTR lpszPipeName = TEXT( "\\\\.\\pipe\\pw3270" );
48   - HANDLE hPipe = INVALID_HANDLE_VALUE;
  49 + char PipeName[4096];
49 50  
50 51 if(length < 0 && string)
51 52 length = strlen(string);
52 53  
53   - if(!WaitNamedPipe(lpszPipeName,10000))
54   - return ETIMEDOUT;
55   -
56   - hPipe = CreateFile( lpszPipeName, // pipe name
57   - GENERIC_WRITE|GENERIC_READ, // Read/Write access
58   - 0, // no sharing
59   - NULL, // default security attributes
60   - OPEN_EXISTING, // opens existing pipe
61   - 0, // Attributes
62   - NULL); // no template file
63   -
64   - if(hPipe == INVALID_HANDLE_VALUE)
65   - return -1;
  54 + snprintf(PipeName,4095,"\\\\.\\pipe\\%s",session_name);
66 55  
67   - if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL))
  56 + if(!WaitNamedPipe(PipeName,NMPWAIT_USE_DEFAULT_WAIT))
68 57 {
69   - result = -1;
  58 + result = ETIMEDOUT;
70 59 }
71 60 else
72 61 {
... ... @@ -82,18 +71,17 @@
82 71 if(string && length > 0)
83 72 memcpy(data->string,string,length);
84 73  
85   - trace("Sending %d bytes",(int) cbSize);
86   -
87   - if(!TransactNamedPipe(hPipe,(LPVOID) data,cbSize,buffer,HLLAPI_MAXLENGTH,&cbSize,NULL))
  74 + if(!CallNamedPipe(PipeName,(LPVOID)data,cbSize,buffer,HLLAPI_MAXLENGTH,&cbSize,NMPWAIT_USE_DEFAULT_WAIT))
88 75 {
89   - result = -1;
  76 + result = GetLastError();
90 77 }
91 78 else
92 79 {
  80 + int sz = length < buffer->len ? length : buffer->len;
93 81 *rc = buffer->rc;
94 82  
95   - if(string && length > 0)
96   - memcpy(string,buffer->string,length);
  83 + if(string && sz > 0)
  84 + memcpy(string,buffer->string,sz);
97 85  
98 86 result = 0;
99 87 }
... ... @@ -102,8 +90,6 @@
102 90 free(buffer);
103 91 }
104 92  
105   - CloseHandle(hPipe);
106   -
107 93 #else
108 94  
109 95 #error NOT IMPLEMENTED
... ... @@ -112,3 +98,35 @@
112 98  
113 99 return result;
114 100 }
  101 +
  102 + static int set_session_name(const char *name)
  103 + {
  104 + if(!session_name)
  105 + free(session_name);
  106 + session_name = strdup(name);
  107 +
  108 + return 0;
  109 + }
  110 +
  111 + LIB3270_EXPORT int hllapi(unsigned long func, char *str, unsigned short length, unsigned short *rc)
  112 + {
  113 + int result = 1;
  114 + switch(func)
  115 + {
  116 + case HLLAPI_CMD_CONNECTPS:
  117 + result = set_session_name(str);
  118 + break;
  119 +
  120 + default:
  121 + if(!session_name)
  122 + {
  123 + if(set_session_name("pw3270"))
  124 + return ENOENT;
  125 + }
  126 + result = run_query(func, str, length, rc);
  127 + }
  128 +
  129 + return result;
  130 + }
  131 +
  132 +
... ...
src/plugins/remotectl/remotectl.c
... ... @@ -32,6 +32,7 @@
32 32 */
33 33  
34 34 #include "remotectl.h"
  35 + #include <pw3270.h>
35 36 #include <pw3270/plugin.h>
36 37 #include <errno.h>
37 38 #include <string.h>
... ... @@ -76,31 +77,40 @@
76 77 LIB3270_EXPORT int pw3270_plugin_init(GtkWidget *window)
77 78 {
78 79 #ifdef WIN32
79   - static const LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\" PACKAGE_NAME );
80   - HANDLE hPipe;
  80 + char id;
81 81  
82   - trace("\n\n%s\n\n",__FUNCTION__);
83   -
84   - hPipe = CreateNamedPipe( lpszPipename, // pipe name
85   - PIPE_ACCESS_DUPLEX | // read/write access
86   - FILE_FLAG_OVERLAPPED, // overlapped mode
87   - PIPE_TYPE_MESSAGE | // pipe type
88   - PIPE_READMODE_MESSAGE | // pipe mode
89   - PIPE_WAIT, // blocking mode
90   - 1, // number of instances
91   - PIPE_BUFFER_LENGTH, // output buffer size
92   - PIPE_BUFFER_LENGTH, // input buffer size
93   - 0, // client time-out
94   - NULL); // default security attributes
95   -
96   -
97   - if (hPipe == INVALID_HANDLE_VALUE)
  82 + for(id='A';id < 'Z';id++)
98 83 {
99   - popup_lasterror( _( "Can´t create pipe %s" ),lpszPipename);
100   - return -1;
  84 + gchar * pipename = g_strdup_printf("\\\\.\\pipe\\%s%c",pw3270_get_session_name(window),id);
  85 +
  86 + HANDLE hPipe = CreateNamedPipe( TEXT(pipename), // pipe name
  87 + PIPE_ACCESS_DUPLEX | // read/write access
  88 + FILE_FLAG_OVERLAPPED, // overlapped mode
  89 + PIPE_TYPE_MESSAGE | // pipe type
  90 + PIPE_READMODE_MESSAGE | // pipe mode
  91 + PIPE_WAIT, // blocking mode
  92 + 1, // number of instances
  93 + PIPE_BUFFER_LENGTH, // output buffer size
  94 + PIPE_BUFFER_LENGTH, // input buffer size
  95 + 0, // client time-out
  96 + NULL); // default security attributes
  97 +
  98 + g_free(pipename);
  99 +
  100 + if(hPipe != INVALID_HANDLE_VALUE)
  101 + {
  102 + gchar *session = g_strdup_printf("%s:%c",pw3270_get_session_name(window),id);
  103 + pw3270_set_session_name(window,session);
  104 + g_free(session);
  105 +
  106 + init_source_pipe(hPipe);
  107 + return 0;
  108 + }
  109 +
101 110 }
102 111  
103   - init_source_pipe(hPipe);
  112 + popup_lasterror( "%s", _( "Can´t create remote control pipe" ));
  113 + return -1;
104 114  
105 115 #else
106 116  
... ... @@ -129,6 +139,17 @@
129 139 return 0;
130 140 }
131 141  
  142 + static int cmd_setcursor(unsigned short rc, char *string, unsigned short length)
  143 + {
  144 + H3270 *hSession = lib3270_get_default_session_handle();
  145 +
  146 + if(!lib3270_connected(hSession))
  147 + return ENOTCONN;
  148 +
  149 + lib3270_set_cursor_address(hSession,(int) rc);
  150 + return 0;
  151 + }
  152 +
132 153 int run_hllapi(unsigned long function, char *string, unsigned short length, unsigned short rc)
133 154 {
134 155 static const struct _cmd
... ... @@ -137,7 +158,8 @@
137 158 int (*exec)(unsigned short rc, char *string, unsigned short length);
138 159 } cmd[] =
139 160 {
140   - { HLLAPI_CMD_GETREVISION, cmd_getrevision }
  161 + { HLLAPI_CMD_SETCURSOR, cmd_setcursor },
  162 + { HLLAPI_CMD_GETREVISION, cmd_getrevision }
141 163 };
142 164 int f;
143 165  
... ...
src/plugins/remotectl/testprogram.c
... ... @@ -38,6 +38,10 @@
38 38 char buffer[1024];
39 39 unsigned short rc;
40 40  
  41 + // Set session name
  42 + strcpy(buffer,"pw3270");
  43 + printf("ConnectPS exits with %d\n[%s]\n",hllapi(HLLAPI_CMD_CONNECTPS,buffer,1024,&rc),buffer);
  44 +
41 45 // Test for GetRevision call
42 46 *buffer = 0;
43 47 printf("GetRevision exits with %d\n[%s]\n",hllapi(HLLAPI_CMD_GETREVISION,buffer,1024,&rc),buffer);
... ...
src/pw3270/main.c
... ... @@ -122,11 +122,12 @@ static gboolean appname(const gchar *option_name, const gchar *value, gpointer d
122 122 int main(int argc, char *argv[])
123 123 {
124 124 #if defined( WIN32 )
125   - static const gchar * appname = PACKAGE_NAME;
  125 + static const gchar * appname = PACKAGE_NAME;
126 126 #endif // WIN32
127 127  
128   - static const gchar * host = NULL;
129   - int rc = 0;
  128 + static const gchar * session_name = PACKAGE_NAME;
  129 + static const gchar * host = NULL;
  130 + int rc = 0;
130 131  
131 132 #if ! GLIB_CHECK_VERSION(2,32,0)
132 133 g_thread_init(NULL);
... ... @@ -183,17 +184,19 @@ int main(int argc, char *argv[])
183 184 static const GOptionEntry app_options[] =
184 185 {
185 186 #if ! defined( WIN32 )
186   - { "appname", 'a', 0, G_OPTION_ARG_CALLBACK, appname, N_( "Application name" ), PACKAGE_NAME },
  187 + { "appname", 'a', 0, G_OPTION_ARG_CALLBACK, appname, N_( "Application name" ), PACKAGE_NAME },
187 188 #else
188   - { "appname", 'a', 0, G_OPTION_ARG_STRING, &appname, N_( "Application name" ), PACKAGE_NAME },
  189 + { "appname", 'a', 0, G_OPTION_ARG_STRING, &appname, N_( "Application name" ), PACKAGE_NAME },
189 190 #endif // WIN32
190   - { "host", 'h', 0, G_OPTION_ARG_STRING, &host, N_( "Host to connect"), NULL },
  191 + { "session", 's', 0, G_OPTION_ARG_STRING, &session_name, N_( "Session name" ), PACKAGE_NAME },
  192 + { "host", 'h', 0, G_OPTION_ARG_STRING, &host, N_( "Host to connect"), NULL },
191 193 { NULL }
192 194 };
193 195  
194 196 GOptionContext * options = g_option_context_new (_("- 3270 Emulator for Gtk"));
195 197 GError * error = NULL;
196 198  
  199 +
197 200 g_option_context_add_main_entries(options, app_options, NULL);
198 201  
199 202 if(!g_option_context_parse( options, &argc, &argv, &error ))
... ... @@ -241,6 +244,7 @@ int main(int argc, char *argv[])
241 244 }
242 245  
243 246 toplevel = pw3270_new(host);
  247 + pw3270_set_session_name(toplevel,session_name);
244 248  
245 249 toplevel_setup(GTK_WINDOW(toplevel));
246 250  
... ...
src/pw3270/window.c
... ... @@ -216,6 +216,20 @@
216 216 return v3270_get_session(GTK_PW3270(widget)->terminal);
217 217 }
218 218  
  219 + const gchar * pw3270_get_session_name(GtkWidget *widget)
  220 + {
  221 + if(widget && GTK_IS_PW3270(widget))
  222 + return v3270_get_session_name(GTK_PW3270(widget)->terminal);
  223 + return g_get_application_name();
  224 + }
  225 +
  226 + LIB3270_EXPORT void pw3270_set_session_name(GtkWidget *widget, const gchar *name)
  227 + {
  228 + g_return_if_fail(GTK_IS_PW3270(widget));
  229 + v3270_set_session_name(GTK_PW3270(widget)->terminal,name);
  230 + gtk_window_set_title(GTK_WINDOW(widget),name);
  231 + }
  232 +
219 233 GtkWidget * pw3270_get_terminal_widget(GtkWidget *widget)
220 234 {
221 235 g_return_val_if_fail(GTK_IS_PW3270(widget),NULL);
... ...