Skip to content

Commit

Permalink
added the sneaky quit functionality, tried to improve some stuff, but…
Browse files Browse the repository at this point in the history
… that didn't work
  • Loading branch information
MHanak1 committed Feb 7, 2024
1 parent 3f86737 commit 026fafd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 153 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ simply download the executable file suitable for your os and run it. **only run

i will not provide instructions on how to compile this, since if you don't know how to do it maybe the safeguarded version will be enough.

## How do i stop this?
while focused on any of the windows type :q!

## Known incompatibilities
The program will not work with wayland, since it doesn't allow for programs to change their position (to my knowledge)

75 changes: 63 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use sysinfo::{ProcessRefreshKind, RefreshKind, System};
use webbrowser;
use winit::dpi::{LogicalPosition, LogicalSize};
use winit::event::{Event, StartCause, WindowEvent};
use winit::keyboard::Key;
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{WindowBuilder, WindowLevel};


fn main() {
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(
Expand All @@ -24,6 +26,11 @@ fn main() {
.build(&event_loop)
.unwrap(),
);

let inbufsize = 3;
let mut input_buffer: [char; 3] = [' ', ' ', ' '];


let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

Expand All @@ -42,43 +49,62 @@ fn main() {
let mut xup = rand::random::<bool>();
let mut yup = rand::random::<bool>();


let mut refresh_rate: u32 = 60;

//let move_delay = Duration::from_millis(30);
let min_speed = 100.0;
let max_speed = 300.0;

let mut move_ammount = 0.0;
println!("{}", move_ammount);

let _persistent_mode = false; //SPICY!!! NO RECOMMEND!!! wether the program should save itself to the autostart (not implemented)
let _persistent_mode = false; //SPICY!!! NO RECOMMEND!!! wether the program should save itself onto the desktop and autostart (not implemented)
let max_instances: i32 = 30; //set to -1 to disable
let clone_ammount = 3; //how many new pierogis to spawn when the app is closed
let closable_window = true; //wether the app should close on alt + f4 or remain open
let random_event_chance = 1.0; //chance for a random event to happen per second if i did the math correctly (see fn random_event())
let open_browser = false; //enable browser opening random event
let _vim_like_exit = false; //wether to stop the program when :q! is typed on a keyboard (not implemented)
let vim_like_exit = true; //wether to stop the program when :q! is typed on a keyboard

if (get_running_instances() as i32 > max_instances) && (max_instances > -1) {
process::exit(0);
}
println!("{}", get_running_instances());

window.set_resizable(false);
// window.set_always_on_top(true); //why you no exist???
//window.set_min_inner_size(Some(LogicalSize::new(w, h)));
//window.set_max_inner_size(Some(LogicalSize::new(w, h)));
let _ = window.request_inner_size(LogicalSize::new(w as f64 / window.scale_factor(), h as f64 / window.scale_factor()));

//event_loop.set_control_flow(ControlFlow::Wait);

event_loop.set_control_flow(ControlFlow::WaitUntil(
Instant::now().checked_add(Duration::from_millis((1000/refresh_rate).into())).unwrap(),
));
event_loop
.run(move |event, elwt| {
match event {



Event::WindowEvent{
event: WindowEvent::KeyboardInput{
event,
is_synthetic: false, ..
}, ..
} => {
if !event.repeat && event.state.is_pressed() && vim_like_exit {
match event.logical_key {
Key::Character(key) => {
input_buffer.rotate_left(1);
input_buffer[inbufsize-1] = key.chars().nth(0).unwrap();
println!("{:?}", input_buffer);
if input_buffer == [':', 'q', '!']{
kill_other_instances();
elwt.exit();
}

}
_ => {}
}
}
}

Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
Expand Down Expand Up @@ -116,6 +142,8 @@ fn main() {
elwt.exit();
}
}

// why did i write this???
Event::WindowEvent{
event: WindowEvent::CloseRequested,
window_id,
Expand Down Expand Up @@ -195,10 +223,28 @@ fn get_running_instances() -> u32 {
);
let mut i = 0;
for process in s.processes_by_exact_name(&get_program_name().unwrap()) {
println!("{}\t{:?}", process.name(), process.group_id());
i += 1;

println!("{}\t{:?}\t{}", process.name(), process.pid(), process.parent().unwrap().as_u32());
//if process.parent().unwrap().as_u32() == 1{ // this only sometimes works, checks if it
i += 1; //is a top level process
//}
}
return i / 5; //for some yet unknown reason each pierogi instance spawns 4 more children, so
//this cheap hack accounts for that
}

fn kill_other_instances() {
let s = System::new_with_specifics(
RefreshKind::new().with_processes(ProcessRefreshKind::everything()),
);
for process in s.processes_by_exact_name(&get_program_name().unwrap()) {
let parent_id = process.parent().unwrap().as_u32();

println!("{}\t{}\t{}", process.pid().as_u32(), parent_id, process::id());
if process.pid().as_u32() != process::id() && parent_id != process::id(){
process.kill();
}
}
return i / 5;
}

fn random_event(open_browser: bool) {
Expand All @@ -208,6 +254,11 @@ fn random_event(open_browser: bool) {
"https://aniagotuje.pl/przepis/pierogi-ruskie",
"https://www.google.com/search?q=pierogi+ruskie",
"https://translate.google.com/?sl=pl&tl=en&text=Pierogi&op=translate",
// "pierogi",
// "test",
// "google translate",
//just a little testin'

];

//println!("opened browser");
Expand Down
141 changes: 0 additions & 141 deletions src/main.rs.bak

This file was deleted.

0 comments on commit 026fafd

Please sign in to comment.