summaryrefslogtreecommitdiffstats
path: root/gtk/zbargtk.c
blob: 8c8601626a5c7aaf0d447ad384699893e1de694f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*------------------------------------------------------------------------
 *  Copyright 2008-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include <gtk/gtk.h>
#ifdef HAVE_X
#include <gdk/gdkx.h>
#elif defined(_WIN32)
#include <gdk/gdkwin32.h>
#endif

#include "zbargtkprivate.h"
#include "zbarmarshal.h"
#include <zbar/zbargtk.h>

#ifndef G_PARAM_STATIC_STRINGS
#define G_PARAM_STATIC_STRINGS \
    (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
#endif

#define DEFAULT_WIDTH  640
#define DEFAULT_HEIGHT 480

enum
{
    DECODED,
    DECODED_TEXT,
    LAST_SIGNAL
};

enum
{
    PROP_0,
    PROP_VIDEO_DEVICE,
    PROP_VIDEO_ENABLED,
    PROP_VIDEO_OPENED,
};

static guint zbar_gtk_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE(ZBarGtk, zbar_gtk, GTK_TYPE_WIDGET);

/* FIXME what todo w/errors? OOM? */
/* FIXME signal failure notifications to main gui idle handler */

void zbar_gtk_release_pixbuf(zbar_image_t *img)
{
    GdkPixbuf *pixbuf = zbar_image_get_userdata(img);

    g_assert(GDK_IS_PIXBUF(pixbuf));

    /* remove reference */
    zbar_image_set_userdata(img, NULL);

    /* release reference to associated pixbuf and it's data */
    g_object_unref(pixbuf);
}

gboolean zbar_gtk_image_from_pixbuf(zbar_image_t *zimg, GdkPixbuf *pixbuf)
{
    unsigned pitch, width, height;
    GdkColorspace colorspace;
    unsigned long datalen;
    int nchannels, bps;
    long type;

    /* apparently should always be packed RGB? */
    colorspace = gdk_pixbuf_get_colorspace(pixbuf);

    if (colorspace != GDK_COLORSPACE_RGB) {
	g_warning("non-RGB color space not supported: %d\n", colorspace);
	return (FALSE);
    }

    nchannels = gdk_pixbuf_get_n_channels(pixbuf);
    bps	      = gdk_pixbuf_get_bits_per_sample(pixbuf);
    type      = 0;

    /* these are all guesses... */
    if (nchannels == 3 && bps == 8)
	type = zbar_fourcc('R', 'G', 'B', '3');
    else if (nchannels == 4 && bps == 8)
	type = zbar_fourcc('B', 'G', 'R', '4'); /* FIXME alpha flipped?! */
    else if (nchannels == 1 && bps == 8)
	type = zbar_fourcc('Y', '8', '0', '0');
    else if (nchannels == 3 && bps == 5)
	type = zbar_fourcc('R', 'G', 'B', 'R');
    else if (nchannels == 3 && bps == 4)
	type = zbar_fourcc('R', '4', '4', '4'); /* FIXME maybe? */
    else {
	g_warning("unsupported combination: nchannels=%d bps=%d\n", nchannels,
		  bps);
	return (FALSE);
    }
    zbar_image_set_format(zimg, type);

    /* FIXME we don't deal w/bpl...
     * this will cause problems w/unpadded pixbufs :|
     */
    pitch = gdk_pixbuf_get_rowstride(pixbuf);
    width = pitch / ((nchannels * bps) / 8);

    if ((width * nchannels * 8 / bps) != pitch) {
	g_warning("unsupported: width=%d nchannels=%d bps=%d rowstride=%d\n",
		  width, nchannels, bps, pitch);
	return (FALSE);
    }
    height = gdk_pixbuf_get_height(pixbuf);
    /* FIXME this isn't correct either */
    datalen = width * height * nchannels;

    zbar_image_set_size(zimg, width, height);

    /* when the zbar image is released, the pixbuf will be
     * automatically be released
     */
    zbar_image_set_userdata(zimg, pixbuf);
    zbar_image_set_data(zimg, gdk_pixbuf_get_pixels(pixbuf), datalen,
			zbar_gtk_release_pixbuf);
#ifdef DEBUG_ZBARGTK
    g_message("colorspace=%d nchannels=%d bps=%d type=%.4s(%08lx)\n"
	      "\tpitch=%d width=%d height=%d datalen=0x%lx\n",
	      colorspace, nchannels, bps, (char *)&type, type, pitch, width,
	      height, datalen);
#endif
    return (TRUE);
}

static inline gboolean zbar_gtk_video_open(ZBarGtk *self,
					   const char *video_device)
{
    ZBarGtkPrivate *zbar  = ZBAR_GTK_PRIVATE(self->_private);
    gboolean video_opened = FALSE;

    zbar->video_opened = FALSE;
    if (zbar->idle_id)
	g_object_notify(G_OBJECT(self), "video-opened");

    if (zbar->window) {
	/* ensure old video doesn't have image ref
         * (FIXME handle video destroyed w/images outstanding)
         */
	zbar_window_draw(zbar->window, NULL);
	gtk_widget_queue_draw(GTK_WIDGET(self));
    }

    if (zbar->video) {
	zbar_video_destroy(zbar->video);
	zbar->video = NULL;
    }

    if (video_device && video_device[0] && zbar->idle_id) {
	/* create video
         * FIXME video should support re-open
         */
	zbar->video = zbar_video_create();
	g_assert(zbar->video);

	if (zbar_video_open(zbar->video, video_device)) {
	    zbar_video_error_spew(zbar->video, 0);
	    zbar_video_destroy(zbar->video);
	    zbar->video = NULL;
	    /* FIXME error propagation */
	    return (FALSE);
	}

	/* negotiation accesses the window format list,
         * so we hold the lock for this part
         */
	if (zbar->video_width && zbar->video_height)
	    zbar_video_request_size(zbar->video, zbar->video_width,
				    zbar->video_height);

	video_opened = !zbar_negotiate_format(zbar->video, zbar->window);

	if (video_opened) {
	    zbar->req_width  = zbar_video_get_width(zbar->video);
	    zbar->req_height = zbar_video_get_height(zbar->video);
	}
	gtk_widget_queue_resize(GTK_WIDGET(self));

	zbar->video_opened = video_opened;
	if (zbar->idle_id)
	    g_object_notify(G_OBJECT(self), "video-opened");
    }

    return (video_opened);
}

static inline int zbar_gtk_process_image(ZBarGtk *self, zbar_image_t *image)
{
    ZBarGtkPrivate *zbar;
    int rc;

    zbar = ZBAR_GTK_PRIVATE(self->_private);

    if (!image)
	return (-1);

    zbar_image_t *tmp =
	zbar_image_convert(image, zbar_fourcc('Y', '8', '0', '0'));
    if (!tmp)
	return (-1);

    zbar_image_scanner_recycle_image(zbar->scanner, image);
    rc = zbar_scan_image(zbar->scanner, tmp);
    zbar_image_set_symbols(image, zbar_image_get_symbols(tmp));
    zbar_image_destroy(tmp);
    if (rc < 0)
	return (rc);

    if (rc && zbar->idle_id) {
	/* update decode results */
	const zbar_symbol_t *sym;
	for (sym = zbar_image_first_symbol(image); sym;
	     sym = zbar_symbol_next(sym))
	    if (!zbar_symbol_get_count(sym)) {
		zbar_symbol_type_t type = zbar_symbol_get_type(sym);
		const char *data	= zbar_symbol_get_data(sym);
		g_signal_emit(self, zbar_gtk_signals[DECODED], 0, type, data);

		/* FIXME skip this when unconnected? */
		gchar *text =
		    g_strconcat(zbar_get_symbol_name(type), ":", data, NULL);
		g_signal_emit(self, zbar_gtk_signals[DECODED_TEXT], 0, text);
		g_free(text);
	    }
    }

    if (zbar->window) {
	rc = zbar_window_draw(zbar->window, image);
	gtk_widget_queue_draw(GTK_WIDGET(self));
    } else
	rc = -1;

    return (rc);
}

static gboolean zbar_processing_idle_callback(gpointer data)
{
    ZBarGtk *self	 = data;
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);
    GValue *msg		 = g_async_queue_try_pop(zbar->queue);
    GType type;

    if (!msg) {
	if (zbar->video_enabled_state) {
	    zbar_image_t *image = zbar_video_next_image(zbar->video);
	    if (zbar_gtk_process_image(self, image) < 0)
		zbar->video_enabled_state = FALSE;
	    if (image)
		zbar_image_destroy(image);

	    if (zbar->video_enabled_state)
		return TRUE;

	    if (zbar_video_enable(zbar->video, 0)) {
		zbar_video_error_spew(zbar->video, 0);
		zbar->video_enabled_state = FALSE;
	    }

	    zbar_image_scanner_enable_cache(zbar->scanner, 0);
	    /* release video image and revert to logo */
	    if (zbar->window) {
		zbar_window_draw(zbar->window, NULL);
		gtk_widget_queue_draw(GTK_WIDGET(self));
	    }

	    /* must have been an error while streaming */
	    zbar_gtk_video_open(self, NULL);
	}

	return TRUE;
    }

    g_assert(G_IS_VALUE(msg));

    type = G_VALUE_TYPE(msg);
    if (type == G_TYPE_INT) {
	/* video state change */
	int state = g_value_get_int(msg);
	if (state < 0) {
	    /* error identifying state */
	    g_value_unset(msg);
	    g_free(msg);
	    return TRUE;
	}
	g_assert(state >= 0 && state <= 1);
	zbar->video_enabled_state = (state != 0);
    } else if (type == G_TYPE_STRING) {
	/* open new video device */
	const char *video_device = g_value_get_string(msg);

	zbar->video_enabled_state = zbar_gtk_video_open(self, video_device);
    } else if (type == GDK_TYPE_PIXBUF) {
	/* scan provided image and broadcast results */
	zbar_image_t *image = zbar_image_create();
	GdkPixbuf *pixbuf   = GDK_PIXBUF(g_value_dup_object(msg));

	if (zbar_gtk_image_from_pixbuf(image, pixbuf))
	    zbar_gtk_process_image(self, image);
	else
	    g_object_unref(pixbuf);
	zbar_image_destroy(image);
    } else {
	gchar *dbg = g_strdup_value_contents(msg);

	g_warning("unknown message type (%x) received: %s\n", (unsigned)type,
		  dbg);
	g_free(dbg);
    }
    g_value_unset(msg);
    g_free(msg);
    msg = NULL;

    if (zbar->video_enabled_state) {
	/* release reference to any previous pixbuf */
	if (zbar->window)
	    zbar_window_draw(zbar->window, NULL);

	if (zbar_video_enable(zbar->video, 1)) {
	    zbar_video_error_spew(zbar->video, 0);
	    zbar->video_enabled_state = FALSE;
	    return TRUE;
	}
	zbar_image_scanner_enable_cache(zbar->scanner, 1);

	return TRUE;
    }

    return TRUE;
}

