Skip to content

Instantly share code, notes, and snippets.

@ricewind012
Created March 15, 2025 08:02
Show Gist options
  • Select an option

  • Save ricewind012/ae8644882f764fc4c56e92fecb4dce00 to your computer and use it in GitHub Desktop.

Select an option

Save ricewind012/ae8644882f764fc4c56e92fecb4dce00 to your computer and use it in GitHub Desktop.
some firefox mpd ctypes shit, does this even work lmaooo
mpd_settings = new ctypes.StructType("mpd_settings", [
{ host: ctypes.char.ptr },
{ port: ctypes.unsigned },
{ timeout_ms: ctypes.unsigned },
{ password: ctypes.char.ptr },
]);
mpd_audio_format = new ctypes.StructType("mpd_audio_format", [
{ sample_rate: ctypes.uint32_t },
{ bits: ctypes.uint8_t },
{ channels: ctypes.uint8_t },
{ reserved1: ctypes.uint32_t },
]);
mpd_status = new ctypes.StructType("mpd_status", [
{ volume: ctypes.int },
{ repeat: ctypes.bool },
{ random: ctypes.bool },
{ single: ctypes.int },
{ consume: ctypes.int },
{ queue_length: ctypes.unsigned },
{ queue_version: ctypes.unsigned },
{ state: ctypes.int },
{ crossfade: ctypes.unsigned },
{ mixrampdb: ctypes.float },
{ mixrampdelay: ctypes.float },
{ song_pos: ctypes.int },
{ song_id: ctypes.int },
{ next_song_pos: ctypes.int },
{ next_song_id: ctypes.int },
{ elapsed_time: ctypes.unsigned },
{ elapsed_ms: ctypes.unsigned },
{ total_time: ctypes.unsigned },
{ kbit_rate: ctypes.unsigned },
{ audio_format: mpd_audio_format },
{ update_id: ctypes.unsigned },
{ partition: ctypes.char.ptr },
{ error: ctypes.char.ptr },
]);
mpd_error_info = new ctypes.StructType("mpd_error_info", [
{ code: ctypes.int },
{ server: ctypes.int },
{ at: ctypes.unsigned },
{ system: ctypes.int },
{ message: ctypes.char.ptr },
]);
mpd_buffer = new ctypes.StructType("mpd_buffer", [
{ write: ctypes.unsigned },
{ read: ctypes.unsigned },
{ data: ctypes.unsigned_char },
]);
mpd_socket_t = ctypes.int;
mpd_async = new ctypes.StructType("mpd_async", [
{ fd: mpd_socket_t },
{ error: mpd_error_info },
{ input: mpd_buffer },
{ output: mpd_buffer },
]);
mpd_timeout = new ctypes.StructType("mpd_timeout", [
{ tv_sec: ctypes.int },
{ tv_usec: ctypes.int },
]);
mpd_pair = new ctypes.StructType("mpd_pair", [
{ name: ctypes.char.ptr },
{ value: ctypes.char.ptr },
]);
mpd_parser = new ctypes.StructType("mpd_parser", [
{ result: ctypes.int },
{ discrete: ctypes.bool },
{ error: mpd_error_info },
{ pair: mpd_pair },
]);
mpd_connection = new ctypes.StructType("mpd_settings", [
{ initial_settings: mpd_settings },
{ settings: mpd_settings },
{ version: ctypes.unsigned },
{ error: mpd_error_info },
{ async: mpd_async },
{ timeout: mpd_timeout },
{ parser: mpd_parser },
{ receiving: ctypes.bool },
{ sending_command_list: ctypes.bool },
{ sending_command_list_ok: ctypes.bool },
{ discrete_finished: ctypes.bool },
{ command_list_remaining: ctypes.int },
{ pair: mpd_pair },
{ pair_state: ctypes.int },
{ request: ctypes.char.ptr },
]);
mpd_tag_value = new ctypes.StructType("mpd_tag_value", [
{ value: ctypes.char.ptr },
]);
time_t = ctypes.int;
mpd_song = new ctypes.StructType("mpd_song", [
{ uri: ctypes.char.ptr },
{ tags: mpd_tag_value },
{ duration: ctypes.unsigned },
{ duration_ms: ctypes.unsigned },
{ start: ctypes.unsigned },
{ end: ctypes.unsigned },
{ last_modified: time_t },
{ added: time_t },
{ pos: ctypes.unsigned },
{ id: ctypes.unsigned },
{ prio: ctypes.unsigned },
{ finished: ctypes.bool },
{ audio_format: mpd_audio_format },
]);
/**
* Wrapper for easily getting `null` instead of being met with a shitty error.
* @todo recursive ver
*/
// biome-ignore lint/style/noVar: <explanation>
var wrapCtypesNulls = (struct, value) =>
struct.fields.flatMap(Object.keys).reduce((obj, key) => {
obj[key] = value[key].isNull?.() ? null : value[key];
return obj;
}, {});
function library(lib, decls) {
const library = ctypes.open(lib);
const dec = Object.entries(decls).map(([name, args]) =>
library.declare(name, ...args),
);
return [library, ...dec];
}
// biome-ignore lint/style/noVar: <explanation>
var [
libmpdclient,
mpd_connection_free,
mpd_connection_new,
mpd_run_current_song,
mpd_settings_new,
mpd_song_free,
] = library("libmpdclient.so", {
mpd_connection_free: [ctypes.default_abi, ctypes.void_t, mpd_connection],
mpd_connection_new: [
ctypes.default_abi,
mpd_connection,
ctypes.char.ptr,
ctypes.unsigned_int,
ctypes.unsigned_int,
],
mpd_run_current_song: [ctypes.default_abi, mpd_song, mpd_connection],
mpd_settings_new: [
ctypes.default_abi,
mpd_settings,
ctypes.char.ptr,
ctypes.unsigned,
ctypes.unsigned,
ctypes.char.ptr,
ctypes.char.ptr,
],
mpd_song_free: [ctypes.default_abi, ctypes.void_t, mpd_song],
});
conn = mpd_connection_new(null, 0, 0);
settings = wrapCtypesNulls(
mpd_settings,
mpd_settings_new(null, 0, 0, null, null),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment