reversi working, fixes

This commit is contained in:
stjet
2024-12-06 03:10:45 +00:00
parent eec8b055d3
commit 5da36108fe
54 changed files with 466 additions and 28 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
target/
Cargo.lock
ming-wm

33
README.md Normal file
View File

@@ -0,0 +1,33 @@
## Running
For best performance:
```
cargo build --release
./target/release/main
```
Though just `cargo run --release` can be done.
## Config
### Desktop Backgrounds
In `$XDG_CONFIG_DIR/ming-wm/desktop-background`, you can configure what the desktop background should be for each workspace. The first line decides the background for the first workspace, and so on. If lines are missing, or empty, or the config file is missing, the default green background is used.
If a line starts with "#", and is followed by 6 lowercase hex characters, then it will interpreted as a RGB colour.
If a line starts with "r", then what follows with be interpreted as a path to a BMP image file in BGRA order, and if it starts with any other character, what follows will be interpreted as a path to a BMP image file in RGBA order.
Example:
```
#008080
#003153
r./bmps/castle1440x842.bmp
r./bmps/ming1440x842.bmp
r./bmps/blur1440x842.bmp
```
//

BIN
bmps/blur1440x842.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

BIN
bmps/ming1440x842.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

View File

@@ -0,0 +1,10 @@
3
0,0,62,65,0,0,0,0,3,88,172
0,0,26,193,0,0,0,1,96,171,10
0,0,20,149,36,14,0,28,32,21,0
111,106,173,176,82,159,12,0,84,148,0
48,81,149,22,0,112,52,1,26,215,27
0,8,170,0,0,138,34,8,57,112,1
0,132,80,0,0,185,1,0,0,0,0
47,195,5,77,79,151,0,0,0,0,0
71,43,0,98,207,22,0,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 B

View File

@@ -0,0 +1,11 @@
2
13,171,100,0,0,0,6,101,49
35,16,148,113,30,25,130,57,147
10,128,147,173,44,17,48,117,0
29,0,0,54,162,249,134,0,0
58,134,152,160,205,84,0,0,0
0,0,0,0,24,169,0,0,0
0,49,92,114,109,207,71,0,0
84,50,0,0,46,172,41,0,0
138,34,0,0,0,0,0,0,0
27,181,216,224,72,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

View File

@@ -0,0 +1,11 @@
2
0,14,180,16,0,0,0
0,34,223,9,1,23,0
0,173,71,34,42,75,174
70,112,0,1,159,91,33
125,0,0,0,8,14,0
131,3,0,0,0,0,0
41,135,0,0,0,0,0
0,144,82,0,0,0,0
0,39,220,1,0,0,0
0,0,176,21,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

View File

@@ -0,0 +1,12 @@
1
0,0,0,0,0,40,20,46,89,146
108,4,0,0,0,59,206,99,141,58
119,75,0,0,0,8,202,100,65,0
126,70,0,19,49,84,230,162,25,0
163,10,9,0,60,91,179,0,0,0
150,0,80,0,0,0,180,0,0,0
142,82,38,0,0,0,190,0,0,0
164,162,0,0,0,24,170,0,0,0
185,134,0,0,0,92,84,0,0,0
106,145,0,0,5,147,4,0,0,0
5,84,0,13,77,15,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

View File

@@ -0,0 +1,9 @@
4
0,101,134,133,119,2,63,19
0,0,74,174,105,76,50,163
0,0,41,0,0,50,162,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
30,0,0,0,0,0,0,0
152,15,0,0,23,33,0,0
69,194,217,246,232,172,1,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 B

View File

@@ -0,0 +1,6 @@
7
0,0,37,127,96,83,109,44
99,170,147,15,0,0,1,170
18,38,0,0,0,0,4,195
0,0,0,0,0,7,151,102
0,0,9,57,81,114,45,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

View File

@@ -0,0 +1,10 @@
3
0,9,53,118,164,193,0,0,0,0
0,0,94,102,95,137,0,0,0,0
0,0,0,19,158,2,8,3,0,0
0,0,0,137,24,0,9,173,0,0
0,34,121,158,102,102,49,214,22,0
71,78,168,5,0,4,115,233,145,2
125,118,82,0,0,0,171,56,155,164
186,154,0,0,0,116,97,0,0,45
0,0,0,53,113,63,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

View File

@@ -0,0 +1,11 @@
2
0,36,192,7,0,0,0,0,0,0
16,0,174,183,41,0,0,0,0,0
96,144,205,87,2,0,0,0,1,0
0,38,124,0,0,0,0,74,142,61
87,72,209,17,0,0,0,66,26,206
128,6,189,0,0,0,7,63,87,139
60,168,174,0,0,0,23,73,0,0
0,117,94,0,0,0,23,118,0,0
0,115,21,0,0,0,83,162,0,0
0,36,155,157,184,186,162,33,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