static void zbar_gtk_realize(GtkWidget *widget)
{
    ZBarGtk *self = ZBAR_GTK(widget);
    ZBarGtkPrivate *zbar;
    GdkWindowAttr attributes;
    GtkAllocation allocation;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    gtk_widget_set_realized(widget, TRUE);

#if GTK_CHECK_VERSION(3, 14, 0)
    // FIXME: this is deprecated after 3.14 - no idea what replaces it
#else
    gtk_widget_set_double_buffered(widget, FALSE);
#endif

    gtk_widget_get_allocation(widget, &allocation);
    attributes.x	   = allocation.x;
    attributes.y	   = allocation.y;
    attributes.width	   = allocation.width;
    attributes.height	   = allocation.height;
    attributes.wclass	   = GDK_INPUT_OUTPUT;
    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.event_mask = (gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK);

    GdkWindow *window = gdk_window_new(gtk_widget_get_parent_window(widget),
				       &attributes, GDK_WA_X | GDK_WA_Y);
    gtk_widget_set_window(widget, window);
    gdk_window_set_user_data(window, widget);
#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION < 18
    gdk_window_set_background_pattern(window, NULL);
#elif GTK_MAJOR_VERSION < 3
    gdk_window_set_back_pixmap(window, NULL, TRUE);
#endif

    /* attach zbar_window to underlying X window */
#ifdef HAVE_X
    if (zbar_window_attach(zbar->window,
			   GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
			   GDK_WINDOW_XID(window)))
#elif defined(_WIN32)
    if (zbar_window_attach(zbar->window, GDK_WINDOW_HWND(window), 0))
#endif
	zbar_window_error_spew(zbar->window, 0);
}

