an xcb window

This commit is contained in:
Skylar Bleed 2025-01-30 17:57:56 -06:00
parent cab93ff652
commit 9dba8ce469
10 changed files with 143 additions and 41 deletions

View file

@ -6,6 +6,11 @@ version = "0.1.0"
edition = "2024"
build = "build.rs"
[[bin]]
rustc-link-lib = ["xcb", "cairo"]
name = "hic"
path = "src/main.rs"
[dependencies]
libc = "0.2.169"
same-proto = { path = "../same-proto" }

View file

@ -1,6 +1,6 @@
cxx = clang++
cxxflags = -g -Wall -Wextra -Wno-c99-extensions -pedantic -fblocks -O3 -Iinc
libs = -lxcb -I/usr/include/cairo -I/usr/include/libpng16 -I/usr/include/freetype2 -DWITH_GZFILEOP -I/usr/include/pixman-1 -lcairo
cxxflags = -g -Wall -Wextra -Wno-c99-extensions -pedantic -fblocks -O3 -Iinc -fPIC
libs = -I/usr/include/cairo -I/usr/include/libpng16 -I/usr/include/freetype2 -DWITH_GZFILEOP -I/usr/include/pixman-1
rule cpp
depfile = $out.d
command = $cxx -c -MD -MF $out.d $cxxflags $libs -o $out $in
@ -22,6 +22,7 @@ rule exe
command = $in
build clean: rm o
build o: mkdir
build o/str.cpp.o: cpp lib/str.cpp
build o/gui.cpp.o: cpp lib/gui.cpp
build o/libhic.a: lib o/gui.cpp.o
build o/libhic.a: lib o/str.cpp.o o/gui.cpp.o
default o/libhic.a

View file

@ -1,6 +1,9 @@
use std::env;
use std::{process::Command, env};
fn main() {
Command::new("ninja").spawn().expect("ninja failed");
let cwd = env::current_dir().unwrap();
println!(r"cargo:rustc-link-search={}/o/", cwd.display());
println!("cargo:rustc-link-search={}/o/", cwd.display());
["cairo", "xcb"].iter().for_each(|x| println!("cargo:rustc-link-arg=-l{x}"));
}

View file

@ -1,17 +1,24 @@
#!/usr/bin/env goal
/ join by newlines
nl:{x+"\n"+y}
/ join by spaces
sp:{x+" "+y}
fs:{f:read x;f["name"]@&~f"dir"}
/ all files in dir x
fs:{f:read x
f["name"]@&~f"dir"}
lib:fs "lib"
cpp:{x@&{"cpp"~+/|3#|("c"$)'"c"$x}'x}
/ get all items of x where ".cpp"~|4#|x
/ now you can cpp@fs x
cpp:{x@&{".cpp"~+/|4#|("c"$)'"c"$x}'x}
/ generate the ninja file
say'("cxx = clang++"
"cxxflags = "+sp/("-g -Wall -Wextra -Wno-c99-extensions"
"-pedantic -fblocks -O3 -Iinc")
"libs = "+sp/{-1_shell "pkg-config --cflags --libs $x"}'"xcb" "cairo"
"-pedantic -fblocks -O3 -Iinc -fPIC")
"libs = "+sp/{-1_shell "pkg-config --cflags $x"}'"xcb" "cairo"
rq/rule cpp
depfile = $out.d
command = $cxx -c -MD -MF $out.d $cxxflags $libs -o $out $in

View file

@ -1,5 +1,15 @@
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define eprintln(...) { \
fprintf(stderr, __VA_ARGS__); \
putc('\n', stderr); \
}
#define fatal(...) { \
eprintln(__VA_ARGS__); \
exit(-1); \
}
template<typename T, typename E>
struct result {
union {

View file

@ -1,25 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <unistd.h>
#include <cairo.h>
#include <cairo-xcb.h>
#include <xcb/xcb.h>
#include <stdio.h>
#include "com.h"
#include "str.h"
#include "gui.h"
/*
auto win_t::mk() -> result_t<win_t, std::string> {
win.dpy = XCBConnectBasic();
if (win.dpy == NULL) {
}
}
xcb_connection_t *con;
xcb_screen_t *scr;
xcb_window_t win;
win_t win;
extern "C" auto hic_init() -> int {
/*
* set up the connection and grab a window id
*/
con = xcb_connect(NULL, NULL);
if (con == NULL) return -1;
extern "C" auto init(int x, int y) -> int {
// win.dpy = XCBConnectBasic();
if (win.dpy == NULL) {
err("failed to open display");
return 1;
}
scr = xcb_setup_roots_iterator(xcb_get_setup(con)).data;
if (scr == NULL) return -2;
win = xcb_generate_id(con);
/* summon a window */
xcb_create_window(
con,
XCB_COPY_FROM_PARENT, /* depth (same as root) */
win,
scr->root, /* parent window */
0, 0, /* x y */
640, 480, /* width height */
5, /* border width */
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* the window class */
scr->root_visual,
0, NULL /* masks (unused) */
);
/* map the window onto the screen */
xcb_map_window(con, win);
/* make sure commands are sent before we pause, so window is shown */
xcb_flush(con);
/* now we wait */
pause();
return 0;
}
*/

View file

@ -1,15 +0,0 @@
/*
struct win_t {
XCBConnection *dpy;
XCBWINDOW win;
XCBGCONTEXT gc;
int width, height;
XCBVISUALTYPE *visual;
static result_t<win_t, str_t> mk();
~win_t();
};
*/

34
hic/lib/str.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include "com.h"
#include "str.h"
str_t::~str_t() {
free(buf);
}
#define CLONE(x) { \
i = x.i, len = x.len; \
auto size = sizeof(str_t::C) * len; \
\
buf = (str_t::C*)malloc(size); \
if (buf == NULL) fatal( \
"malloc() returned NULL in str_t clone. aborting." \
); \
\
memcpy(buf, x.buf, size); \
}
auto str_t::operator=(const str_t& x) -> str_t& {
CLONE(x);
return *this;
}
str_t::str_t(const str_t& x) {
CLONE(x)
}

25
hic/lib/str.h Normal file
View file

@ -0,0 +1,25 @@
struct str_t {
/* the char type
* declared because i might change it later and i don't want to rewrite
* all the types */
using C = uint8_t;
C *buf;
size_t i;
size_t len;
static auto mk() -> option<str_t> {
size_t i = 0, len = 8;
auto buf = (C*)malloc(sizeof(C) * len);
return buf == NULL
? option<str_t>()
: option<str_t>(str_t(buf, i, len));
}
str_t(C *buf, size_t i, size_t len) : buf{buf}, i{i}, len{len} {}
~str_t();
str_t& operator=(const str_t& x);
str_t(const str_t& x);
};

View file

@ -2,10 +2,12 @@ use libc::c_int;
#[link(name="hic")]
unsafe extern "C" {
fn add(x: c_int, y: c_int) -> c_int;
fn hic_init() -> c_int;
}
fn main() {
println!("{}", unsafe { add(1, 10) });
unsafe {
hic_init();
}
}