View File

@@ -0,0 +1,10 @@
3
0,0,0,16,173,0,0,0
71,6,0,0,194,0,0,0
39,107,97,130,196,154,107,1
11,223,27,104,57,0,112,113
131,113,113,148,0,0,4,213
150,0,158,191,7,0,1,232
151,31,148,36,5,0,55,189
149,160,13,0,0,34,187,42
0,0,0,45,82,106,11,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 B

View File

@@ -0,0 +1,12 @@
1
0,111,65,0,0,0
0,66,123,0,0,0
85,136,55,0,0,0
7,159,233,41,0,0
1,142,52,4,0,0
73,110,1,79,56,0
155,134,29,1,126,52
42,183,156,19,11,180
3,112,0,0,0,207
0,145,3,0,70,193
0,62,180,191,177,29

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 B

View File

@@ -0,0 +1,11 @@
2
0,11,72,59,70,117,0,0,0,0
0,163,57,39,116,168,40,54,14,0
0,134,22,14,116,124,88,63,131,106
0,16,170,171,32,0,0,0,0,197
72,160,209,57,10,99,33,0,50,200
59,92,6,158,0,15,141,196,170,38
0,0,0,117,58,0,0,0,0,0
0,0,0,36,173,0,0,0,0,0
0,0,0,0,225,25,0,0,0,0
0,0,0,0,155,64,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

View File

@@ -0,0 +1,11 @@
2
69,0,0,56,139,142,73,8,0
161,37,4,145,52,133,26,134,32
156,11,119,62,0,92,70,11,155
138,49,123,50,0,76,95,0,169
139,127,39,80,0,82,94,0,183
187,136,9,147,0,110,71,22,176
121,157,0,129,112,172,76,191,53
5,54,0,1,110,211,142,39,0
0,0,0,0,118,20,0,0,0
0,0,2,73,26,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

View File

@@ -0,0 +1,11 @@
2
0,0,6,184,53,0,0
0,0,0,128,70,0,0
0,0,0,118,53,61,40
0,0,0,114,155,141,59
0,0,0,105,50,0,0
0,0,0,93,64,0,0
0,0,0,81,93,0,0
32,119,140,180,155,9,0
142,10,0,100,196,217,98
117,152,167,188,36,18,112

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

View File

@@ -0,0 +1,11 @@
2
0,34,182,28,0,0
37,81,138,63,0,0
133,0,0,0,0,0
160,0,0,0,0,0
153,0,0,0,0,0
147,0,76,77,113,48
211,179,100,0,2,183
99,110,0,0,6,222
0,0,0,8,155,112
1,58,75,107,39,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

View File

@@ -0,0 +1,11 @@
2
110,24,0,0
101,39,0,0
129,97,141,46
170,64,8,170
213,0,0,183
199,0,0,191
117,22,1,191
0,0,56,132
0,3,159,20
25,93,26,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

View File

@@ -0,0 +1,10 @@
3
0,47,77,165,178,56,0
0,68,158,54,210,54,0
0,0,5,167,49,0,0
0,0,136,40,0,0,0
0,115,162,89,95,139,40
75,204,15,0,0,11,187
217,42,0,0,0,0,206
28,0,142,168,74,89,165
0,0,127,152,217,140,9

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 B

View File

@@ -0,0 +1,11 @@
2
0,25,169,13,0,0,0,0,0,0,0
0,0,156,31,0,18,93,48,0,0,0
0,0,149,11,76,93,35,189,20,0,0
64,123,219,179,43,0,0,167,33,0,0
92,6,210,47,0,0,0,203,2,0,0
79,119,174,0,0,0,21,185,0,0,0
66,185,134,0,0,0,66,142,0,0,34
161,121,141,0,0,0,74,145,0,38,83
120,82,195,0,0,0,8,171,154,113,0
0,18,192,0,0,0,0,0,0,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

View File

@@ -0,0 +1,10 @@
3
0,0,43,178,213,1,0,0
0,97,185,116,128,0,0,0
0,0,12,158,2,0,0,0
0,0,130,33,42,69,32,0
0,77,186,118,74,74,179,112
28,204,28,0,0,0,7,225
186,101,0,0,0,0,12,203
145,12,0,0,0,14,161,58
0,0,0,11,72,96,22,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 B

View File

