2022-11-16 22:55:11 +01:00
|
|
|
const std = @import("std");
|
2023-07-03 19:10:00 +02:00
|
|
|
const info = @import("info.zig");
|
2022-11-16 22:55:11 +01:00
|
|
|
|
2024-02-11 13:10:53 +01:00
|
|
|
pub const std_options = std.Options{
|
|
|
|
.log_level = .debug,
|
2024-03-20 20:45:59 +01:00
|
|
|
.logFn = @import("common").logFn,
|
2023-04-07 15:38:12 +02:00
|
|
|
};
|
2022-11-16 22:55:11 +01:00
|
|
|
|
|
|
|
const browsers = &[_][]const u8{
|
2023-08-28 21:09:34 +02:00
|
|
|
"brave",
|
2022-11-16 22:55:11 +01:00
|
|
|
"firefox",
|
2023-07-01 19:20:02 +02:00
|
|
|
"luakit",
|
2022-11-16 22:55:11 +01:00
|
|
|
"chromium",
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
const alloc = gpa.allocator();
|
|
|
|
|
2023-07-03 19:10:00 +02:00
|
|
|
var queries: [browsers.len]info.ProcessQuery = undefined;
|
|
|
|
for (browsers, &queries) |b, *q|
|
|
|
|
q.* = .{ .name = b };
|
2022-11-16 22:55:11 +01:00
|
|
|
|
2023-07-03 19:10:00 +02:00
|
|
|
try info.query(alloc, &queries);
|
|
|
|
defer for (&queries) |*q| q.deinit(alloc);
|
2022-11-16 22:55:11 +01:00
|
|
|
|
2023-07-03 19:10:00 +02:00
|
|
|
for (queries) |q| {
|
|
|
|
if (q.found_exepath) |path| {
|
|
|
|
std.log.info("found running browser: {s}", .{path});
|
2022-11-16 22:55:11 +01:00
|
|
|
|
2023-07-03 19:10:00 +02:00
|
|
|
try start(q.name, alloc);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-16 22:55:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std.log.info("no running browser, using first choice", .{});
|
|
|
|
try start(browsers[0], alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(browser: []const u8, alloc: std.mem.Allocator) !void {
|
|
|
|
// args to browser will be same length as argv
|
|
|
|
const argv = try alloc.alloc([]const u8, std.os.argv.len);
|
|
|
|
defer alloc.free(argv);
|
|
|
|
argv[0] = browser;
|
|
|
|
|
2023-02-23 10:56:34 +01:00
|
|
|
for (std.os.argv[1..], 0..) |arg, i| {
|
2022-11-16 22:55:11 +01:00
|
|
|
argv[i + 1] = std.mem.span(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
std.log.info("child argv: {s}", .{argv});
|
|
|
|
|
|
|
|
var child = std.ChildProcess.init(argv, alloc);
|
|
|
|
_ = try child.spawnAndWait();
|
|
|
|
}
|