static inline GValue *zbar_gtk_new_value(GType type)
{
    return (g_value_init(g_malloc0(sizeof(GValue)), type));
}

static void zbar_gtk_unrealize(GtkWidget *widget)
{
    ZBarGtkPrivate *zbar;
    GdkWindow *window;
    ZBarGtk *self;

    if (gtk_widget_get_mapped(widget))
	gtk_widget_unmap(widget);

    gtk_widget_set_mapped(widget, FALSE);
    self = ZBAR_GTK(widget);
    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    if (zbar->video_enabled) {
	zbar->video_enabled = FALSE;
	GValue *msg	    = zbar_gtk_new_value(G_TYPE_INT);
	g_value_set_int(msg, 0);
	g_async_queue_push(zbar->queue, msg);
    }

    zbar_window_attach(zbar->window, NULL, 0);

    gtk_widget_set_realized(widget, FALSE);

    window = gtk_widget_get_window(widget);
    gdk_window_set_user_data(window, NULL);
    gdk_window_destroy(window);
    gtk_widget_set_window(widget, NULL);
}

#if GTK_MAJOR_VERSION >= 3

static void zbar_get_preferred_width(GtkWidget *widget, gint *minimum_width,
				     gint *natural_width)
{
    unsigned int screen_width;
    ZBarGtkPrivate *zbar;
    ZBarGtk *self;
#if GTK_CHECK_VERSION(3, 22, 0)
    GdkDisplay *display;
    GdkMonitor *monitor;
    GdkRectangle geo;
#endif
    self = ZBAR_GTK(widget);

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    /* use native video size (max) if available,
     * arbitrary defaults otherwise.
     * video attributes maintained under main gui idle handler
     */
#if GTK_CHECK_VERSION(3, 22, 0)
    display = gdk_display_get_default();
    monitor = gdk_display_get_monitor(display, 0);
    gdk_monitor_get_geometry(monitor, &geo);

    screen_width = geo.width;
#else
    screen_width = gdk_screen_width();
#endif

    if (zbar->req_width > screen_width) {
	float scale = screen_width * .8 / zbar->req_width;

	zbar->req_width *= scale;
	zbar->req_height *= scale;
    }

    *minimum_width = zbar->req_width;
    *natural_width = zbar->req_width;
}