@@ -0,0 +1,11 @@
2
0,0,137,33,0,0,0,0,0
0,0,119,60,0,0,0,0,0
0,0,132,17,0,0,0,0,0
0,26,212,134,57,90,125,71,0
126,139,203,118,33,0,1,113,117
44,58,166,0,0,0,0,1,217
13,169,124,0,0,0,0,10,228
167,135,160,0,0,0,0,120,133
90,51,195,0,0,0,90,149,4
0,3,137,0,53,90,61,0,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

View File

@@ -0,0 +1,11 @@
2
0,0,119,88,21,0,0,0
34,30,132,203,53,0,0,0
26,96,154,3,0,0,6,5
0,26,103,0,0,80,78,181
0,140,132,95,77,27,99,167
88,165,1,133,152,125,81,0
187,11,21,170,85,0,0,0
0,24,97,140,52,0,0,0
0,82,40,55,11,0,0,0
0,14,163,176,189,187,9,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 374 B

View File

@@ -0,0 +1,11 @@
2
0,0,0,0,157,13,0,0,0,0
0,0,0,10,229,31,0,0,0,0
0,0,0,141,110,0,0,0,0,0
0,0,54,134,0,0,0,0,0,0
0,5,155,102,107,116,1,0,0,0
0,120,167,18,0,143,58,0,0,0
18,211,12,0,0,131,69,0,0,66
119,103,0,0,0,149,45,0,11,97
222,33,0,0,0,155,60,12,146,16
191,0,0,0,0,55,200,176,37,0

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

View File

@@ -63,6 +63,7 @@ impl WindowLike for AudioPlayer {
fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
let mut instructions = vec![DrawInstructions::Text([2, self.dimensions[1] - LINE_HEIGHT], vec!["times-new-roman".to_string()], if self.command.len() > 0 { self.command.clone() } else { self.response.clone() }, theme_info.text, theme_info.background, None, None)];
if let Some(sink) = &self.sink {
if sink.len() > 0 {
let current = &self.queue[self.queue.len() - sink.len()];
let current_name = current.0.file_name().unwrap().to_string_lossy().into_owned();
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - current_name.len() * MONO_WIDTH as usize / 2, 2], vec!["times-new-romono".to_string(), "shippori-mincho".to_string()], current_name.clone(), theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
@@ -73,6 +74,7 @@ impl WindowLike for AudioPlayer {
let time_string = format!("{}/{}", format_seconds(sink.get_pos().as_secs()), format_seconds(current.1));
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - time_string.len() * MONO_WIDTH as usize / 2, LINE_HEIGHT * 2 + 2], vec!["times-new-romono".to_string()], time_string, theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
}
}
//
instructions
}

View File

