How do I make it so I can call an embed to another file and post it as such? - arguments

This is my index.js: (bot > index) The index is the main file. It's not the main focus of what I'm asking, but maybe the error could stem from here.
const Discord = require("discord.js");
const fs = require("fs");
const prefix = "$";
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync("./commands/")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once("ready", () => {
console.log("This bot is online!");
});
client.on("message", (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) {
return;
}
// Destructure the first word from message.content, leave the rest as an array
const [commandName, ...args] = message.content
.slice(prefix.length)
.split(/ +/);
const command = client.commands.get(commandName.toLowerCase());
if (!command) {
return; // do something
}
command.execute(message, args);
});
client.login("token")
;
This is my effect.js: (bot > commands > effect) This is where error codes are usually from. This is more of a command subfolder specifically for Pokemon effects. This specifically gives me the hardest for my goal because I don't know how to send code into another file while also not cramping it. My goal is simply: make the embed message without code junk.
//REMINDER: PREFIX IS $
//RESET BOT TERMINAL CODE: NODE .
module.exports = {
name: "effect",
description: "Information on Pokemon!",
execute(message, args) {
// args[0] would be the second word from message.content
// You should check if they used the command the right way
// $effect some-type would make args[0] some-type
var command = args[0]
var channel = 0x792853588388872202
//POKEMON TYPE -- FAIRY
if(command === 'fairy'){
message.channel.send({embed: fairyEmbed});
}
//POKEMON TYPE -- FIGHTING
if(command === 'fighting'){
channel.send(fightingEmbed);
}
//POKEMON TYPE -- STEEL
if(command === 'steel'){
channel.send(steelEmbed);
}
//POKEMON TYPE -- DARK
if(command === 'dark'){
channel.send(darkEmbed);
}
//POKEMON TYPE -- DRAGON
if(command === 'dragon'){
channel.send(dragonEmbed);
}
//POKEMON TYPE -- GHOST
if(command === 'ghost'){
channel.send(ghostEmbed);
}
//POKEMON TYPE -- PSYCHIC
if(command === 'psychic'){
channel.send(psychicEmbed);
}
//POKEMON TYPE -- ROCK
if(command === 'rock'){
channel.send(rockEmbed);
}
//POKEMON TYPE -- BUG
if(command === 'bug'){
channel.send(bugEmbed);
}
//POKEMON TYPE -- FLYING
if(command === 'flying'){
channel.send(flyingEmbed);
}
//POKEMON TYPE -- GROUND
if(command === 'ground'){
channel.send(groundEmbed);
}
//POKEMON TYPE -- POISON
if(command === 'poison'){
channel.send(poisonEmbed);
}
//POKEMON TYPE -- ICE
if(command === 'ice'){
channel.send(ice);
}
//POKEMON TYPE -- GRASS
if(command === 'grass'){
channel.send(grassEmbed);
}
//POKEMON TYPE -- ELECTRIC
if(command === 'electric'){
channel.send(electricEmbed);
}
//POKEMON TYPE -- WATER
if(command === 'water'){
channel.send(waterEmbed);
}
//POKEMON TYPE -- FIRE
if(command === 'fire'){
channel.send(fireEmbed);
}
//POKEMON TYPE -- NORMAL
if(command === 'normal'){
channel.send(normalEmbed);
}
//POKEMON TYPE -- ALL
if(command === 'all'){
channel.send(allEmbed);
}
},
};
This is my effect-embed.js: (same as effect.js) Aka problem #2. This is where the embeds themselves are held. I have NO idea how to transfer them without copying and pasting so here I am.
// inside a command, event listener, etc.
const Discord = require("discord.js");
const fairyEmbed = new Discord.MessageEmbed()
.setColor('#dd525d')
.setTitle('Fairy')
.setURL('https://youtu.be/0K6qBmnRizU')
.setDescription('Fairy types are too cutesy for my preference. They’re weak to Poison and Steel type and Fire, Steel and Poison types resist their attacks. But they do a lot of damage to Dark, Fighting and Dragon types. Dragon type attacks don’t effect them at all, so they’re good in that department.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.pinterest.com%2Fpin%2F627055948095995745%2F&psig=AOvVaw1Vu5xwOk0k9tBEJ9Yclz2F&ust=1609622152653000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCICQ3vXT--0CFQAAAAAdAAAAABAD')
message.channel.send({embed: fairyEmbed});
const fightingEmbed = new Discord.MessageEmbed()
.setColor('#61dde6')
.setTitle('Fighting')
.setURL('https://youtu.be/FJgRlbjzsrs')
.setDescription('Fighting types are very resilient. Their attacks are super effective against Rock, Steel, Dark, Normal and Ice types and they resist Dark, Rock and Bug type attacks. Fighting type attacks won’t do well against Flying, Psychic, Fairy, Poison or Bug types and they won’t do anything to Ghost types and they’re very weak to Fairy, Psychic and Flying type attacks.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fpokemon.fandom.com%2Fwiki%2FZamazenta_(anime)&psig=AOvVaw1b8V8kCknIyef2c_zxuFjV&ust=1609622421791000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCJCsvPXU--0CFQAAAAAdAAAAABAM')
const steelEmbed = new Discord.MessageEmbed()
.setColor('#ba2838')
.setTitle('Steel')
.setURL('https://youtu.be/I_57ptO3TKc')
.setDescription('Steel types are the prime definition of defense with an amazing resistance to Normal, Grass, Ice, Flying, Psychic, Bug, Rock, Dragon, Steel and Fairy type attacks and are immune to Poison attacks. They’ll do immense damage to Rock, Fairy and Ice types. If they get hit by Fire, Ground or Fighting type attacks, they’ll be greatly damaged and they don’t do much to Water, Fire, Electric or other Steel types.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fpokemongohub.net%2Fpost%2Fguide%2Fdialga-heat-map%2F&psig=AOvVaw3l68mb5ltZZZusGgHSGLTd&ust=1609622768057000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKjLpZ_W--0CFQAAAAAdAAAAABAV')
const darkEmbed = new Discord.MessageEmbed()
.setColor('#3a4c7b')
.setTitle('Dark')
.setURL('https://youtu.be/0K6qBmnRizU')
.setDescription('Dark type Pokémon are masters of the night and dirty, underhanded tactics. They’re very strong against Psychic and Ghost and resist Dark and Ghost type attacks. Psychic type attacks don’t stand a chance due to their immunity. It’s best to avoid putting them against Fighting, Fairy and Bug type attacks because of their weakness and they won’t do as much damage against Dark, Fighting and Fairy type Pokémon. Either way you slice it, Dark type Pokémon are very strong. Spikemuth’s gym is a prime example of that.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fbulbapedia.bulbagarden.net%2Fwiki%2FYveltal_(Pok%25C3%25A9mon)&psig=AOvVaw01HxoY93uTikkUD-yDrdJU&ust=1609623455517000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCIDG2OLY--0CFQAAAAAdAAAAABAD')
const dragonEmbed = new Discord.MessageEmbed()
.setColor('#352822')
.setTitle('Dragon')
.setURL('https://youtu.be/8lrMjcNJGfE')
.setDescription('Dragon types are very rare and thought to be a mystical type. If you want to stop them in their tracks, use Fairy, Ice or Dragon type moves against them. Dragon types resist Fire, Water, Grass and Electric type attacks and Dragon Type attacks are only strong against themselves but have a wide variety of attacks. Dragons type moves don’t do much to Steel types and Fairy types are completely immune to their attacks. My bro said Raihan was infamous for completely demolishing trainers with his overwhelming dragons, so I’d watch your choices around them.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fbleedingcool.com%2Fgames%2Frayquaza-raid-guide-how-to-catch-a-shiny-rayquaza-in-pokemon-go%2F&psig=AOvVaw02L1u59NkP3kqQSp5xAJKB&ust=1609623775222000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCIiX9PvZ--0CFQAAAAAdAAAAABAD')
const ghostEmbed = new Discord.MessageEmbed()
.setColor('#352822')
.setTitle('Dragon')
.setURL('https://youtu.be/8lrMjcNJGfE')
.setDescription('Dragon types are very rare and thought to be a mystical type. If you want to stop them in their tracks, use Fairy, Ice or Dragon type moves against them. Dragon types resist Fire, Water, Grass and Electric type attacks and Dragon Type attacks are only strong against themselves but have a wide variety of attacks. Dragons type moves don’t do much to Steel types and Fairy types are completely immune to their attacks. My bro said Raihan was infamous for completely demolishing trainers with his overwhelming dragons, so I’d watch your choices around them.')
.setThumbnail('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.zedge.net%2Fwallpaper%2F23b8d5ed-9f57-39e7-b27c-ffd2df026702&psig=AOvVaw3xIwv-YzXf8AuUEaTGfBp4&ust=1609622014464000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKDrk7TT--0CFQAAAAAdAAAAABAJ')
.setImage('https://www.google.com/url?sa=i&url=https%3A%2F%2Fbleedingcool.com%2Fgames%2Frayquaza-raid-guide-how-to-catch-a-shiny-rayquaza-in-pokemon-go%2F&psig=AOvVaw02L1u59NkP3kqQSp5xAJKB&ust=1609623775222000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCIiX9PvZ--0CFQAAAAAdAAAAABAD')

