[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to launch Octave with its GUI ?
From: |
John Swensen |
Subject: |
Re: How to launch Octave with its GUI ? |
Date: |
Thu, 02 Aug 2007 00:50:42 -0400 |
User-agent: |
Thunderbird 2.0.0.5 (Macintosh/20070716) |
Shai Ayal wrote:
On 8/2/07, John Swensen <address@hidden> wrote:
See my patch from
http://www.nabble.com/Proposed-patch-for-threadsafe-access-to-octave-internal-data-tf4080964.html
No one ever commented on it, so for the time being I am just going to
put it in my program and register for the octave_rl_event_hook()
callback to determine when octave is idle at the prompt.
I have a feeling that the rl_event_hook is going to be heavily used in
the near future by several in-the-works GUI components. I suggest we
implement a mechanism which will allow several hooks to be registered
at once. I will try to implement this, but it will take me sometime,
so if anyone can do it faster, please do.
Shai
Here is a patch for allowing multiple "users" to register with the
rl_event_hook. I basically made a toplevel event_hook function that
resides in oct-rl-edit.cc and in turn calls all other registered
event_hook functions in the order they were registered. I added the
functions
octave_rl_add_event_hook
octave_rl_remove_event_hook
octave_rl_get_event_hook_list
The part I am not 100% sure about is the changes I made to cmd-edit.cc.
They work fine for what I am doing, but I am not sure how these are used
elsewhere. I simply made the do_set_event_hook() function call the
octave_rl_add_event_hook() function and the do_restore_event_hook() call
octave_rl_remove_event_hook() with the most recently added event_hook.
Probably not the best method, but am open to suggestions from someone
who has a better understanding of the cmd-edit.cc file and the
gnu_readline class.
John Swensen
Index: liboctave/cmd-edit.cc
===================================================================
RCS file: /cvs/octave/liboctave/cmd-edit.cc,v
retrieving revision 1.31
diff -u -r1.31 cmd-edit.cc
--- liboctave/cmd-edit.cc 30 Jun 2006 18:19:42 -0000 1.31
+++ liboctave/cmd-edit.cc 2 Aug 2007 04:42:06 -0000
@@ -404,20 +404,21 @@
gnu_readline::do_restore_startup_hook (void)
{
::octave_rl_set_startup_hook (previous_startup_hook);
+
}
void
gnu_readline::do_set_event_hook (event_hook_fcn f)
{
- previous_event_hook = octave_rl_get_event_hook ();
-
- ::octave_rl_set_event_hook (f);
+ previous_event_hook = f; //octave_rl_get_event_hook ();
+ ::octave_rl_add_event_hook (f);
}
void
gnu_readline::do_restore_event_hook (void)
{
- ::octave_rl_set_event_hook (previous_event_hook);
+ //::octave_rl_add_event_hook (previous_event_hook);
+ ::octave_rl_remove_event_hook( previous_event_hook );
}
void
Index: liboctave/oct-rl-edit.c
===================================================================
RCS file: /cvs/octave/liboctave/oct-rl-edit.c,v
retrieving revision 1.20
diff -u -r1.20 oct-rl-edit.c
--- liboctave/oct-rl-edit.c 24 Apr 2006 19:13:08 -0000 1.20
+++ liboctave/oct-rl-edit.c 2 Aug 2007 04:42:07 -0000
@@ -34,6 +34,24 @@
#include "oct-rl-edit.h"
+#define MAX_RL_EVENT_HOOK_COUNT 256
+
+/**
+ * Variables containing the number of registered readline event hooks.
+ */
+int rl_event_hook_list_size = 0;
+
+/**
+ * A list of registered readline event hooks.
+ */
+rl_event_hook_fcn_ptr rl_event_hook_list[MAX_RL_EVENT_HOOK_COUNT];
+
+/**
+ * Mutex for providing threadsafe access to the list of readline
+ * event hooks.
+ */
+pthread_mutex_t event_hook_list_mutex = 0;
+
#define OCTAVE_RL_SAVE_STRING(ss, s) \
static char *ss = 0; \
\
@@ -252,7 +270,9 @@
void
octave_rl_set_event_hook (rl_event_hook_fcn_ptr f)
-{
+{
+ pthread_mutex_destroy(&event_hook_list_mutex);
+ pthread_mutex_init(&event_hook_list_mutex, NULL);
rl_event_hook = f;
}
@@ -262,6 +282,75 @@
return rl_event_hook;
}
+int
+octave_rl_event_hook( void )
+{
+ pthread_mutex_lock( &event_hook_list_mutex );
+
+ // Iterate through the registered event hook functions in the order they were
+ // received.
+ int i = 0;
+ for( i = 0 ; i < rl_event_hook_list_size ; i++ )
+ {
+ rl_event_hook_list[i]();
+ }
+
+ pthread_mutex_unlock( &event_hook_list_mutex );
+
+ return 0;
+}
+
+void
+octave_rl_add_event_hook (rl_event_hook_fcn_ptr f)
+{
+ pthread_mutex_lock( &event_hook_list_mutex );
+
+ if( rl_event_hook_list_size > MAX_RL_EVENT_HOOK_COUNT )
+ {
+ printf( "The maximum number of readline event hooks is already in use.\n"
);
+ }
+ else
+ {
+ rl_event_hook_list[rl_event_hook_list_size++] = f ;
+ }
+
+ pthread_mutex_unlock( &event_hook_list_mutex );
+}
+
+void
+octave_rl_remove_event_hook( rl_event_hook_fcn_ptr f )
+{
+ pthread_mutex_lock( &event_hook_list_mutex );
+
+ // Iterate through the registered event hook functions in the order they were
+ // received, find the matching event_hook, remove it, and shift all the
remaining
+ // entries up
+ int i = 0;
+ int shiftEntries = 0;
+ for( i = 0 ; i < rl_event_hook_list_size ; i++ )
+ {
+ if( rl_event_hook_list[i] == f )
+ {
+ shiftEntries = 1;
+ }
+
+ if( shiftEntries && i < (rl_event_hook_list_size-1) )
+ {
+ rl_event_hook_list[i] = rl_event_hook_list[i+1];
+ }
+ }
+ if( shiftEntries )
+ rl_event_hook_list_size--;
+
+ pthread_mutex_unlock( &event_hook_list_mutex );
+}
+
+rl_event_hook_fcn_ptr*
+octave_rl_get_event_hook_list (void)
+{
+ return &rl_event_hook_list[0];
+}
+
char **
octave_rl_completion_matches (const char *text, rl_completer_fcn_ptr f)
{
Index: liboctave/oct-rl-edit.h
===================================================================
RCS file: /cvs/octave/liboctave/oct-rl-edit.h,v
retrieving revision 1.7
diff -u -r1.7 oct-rl-edit.h
--- liboctave/oct-rl-edit.h 26 Apr 2005 19:24:29 -0000 1.7
+++ liboctave/oct-rl-edit.h 2 Aug 2007 04:42:07 -0000
@@ -96,6 +96,14 @@
extern rl_event_hook_fcn_ptr octave_rl_get_event_hook (void);
+extern int octave_rl_event_hook( void );
+
+extern void octave_rl_add_event_hook( rl_event_hook_fcn_ptr f );
+
+extern void octave_rl_remove_event_hook( rl_event_hook_fcn_ptr f );
+
+extern rl_event_hook_fcn_ptr* octave_rl_get_event_hook_list(void);
+
extern char **
octave_rl_completion_matches (const char *, rl_completer_fcn_ptr);
Index: src/toplev.cc
===================================================================
RCS file: /cvs/octave/src/toplev.cc,v
retrieving revision 1.200
diff -u -r1.200 toplev.cc
--- src/toplev.cc 31 May 2007 20:23:45 -0000 1.200
+++ src/toplev.cc 2 Aug 2007 04:42:10 -0000
@@ -65,9 +65,10 @@
#include "parse.h"
#include "pathsearch.h"
#include "procstream.h"
+#include "oct-rl-edit.h"
#include "ov.h"
#include "pt-jump.h"
#include "pt-stmt.h"
#include "sighandlers.h"
#include "sysdep.h"
#include "syswait.h"
@@ -203,6 +205,7 @@
octave_initialized = true;
// The big loop.
+ octave_rl_set_event_hook( &octave_rl_event_hook );
int retval = 0;
do