summaryrefslogtreecommitdiffstats
path: root/thk04/plugger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'thk04/plugger.cpp')
-rw-r--r--thk04/plugger.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/thk04/plugger.cpp b/thk04/plugger.cpp
new file mode 100644
index 0000000..4ad30a4
--- /dev/null
+++ b/thk04/plugger.cpp
@@ -0,0 +1,155 @@
+/* FreeJ
+ * (c) Copyright 2001-2002 Denis Rojo aka jaromil <jaromil@dyne.org>
+ *
+ * This source code is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Please refer to the GNU Public License for more details.
+ *
+ * You should have received a copy of the GNU Public License along with
+ * this source code; if not, write to:
+ * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * "$Id: plugger.cpp,v 1.14 2003/12/29 23:12:10 jaromil Exp $"
+ *
+ */
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <jutils.h>
+#include <plugger.h>
+#include <config.h>
+
+Plugger::Plugger() {
+ char temp[256];
+
+ _searchpath = NULL;
+
+ sprintf(temp,"%s",DATADIR);
+ _addsearchdir(temp);
+ _addsearchdir("./");
+ // _addsearchdir("/usr/lib/freej");
+ // _addsearchdir("/usr/local/lib/freej");
+ for(int i=0;i<MAX_PLUGINS;i++) plugs[i] = NULL;
+}
+
+Plugger::~Plugger() {
+ func("Plugger::~Plugger()");
+ _delete();
+}
+
+Filter *Plugger::pick(char *name) {
+ for(int c=0;c<MAX_PLUGINS;c++)
+ if(plugs[c]) {
+ if( strcasecmp( plugs[c]->getname() , name ) ==0 )
+ return plugs[c];
+ } else break;
+ return(NULL);
+}
+
+int selector(const struct dirent *dir) {
+ // if(strstr(dir->d_name,".")) return(1);
+ // if(dir->d_name[0]=='.') return(0);
+ // if(strstr(dir->d_name,".la")) return(0);
+#ifdef HAVE_DARWIN
+ if(strstr(dir->d_name,"-darwin.so")) return(1);
+#else
+ if(strstr(dir->d_name,"-linux.so")) return(1);
+ if(strstr(dir->d_name,".so")) return(1);
+#endif
+ return(0);
+}
+
+/* searches into the lt_searchpath for valid modules */
+int Plugger::refresh() {
+ char *dir;
+ struct dirent **filelist;
+ int found;
+ char *path = _getsearchpath();
+ _delete();
+
+ // notice("loading available plugins");
+
+ if(!path) { warning("can't find any valid plugin directory"); return(-1); }
+ dir = strtok(path,":");
+ do {
+ found = scandir(dir,&filelist,selector,alphasort);
+ if(found<0) { error("Plugger::scandir"); return(-1); };
+ /* .so files found, check if they are plugins */
+ while(found--) {
+ char temp[256];
+ snprintf(temp,255,"%s/%s",dir,filelist[found]->d_name);
+ Filter *filt = new Filter;
+ if(filt->open(temp)) {
+ // act("plugged: %s filter v%u by %s",
+ // filt->getname(), filt->getversion(), filt->getauthor());
+ _add_plug(filt);
+ } else delete(filt);
+ // free(filelist[found]);
+ }
+ break;
+ // free(filelist);
+ } while((dir = strtok(NULL,":")));
+
+ return 0;
+}
+
+Filter *Plugger::operator[](const int num) {
+
+ if(!plugs[num]) return(NULL);
+
+ /* if the plugin is allready in use we can't instantiate it
+ from the same DSO shared object */
+ if(plugs[num]->inuse) return(NULL);
+ /* this is handled by the keyboard class
+ plugs[num]->inuse = true; */
+ return(plugs[num]);
+}
+
+int Plugger::_delete() {
+ func("Plugger::_delete");
+ // for(int c=0;c<MAX_PLUGINS;c++) if(plugs[c]) delete(plugs[c]);
+ return 0;
+}
+
+bool Plugger::_add_plug(Filter *f) {
+ for(int c=0;c<MAX_PLUGINS;c++)
+ if(!plugs[c]) {
+ plugs[c]=f;
+ return(true);
+ }
+ return(false);
+}
+
+bool Plugger::_filecheck(const char *file) {
+ bool res = true;
+ FILE *f = fopen(file,"r");
+ if(!f) res = false;
+ fclose(f);
+ return(res);
+}
+
+bool Plugger::_dircheck(const char *dir) {
+ bool res = true;
+ DIR *d = opendir(dir);
+ if(!d) res = false;
+ else closedir(d);
+ return(res);
+}
+
+void Plugger::_addsearchdir(const char *dir) {
+ char temp[1024];
+ if(!_dircheck(dir)) return;
+ if(_searchpath) {
+ snprintf(temp,1024,"%s:%s",_searchpath,dir);
+ jfree(_searchpath);
+ _searchpath = strdup(temp);
+ } else _searchpath = strdup(dir);
+}