Skip to content

Commit

Permalink
Render some pixels in the window example.
Browse files Browse the repository at this point in the history
This assumes a lot about padding and is very inefficient.
  • Loading branch information
robert-ancell committed Nov 29, 2023
1 parent 273611e commit 86ef561
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion example/window.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
import 'package:x11/x11.dart';

void drawWindow(X11Client client, X11ResourceId window, X11Rectangle area) {
//client.clearArea(window, area);
var gc = client.generateId();
client.createGC(gc, window);

// Write image in tiles (can't fit full image data into a single call).
var tile_size = 100;
for (var y0 = 0; y0 < area.height; y0 += tile_size) {
var y1 = y0 + tile_size;
if (y1 > area.height) {
y1 = area.height;
}

for (var x0 = 0; x0 < area.width; x0 += tile_size) {
var x1 = x0 + tile_size;
if (x1 > area.width) {
x1 = area.width;
}

var tile_area = X11Rectangle(
x: area.x + x0, y: area.y + y0, width: x1 - x0, height: y1 - y0);

// FIXME: Padding should be taken from X11Format
var data = <int>[];
for (var y = 0; y < tile_area.height; y++) {
for (var x = 0; x < tile_area.width; x++) {
data.add(255 * (tile_area.x + x) ~/ area.width);
data.add(255 * (tile_area.y + y) ~/ area.height);
data.add(255);
data.add(0);
}

// Each row needs to be padded.
while (data.length % 4 != 0) {
data.add(0);
}
}
client.putImage(gc, window, tile_area, data);
}
}
client.freeGC(gc);
}

void main() async {
var client = X11Client();
await client.connect();

var wmProtocolsAtom = await client.internAtom('WM_PROTOCOLS');
var wmDeleteWindowAtom = await client.internAtom('WM_DELETE_WINDOW');

client.errorStream.listen((error) async {
print('$error ${error.majorOpcode}.${error.minorOpcode}');
});
client.eventStream.listen((event) async {
if (event is X11KeyPressEvent) {
print('KeyPress ${event.key}');
Expand All @@ -27,7 +73,7 @@ void main() async {
} else if (event is X11FocusOutEvent) {
print('FocusOut');
} else if (event is X11ExposeEvent) {
client.clearArea(event.window, event.area);
drawWindow(client, event.window, event.area);
} else if (event is X11ClientMessageEvent) {
if (event.type == wmProtocolsAtom) {
if (X11Atom(event.data[0]) == wmDeleteWindowAtom) {
Expand Down

0 comments on commit 86ef561

Please sign in to comment.