Migrate NFC init to object init from class init

Probably not the right way to do it.
But for this program it shall work...
This commit is contained in:
Markus Koch 2019-07-23 19:24:25 +02:00
parent 43d3e7f04d
commit f473106695

View File

@ -3,12 +3,10 @@
struct _YacosScanner { struct _YacosScanner {
GObject parent_instance; GObject parent_instance;
// TODO: Private data goes here nfc_context *nfc_ctx;
nfc_device *nfc_pnd;
}; };
static nfc_context *nfc_ctx; // TODO: Migrate to klass
static nfc_device *nfc_pnd = NULL; // TODO: Migrate to klass
G_DEFINE_TYPE(YacosScanner, yacos_scanner, G_TYPE_OBJECT) G_DEFINE_TYPE(YacosScanner, yacos_scanner, G_TYPE_OBJECT)
enum { enum {
@ -29,12 +27,12 @@ static gboolean *yacos_scanner_read (gpointer user_data)
static void yacos_scanner_finalize(GObject *gobject) static void yacos_scanner_finalize(GObject *gobject)
{ {
YacosScanner *obj = yacos_scanner_get_instance_private(YACOS_SCANNER(gobject)); YacosScanner *self = yacos_scanner_get_instance_private(YACOS_SCANNER(gobject));
if (nfc_pnd) if (self->nfc_pnd)
nfc_close(nfc_pnd); nfc_close(self->nfc_pnd);
if (nfc_ctx) if (self->nfc_ctx)
nfc_exit(nfc_ctx); nfc_exit(self->nfc_ctx);
G_OBJECT_CLASS(yacos_scanner_parent_class)->finalize(gobject); G_OBJECT_CLASS(yacos_scanner_parent_class)->finalize(gobject);
} }
@ -53,22 +51,22 @@ static void yacos_scanner_class_init(YacosScannerClass *klass)
NULL); NULL);
object_class->finalize = yacos_scanner_finalize; object_class->finalize = yacos_scanner_finalize;
g_debug("Using libnfc %s", nfc_version());
nfc_init(&nfc_ctx);
if (!nfc_ctx) {
g_error("Error initializing libnfc");
return;
}
nfc_pnd = nfc_open(nfc_ctx, NULL);
if (!nfc_pnd) {
g_error("Error initializing NFC device");
return;
}
} }
static void yacos_scanner_init(YacosScanner *self) static void yacos_scanner_init(YacosScanner *self)
{ {
g_debug("Using libnfc %s", nfc_version());
nfc_init(&self->nfc_ctx);
if (!self->nfc_ctx) {
g_error("Error initializing libnfc");
return;
}
self->nfc_pnd = nfc_open(self->nfc_ctx, NULL);
if (!self->nfc_pnd) {
g_error("Error initializing NFC device");
return;
}
g_timeout_add(1000, G_SOURCE_FUNC(yacos_scanner_read), self); g_timeout_add(1000, G_SOURCE_FUNC(yacos_scanner_read), self);
} }