/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: dam/src/PluginManager.cc
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */
#include <dlfcn.h>
#include "PluginManager.hh"

using namespace DAM;

Utility::Logger <DefaultLogging> DAM::PluginManagerLog (DAM::Log);
std::map <PluginId__, Plugin*> DAM::PluginRegistry;

DAM::DlException::DlException ()
{
  error = dlerror ();
  Q_WARN(PluginManagerLog) << "dl error: " << error << endl;
};

void DAM::Register (PluginId id, std::string so_file)
{
  if (PluginRegistry.find (id))
    Q_WARN(PluginManagerLog) << "duplicate registration of " << id << std::endl;

  Plugin::dl_t handle = dlopen (so_file.c_str (), RTLD_NOW);
  if (!handle)
    throw DlException ();
  
  void (DAM_InitPlugin*)() = dlsym (handle, "DAM_InitPlugin");
  if (!handle)
    throw DlException ();


  Q_LOG(PluginManagerLog) << "calling " << so_file << "::DAM_InitPlugin ()";

  void* error = DAM_InitPlugin ();
  if (error)
    throw PluginConflict ();

  Q_LOG(PluginManagerLog) << so_file " loaded successfully" << endl;


};
