Skip to content

Commit 558a0d7

Browse files
committed
Rearchitect library
1 parent 1f569f3 commit 558a0d7

21 files changed

+589
-487
lines changed

packages/sane/example/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void main(List<String> args) async {
1212
print('${record.level.name}: ${record.time}: ${record.message}');
1313
});
1414

15-
final sane = SaneNative(sane: SaneDev());
15+
final sane = NativeSane(sane: SaneDev());
1616
await sane.spawn();
1717

1818
await sane.init();

packages/sane/lib/src/bindings.g.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ class LibSane {
250250
'sane_strstatus');
251251
late final _sane_strstatus =
252252
_sane_strstatusPtr.asFunction<SANE_String_Const Function(int)>();
253+
254+
late final addresses = _SymbolAddresses(this);
255+
}
256+
257+
class _SymbolAddresses {
258+
final LibSane _library;
259+
260+
_SymbolAddresses(this._library);
261+
262+
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(SANE_Handle)>>
263+
get sane_close => _library._sane_closePtr;
253264
}
254265

255266
enum SANE_Status {
Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1+
import 'dart:async';
12
import 'dart:typed_data';
23

34
import 'package:logging/logging.dart';
45
import 'package:sane/sane.dart';
6+
import 'package:sane/src/sane.dart';
57

68
final _logger = Logger('sane.dev');
79

810
class SaneDev implements Sane {
9-
@override
10-
Future<void> cancel(SaneHandle handle) {
11-
return Future.delayed(const Duration(seconds: 1), () {
12-
_logger.finest('sane_cancel()');
13-
});
14-
}
15-
16-
@override
17-
Future<void> close(SaneHandle handle) {
18-
return Future.delayed(const Duration(seconds: 1), () {
19-
_logger.finest('sane_close()');
20-
});
21-
}
22-
2311
@override
2412
Future<SaneOptionResult<bool>> controlBoolOption({
25-
required SaneHandle handle,
13+
required int handle,
2614
required int index,
2715
required SaneAction action,
2816
bool? value,
@@ -35,7 +23,7 @@ class SaneDev implements Sane {
3523

3624
@override
3725
Future<SaneOptionResult<Null>> controlButtonOption({
38-
required SaneHandle handle,
26+
required int handle,
3927
required int index,
4028
}) {
4129
return Future.delayed(const Duration(seconds: 1), () {
@@ -46,7 +34,7 @@ class SaneDev implements Sane {
4634

4735
@override
4836
Future<SaneOptionResult<double>> controlFixedOption({
49-
required SaneHandle handle,
37+
required int handle,
5038
required int index,
5139
required SaneAction action,
5240
double? value,
@@ -59,7 +47,7 @@ class SaneDev implements Sane {
5947

6048
@override
6149
Future<SaneOptionResult<int>> controlIntOption({
62-
required SaneHandle handle,
50+
required int handle,
6351
required int index,
6452
required SaneAction action,
6553
int? value,
@@ -72,7 +60,7 @@ class SaneDev implements Sane {
7260

7361
@override
7462
Future<SaneOptionResult<String>> controlStringOption({
75-
required SaneHandle handle,
63+
required int handle,
7664
required int index,
7765
required SaneAction action,
7866
String? value,
@@ -84,15 +72,14 @@ class SaneDev implements Sane {
8472
}
8573

8674
@override
87-
Future<void> exit() {
75+
Future<void> dispose() {
8876
return Future(() {
8977
_logger.finest('sane_exit()');
9078
});
9179
}
9280

9381
@override
94-
Future<List<SaneOptionDescriptor>> getAllOptionDescriptors(
95-
SaneHandle handle,
82+
Future<List<SaneOptionDescriptor>> getAllOptionDescriptors(int handle,
9683
) {
9784
return Future.delayed(const Duration(seconds: 1), () {
9885
_logger.finest('sane_getAllOptionDescriptors()');
@@ -113,26 +100,19 @@ class SaneDev implements Sane {
113100
}
114101

115102
@override
116-
Future<List<SaneDevice>> getDevices({
103+
Future<List<SaneDevDevice>> getDevices({
117104
required bool localOnly,
118105
}) {
119106
return Future.delayed(const Duration(seconds: 1), () {
120107
_logger.finest('sane_getDevices()');
121108
return [
122-
for (var i = 0; i < 3; i++)
123-
SaneDevice(
124-
name: 'name $i',
125-
vendor: 'Vendor$i',
126-
model: 'Model$i',
127-
type: 'Type$i',
128-
),
109+
for (var i = 0; i < 3; i++) SaneDevDevice(i),
129110
];
130111
});
131112
}
132113

133114
@override
134-
Future<SaneOptionDescriptor> getOptionDescriptor(
135-
SaneHandle handle,
115+
Future<SaneOptionDescriptor> getOptionDescriptor(int handle,
136116
int index,
137117
) {
138118
return Future.delayed(const Duration(seconds: 1), () {
@@ -152,7 +132,7 @@ class SaneDev implements Sane {
152132
}
153133

154134
@override
155-
Future<SaneParameters> getParameters(SaneHandle handle) {
135+
Future<SaneParameters> getParameters(int handle) {
156136
return Future.delayed(const Duration(seconds: 1), () {
157137
_logger.finest('sane_getParameters()');
158138
return SaneParameters(
@@ -165,52 +145,51 @@ class SaneDev implements Sane {
165145
);
166146
});
167147
}
148+
}
168149

169-
@override
170-
Future<int> init({
171-
AuthCallback? authCallback,
172-
}) {
173-
return Future(() {
174-
_logger.finest('sane_init()');
175-
return 1;
176-
});
177-
}
150+
class SaneDevDevice implements SaneDevice {
151+
const SaneDevDevice(this.index);
152+
153+
final int index;
178154

179155
@override
180-
Future<SaneHandle> open(String deviceName) {
156+
Future<void> cancel() {
181157
return Future.delayed(const Duration(seconds: 1), () {
182-
_logger.finest('sane_open()');
183-
return SaneHandle(deviceName: deviceName);
158+
_logger.finest('sane_cancel()');
184159
});
185160
}
186161

187162
@override
188-
Future<SaneHandle> openDevice(SaneDevice device) {
163+
Future<void> close() {
189164
return Future.delayed(const Duration(seconds: 1), () {
190-
_logger.finest('sane_openDevice()');
191-
return SaneHandle(deviceName: device.name);
165+
_logger.finest('sane_close()');
192166
});
193167
}
194168

195169
@override
196-
Future<Uint8List> read(SaneHandle handle, int bufferSize) {
170+
String get model => 'Model $index';
171+
172+
@override
173+
String get name => 'Name $index';
174+
175+
@override
176+
Future<Uint8List> read({required int bufferSize}) {
197177
return Future.delayed(const Duration(seconds: 1), () {
198178
_logger.finest('sane_read()');
199179
return Uint8List.fromList([]);
200180
});
201181
}
202182

203183
@override
204-
Future<void> setIOMode(SaneHandle handle, SaneIOMode mode) {
184+
Future<void> start() {
205185
return Future.delayed(const Duration(seconds: 1), () {
206-
_logger.finest('sane_setIOMode()');
186+
_logger.finest('sane_start()');
207187
});
208188
}
209189

210190
@override
211-
Future<void> start(SaneHandle handle) {
212-
return Future.delayed(const Duration(seconds: 1), () {
213-
_logger.finest('sane_start()');
214-
});
215-
}
191+
String get type => 'Type $index';
192+
193+
@override
194+
String? get vendor => 'Vendor $index';
216195
}

0 commit comments

Comments
 (0)