static void zbar_get_preferred_height(GtkWidget *widget, gint *minimum_height,
				      gint *natural_height)
{
    ZBarGtk *self = ZBAR_GTK(widget);
    unsigned int screen_height;
#if GTK_CHECK_VERSION(3, 22, 0)
    GdkDisplay *display;
    GdkMonitor *monitor;
    GdkRectangle geo;
#endif

    if (!self->_private)
	return;
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);

    /* use native video size (max) if available,
     * arbitrary defaults otherwise.
     * video attributes maintained under main gui idle handler
     */
#if GTK_CHECK_VERSION(3, 22, 0)
    display = gdk_display_get_default();
    monitor = gdk_display_get_monitor(display, 0);
    gdk_monitor_get_geometry(monitor, &geo);

    screen_height = geo.height;
#else
    screen_height = gdk_screen_height();
#endif

    if (zbar->req_height > screen_height) {
	float scale = screen_height * .8 / zbar->req_height;

	zbar->req_width *= scale;
	zbar->req_height *= scale;
    }

    *minimum_height = zbar->req_height;
    *natural_height = zbar->req_height;
}

static gboolean zbar_gtk_scale_draw(GtkWidget *widget, cairo_t *cr)
{
    // NOTE: should we change something here?

    ZBarGtk *self = ZBAR_GTK(widget);

    if (!self->_private)
	return (FALSE);
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);

    if (gtk_widget_get_visible(widget) && gtk_widget_get_mapped(widget) &&
	zbar_window_redraw(zbar->window))
	return (TRUE);
    return (FALSE);
}

