hlfw.ca

binpack

Download patch

ref: 96a6d18d807bb75adde746309ccf86d62ff666a9
parent: 9f9196a4688b9e99e39eb04a87f6a10501e120e4
author: Halfwit <michaelmisch1985@gmail.com>
date: Sun Oct 15 12:59:01 PDT 2017

Rework file structure

--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,9 @@
 # binpack - lay out windows automatically
 # See LICENSE file for copyright and license details.
 
-SRC = binpack.c
+SRC = main.c    \
+	  binpack.c \
+	  bin_utils.c
 OBJ = ${SRC:.c=.o}
 
 all: binpack
--- /dev/null
+++ b/bin_utils.c
@@ -1,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "binpack.h"
+
+void center(unsigned width, unsigned height, struct Output out[], unsigned gaps) {
+	// r = rightmost edge of bins
+	// d = bottommost edge of bins
+	// Move all windows (width - r) / 2
+	// Move all windows (height - d) / 2
+	// g = gaps / 2
+	// x - g, y + g, w - g, h - g for all windows 
+}
+
+void
+sort_bins(struct Input r[], const size_t length) {
+	/* arrange rectangles largest to smallest, normalized some over min/max */
+	struct Input temp;
+	for (size_t i = 1; i < length; i++) {
+		for (size_t j = 0; j < length - i; j++) {
+			if ((r[j + 1].maxw * r[j + 1].minh) > (r[j].maxw * r[j].minh)) {
+				temp = r[j];
+				r[j] = r[j + 1];
+				r[j + 1] = temp;
+			}
+		}
+	}
+}
+
+size_t
+init_bins(struct Input r[]) {
+	size_t length = 0;
+	char line[MAX_BIN];
+	for (unsigned i = 0; fgets(line, sizeof(line), stdin); ++i) {
+		sscanf(line, "%d %d %d %d %lx", &r[i].minw, &r[i].minh, &r[i].maxw, &r[i].maxh, &r[i].wid
+);
+		length++;
+	}
+	return length;
+}
\ No newline at end of file
--- a/binpack.c
+++ b/binpack.c
@@ -1,113 +1,8 @@
-#include <stdio.h>
-#include <stdlib.h>
 #include <stdbool.h>
-#include <getopt.h>
+#include <stdlib.h>
 
 #include "binpack.h"
 
-#define MAX_BIN 64
-
-void center(unsigned width, unsigned height, struct Output out[], unsigned gaps) {
-	// r = rightmost edge of bins
-	// d = bottommost edge of bins
-	// Move all windows (width - r) / 2
-	// Move all windows (height - d) / 2
-	// g = gaps / 2
-	// x - g, y + g, w - g, h - g for all windows 
-}
-
 void