@@ -1,6 +1,5 @@
use std::vec::Vec;
use std::vec;
use std::fmt;
use ming_wm::window_manager::{ DrawInstructions, WindowLike, WindowLikeType };
use ming_wm::messages::{ WindowMessage, WindowMessageResponse };
@@ -8,6 +7,24 @@ use ming_wm::framebuffer::{ Dimensions, RGBColor };
use ming_wm::themes::ThemeInfo;
use ming_wm::ipc::listen;
const REVERSI_GREEN: RGBColor = [72, 93, 63];
struct ValidMove {
pub point: [usize; 2],
pub will_flip: Vec<[usize; 2]>,
}
//tried to do some PartialEq implementation but didn't play nice with .contains
//so just do this instead
fn valid_moves_contains(valid_moves: &Vec<ValidMove>, point: &[usize; 2]) -> Option<Vec<[usize; 2]>> {
for valid_move in valid_moves {
if &valid_move.point == point {
return Some(valid_move.will_flip.clone());
}
}
None
}
#[derive(Default, PartialEq)]
enum Tile {
#[default]
@@ -30,7 +47,9 @@ impl Tile {
struct Reversi {
dimensions: Dimensions,
tiles: [[Tile; 8]; 8],
//
current_number: Option<u8>, //the first number of the tile that user wants to place piece on
valid_moves: Vec<ValidMove>,
white_turn: bool, //if false, black turn
}
impl WindowLike for Reversi {
@@ -39,16 +58,39 @@ impl WindowLike for Reversi {
WindowMessage::Init(dimensions) => {
self.dimensions = dimensions;
self.new_tiles();
self.valid_moves = self.get_valid_moves();
WindowMessageResponse::JustRerender
},
WindowMessage::KeyPress(key_press) => {
if let Ok(n) = key_press.key.to_string().parse::<u8>() {
if let Some(current_number) = self.current_number {
let y = current_number as usize;
let x = n as usize;
if let Some(mut will_flip) = valid_moves_contains(&self.valid_moves, &[x, y]) {
self.tiles[y][x] = if self.white_turn { Tile::White } else { Tile::Black };
will_flip.push([x, y]);
for point in will_flip {
self.tiles[point[1]][point[0]] = if self.white_turn { Tile::White } else { Tile::Black };
}
self.white_turn = !self.white_turn;
self.valid_moves = self.get_valid_moves();
}
self.current_number = None;
} else {
self.current_number = Some(n);
}
} else if key_press.key == '𐘁' { //backspace
self.current_number = None;
}
WindowMessageResponse::JustRerender
},
//
_ => WindowMessageResponse::DoNothing,
}
}
fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
let mut instructions = vec![
DrawInstructions::Rect([0, 0], self.dimensions, [72, 93, 63]),
DrawInstructions::Rect([0, 0], self.dimensions, REVERSI_GREEN),
];
let square_width = (self.dimensions[0] - 10) / 8;
for l in 0..9 {
@@ -67,7 +109,16 @@ impl WindowLike for Reversi {
for x in 0..8 {
let tile = &self.tiles[y][x];
if tile == &Tile::Empty {
//
instructions.push(DrawInstructions::Text([x * square_width + square_width / 2, y * square_width + square_width / 2], vec!["times-new-roman".to_string()], format!("{}{}", y, x), theme_info.text, REVERSI_GREEN, None, None));
if valid_moves_contains(&self.valid_moves, &[x, y]).is_some() {
//yellow border
instructions.extend([
DrawInstructions::Rect([5 + x * square_width, 5 + y * square_width], [square_width + 2, 2], [255, 255, 0]),
DrawInstructions::Rect([5 + x * square_width, 5 + y * square_width], [2, square_width], [255, 255, 0]),
DrawInstructions::Rect([5 + (x + 1) * square_width, 5 + y * square_width], [2, square_width], [255, 255, 0]),
DrawInstructions::Rect([5 + x * square_width + 2, 5 + (y + 1) * square_width], [square_width + 2, 2], [255, 255, 0]),
]);
}
} else {
instructions.push(DrawInstructions::Circle([x * square_width + square_width / 2 + 5, y * square_width + square_width / 2 + 5], square_width / 2 - 3, tile.to_color().unwrap()));
}
@@ -103,6 +154,97 @@ impl Reversi {
self.tiles[3][4] = Tile::Black;
self.tiles[4][4] = Tile::White;
}
pub fn get_valid_moves(&self) -> Vec<ValidMove> {
let mut valid_moves = Vec::new();
for y0 in 0..8 {
for x0 in 0..8 {
let current_tile = &self.tiles[y0][x0];
if (current_tile == &Tile::White && self.white_turn) || (current_tile == &Tile::Black && !self.white_turn) {
for t in 0..8 {
let mut potential_move = false; //true once opposite colour tile found
let mut point = [x0, y0];
let mut will_flip = Vec::new();
loop {
let x = point[0];
let y = point[1];
if t == 0 {
//up left
if y > 0 && x > 0 {
point = [x - 1, y - 1];
} else {
break;
}
} else if t == 1 {
//up
if y > 0 {
point = [x, y - 1];
} else {
break;
}
} else if t == 2 {
//up right
if y > 0 && x < 7 {
point = [x, y - 1];
} else {
break;
}
} else if t == 3 {
//left
if x > 0 {
point = [x - 1, y];
} else {
break;
}
} else if t == 4 {
//right
if x < 7 {
point = [x + 1, y];
} else {
break;
}
} else if t == 5 {
//down left
if y < 7 && x > 0 {
point = [x - 1, y + 1];
} else {
break;
}
} else if t == 6 {
//down
if y < 7 {
point = [x, y + 1];
} else {
break;
}
} else if t == 7 {
//down right
if y < 7 && x < 7 {
point = [x + 1, y + 1];
} else {
break;
}
}
let tile = &self.tiles[point[1]][point[0]];
if tile == &Tile::Empty && potential_move {
valid_moves.push(ValidMove {
point,
will_flip,
});
break;
} else if (tile == &Tile::Black && self.white_turn) || (tile == &Tile::White && !self.white_turn) {
will_flip.push(point);
potential_move = true;
} else {
break;
}
}
}
}
}
}
valid_moves
}
}
pub fn main() {

View File

@@ -9,7 +9,7 @@ fn main() {
let mut a = Command::new("cargo").arg("run").arg("-q").arg("--bin").arg("start_menu").stdout(Stdio::piped()).stdin(Stdio::piped()).stderr(Stdio::null()).spawn().unwrap();
a.stdin.unwrap().write_all("subtype\n".to_string().as_bytes());
let mut output = String::new();
a.stdout.as_mut().unwrap().read_to_string(&mut output);
let _ = a.stdout.as_mut().unwrap().read_to_string(&mut output);
println!("{}", output);
//println!("{}", &ron::to_string(&[122, 400]).unwrap());
}

View File

@@ -52,7 +52,7 @@ impl WindowLike for DesktopBackground {
let color = [hex_to_u8(chars.next().unwrap(), chars.next().unwrap()), hex_to_u8(chars.next().unwrap(), chars.next().unwrap()), hex_to_u8(chars.next().unwrap(), chars.next().unwrap())];
return vec![DrawInstructions::Rect([0, 0], self.dimensions, color)];
}
} else {
} else if line.len() > 1 {
//first character of line is either r or any other character, but is not part of the path
return vec![DrawInstructions::Bmp([0, 0], line[1..].to_string(), line.chars().next().unwrap() == 'r')];
}

View File

@@ -4,6 +4,7 @@ use std::io::{ stdin, BufRead };
use ron;
use crate::window_manager::WindowLike;
use crate::logging::log;
/*
pub trait WindowLike {
@@ -26,25 +27,44 @@ pub trait WindowLike {
}
*/
const LOG: bool = false;
pub fn listen(mut window_like: impl WindowLike) {
let stdin = stdin();
for line in stdin.lock().lines() {
let line = line.unwrap().clone();
if LOG {
log(&line);
}
let mut parts = line.split(" ");
let method = parts.next().unwrap();
let arg = &parts.collect::<Vec<&str>>().join(" ");
if method == "handle_message" {
println!("{}", ron::to_string(&window_like.handle_message(ron::from_str(arg).unwrap())).unwrap());
} else if method == "draw" {
println!("{}", ron::to_string(&window_like.draw(&ron::from_str(arg).unwrap())).unwrap());
} else if method == "title" {
println!("{}", window_like.title());
} else if method == "resizable" {
println!("{}", window_like.resizable());
} else if method == "subtype" {
println!("{}", ron::to_string(&window_like.subtype()).unwrap());
} else if method == "ideal_dimensions" {
println!("{}", ron::to_string(&window_like.ideal_dimensions(ron::from_str(arg).unwrap())).unwrap());
let output = match method {
"handle_message" => {
format!("{}", ron::to_string(&window_like.handle_message(ron::from_str(arg).unwrap())).unwrap())
},
"draw" => {
format!("{}", ron::to_string(&window_like.draw(&ron::from_str(arg).unwrap())).unwrap())
},
"title" => {
format!("{}", window_like.title())
},
"resizable" => {
format!("{}", window_like.resizable())
},
"subtype" => {
format!("{}", ron::to_string(&window_like.subtype()).unwrap())
},
"ideal_dimensions" => {
format!("{}", ron::to_string(&window_like.ideal_dimensions(ron::from_str(arg).unwrap())).unwrap())
},
_ => String::new(),
};
if output != String::new() {
if LOG {
log(&output);
}
println!("{}", output);
}
}
}

View File

@@ -5,6 +5,7 @@ pub mod themes;
pub mod messages;
pub mod fs;
pub mod utils;
pub mod logging;
pub mod ipc;
mod proxy_window_like;
mod keyboard;

11
src/logging.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::fs::{ OpenOptions, create_dir };
use std::io::Write;
use dirs::data_dir;
pub fn log(message: &str) {
let data = data_dir().unwrap().into_os_string().into_string().unwrap();
let _ = create_dir(format!("{}/ming-wm", data));
let _ = writeln!(OpenOptions::new().append(true).create(true).open(format!("{}/ming-wm/logs.txt", data)).unwrap(), "{}", message);
}

View File

@@ -2,6 +2,8 @@ use std::vec::Vec;
use std::process::{ Command, Child, Stdio };
use std::io::{ BufReader, BufRead, Write };
use std::cell::RefCell;
use std::path::Path;
use std::io::Read;
use ron;
@@ -81,8 +83,13 @@ impl ProxyWindowLike {
}
pub fn new_rust(file: &str) -> Self {
let loc = format!("./target/release/{}", file);
if Path::new(&loc).exists() {
ProxyWindowLike::new(Command::new(loc).stdout(Stdio::piped()).stdin(Stdio::piped()).stderr(Stdio::null()))
} else {
ProxyWindowLike::new(Command::new("cargo").arg("run").arg("--quiet").arg("--release").arg("--bin").arg(file).stdout(Stdio::piped()).stdin(Stdio::piped()).stderr(Stdio::null()))
}
}
//return empty string if error, do not propogate Err becuase that's messy
//or maybe return "panicked"?