You'll need to module.exports every embed. I'll give you an example:
module.exports = darkEmbed;
And then you're going to import darkEmbed in your other file.
const { darkEmbed } = require("file")
The { } are there because you have more then one object in the embeds file.

You can use module.exports in your effect-embeds.js file, and require it in your effect.js file.
E.g.
// effect-embeds.js
module.exports.effect = new Discord.MessageEmbed()
//embed information
// Compared to (DO NOT DO)
const effect = new Discord.MessageEmbed()
//embed information
Then in your other file...
// effect.js
const { effect } = require("./effect.embeds.js"); // Change the file import based on where it is located
channel.send(effect);
Also, instead of using if statements over and over
if(...) {
//code
}
Use a switch.
switch(command) {
case '...':
// code
break;
}

Related

Can asynchronous module definitions be used with abstract syntax trees on v8 engine to read third party dependencies? [duplicate]

I understand eval string-to-function is impossible to use on the browsers' application programming interfaces, but there must be another strategy to use third party dependencies without node.js on v8 engine, given Cloudflare does it in-house, unless they disable the exclusive method by necessity or otherwise on their edge servers for Workers. I imagine I could gather the AST of the commonjs module, as I was able to by rollup watch, but what might the actual steps be, by tooling? I mention AMD for it seems to rely on string-to-function (to-which I've notice Mozilla MDN says nothing much about it).
I have been exploring the require.js repositories, and they either use eval or AST
function DEFNODE(type, props, methods, base) {
if (arguments.length < 4) base = AST_Node;
if (!props) props = [];
else props = props.split(/\s+/);
var self_props = props;
if (base && base.PROPS) props = props.concat(base.PROPS);
var code = "return function AST_" + type + "(props){ if (props) { ";
for (var i = props.length; --i >= 0; ) {
code += "this." + props[i] + " = props." + props[i] + ";";
}
var proto = base && new base();
if ((proto && proto.initialize) || (methods && methods.initialize))
code += "this.initialize();";
code += "}}";
//constructor
var cnstor = new Function(code)();
if (proto) {
cnstor.prototype = proto;
cnstor.BASE = base;
}
if (base) base.SUBCLASSES.push(cnstor);
cnstor.prototype.CTOR = cnstor;
cnstor.PROPS = props || null;
cnstor.SELF_PROPS = self_props;
cnstor.SUBCLASSES = [];
if (type) {
cnstor.prototype.TYPE = cnstor.TYPE = type;
}
if (methods)
for (i in methods)
if (HOP(methods, i)) {
if (/^\$/.test(i)) {
cnstor[i.substr(1)] = methods[i];
} else {
cnstor.prototype[i] = methods[i];
}
}
//a function that returns an object with [name]:method
cnstor.DEFMETHOD = function (name, method) {
this.prototype[name] = method;
};
if (typeof exports !== "undefined") exports[`AST_${type}`] = cnstor;
return cnstor;
}
var AST_Token = DEFNODE(
"Token",
"type value line col pos endline endcol endpos nlb comments_before file raw",
{},
null
);
https://codesandbox.io/s/infallible-darwin-8jcl2k?file=/src/mastercard-backbank/uglify/index.js
https://www.youtube.com/watch?v=EF7UW9HxOe4
Is it possible to make a C++ addon just to add a default object for
node.js named exports or am I Y’ing up the wrong X
'.so' shared library for C++ dlopen/LoadLibrary (or #include?)
“I have to say that I'm amazed that there is code out there that loads one native addon from another native addon! Is it done by acquiring and then calling an instance of the require() function, or perhaps by using uv_dlopen() directly?”
N-API: An api for embedding Node in applications
"[there is no ]napi_env[ just yet]."
node-api: allow retrieval of add-on file name - Missing module in Init
Andreas Rossberg - is AST parsing, or initialize node.js abstraction for native c++, enough?
v8::String::NewFromUtf8(isolate, "Index from C++!");
Rising Stack - Node Source
"a macro implicit" parameter - bridge object between
C++ and JavaScript runtimes
extract a function's parameters and set the return value.
#include <nan.h>
int build () {
NAN_METHOD(Index) {
info.GetReturnValue().Set(
Nan::New("Index from C++!").ToLocalChecked()
);
}
}
// Module initialization logic
NAN_MODULE_INIT(Initialize) {
/*Export the `Index` function
(equivalent to `export function Index (...)` in JS)*/
NAN_EXPORT(target, Index);
}
New module "App" Initialize function from NAN_MODULE_INIT (an atomic?-macro)
"__napi_something doesn't exist."
"node-addon-API module for C++ code (N-API's C code's headers)"
NODE_MODULE(App, Initialize);
Sep 17, 2013, 4:42:17 AM to v8-u...#googlegroups.com "This comes up
frequently, but the answer remains the same: scrap the idea. ;)
Neither the V8 parser nor its AST are designed for external
interfacing. In particular (1) V8's AST does not necessarily reflect
JavaScript syntax 1-to-1, (2) we change it all the time, and (3) it
depends on various V8 internals. And since all these points are
important for V8, don't expect the situation to change.
/Andreas"
V8 c++: How to import module via code to script context (5/28/22, edit)
"The export keyword may only be used in a module interface unit.
The keyword is attached to a declaration of an entity, and causes that
declaration (and sometimes the definition) to become visible to module
importers[ - except for] the export keyword in the module-declaration, which is just a re-use of the keyword (and does not actually “export” ...entities)."
SyntheticModule::virtual
ScriptCompiler::CompileModule() - "Corresponds to the ParseModule abstract operation in the ECMAScript specification."
Local<Function> foo_func = ...;//external
Local<Module> module = Module::CreateSyntheticModule(
isolate, name,
{String::NewFromUtf8(isolate, "foo")},
[](Local<Context> context, Local<Module> module) {
module->SetSyntheticModuleExport(
String::NewFromUtf8(isolate, "foo"), foo_func
);
});
Context-Aware addons from node.js' commonjs modules
export module index;
export class Index {
public:
const char* app() {
return "done!";
}
};
import index;
import <iostream>;
int main() {
std::cout << Index().app() << '\n';
}
node-addon-api (new)
native abstractions (old)
"Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12 to 4.0, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect NODE_MODULE_VERSION and get yourself into a macro-tangle[ macro = extern atomics?]."
Scope Isolate (v8::Isolate), variable Local (v8::Local)
typed_array_to_native.cc
"require is part of the Asynchronous Module Definition AMD API[, without "string-to-function" eval/new Function()],"
node.js makes objects, for it is written in C++.
"According to the algorithm, before finding
./node_modules/_/index.js, it tried looking for express in the
core Node.js modules. This didn’t exist, so it looked in node_modules,
and found a directory called _. (If there was a
./node_modules/_.js, it would load that directly.) It then
loaded ./node_modules/_/package.json, and looked for an exports
field, but this didn’t exist. It also looked for a main field, but
this didn’t exist either. It then fell back to index.js, which it
found. ...require() looks for node_modules in all of the parent directories of the caller."
But java?
I won't accept this answer until it works, but this looks promising:
https://developer.oracle.com/databases/nashorn-javascript-part1.html
If not to run a jar file or something, in the Worker:
https://github.com/nodyn/jvm-npm
require and build equivalent in maven, first, use "dist/index.js".
Specifically: [ScriptEngineManager][21]
https://stackoverflow.com/a/15787930/11711280
Actually: js.commonjs-require experimental
https://docs.oracle.com/en/graalvm/enterprise/21/docs/reference-manual/js/Modules/
Alternatively/favorably: commonjs builder in C (v8 and node.js)
https://www.reddit.com/r/java/comments/u7elf4/what_are_your_thoughts_on_java_isolates_on_graalvm/
Here I will explore v8/node.js src .h and .cc for this purpose
https://codesandbox.io/s/infallible-darwin-8jcl2k?file=/src/c.cpp
I'm curious why there is near machine-level C operability in Workers if not to use std::ifstream, and/or build-locally, without node.js require.