#else
static void zbar_gtk_size_request(GtkWidget *widget,
				  GtkRequisition *requisition)
{
    ZBarGtk *self = ZBAR_GTK(widget);
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    /* use native video size (max) if available,
     * arbitrary defaults otherwise.
     * video attributes maintained under main gui idle handler
     */
    requisition->width = zbar->req_width;
    requisition->height = zbar->req_height;
}

static gboolean zbar_gtk_expose(GtkWidget *widget, GdkEventExpose *event)
{
    ZBarGtk *self = ZBAR_GTK(widget);
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return (FALSE);
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    if (gtk_widget_get_visible(widget) && gtk_widget_get_mapped(widget) &&
	zbar_window_redraw(zbar->window))
	return (TRUE);
    return (FALSE);
}
#endif

static void zbar_gtk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
    ZBarGtk *self = ZBAR_GTK(widget);
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    (*GTK_WIDGET_CLASS(zbar_gtk_parent_class)->size_allocate)(widget,
							      allocation);
    if (zbar->window)
	zbar_window_resize(zbar->window, allocation->width, allocation->height);
}

void zbar_gtk_scan_image(ZBarGtk *self, GdkPixbuf *img)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    g_object_ref(G_OBJECT(img));

    /* queue for scanning by the processor idle handler */
    GValue *msg = zbar_gtk_new_value(GDK_TYPE_PIXBUF);

    /* this grabs a new reference to the image,
     * eventually released by the processor idle handler
     */
    g_value_set_object(msg, img);
    g_async_queue_push(zbar->queue, msg);
}

const char *zbar_gtk_get_video_device(ZBarGtk *self)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return (NULL);
    zbar = ZBAR_GTK_PRIVATE(self->_private);
    if (zbar->video_device)
	return (zbar->video_device);
    else
	return ("");
}

void zbar_gtk_set_video_device(ZBarGtk *self, const char *video_device)
{
    ZBarGtkPrivate *zbar;
    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    g_free((void *)zbar->video_device);
    zbar->video_device	= g_strdup(video_device);
    zbar->video_enabled = video_device && video_device[0];

    /* push another copy to processor idle handler */
    GValue *msg = zbar_gtk_new_value(G_TYPE_STRING);
    if (video_device)
	g_value_set_string(msg, video_device);
    else
	g_value_set_static_string(msg, "");
    g_async_queue_push(zbar->queue, msg);

    g_object_freeze_notify(G_OBJECT(self));
    g_object_notify(G_OBJECT(self), "video-device");
    g_object_notify(G_OBJECT(self), "video-enabled");
    g_object_thaw_notify(G_OBJECT(self));
}

gboolean zbar_gtk_get_video_enabled(ZBarGtk *self)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return (FALSE);
    zbar = ZBAR_GTK_PRIVATE(self->_private);
    return (zbar->video_enabled);
}