-sort_input(struct Input r[], const size_t length)
-{
-	/* arrange rectangles largest to smallest, normalized some over min/max */
-	struct Input temp;
-	for (size_t i = 1; i < length; i++) {
-		for (size_t j = 0; j < length - i; j++) {
-			if ((r[j + 1].maxw * r[j + 1].minh) > (r[j].maxw * r[j].minh)) {
-				temp = r[j];
-				r[j] = r[j + 1];
-				r[j + 1] = temp;
-			}
-		}
-	}
+bin_pack(unsigned width, unsigned height, struct Output out[], struct Input in[]) {
 }
-
-size_t
-input_read(struct Input r[]) {
-	size_t length = 0;
-	char line[MAX_BIN];
-	for (unsigned i = 0; fgets(line, sizeof(line), stdin); ++i) {
-		sscanf(line, "%d %d %d %d %lx", &r[i].minw, &r[i].minh, &r[i].maxw, &r[i].maxh, &r[i].wid
-);
-		length++;
-	}
-	return length;
-}
-
-int main(int argc, char* argv[]) {
-
-	unsigned gaps = 0;
-	unsigned width = 0;
-	unsigned height = 0;
-	unsigned screens = 1;
-	char c;
-	
-	while ((c = getopt(argc, argv, "hg:w:h:s:")) != -1) {
-		switch (c) {
-		case 'g':
-			sscanf(optarg, "%u", gaps);
-	        break;
-    	case 'x':
-        	sscanf(optarg, "%u", width);
-           	break;
-        case 'y':
-        	sscanf(optarg, "%u", height);
-        	break;
-    	case 's':
-        	sscanf(optarg, "%u", screens);
-        	break;
-    	case 'h':
-			fputs("Usage: bin_pack -x screen_width -y screen_height -g gaps -s number_of_screens\n", stderr);
-			return EXIT_SUCCESS;
-        }
-	}
-
-	struct Input r[MAX_BIN];
-	struct Output out[MAX_BIN];
-	
-	const size_t length = input_read(r);
-	
-	/* Sanity checks */
-	if (length == 0 || height == 0 || width == 0)
-		return EXIT_SUCCESS;
-
-	sort_input(r, length);
-
-	switch (screens) {
-		case 1:	   		
-//	   		bin_pack(width, height, *output, *input);
-//	   		center(width, height, *output, gaps);
-	   		break;
-
-		case 2:
-//	   		split total width into two 'bins'
-//	   		split into two Input, a and b
-//	   		bin_pack a and b
-//	  		center a and b
-//	  		add $width to all bins in b (shift to the right fro second monitor)
-	  		break;
-		case 3:
-//	   		split total width into three 'bins'
-//	   		bin_pack a
-//	    	- if bin_pack fails, split out second window into bin b
-//			- sanity check that there is infact a second window
-//	    	- all subsequent fails will split out second window into bin b
-//	   		once success, split b into two Input, b and c
-//	   		bin_pack b and c
-//	   		center a b and c
-//	   		add $width to everything in b, and 2 * $width to everything in c
-			break;
-	}
-//	print everything to stdout
-}
-
--- a/binpack.h
+++ b/binpack.h
@@ -1,5 +1,7 @@
 #include <stdbool.h>
 
+#define MAX_BIN 64
+
 struct Input {
 	unsigned minw;
 	unsigned minh;
@@ -16,4 +18,7 @@
 	unsigned id;
 };
 
-bool bin_pack(unsigned width, unsigned height, struct Output[], struct Input[]);
+void bin_pack(unsigned width, unsigned height, struct Output out[], struct Input in[]);
+void center(unsigned width, unsigned height, struct Output out[], unsigned gaps);
+void sort_bins(struct Input r[], const size_t length);
+size_t init_bins(struct Input r[]);
--- /dev/null
+++ b/main.c
@@ -1,0 +1,74 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <getopt.h>
+
+#include "binpack.h"
+
+int main(int argc, char* argv[]) {
+
+	unsigned gaps = 0;
+	unsigned width = 0;
+	unsigned height = 0;
+	unsigned screens = 1;
+	char c;
+	
+	while ((c = getopt(argc, argv, "hg:w:h:s:")) != -1) {
+		switch (c) {
+		case 'g':
+			sscanf(optarg, "%u", gaps);
+	        break;
+    	case 'x':
+        	sscanf(optarg, "%u", width);
+           	break;
+        case 'y':
+        	sscanf(optarg, "%u", height);
+        	break;
+    	case 's':
+        	sscanf(optarg, "%u", screens);
+        	break;
+    	case 'h':
+			fputs("Usage: bin_pack -x screen_width -y screen_height -g gaps -s number_of_screens\n", stderr);
+			return EXIT_SUCCESS;
+        }
+	}
+
+	struct Input input[MAX_BIN];
+	struct Output output[MAX_BIN];
+	
+	const size_t length = init_bins(input);
+	
+	/* Sanity checks */
+	if (length == 0 || height == 0 || width == 0)
+		return EXIT_SUCCESS;
+
+	sort_bins(input, length);
+
+	switch (screens) {
+		case 1:	   		
+	   		bin_pack(width, height, output, input);
+	   		center(width, height, output, gaps);
+	   		break;
+
+		case 2:
+//	   		split total width into two 'bins'
+//	   		split into two Input, a and b
+//	   		bin_pack a and b
+//	  		center a and b
+//	  		add $width to all bins in b (shift to the right fro second monitor)
+	  		break;
+		case 3:
+//	   		split total width into three 'bins'
+//	   		bin_pack a
+//	    	- if bin_pack fails, split out second window into bin b
+//			- sanity check that there is infact a second window
+//	    	- all subsequent fails will split out second window into bin b
+//	   		once success, split b into two Input, b and c
+//	   		bin_pack b and c
+//	   		center a b and c
+//	   		add $width to everything in b, and 2 * $width to everything in c
+			break;
+	}
+//	print everything to stdout
+}
+
--- a/test/test
+++ b/test/test
@@ -5,5 +5,6 @@
 for i in */in*; do
 	size="$(dirname "$i")"
 	in="$(binpack -x "${size%x*}" -y "${size#*x}" -g 6 < "$i")"
-	grep "$in" "$size/out${i#*in*}" >/dev/null && echo "Test $size - ${i#*in*} success"
+	echo "$in"
+	#grep "$in" "$size/out${i#*in*}" >/dev/null && echo "Test $size - ${i#*in*} success"
 done
\ No newline at end of file