diff --git a/lib/InjectedAPI/hookXautoload.php b/lib/InjectedAPI/hookXautoload.php
new file mode 100644
index 0000000..b9fccc6
--- /dev/null
+++ b/lib/InjectedAPI/hookXautoload.php
@@ -0,0 +1,156 @@
+<?php
+
+
+class xautoload_InjectedAPI_hookXautoload {
+
+  protected $finder;
+  protected $extensionDir;
+
+  /**
+   * @param xautoload_ClassFinder $finder
+   *   The class finder.
+   */
+  function __construct($finder) {
+    $this->finder = $finder;
+  }
+
+  /**
+   * Register an additional namespace for this module.
+   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
+   *
+   * @param string $namespace
+   *   The namespace
+   * @param string $psr_0_root_dir
+   *   PSR-0 root dir.
+   *   If $relative is TRUE, this is relative to the current module dir.
+   *   If $relative is FALSE, this is an absolute path.
+   * @param boolean $relative
+   *   Whether or not the path is relative to the current extension dir.
+   */
+  function namespaceRoot($namespace, $psr_0_root_dir = NULL, $relative = TRUE) {
+    $psr_0_root_dir = $this->processDir($psr_0_root_dir, $relative);
+    $this->finder->registerNamespaceRoot($namespace, $psr_0_root_dir);
+  }
+
+  /**
+   * Register an additional namespace for this module.
+   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
+   *
+   * @param string $namespace
+   *   The namespace
+   * @param string $prefix_root_dir
+   *   Prefix root dir.
+   *   If $relative is TRUE, this is relative to the extension module dir.
+   *   If $relative is FALSE, this is an absolute path.
+   * @param boolean $relative
+   *   Whether or not the path is relative to the current extension dir.
+   */
+  function prefixRoot($prefix, $prefix_root_dir = NULL, $relative = TRUE) {
+    $prefix_root_dir = $this->processDir($prefix_root_dir, $relative);
+    $this->finder->registerPrefixRoot($namespace, $prefix_root_dir);
+  }
+
+  /**
+   * Register an additional namespace for this module.
+   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
+   *
+   * @param string $namespace
+   *   The namespace
+   * @param string $psr_0_root_dir
+   *   PSR-0 root dir.
+   *   If $relative is TRUE, this is relative to the current extension dir.
+   *   If $relative is FALSE, this is an absolute path.
+   * @param boolean $relative
+   *   Whether or not the path is relative to the current extension dir.
+   */
+  function namespaceDeep($namespace, $namespace_deep_dir = NULL, $relative = TRUE) {
+    $namespace_deep_dir = $this->processDir($namespace_deep_dir, $relative);
+    $this->finder->registerNamespaceDeep($namespace, $namespace_deep_dir);
+  }
+
+  /**
+   * Register an additional namespace for this module.
+   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
+   *
+   * @param string $namespace
+   *   The namespace
+   * @param string $prefix_deep_dir
+   *   PSR-0 root dir.
+   *   If $relative is TRUE, this is relative to the current extension dir.
+   *   If $relative is FALSE, this is an absolute path.
+   * @param boolean $relative
+   *   Whether or not the path is relative to the current extension dir.   
+   */
+  function prefixDeep($prefix, $prefix_deep_dir = NULL, $relative = TRUE) {
+    $prefix_root_dir = $this->processDir($prefix_deep_dir, $relative);
+    $this->finder->registerPrefixDeep($namespace, $prefix_deep_dir);
+  }
+
+  /**
+   * Register a namespace handler object
+   */
+  function namespaceHandler($namespace, $handler) {
+    $this->finder->registerNamespaceHandler($namespace, $handler);
+  }
+
+  /**
+   * Register a prefix handler object
+   */
+  function prefixHandler($prefix, $handler) {
+    $this->finder->registerNamespaceHandler($prefix, $handler);
+  }
+
+  /**
+   * Process a given directory to make it relative to Drupal root,
+   * instead of relative to the current extension dir.
+   */
+  protected function processDir($dir, $relative) {
+    if (!isset($dir)) {
+      $dir = $this->extensionDir . '/lib';
+    }
+    elseif ($relative) {
+      // Root dir is relative to module root.
+      if (empty($psr_0_root_dir)) {
+        $psr_0_root_dir = $this->extensionDir;
+      }
+      else {
+        $psr_0_root_dir = $this->extensionDir . '/' . $psr_0_root_dir;
+      }
+    }
+    else {
+      // Leave the $dir as it is.
+    }
+    return $dir;
+  }
+
+  /**
+   * Set a module to use as base for relative paths.
+   */
+  function setModule($module) {
+    $this->extensionDir = drupal_get_path('module', $module);
+  }
+
+  /**
+   * Set a theme to use as base for relative paths.
+   */
+  function setTheme($theme) {
+    $this->extensionDir = drupal_get_path('theme', $theme);
+  }
+
+  /**
+   * Set a library to use as base for relative paths.
+   */
+  function setLibrary($library) {
+    if (!module_exists('libraries')) {
+      throw new Exception('Libraries module not installed.');
+    }
+    $this->extensionDir = libraries_get_path($library);
+  }
+
+  /**
+   * Explicitly set the base for relative paths.
+   */
+  function setExtensionDir($dir) {
+    $this->extensionDir = $dir;
+  }
+}
\ No newline at end of file
diff --git a/xautoload.module b/xautoload.module
index f6926d1..416f4d6 100644
--- a/xautoload.module
+++ b/xautoload.module
@@ -11,6 +11,60 @@ function xautoload_boot() {
 }
 
 
+/**
+ * Implements hook_init()
+ *
+ * Note:
+ *   This is a first step to allow modules to register foreign namespaces.
+ *   We will probably change this, to allow bootstrap modules to register their
+ *   namespaces earlier in the request.
+ *   We might also find a solution to cache the result of this hook between
+ *   requests. This would require a different implementation of the InjectedAPI,
+ *   which would no longer have a direct reference to the finder object.
+ */
+function xautoload_init() {
+  $api = new xautoload_InjectedAPI_hookXautoload(xautoload_get_finder());
+  foreach (module_implements('xautoload') as $module) {
+    $api->setModule($module);
+    $f = $module . '_xautoload';
+    $f($api);
+  }
+}
+
+
+/**
+ * Implements hook_module_implements_alter()
+ */
+function xautoload_module_implements_alter(&$implementations, $hook) {
+  if ($hook === 'init') {
+    // Move xautoload_init() to the start.
+    $implementations = array('xautoload' => FALSE) + $implementations;
+  }
+}
+
+
+/**
+ * Implements hook_xautoload on behalf of libraries module
+ */
+function libraries_xautoload($api) {
+  foreach (libraries_info() as $name => $info) {
+    if (isset($info['xautoload'])) {
+      $xinfo = $info['xautoload'];
+      $api->setLibrary($name);
+      if (is_array($xinfo)) {
+        // TODO: Support array settings.
+        if (isset($xinfo['callback'])) {
+          call_user_func($xinfo, $api);
+        }
+      }
+      elseif (is_callable($xinfo)) {
+        call_user_func($xinfo, $api);
+      }
+    }
+  }
+}
+
+
 /*
  * When the module has just been installed,
  * Drupal does not know yet this is a boot-level module.