void zbar_gtk_set_video_enabled(ZBarGtk *self, gboolean video_enabled)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    video_enabled = (video_enabled != FALSE);
    if (zbar->video_enabled != video_enabled) {
	zbar->video_enabled = video_enabled;

	/* push state change to processor idle handler */
	GValue *msg = zbar_gtk_new_value(G_TYPE_INT);
	g_value_set_int(msg, zbar->video_enabled);
	g_async_queue_push(zbar->queue, msg);

	g_object_notify(G_OBJECT(self), "video-enabled");
    }
}

gboolean zbar_gtk_get_video_opened(ZBarGtk *self)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return (FALSE);
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    return (zbar->video_opened);
}

void zbar_gtk_request_video_size(ZBarGtk *self, int width, int height)
{
    ZBarGtkPrivate *zbar;

    if (!self->_private || width < 0 || height < 0)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    zbar->req_width = zbar->video_width = width;
    zbar->req_height = zbar->video_height = height;
    gtk_widget_queue_resize(GTK_WIDGET(self));
}

static void zbar_gtk_set_property(GObject *object, guint prop_id,
				  const GValue *value, GParamSpec *pspec)
{
    ZBarGtk *self = ZBAR_GTK(object);

    switch (prop_id) {
    case PROP_VIDEO_DEVICE:
	zbar_gtk_set_video_device(self, g_value_get_string(value));
	break;
    case PROP_VIDEO_ENABLED:
	zbar_gtk_set_video_enabled(self, g_value_get_boolean(value));
	break;
    default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}

static void zbar_gtk_get_property(GObject *object, guint prop_id, GValue *value,
				  GParamSpec *pspec)
{
    ZBarGtk *self = ZBAR_GTK(object);
    ZBarGtkPrivate *zbar;

    if (!self->_private)
	return;
    zbar = ZBAR_GTK_PRIVATE(self->_private);

    switch (prop_id) {
    case PROP_VIDEO_DEVICE:
	if (zbar->video_device)
	    g_value_set_string(value, zbar->video_device);
	else
	    g_value_set_static_string(value, "");
	break;
    case PROP_VIDEO_ENABLED:
	g_value_set_boolean(value, zbar->video_enabled);
	break;
    case PROP_VIDEO_OPENED:
	g_value_set_boolean(value, zbar->video_opened);
	break;
    default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}

static void zbar_gtk_init(ZBarGtk *self)
{
    ZBarGtkPrivate *zbar = g_object_new(ZBAR_TYPE_GTK_PRIVATE, NULL);
    self->_private	 = (void *)zbar;

    zbar->window = zbar_window_create();
    g_assert(zbar->window);

    zbar->req_width = zbar->video_width = DEFAULT_WIDTH;
    zbar->req_height = zbar->video_height = DEFAULT_HEIGHT;

    /* use a queue to signalize about the need to handle decoding and video */
    zbar->queue		      = g_async_queue_new();
    zbar->idle_id	      = g_idle_add(zbar_processing_idle_callback, self);
    zbar->video_enabled_state = FALSE;

    /* Prepare for idle handler */
    g_object_ref(zbar);
    g_assert(zbar->queue);
    g_async_queue_ref(zbar->queue);

    zbar->scanner = zbar_image_scanner_create();
    g_assert(zbar->scanner);
}

static void zbar_gtk_dispose(GObject *object)
{
    ZBarGtk *self = ZBAR_GTK(object);

    if (!self->_private)
	return;

    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);
    self->_private	 = NULL;

    g_free((void *)zbar->video_device);
    zbar->video_device = NULL;

    /* signal processor idle handler to exit */
    GValue *msg = zbar_gtk_new_value(G_TYPE_INT);
    g_value_set_int(msg, -1);
    g_async_queue_push(zbar->queue, msg);
    zbar->idle_id = 0;

    /* there are no external references which might call other APIs */
    g_async_queue_unref(zbar->queue);

    g_object_unref(G_OBJECT(zbar));
}

static void zbar_gtk_private_finalize(GObject *object)
{
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(object);

    if (zbar->idle_id) {
	if (zbar->window)
	    zbar_window_draw(zbar->window, NULL);
	g_object_unref(zbar);
	g_source_remove(zbar->idle_id);
	zbar->idle_id = 0;
    }

    if (zbar->window) {
	zbar_window_destroy(zbar->window);
	zbar->window = NULL;
    }
    if (zbar->scanner) {
	zbar_image_scanner_destroy(zbar->scanner);
	zbar->scanner = NULL;
    }
    if (zbar->video) {
	zbar_video_destroy(zbar->video);
	zbar->video = NULL;
    }
    g_async_queue_unref(zbar->queue);
    zbar->queue = NULL;
}

static void zbar_gtk_class_init(ZBarGtkClass *klass)
{
    GParamSpec *p;

    zbar_gtk_parent_class = g_type_class_peek_parent(klass);

    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    object_class->dispose      = zbar_gtk_dispose;
    object_class->set_property = zbar_gtk_set_property;
    object_class->get_property = zbar_gtk_get_property;

    GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
    widget_class->realize	 = zbar_gtk_realize;
    widget_class->unrealize	 = zbar_gtk_unrealize;
#if GTK_MAJOR_VERSION >= 3
    widget_class->get_preferred_width  = zbar_get_preferred_width;
    widget_class->get_preferred_height = zbar_get_preferred_height;
    widget_class->draw		       = zbar_gtk_scale_draw;
#else
    widget_class->size_request = zbar_gtk_size_request;
    widget_class->expose_event = zbar_gtk_expose;
#endif
    widget_class->size_allocate = zbar_gtk_size_allocate;

    widget_class->unmap = NULL;

    zbar_gtk_signals[DECODED] =
	g_signal_new("decoded", G_TYPE_FROM_CLASS(object_class),
		     G_SIGNAL_RUN_CLEANUP,
		     G_STRUCT_OFFSET(ZBarGtkClass, decoded), NULL, NULL,
		     zbar_marshal_VOID__INT_STRING, G_TYPE_NONE, 2, G_TYPE_INT,
		     G_TYPE_STRING);

    zbar_gtk_signals[DECODED_TEXT] = g_signal_new(
	"decoded-text", G_TYPE_FROM_CLASS(object_class), G_SIGNAL_RUN_CLEANUP,
	G_STRUCT_OFFSET(ZBarGtkClass, decoded_text), NULL, NULL,
	g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);

    p = g_param_spec_string("video-device", "Video device",
			    "the platform specific name of the video device",
			    NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
    g_object_class_install_property(object_class, PROP_VIDEO_DEVICE, p);

    p = g_param_spec_boolean("video-enabled", "Video enabled",
			     "controls streaming from the video device", FALSE,
			     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
    g_object_class_install_property(object_class, PROP_VIDEO_ENABLED, p);

    p = g_param_spec_boolean("video-opened", "Video opened",
			     "current opened state of the video device", FALSE,
			     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
    g_object_class_install_property(object_class, PROP_VIDEO_OPENED, p);
}

static void zbar_gtk_private_class_init(ZBarGtkPrivateClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    object_class->finalize     = zbar_gtk_private_finalize;
}

static GType zbar_gtk_private_get_type(void)
{
    static GType type = 0;

    if (!type) {
	static const GTypeInfo info = {
	    sizeof(ZBarGtkPrivateClass),
	    NULL,
	    NULL,
	    (GClassInitFunc)zbar_gtk_private_class_init,
	    NULL,
	    NULL,
	    sizeof(ZBarGtkPrivate),
	};
	type =
	    g_type_register_static(G_TYPE_OBJECT, "ZBarGtkPrivate", &info, 0);
    }
    return (type);
}

GtkWidget *zbar_gtk_new(void)
{
    return (GTK_WIDGET(g_object_new(ZBAR_TYPE_GTK, NULL)));
}