void volume_module::setup() { // Load configuration values {{{ string master_mixer_name{"Master"}; string speaker_mixer_name; string headphone_mixer_name; GET_CONFIG_VALUE(name(), master_mixer_name, "master-mixer"); GET_CONFIG_VALUE(name(), speaker_mixer_name, "speaker-mixer"); GET_CONFIG_VALUE(name(), headphone_mixer_name, "headphone-mixer"); if (!headphone_mixer_name.empty()) REQ_CONFIG_VALUE(name(), m_headphoneid, "headphone-id"); if (string_util::compare(speaker_mixer_name, "master")) throw module_error("Master mixer is already defined"); if (string_util::compare(headphone_mixer_name, "master")) throw module_error("Master mixer is already defined"); // }}} // Setup mixers {{{ try { if (!master_mixer_name.empty()) m_mixers[mixer::MASTER].reset(new mixer_t::element_type{master_mixer_name}); if (!speaker_mixer_name.empty()) m_mixers[mixer::SPEAKER].reset(new mixer_t::element_type{speaker_mixer_name}); if (!headphone_mixer_name.empty()) m_mixers[mixer::HEADPHONE].reset(new mixer_t::element_type{headphone_mixer_name}); if (m_mixers[mixer::HEADPHONE]) m_controls[control::HEADPHONE].reset(new control_t::element_type{m_headphoneid}); if (m_mixers.empty()) throw module_error("No configured mixers"); } catch (const alsa_mixer_error& err) { throw module_error(err.what()); } catch (const alsa_ctl_interface_error& err) { throw module_error(err.what()); } // }}} // Add formats and elements {{{ m_formatter->add( FORMAT_VOLUME, TAG_LABEL_VOLUME, {TAG_RAMP_VOLUME, TAG_LABEL_VOLUME, TAG_BAR_VOLUME}); m_formatter->add( FORMAT_MUTED, TAG_LABEL_MUTED, {TAG_RAMP_VOLUME, TAG_LABEL_MUTED, TAG_BAR_VOLUME}); if (m_formatter->has(TAG_BAR_VOLUME)) m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME); if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%"); if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%"); if (m_formatter->has(TAG_RAMP_VOLUME)) { m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME); m_ramp_headphones = load_ramp(m_conf, name(), TAG_RAMP_HEADPHONES, false); } // }}} }
int main(int argc, char *argv[]) { mod_t *m; void (*init) (int, char **); int (*deinit) (void); m = module_open(argv[1]); if (!m) { fprintf(stderr, "unable to open module %s: %s\n", argv[1], module_error()); return 1; } if (!module_symbol(m, "init", (void **)&init)) { fprintf(stderr, "unable to find init function: %s\n", module_error()); return 1; } init(argc - 2, &argv[2]); if (!module_symbol(m, "deinit", (void **)&deinit)) { fprintf(stderr, "unable to find deinit function: %s\n", module_error()); return 1; } return deinit(); }
static int module_load_full(const char *path, const char *rootmodule, const char *submodule, int start, int end, char **prefixes) { MODULE_REC *module; int status, try_prefixes; if (!g_module_supported()) return FALSE; module = module_find(rootmodule); if (module != NULL && (strcmp(submodule, rootmodule) == 0 || module_file_find(module, submodule) != NULL)) { /* module is already loaded */ module_error(MODULE_ERROR_ALREADY_LOADED, NULL, rootmodule, submodule); return FALSE; } /* check if the given module exists.. */ try_prefixes = strcmp(rootmodule, submodule) == 0; status = module_load_name(path, rootmodule, submodule, try_prefixes); if (status == -1 && try_prefixes) { /* nope, try loading the module_core, fe_module, etc. */ status = module_load_prefixes(path, rootmodule, start, end, prefixes); } return status > 0; }
pulseaudio_module::pulseaudio_module(const bar_settings& bar, string name_) : event_module<pulseaudio_module>(bar, move(name_)) { // Load configuration values auto sink_name = m_conf.get(name(), "sink", ""s); bool m_max_volume = m_conf.get(name(), "use-ui-max", true); try { m_pulseaudio = factory_util::unique<pulseaudio>(m_log, move(sink_name), m_max_volume); } catch (const pulseaudio_error& err) { throw module_error(err.what()); } // Add formats and elements m_formatter->add(FORMAT_VOLUME, TAG_LABEL_VOLUME, {TAG_RAMP_VOLUME, TAG_LABEL_VOLUME, TAG_BAR_VOLUME}); m_formatter->add(FORMAT_MUTED, TAG_LABEL_MUTED, {TAG_RAMP_VOLUME, TAG_LABEL_MUTED, TAG_BAR_VOLUME}); if (m_formatter->has(TAG_BAR_VOLUME)) { m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME); } if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) { m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%%"); } if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) { m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%%"); } if (m_formatter->has(TAG_RAMP_VOLUME)) { m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME); } }
text_module::text_module(const bar_settings& bar, string name_) : static_module<text_module>(bar, move(name_)) { m_formatter->add("content", "", {}); if (m_formatter->get("content")->value.empty()) { throw module_error(name() + ".content is empty or undefined"); } m_formatter->get("content")->value = string_util::replace_all(m_formatter->get("content")->value, " ", BUILDER_SPACE_TOKEN); }
/** * Construct module */ xwindow_module::xwindow_module(const bar_settings& bar, string name_) : static_module<xwindow_module>(bar, move(name_)), m_connection(connection::make()) { // Initialize ewmh atoms if ((ewmh_util::initialize()) == nullptr) { throw module_error("Failed to initialize ewmh atoms"); } // Check if the WM supports _NET_ACTIVE_WINDOW if (!ewmh_util::supports(_NET_ACTIVE_WINDOW)) { throw module_error("The WM does not list _NET_ACTIVE_WINDOW as a supported hint"); } // Add formats and elements m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL}); if (m_formatter->has(TAG_LABEL)) { m_statelabels.emplace(state::ACTIVE, load_optional_label(m_conf, name(), "label", "%title%")); m_statelabels.emplace(state::EMPTY, load_optional_label(m_conf, name(), "label-empty", "")); } }
/* Returns 1 if ok, 0 if error in module and -1 if module wasn't found */ static int module_load_name(const char *path, const char *rootmodule, const char *submodule, int silent) { void (*module_init) (void); void (*module_deinit) (void); GModule *gmodule; MODULE_REC *module; MODULE_FILE_REC *rec; gpointer value1, value2; char *initfunc, *deinitfunc; int found; gmodule = module_open(path, &found); if (gmodule == NULL) { if (!silent || found) { module_error(MODULE_ERROR_LOAD, g_module_error(), rootmodule, submodule); } return found ? 0 : -1; } /* get the module's init() and deinit() functions */ initfunc = module_get_func(rootmodule, submodule, "init"); deinitfunc = module_get_func(rootmodule, submodule, "deinit"); found = g_module_symbol(gmodule, initfunc, &value1) && g_module_symbol(gmodule, deinitfunc, &value2); g_free(initfunc); g_free(deinitfunc); module_init = value1; module_deinit = value2; if (!found) { module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); g_module_close(gmodule); return 0; } /* Call the module's init() function - it should register itself with module_register() function, abort if it doesn't. */ module_init(); module = module_find(rootmodule); rec = module == NULL ? NULL : strcmp(rootmodule, submodule) == 0 ? module_file_find(module, "core") : module_file_find(module, submodule); if (rec == NULL) { rec = module_register_full(rootmodule, submodule, NULL); rec->gmodule = gmodule; module_file_unload(rec); module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); return 0; } rec->module_deinit = module_deinit; rec->gmodule = gmodule; rec->initialized = TRUE; settings_check_module(rec->defined_module_name); signal_emit("module loaded", 2, rec->root, rec); return 1; }
alsa_module::alsa_module(const bar_settings& bar, string name_) : event_module<alsa_module>(bar, move(name_)) { // Load configuration values m_mapped = m_conf.get(name(), "mapped", m_mapped); auto master_mixer_name = m_conf.get(name(), "master-mixer", "Master"s); auto speaker_mixer_name = m_conf.get(name(), "speaker-mixer", ""s); auto headphone_mixer_name = m_conf.get(name(), "headphone-mixer", ""s); // m_soundcard_name: Master Soundcard Name // s_soundcard_name: Speaker Soundcard Name // h_soundcard_name: Headphone Soundcard Name auto m_soundcard_name = m_conf.get(name(), "master-soundcard", "default"s); auto s_soundcard_name = m_conf.get(name(), "speaker-soundcard", "default"s); auto h_soundcard_name = m_conf.get(name(), "headphone-soundcard", "default"s); if (!headphone_mixer_name.empty()) { m_headphoneid = m_conf.get<decltype(m_headphoneid)>(name(), "headphone-id"); } if (string_util::compare(speaker_mixer_name, "master")) { throw module_error("Master mixer is already defined"); } if (string_util::compare(headphone_mixer_name, "master")) { throw module_error("Master mixer is already defined"); } // Setup mixers try { if (!master_mixer_name.empty()) { m_mixer[mixer::MASTER].reset(new mixer_t::element_type{move(master_mixer_name), move(m_soundcard_name)}); } if (!speaker_mixer_name.empty()) { m_mixer[mixer::SPEAKER].reset(new mixer_t::element_type{move(speaker_mixer_name), move(s_soundcard_name)}); } if (!headphone_mixer_name.empty()) { m_mixer[mixer::HEADPHONE].reset(new mixer_t::element_type{move(headphone_mixer_name), move(h_soundcard_name)}); } if (m_mixer[mixer::HEADPHONE]) { m_ctrl[control::HEADPHONE].reset(new control_t::element_type{m_headphoneid}); } if (m_mixer.empty()) { throw module_error("No configured mixers"); } } catch (const mixer_error& err) { throw module_error(err.what()); } catch (const control_error& err) { throw module_error(err.what()); } // Add formats and elements m_formatter->add(FORMAT_VOLUME, TAG_LABEL_VOLUME, {TAG_RAMP_VOLUME, TAG_LABEL_VOLUME, TAG_BAR_VOLUME}); m_formatter->add(FORMAT_MUTED, TAG_LABEL_MUTED, {TAG_RAMP_VOLUME, TAG_LABEL_MUTED, TAG_BAR_VOLUME}); if (m_formatter->has(TAG_BAR_VOLUME)) { m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME); } if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) { m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%%"); } if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) { m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%%"); } if (m_formatter->has(TAG_RAMP_VOLUME)) { m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME); m_ramp_headphones = load_ramp(m_conf, name(), TAG_RAMP_HEADPHONES, false); } }
/* Returns 1 if ok, 0 if error in module and -1 if module wasn't found */ static int module_load_name(const char *path, const char *rootmodule, const char *submodule, int silent) { void (*module_init) (void); void (*module_deinit) (void); void (*module_version) (int *); GModule *gmodule; MODULE_REC *module; MODULE_FILE_REC *rec; gpointer value_version = NULL; gpointer value1, value2 = NULL; char *versionfunc, *initfunc, *deinitfunc; int module_abi_version = 0; int found; gmodule = module_open(path, &found); if (gmodule == NULL) { if (!silent || found) { module_error(MODULE_ERROR_LOAD, g_module_error(), rootmodule, submodule); } return found ? 0 : -1; } /* get the module's irssi abi version and bail out on mismatch */ versionfunc = module_get_func(rootmodule, submodule, "abicheck"); if (!g_module_symbol(gmodule, versionfunc, &value_version)) { g_free(versionfunc); module_error(MODULE_ERROR_VERSION_MISMATCH, "0", rootmodule, submodule); g_module_close(gmodule); return 0; } g_free(versionfunc); module_version = value_version; module_version(&module_abi_version); if (module_abi_version != IRSSI_ABI_VERSION) { char *module_abi_versionstr = g_strdup_printf("%d", module_abi_version); module_error(MODULE_ERROR_VERSION_MISMATCH, module_abi_versionstr, rootmodule, submodule); g_free(module_abi_versionstr); g_module_close(gmodule); return 0; } /* get the module's init() and deinit() functions */ initfunc = module_get_func(rootmodule, submodule, "init"); deinitfunc = module_get_func(rootmodule, submodule, "deinit"); found = g_module_symbol(gmodule, initfunc, &value1) && g_module_symbol(gmodule, deinitfunc, &value2); g_free(initfunc); g_free(deinitfunc); if (!found) { module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); g_module_close(gmodule); return 0; } module_init = value1; module_deinit = value2; /* Call the module's init() function - it should register itself with module_register() function, abort if it doesn't. */ module_init(); module = module_find(rootmodule); rec = module == NULL ? NULL : g_strcmp0(rootmodule, submodule) == 0 ? module_file_find(module, "core") : module_file_find(module, submodule); if (rec == NULL) { rec = module_register_full(rootmodule, submodule, NULL); rec->gmodule = gmodule; module_file_unload(rec); module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); return 0; } rec->module_deinit = module_deinit; rec->gmodule = gmodule; rec->initialized = TRUE; settings_check_module(rec->defined_module_name); signal_emit("module loaded", 2, rec->root, rec); return 1; }
void mapfile_apply(list_t* names) { TCHAR* undecorated; ULONG total = 0, filtered = 0, applied = 0, addr; int err, result; name_t* nm, *nm_last; list_t* rmtable; module_t* module = module_info(&err); if (!err) { Addtolist(0, 0, "Applying names from map file to module '%s'", module->name); if (!g_Config->collisionchecks) { rmtable = list_create(); } nm = (name_t*)names->first; while (nm) { if (nm->segment < module->nseg) { if (g_Config->demangle) { undecorated = (TCHAR*)malloc(2 * nm->size * sizeof(TCHAR)); if (result = Demanglename(nm->buffer, NM_LIBRARY, undecorated)) { free(nm->buffer); nm->size = result + 1; nm->buffer = undecorated; } else { free(undecorated); } } addr = module->base + module->segments[nm->segment] + nm->offset; if (g_Config->usemasks) { if (result = mask_filter(nm)) { filtered++; if ((result & FILTER_SKIP) && !g_Config->collisionchecks && /* Findname for NM_ANYNAME fails everytime, dunno why */ (Findname(addr, NM_COMMENT, NULL) || Findname(addr, NM_LABEL, NULL))) { list_addname(rmtable, NULL, 0, nm->segment, nm->offset); total++; nm = nm->next; continue; } } } if (g_Config->comments) { if (g_Config->collisionchecks) { if (!Findname(addr, NM_COMMENT, NULL) && !Quickinsertname(addr, NM_COMMENT, nm->buffer)) { applied++; } } else if (!Quickinsertname(addr, NM_COMMENT, nm->buffer)) { applied++; } } if (g_Config->labels) { if (g_Config->collisionchecks) { if (!Findlabel(addr, NULL) && !Quickinsertname(addr, NM_LABEL, nm->buffer)) { applied++; } } else if (!Quickinsertname(addr, NM_LABEL, nm->buffer)) { applied++; } } } total++; Progress(total * 1000 / names->count, "Inserting names"); nm = nm->next; } Progress(0, ""); Infoline("Merging names"); Mergequicknames(); if (!g_Config->collisionchecks) { Infoline("Cleaning skipped entries"); nm = (name_t*)rmtable->first; while (nm) { addr = module->base + module->segments[nm->segment] + nm->offset; if (g_Config->comments) { Insertname(addr, NM_COMMENT, ""); } if (g_Config->labels) { Insertname(addr, NM_LABEL, ""); } nm_last = nm; nm = nm->next; /* Manual list_freenames expansion to speed it up somehow */ free(nm_last); } } Infoline("Total loaded: %d, Names applied: %d, Names filtered: %d", total, applied, filtered); Addtolist(0, -1, " Total loaded: %d, Names applied: %d, Names filtered: %d", total, applied, filtered); module_free(module); } else { module_error(err); } }
int loadVpiModule (const char* modulename) { static void *libghdlvpi_mod; int i; void *vpimod; fprintf (stderr, "loading VPI module '%s'\n", modulename); /* TODO: on windows, use SetDllDirectory with: - install dir (libdir) => add -DLIBDIR=xxx - exec path\lib => see windows_default_path */ vpimod = module_open (modulename); if (vpimod == NULL) { const char *msg = module_error (); fprintf (stderr, "%s\n", msg == NULL ? "unknown dlopen error" : msg); return -1; } /* Try to load the libghdlvpi library and set the thunk. No need to load the library several times. */ if (libghdlvpi_mod == NULL) { libghdlvpi_mod = module_open (libghdlvpi_name); if (libghdlvpi_mod != NULL) { vpi_thunk **vpi_thunk_ptr; for (i = 0; i < 2; i++) { vpi_thunk_ptr = module_symbol (libghdlvpi_mod, &"_VPI_THUNK"[i]); if (vpi_thunk_ptr != NULL) { *vpi_thunk_ptr = &__ghdl_vpi_thunk_v1; break; } } if (vpi_thunk_ptr == NULL) fprintf (stderr, "warning: VPI_THUNK not found in %s\n", libghdlvpi_name); } } for (i = 0; i < 2; i++) // try with and w/o leading underscores { void *vpitable = module_symbol (vpimod, &"_vlog_startup_routines"[i]); if (vpitable) { unsigned int j; typedef void (*vlog_startup_routines_t)(void); vlog_startup_routines_t *vpifuns; vpifuns = (vlog_startup_routines_t*)vpitable; for (j = 0; vpifuns[j]; j++) vpifuns[j](); fprintf (stderr, "VPI module loaded!\n"); return 0; // successfully registered VPI module } } fprintf (stderr, "vlog_startup_routines not found\n"); return -1; // failed to register VPI module }
int module_load_vfs(const char *name, int flags, bool autoload, module_t *mod, prop_dictionary_t *filedictp) { char *path; bool nochroot; int error; prop_bool_t noload; prop_dictionary_t moduledict; nochroot = false; error = 0; path = NULL; moduledict = NULL; if (filedictp) *filedictp = NULL; path = PNBUF_GET(); if (!autoload) { if (strchr(name, '/') != NULL) { nochroot = false; snprintf(path, MAXPATHLEN, "%s", name); error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); } else error = ENOENT; } if (autoload || (error == ENOENT)) { if (strchr(name, '/') == NULL) { nochroot = true; snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod", module_base, name, name); error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); } else error = ENOENT; } if (error != 0) { PNBUF_PUT(path); module_print("Cannot %sload kernel object `%s'" " error=%d", autoload ? "auto" : "", name, error); return error; } /* * Load and process <module>.plist if it exists. */ if (((flags & MODCTL_NO_PROP) == 0 && filedictp) || autoload) { error = module_load_plist_vfs(path, nochroot, &moduledict); if (error != 0) { module_print("plist load returned error %d for `%s'", error, path); if (error != ENOENT) goto fail; } else if (autoload) { noload = prop_dictionary_get(moduledict, "noautoload"); if (noload != NULL && prop_bool_true(noload)) { module_error("autoloading is disallowed for %s", path); prop_object_release(moduledict); error = EPERM; goto fail; } } if (error == 0) { /* can get here if error == ENOENT */ if ((flags & MODCTL_NO_PROP) == 0 && filedictp) *filedictp = moduledict; else prop_object_release(moduledict); } } PNBUF_PUT(path); return 0; fail: kobj_unload(mod->mod_kobj); PNBUF_PUT(path); return error; }
i3_module::i3_module(const bar_settings& bar, string name_) : event_module<i3_module>(bar, move(name_)) { auto socket_path = i3ipc::get_socketpath(); if (!file_util::exists(socket_path)) { throw module_error("Could not find socket: " + (socket_path.empty() ? "<empty>" : socket_path)); } m_ipc = factory_util::unique<i3ipc::connection>(); // Load configuration values m_click = m_conf.get(name(), "enable-click", m_click); m_scroll = m_conf.get(name(), "enable-scroll", m_scroll); m_revscroll = m_conf.get(name(), "reverse-scroll", m_revscroll); m_wrap = m_conf.get(name(), "wrapping-scroll", m_wrap); m_indexsort = m_conf.get(name(), "index-sort", m_indexsort); m_pinworkspaces = m_conf.get(name(), "pin-workspaces", m_pinworkspaces); m_strip_wsnumbers = m_conf.get(name(), "strip-wsnumbers", m_strip_wsnumbers); m_fuzzy_match = m_conf.get(name(), "fuzzy-match", m_fuzzy_match); m_conf.warn_deprecated(name(), "wsname-maxlen", "%name:min:max%"); // Add formats and create components m_formatter->add(DEFAULT_FORMAT, DEFAULT_TAGS, {TAG_LABEL_STATE, TAG_LABEL_MODE}); if (m_formatter->has(TAG_LABEL_STATE)) { m_statelabels.insert( make_pair(state::FOCUSED, load_optional_label(m_conf, name(), "label-focused", DEFAULT_WS_LABEL))); m_statelabels.insert( make_pair(state::UNFOCUSED, load_optional_label(m_conf, name(), "label-unfocused", DEFAULT_WS_LABEL))); m_statelabels.insert( make_pair(state::VISIBLE, load_optional_label(m_conf, name(), "label-visible", DEFAULT_WS_LABEL))); m_statelabels.insert( make_pair(state::URGENT, load_optional_label(m_conf, name(), "label-urgent", DEFAULT_WS_LABEL))); } if (m_formatter->has(TAG_LABEL_MODE)) { m_modelabel = load_optional_label(m_conf, name(), "label-mode", "%mode%"); } m_labelseparator = load_optional_label(m_conf, name(), "label-separator", ""); m_icons = factory_util::shared<iconset>(); m_icons->add(DEFAULT_WS_ICON, factory_util::shared<label>(m_conf.get(name(), DEFAULT_WS_ICON, ""s))); for (const auto& workspace : m_conf.get_list<string>(name(), "ws-icon", {})) { auto vec = string_util::split(workspace, ';'); if (vec.size() == 2) { m_icons->add(vec[0], factory_util::shared<label>(vec[1])); } } try { if (m_modelabel) { m_ipc->on_mode_event = [this](const i3ipc::mode_t& mode) { m_modeactive = (mode.change != DEFAULT_MODE); if (m_modeactive) { m_modelabel->reset_tokens(); m_modelabel->replace_token("%mode%", mode.change); } }; } m_ipc->subscribe(i3ipc::ET_WORKSPACE | i3ipc::ET_MODE); } catch (const exception& err) { throw module_error(err.what()); } }
void backlight_module::brightness_handle::filepath(const string& path) { if (!file_util::exists(path)) { throw module_error("The file '" + path + "' does not exist"); } m_path = path; }