Skip to content

Commit

Permalink
feat: add support for multiple bars (#8)
Browse files Browse the repository at this point in the history
SketchyBar allows for multiple bar by symlinking to `sketchybar` and
running a second process (eg. `bottombar`). In order for the helper to
find the `mach` port for the targeted bar we need to provide the bar
processes name.

FelixKratz/SketchyBar#353 (comment)
FelixKratz/SketchyBarHelper#2
  • Loading branch information
johnallen3d committed Jan 6, 2024
1 parent a55e21f commit c856abc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ license = "MIT"
libc = "0.2.151"

[build-dependencies]
cc = "1.0.84"
cc = "1.0.83"
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Error for SketchybarError {}

#[link(name = "sketchybar", kind = "static")]
extern "C" {
fn sketchybar(message: *mut i8) -> *mut i8;
fn sketchybar(message: *mut i8, bar_name: *mut i8) -> *mut i8;
}

/// Sends a message to `SketchyBar` and returns the response.
Expand All @@ -38,6 +38,10 @@ extern "C" {
///
/// * `message` - A string slice containing the message to be sent to
/// `SketchyBar`.
/// * `bar_name` - An optional string slice containing the name of the process
/// of the target bar. This defaults to `sketchybar` however, if you're using a
/// secondary bar (eg. a `bottombar`) you can override the default there to pass
/// a message to this other bar.
///
/// # Returns
///
Expand Down Expand Up @@ -68,12 +72,18 @@ extern "C" {
/// println!("Response from SketchyBar: {}", response);
/// }
/// ```
pub fn message(message: &str) -> Result<String, SketchybarError> {
pub fn message(
message: &str,
bar_name: Option<&str>,
) -> Result<String, SketchybarError> {
let command = CString::new(message)
.map_err(|_| SketchybarError::MessageConversionError)?;

let bar_name = CString::new(bar_name.unwrap_or("sketchybar"))
.map_err(|_| SketchybarError::MessageConversionError)?;

let result = unsafe {
CStr::from_ptr(sketchybar(command.into_raw()))
CStr::from_ptr(sketchybar(command.into_raw(), bar_name.into_raw()))
.to_string_lossy()
.into_owned()
};
Expand Down
11 changes: 7 additions & 4 deletions src/sketchybar.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ char* env_get_value_for_key(env env, char* key) {
return (char*)"";
}

mach_port_t mach_get_bs_port() {
mach_port_t mach_get_bs_port(char* bar_name) {
mach_port_name_t task = mach_task_self();

mach_port_t bs_port;
Expand All @@ -60,9 +60,12 @@ mach_port_t mach_get_bs_port() {
return 0;
}

char service_name[256]; // Assuming the service name will not exceed 255 chars
snprintf(service_name, sizeof(service_name), "git.felix.%s", bar_name);

mach_port_t port;
if (bootstrap_look_up(bs_port,
"git.felix.sketchybar",
service_name,
&port ) != KERN_SUCCESS) {
return 0;
}
Expand Down Expand Up @@ -190,7 +193,7 @@ bool mach_server_begin(struct mach_server* mach_server, mach_handler handler, ch
}
#pragma clang diagnostic pop

char* sketchybar(char* message) {
char* sketchybar(char* message, char* bar_name) {
uint32_t message_length = strlen(message) + 1;
char formatted_message[message_length + 1];

Expand All @@ -213,7 +216,7 @@ char* sketchybar(char* message) {
}

formatted_message[caret] = '\0';
if (!g_mach_port) g_mach_port = mach_get_bs_port();
if (!g_mach_port) g_mach_port = mach_get_bs_port(bar_name);
char* response = mach_send_message(g_mach_port,
formatted_message,
caret + 1 );
Expand Down

0 comments on commit c856abc

Please sign in to comment.