Enabling Closed-Display Mode w/o Meeting Apple's Requirements

EDIT:
I have heavily edited this question after making some significant new discoveries and the question not having any answers yet.
Historically/AFAIK, keeping your Mac awake while in closed-display mode and not meeting Apple's requirements, has only been possible with a kernel extension (kext), or a command run as root. Recently however, I have discovered that there must be another way. I could really use some help figuring out how to get this working for use in a (100% free, no IAP) sandboxed Mac App Store (MAS) compatible app.
I have confirmed that some other MAS apps are able to do this, and it looks like they might be writing YES to a key named clamshellSleepDisabled. Or perhaps there's some other trickery involved that causes the key value to be set to YES? I found the function in IOPMrootDomain.cpp:
void IOPMrootDomain::setDisableClamShellSleep( bool val )
{
if (gIOPMWorkLoop->inGate() == false) {
gIOPMWorkLoop->runAction(
OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setDisableClamShellSleep),
(OSObject *)this,
(void *)val);
return;
}
else {
DLOG("setDisableClamShellSleep(%x)\n", (uint32_t) val);
if ( clamshellSleepDisabled != val )
{
clamshellSleepDisabled = val;
// If clamshellSleepDisabled is reset to 0, reevaluate if
// system need to go to sleep due to clamshell state
if ( !clamshellSleepDisabled && clamshellClosed)
handlePowerNotification(kLocalEvalClamshellCommand);
}
}
}
I'd like to give this a try and see if that's all it takes, but I don't really have any idea about how to go about calling this function. It's certainly not a part of the IOPMrootDomain documentation, and I can't seem to find any helpful example code for functions that are in the IOPMrootDomain documentation, such as setAggressiveness or setPMAssertionLevel. Here's some evidence of what's going on behind the scenes according to Console:
I've had a tiny bit of experience working with IOMProotDomain via adapting some of ControlPlane's source for another project, but I'm at a loss for how to get started on this. Any help would be greatly appreciated. Thank you!
EDIT:
With #pmdj's contribution/answer, this has been solved!
Full example project:
https://github.com/x74353/CDMManager
This ended up being surprisingly simple/straightforward:
1. Import header:
#import <IOKit/pwr_mgt/IOPMLib.h>
2. Add this function in your implementation file:
IOReturn RootDomain_SetDisableClamShellSleep (io_connect_t root_domain_connection, bool disable)
{
uint32_t num_outputs = 0;
uint32_t input_count = 1;
uint64_t input[input_count];
input[0] = (uint64_t) { disable ? 1 : 0 };
return IOConnectCallScalarMethod(root_domain_connection, kPMSetClamshellSleepState, input, input_count, NULL, &num_outputs);
}
3. Use the following to call the above function from somewhere else in your implementation:
io_connect_t connection = IO_OBJECT_NULL;
io_service_t pmRootDomain = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPMrootDomain"));
IOServiceOpen (pmRootDomain, current_task(), 0, &connection);
// 'enable' is a bool you should assign a YES or NO value to prior to making this call
RootDomain_SetDisableClamShellSleep(connection, enable);
IOServiceClose(connection);
I have no personal experience with the PM root domain, but I do have extensive experience with IOKit, so here goes:
You want IOPMrootDomain::setDisableClamShellSleep() to be called.
A code search for sites calling setDisableClamShellSleep() quickly reveals a location in RootDomainUserClient::externalMethod(), in the file iokit/Kernel/RootDomainUserClient.cpp. This is certainly promising, as externalMethod() is what gets called in response to user space programs calling the IOConnectCall*() family of functions.
Let's dig in:
IOReturn RootDomainUserClient::externalMethod(
uint32_t selector,
IOExternalMethodArguments * arguments,
IOExternalMethodDispatch * dispatch __unused,
OSObject * target __unused,
void * reference __unused )
{
IOReturn ret = kIOReturnBadArgument;
switch (selector)
{
…
…
…
case kPMSetClamshellSleepState:
fOwner->setDisableClamShellSleep(arguments->scalarInput[0] ? true : false);
ret = kIOReturnSuccess;
break;
…
So, to invoke setDisableClamShellSleep() you'll need to:
Open a user client connection to IOPMrootDomain. This looks straightforward, because:
Upon inspection, IOPMrootDomain has an IOUserClientClass property of RootDomainUserClient, so IOServiceOpen() from user space will by default create an RootDomainUserClient instance.
IOPMrootDomain does not override the newUserClient member function, so there are no access controls there.
RootDomainUserClient::initWithTask() does not appear to place any restrictions (e.g. root user, code signing) on the connecting user space process.
So it should simply be a case of running this code in your program:
io_connect_t connection = IO_OBJECT_NULL;
IOReturn ret = IOServiceOpen(
root_domain_service,
current_task(),
0, // user client type, ignored
&connection);
Call the appropriate external method.
From the code excerpt earlier on, we know that the selector must be kPMSetClamshellSleepState.
arguments->scalarInput[0] being zero will call setDisableClamShellSleep(false), while a nonzero value will call setDisableClamShellSleep(true).
This amounts to:
IOReturn RootDomain_SetDisableClamShellSleep(io_connect_t root_domain_connection, bool disable)
{
uint32_t num_outputs = 0;
uint64_t inputs[] = { disable ? 1 : 0 };
return IOConnectCallScalarMethod(
root_domain_connection, kPMSetClamshellSleepState,
&inputs, 1, // 1 = length of array 'inputs'
NULL, &num_outputs);
}
When you're done with your io_connect_t handle, don't forget to IOServiceClose() it.
This should let you toggle clamshell sleep on or off. Note that there does not appear to be any provision for automatically resetting the value to its original state, so if your program crashes or exits without cleaning up after itself, whatever state was last set will remain. This might not be great from a user experience perspective, so perhaps try to defend against it somehow, for example in a crash handler.

Switch structure control for OSC msg

I’m new to SC and the whole music programming thing in general.
I’ve done a python app, that reads a text and sends word by word to SC through OSC.
The text is only the words ‘miau’ and ‘guau’ repeated for fun and to try it out.
Another thing kind of weird happening, is that on the SC console I get three times the same word than on the text and on terminal (that python prints each word). So for each ‘miau’ on the txt/terminal, I get ‘miau miau miau’ on the SC console.
The OSC communication is working, but I hear no sound. I’ve played my buffers separately, and they are working.
When I play the buffers or the SynthDef I can hear the samples being played, so I assume the issue is on the switch.
s.boot;
~b0 = Buffer.read(s, "/path/to/bd/BT0A0A7.wav")
~b1 =Buffer.read(s, "/path/to/hh/000_hh3closedhh.wav")
~b0.play;
(
SynthDef.new(\playbuf, {|amp=1, out=0, buf, da=2, rate =1|
var sig;
sig = PlayBuf.ar(2, buf, BufRateScale.kr(buf) * rate, doneAction:da);
sig = sig*amp;
Out.ar(out, sig);
}).add;
)
Synth.new(\playbuf, [\buf, ~b1.bufnum]);
(
OSCdef.new("texto",{
|msg, time, addr, port|
msg[1].postln;
switch(msg[1],
"miau", {Synth.new(\playbuf, [\buf, ~b1.bufnum])},
"guau", {Synth.new(\playbuf, [\buf, ~b0.bufnum])}
);
},
'/supercollider',
)
)
Although it copies the text, so I know the OSC is working, the samples won't play.
Any tip appreciated!
Just use single quotes instead of double quotes in the switch. Like this:
(
OSCdef(\texto,{ |msg|
switch ( msg[1],
'miau', { Synth.new(\playbuf, [\buf, ~b1.bufnum] ) },
'guau', { Synth.new(\playbuf, [\buf, ~b0.bufnum] ) }
)
}, '/supercollider' )
)
saudações,
Gil
Edited to add:
Text surrounded by double quotes are strings. Text surrounded by single quotes are symbols. These are different classes. OSC messages use symbols instead of strings, which is why using double quotes won't work.
Symbols look like any of the following:
'symbol'
\symbol
"symbol".asSymbol
'symbol'.asSymbol
Those last two can help if for some reason you have text, but you aren't sure what it is.

Workaround for a certain IOServiceOpen() call requiring root privileges

Background
It is possible to perform a software-controlled disconnection of the power adapter of a Mac laptop by creating an DisableInflow power management assertion.
Code from this answer to an SO question can be used to create said assertion. The following is a working example that creates this assertion until the process is killed:
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <unistd.h>
int main()
{
IOPMAssertionID neverSleep = 0;
IOPMAssertionCreateWithName(kIOPMAssertionTypeDisableInflow,
kIOPMAssertionLevelOn,
CFSTR("disable inflow"),
&neverSleep);
while (1)
{
sleep(1);
}
}
This runs successfully and the power adapter is disconnected by software while the process is running.
What's interesting, though, is that I was able to run this code as a regular user, without root privileges, which wasn't supposed to happen. For instance, note the comment in this file from Apple's open source repositories:
// Disables AC Power Inflow (requires root to initiate)
#define kIOPMAssertionTypeDisableInflow CFSTR("DisableInflow")
#define kIOPMInflowDisableAssertion kIOPMAssertionTypeDisableInflow
I found some code which apparently performs the actual communication with the charger; it can be found here. The following functions, from this file, appears to be of particular interest:
IOReturn
AppleSmartBatteryManagerUserClient::externalMethod(
uint32_t selector,
IOExternalMethodArguments * arguments,
IOExternalMethodDispatch * dispatch __unused,
OSObject * target __unused,
void * reference __unused )
{
if (selector >= kNumBattMethods) {
// Invalid selector
return kIOReturnBadArgument;
}
switch (selector)
{
case kSBInflowDisable:
// 1 scalar in, 1 scalar out
return this->secureInflowDisable((int)arguments->scalarInput[0],
(int *)&arguments->scalarOutput[0]);
break;
// ...
}
// ...
}
IOReturn AppleSmartBatteryManagerUserClient::secureInflowDisable(
int level,
int *return_code)
{
int admin_priv = 0;
IOReturn ret = kIOReturnNotPrivileged;
if( !(level == 0 || level == 1))
{
*return_code = kIOReturnBadArgument;
return kIOReturnSuccess;
}
ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
admin_priv = (kIOReturnSuccess == ret);
if(admin_priv && fOwner) {
*return_code = fOwner->disableInflow( level );
return kIOReturnSuccess;
} else {
*return_code = kIOReturnNotPrivileged;
return kIOReturnSuccess;
}
}
Note how, in secureInflowDisable(), root privileges are checked for prior to running the code. Note also this initialization code in the same file, again requiring root privileges, as explicitly pointed out in the comments:
bool AppleSmartBatteryManagerUserClient::initWithTask(task_t owningTask,
void *security_id, UInt32 type, OSDictionary * properties)
{
uint32_t _pid;
/* 1. Only root processes may open a SmartBatteryManagerUserClient.
* 2. Attempts to create exclusive UserClients will fail if an
* exclusive user client is attached.
* 3. Non-exclusive clients will not be able to perform transactions
* while an exclusive client is attached.
* 3a. Only battery firmware updaters should bother being exclusive.
*/
if ( kIOReturnSuccess !=
clientHasPrivilege(owningTask, kIOClientPrivilegeAdministrator))
{
return false;
}
// ...
}
Starting from the code from the same SO question above (the question itself, not the answer), for the sendSmartBatteryCommand() function, I wrote some code that calls the function passing kSBInflowDisable as the selector (the variable which in the code).
Unlike the code using assertions, this one only works as root. If running as a regular user, IOServiceOpen() returns, weirdly enough, kIOReturnBadArgument (not kIOReturnNotPrivileged, as I would have expected). Perhaps this has to do with the initWithTask() method above.
The question
I need to perform a call with a different selector to this same Smart Battery Manager kext. Even so, I can't even get to the IOConnectCallMethod() since IOServiceOpen() fails, presumably because the initWithTask() method prevents any non-root users from opening the service.
The question, therefore, is this: how is IOPMAssertionCreateWithName() capable of creating a DisableInflow assertion without root privileges?
The only possibility I can think of is if there's a root-owned process to which requests are forwarded, and which performs the actual work of calling IOServiceOpen() and later IOConnectCallMethod() as root.
However, I'm hoping there's a different way of calling the Smart Battery Manager kext which doesn't require root (one that doesn't involve the IOServiceOpen() call.) Using IOPMAssertionCreateWithName() itself is not possible in my application, since I need to call a different selector within that kext, not the one that disables inflow.
It's also possible this is in fact a security vulnerability, which Apple will now fix in a future release as soon as it is alerted to this question. That would be too bad, but understandable.
Although running as root is a possibility in macOS, it's obviously desirable to avoid privilege elevation unless absolutely necessary. Also, in the future I'd like to run the same code under iOS, where it's impossible to run anything as root, in my understanding (note this is an app I'm developing for my own personal use; I understand linking to IOKit wipes out any chance of getting the app published in the App Store).

Setting device identifier using LoadKeyboardLayout()

The problem is this: I must set the user's language to Simplified Chinese and the keyboard to "Chinese (Simplified) - Microsoft Pinyin New Experience Input st".
By setting this combo manually in the Control Panel (Region -> Keyboards and languages) and then running a small test program which calls GetKeyboardLayoutName(), I've found out that the KLID is 00000804 (supposedly). If I remove Chinese from Keyboards and languages in the Control Panel and run this:
HKL hKeyboardLayout = ::LoadKeyboardLayout(_T("00000804"), KLF_ACTIVATE | KLF_SETFORPROCESS);
Then the language is indeed changed to Chinese, but the keyboard settings are wrong. The little "IME box" is missing when typing something.
The MSDN page for LoadKeyboardLayout() says this about the pwszKLID parameter:
The name of the input locale identifier to load. This name is a string composed of the hexadecimal value of the Language Identifier (low word) and a device identifier (high word). For example, U.S. English has a language identifier of 0x0409, so the primary U.S. English layout is named "00000409". Variants of U.S. English layout (such as the Dvorak layout) are named "00010409", "00020409", and so on.
So it appears as if GeyKeyboardLayout() only reports the language identifier (0x0804 for Chinese), but the "device identifier" is missing. How can I find out the device identifier for "Microsoft Pinyin New Experience Input st"?
Found the solution. Apparently in Vista (and onwards) you must use InstallLayoutOrTip() with the correct GUID (not KLID) to install the proper language-keyboard combo. Then you can call LoadKeyboardLayout() to load it.
typedef HRESULT (WINAPI *PTF_INSTALLLAYOUTORTIP)(LPCWSTR psz, DWORD dwFlasg);
// Install.
HMODULE hInputDLL = LoadLibrary(_T("input.dll"));
BOOL bRet = FALSE;
if(hInputDLL == NULL)
{
// Error
}
else
{
PTF_INSTALLLAYOUTORTIP pfnInputLayoutOrTip;
pfnInputLayoutOrTip = (PTF_INSTALLLAYOUTORTIP)GetProcAddress(hInputDLL, "InstallLayoutOrTip");
if(pfnInputLayoutOrTip)
{
bRet = (*pfnInputLayoutOrTip)(_T("0804:{81D4E9C9-1D3B-41BC-9E6C-4B40BF79E35E}{F3BA9077-6C7E-11D4-97FA-0080C882687E}"), 0);
if(! bRet)
{
// Error
}
}
else
{
// Error
}
FreeLibrary(hInputDLL);
}
// Load.
HKL hKeyboardLayout = ::LoadKeyboardLayout(_T("00000804"), KLF_ACTIVATE | KLF_SETFORPROCESS);
References:
http://msdn.microsoft.com/library/bb847909.aspx
http://www.siao2.com/2007/12/01/6631463.aspx

Resources