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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
|
/* -*- mode: c; c-basic-offset: 2 -*-
*
* This is kiba, a useless but fun GNOME dock.
*/
#include <gtk/gtk.h>
#include <gtk/gtkprivate.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <gdk/gdkx.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <gconf/gconf-client.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "akamaru.h"
#define KIBA_GCONF_PATH "/apps/kiba"
#define PIXMAP_PATH "/usr/share/pixmaps"
#define ICON_SIZE 48
GQuark kiba_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("kiba-quark");
return q;
}
#define KIBA_ERROR kiba_error_quark ()
typedef struct _KibaDock KibaDock;
typedef struct _KibaDockClass KibaDockClass;
typedef struct _KibaLauncher KibaLauncher;
typedef enum {
KIBA_DOCK_POSITION_LEFT_EDGE_TOP,
KIBA_DOCK_POSITION_LEFT_EDGE_CENTER,
KIBA_DOCK_POSITION_LEFT_EDGE_BOTTOM,
KIBA_DOCK_POSITION_RIGHT_EDGE_TOP,
KIBA_DOCK_POSITION_RIGHT_EDGE_CENTER,
KIBA_DOCK_POSITION_RIGHT_EDGE_BOTTOM,
KIBA_DOCK_POSITION_TOP_EDGE_LEFT,
KIBA_DOCK_POSITION_TOP_EDGE_CENTER,
KIBA_DOCK_POSITION_TOP_EDGE_RIGHT,
KIBA_DOCK_POSITION_BOTTOM_EDGE_LEFT,
KIBA_DOCK_POSITION_BOTTOM_EDGE_CENTER,
KIBA_DOCK_POSITION_BOTTOM_EDGE_RIGHT
} KibaDockPosition;
struct _KibaDock
{
GtkWidget widget;
KibaDockPosition position;
Model model;
Anchor *anchor;
Anchor *mouse_anchor;
int spacing;
int num_launchers;
GList *launchers;
int drag_offset_x;
int drag_offset_y;
int drag_distance;
KibaLauncher *dragging_launcher;
GConfClient *gconf_client;
Object *object;
};
struct _KibaDockClass
{
GtkWidgetClass parent_class;
};
struct _KibaLauncher {
GdkWindow *window;
GdkPixbuf *pixbuf;
KibaDock *dock;
Object *object;
char *name;
char *exec;
char *comment;
char *icon;
gboolean prelight;
};
#define KIBA_TYPE_DOCK (kiba_dock_get_type ())
#define KIBA_DOCK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KIBA_TYPE_DOCK, KibaDock))
#define KIBA_DOCK_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), KIBA_DOCK, KibaDockClass))
#define KIBA_IS_DOCK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KIBA_TYPE_DOCK))
#define KIBA_IS_DOCK_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), KIBA_TYPE_DOCK))
#define KIBA_DOCK_GET_CLASS (G_TYPE_INSTANCE_GET_CLASS ((obj), KIBA_TYPE_DOCK, KibaDockClass))
static void kiba_dock_realize (GtkWidget *widget);
static void kiba_dock_show (GtkWidget *widget);
static void kiba_dock_map (GtkWidget *widget);
static gboolean kiba_dock_expose_event (GtkWidget *clock,
GdkEventExpose *event);
static gboolean kiba_dock_button_press_event (GtkWidget *widget,
GdkEventButton *event);
static gboolean kiba_dock_button_release_event (GtkWidget *widget,
GdkEventButton *event);
static gboolean kiba_dock_motion_notify_event (GtkWidget *widget,
GdkEventMotion *event);
static gboolean kiba_dock_enter_notify_event (GtkWidget *widget,
GdkEventCrossing *event);
static gboolean kiba_dock_leave_notify_event (GtkWidget *widget,
GdkEventCrossing *event);
static void kiba_dock_drag_data_received (GtkWidget *widget,
GdkDragContext *drag_context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time);
static void kiba_dock_init_model (KibaDock *dock,
int width,
int height);
static KibaLauncher *kiba_launcher_new (KibaDock *dock,
const char *gconf_path);
static void kiba_launcher_create_window (KibaLauncher *launcher);
static void kiba_dock_add_launcher (KibaDock *dock, KibaLauncher *launcher,
int x, int y);
static gint timeout_callback (gpointer data);
G_DEFINE_TYPE (KibaDock, kiba_dock, GTK_TYPE_WIDGET)
typedef struct {
const char *name;
GtkOrientation orientation;
int halignment;
int valignment;
} KibaDockLayoutData;
static const KibaDockLayoutData kiba_dock_layout_data[] = {
{ "left-edge-top", GTK_ORIENTATION_VERTICAL, 0, 0 },
{ "left-edge-center", GTK_ORIENTATION_VERTICAL, 0, 1 },
{ "left-edge-bottom", GTK_ORIENTATION_VERTICAL, 0, 2 },
{ "right-edge-top", GTK_ORIENTATION_VERTICAL, 2, 0 },
{ "right-edge-center", GTK_ORIENTATION_VERTICAL, 2, 1 },
{ "right-edge-bottom", GTK_ORIENTATION_VERTICAL, 2, 2 },
{ "top-edge-left", GTK_ORIENTATION_HORIZONTAL, 0, 0 },
{ "top-edge-center", GTK_ORIENTATION_HORIZONTAL, 1, 0 },
{ "top-edge-right", GTK_ORIENTATION_HORIZONTAL, 2, 0 },
{ "bottom-edge-left", GTK_ORIENTATION_HORIZONTAL, 0, 2 },
{ "bottom-edge-center", GTK_ORIENTATION_HORIZONTAL, 1, 2 },
{ "bottom-edge-right", GTK_ORIENTATION_HORIZONTAL, 2, 2 }
};
static void
kiba_dock_class_init (KibaDockClass *class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
widget_class->realize = kiba_dock_realize;
widget_class->show = kiba_dock_show;
widget_class->map = kiba_dock_map;
widget_class->expose_event = kiba_dock_expose_event;
widget_class->drag_data_received = kiba_dock_drag_data_received;
widget_class->button_press_event = kiba_dock_button_press_event;
widget_class->button_release_event = kiba_dock_button_release_event;
widget_class->motion_notify_event = kiba_dock_motion_notify_event;
widget_class->enter_notify_event = kiba_dock_enter_notify_event;
widget_class->leave_notify_event = kiba_dock_leave_notify_event;
}
static void
kiba_dock_init (KibaDock *dock)
{
GdkScreen *screen;
int width, height;
static const GtkTargetEntry targets[] = {
{ "text/plain", 0, 0 }
};
GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (dock), GTK_NO_WINDOW);
GTK_WIDGET_SET_FLAGS (GTK_WIDGET (dock), GTK_TOPLEVEL);
GTK_PRIVATE_SET_FLAG (GTK_WIDGET (dock), GTK_ANCHORED);
screen = gdk_screen_get_default ();
width = gdk_screen_get_width (screen);
height = gdk_screen_get_height (screen);
dock->position = KIBA_DOCK_POSITION_BOTTOM_EDGE_CENTER;
dock->spacing = ICON_SIZE;
kiba_dock_init_model (dock, width, height);
gtk_drag_dest_set (GTK_WIDGET (dock),
GTK_DEST_DEFAULT_ALL,
targets, G_N_ELEMENTS (targets),
GDK_ACTION_MOVE | GDK_ACTION_COPY);
gtk_drag_dest_add_uri_targets (GTK_WIDGET (dock));
g_timeout_add (20, timeout_callback, dock);
}
GtkWidget*
kiba_dock_new (GConfClient *gconf_client)
{
GtkWidget *dock = g_object_new (KIBA_TYPE_DOCK, NULL);
KIBA_DOCK (dock)->gconf_client = gconf_client;
return dock;
}
static KibaLauncher *
kiba_dock_get_launcher_for_window (KibaDock *dock,
GdkWindow *window)
{
GList *l;
for (l = dock->launchers; l != NULL; l = l->next)
{
KibaLauncher *launcher = l->data;
if (launcher->window == window)
return launcher;
}
return NULL;
}
static void
kiba_dock_paint (KibaDock *dock)
{
GtkWidget *widget = GTK_WIDGET (dock);
cairo_pattern_t *gradient;
cairo_t *cr;
const int margin = 1, radius = 5;
int x0, y0, x1, y1;
const KibaDockLayoutData *d;
d = &kiba_dock_layout_data[dock->position];
x0 = (d->halignment == 0 ? -radius : margin);
y0 = (d->valignment == 0 ? -radius : margin);;
x1 = widget->allocation.width - (d->halignment == 2 ? -radius : margin);
y1 = widget->allocation.height - (d->valignment == 2 ? -radius : margin);
cr = gdk_cairo_create (widget->window);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_arc (cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
cairo_line_to (cr, x0, y0 + radius);
cairo_arc (cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
cairo_line_to (cr, x1 - radius, y0);
cairo_arc (cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
cairo_line_to (cr, x1, y1 - radius);
cairo_arc (cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
cairo_close_path (cr);
gradient = cairo_pattern_create_linear ((x0 + x1) / 2 - 1, y0,
(x0 + x1) / 2 + 1, y1);
cairo_pattern_add_color_stop_rgba (gradient, 0, 1, 1, 1, 0.4);
cairo_pattern_add_color_stop_rgba (gradient, 1, 1, 1, 1, 0.6);
cairo_set_source (cr, gradient);
cairo_fill_preserve (cr);
cairo_set_source_rgba (cr, 1, 1, 1, 1);
cairo_set_line_width (cr, 1);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_stroke (cr);
cairo_destroy (cr);
}
static void
kiba_launcher_paint (KibaLauncher *launcher)
{
cairo_t *cr;
cr = gdk_cairo_create (launcher->window);
gdk_cairo_set_source_pixbuf (cr, launcher->pixbuf, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
if (launcher->prelight)
{
cairo_set_source_rgba (cr, 1, 1, 1, 0.3);
cairo_set_operator (cr, CAIRO_OPERATOR_ATOP);
cairo_paint (cr);
}
cairo_destroy (cr);
}
static gboolean
kiba_dock_expose_event (GtkWidget *widget,
GdkEventExpose *event)
{
KibaDock *dock = KIBA_DOCK (widget);
KibaLauncher *launcher;
if (event->window == widget->window)
kiba_dock_paint (dock);
else
{
launcher = kiba_dock_get_launcher_for_window (dock, event->window);
if (launcher != NULL)
kiba_launcher_paint (launcher);
}
return TRUE;
}
static void
kiba_dock_realize (GtkWidget *widget)
{
KibaDock *dock = KIBA_DOCK (widget);
GdkScreen *screen;
GdkWindowAttr attributes;
gint attributes_mask;
GList *l;
g_return_if_fail (KIBA_IS_DOCK (widget));
GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
screen = gdk_screen_get_default ();
attributes.window_type = GDK_WINDOW_TEMP;
attributes.x = widget->allocation.x;
attributes.y = widget->allocation.y;
attributes.width = widget->allocation.width;
attributes.height = widget->allocation.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gdk_screen_get_rgba_visual (screen);
attributes.colormap = gdk_screen_get_rgba_colormap (screen);
attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
widget->window = gdk_window_new (gdk_screen_get_root_window (screen),
&attributes, attributes_mask);
gdk_window_set_user_data (widget->window, dock);
for (l = dock->launchers; l != NULL; l = l->next)
{
KibaLauncher *launcher = l->data;
kiba_launcher_create_window (launcher);
}
}
static void
kiba_dock_layout (KibaDock *dock)
{
const KibaDockLayoutData *d;
GtkAllocation allocation;
GdkScreen *screen;
int width, height;
const int cap_size = 20;
screen = gdk_screen_get_default ();
width = gdk_screen_get_width (screen);
height = gdk_screen_get_height (screen);
d = &kiba_dock_layout_data[dock->position];
if (d->orientation == GTK_ORIENTATION_VERTICAL)
{
allocation.width = 2 * ICON_SIZE / 3;
allocation.height = dock->num_launchers * dock->spacing +
(d->valignment == 0 ? 2 * cap_size : cap_size);
}
else
{
allocation.width = dock->num_launchers * dock->spacing +
(d->halignment == 0 ? 2 * cap_size : cap_size);
allocation.height = 2 * ICON_SIZE / 3;
}
allocation.x = d->halignment * (width - allocation.width) / 2;
allocation.y = d->valignment * (height - allocation.height) / 2;
dock->anchor->x = allocation.x + allocation.width / 2;
dock->anchor->y = allocation.y + allocation.height / 2;
if (d->orientation == GTK_ORIENTATION_VERTICAL)
{
dock->model.gravity.x = (d->halignment - 1) * 50;
dock->model.gravity.y = 0;
}
else
{
dock->model.gravity.x = 0;
dock->model.gravity.y = (d->valignment - 1) * 50;
}
gtk_widget_size_allocate (GTK_WIDGET (dock), &allocation);
gtk_widget_queue_draw (GTK_WIDGET (dock));
}
static void
kiba_dock_set_position (KibaDock *dock, KibaDockPosition position)
{
dock->position = position;
kiba_dock_layout (dock);
}
static void
kiba_dock_show (GtkWidget *widget)
{
KibaDock *dock = KIBA_DOCK (widget);
GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
if (!GTK_WIDGET_REALIZED (widget))
{
kiba_dock_layout (dock);
gtk_widget_realize (widget);
}
gtk_widget_map (widget);
}
static void
kiba_dock_map (GtkWidget *widget)
{
KibaDock *dock = KIBA_DOCK (widget);
GList *l;
GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
gdk_window_show (widget->window);
for (l = dock->launchers; l != NULL; l = l->next)
{
KibaLauncher *launcher = l->data;
gdk_window_show (launcher->window);
}
}
static gint
timeout_callback (gpointer data)
{
KibaDock *dock = data;
GList *l;
int i;
for (l = dock->launchers; l != NULL; l = l->next)
{
KibaLauncher *launcher = l->data;
gdk_window_move (launcher->window,
launcher->object->position.x + 0.5 - ICON_SIZE / 2,
launcher->object->position.y + 0.5 - ICON_SIZE / 2);
}
for (i = 0; i < 6; i++)
model_step (&dock->model, 0.01);
return TRUE;
}
static void
add_spacer (Object *object, void *data)
{
KibaDock *dock = data;
/* Skip the anchor object. */
if (object != dock->anchor->object && object != dock->object)
model_add_spacer (&dock->model, dock->object, object, dock->spacing);
}
static void
kiba_dock_add_launcher (KibaDock *dock, KibaLauncher *launcher, int x, int y)
{
Object *object;
object = model_add_object (&dock->model, x, y, 12, NULL);
model_add_spring (&dock->model, dock->anchor->object, object, 0);
dock->object = object;
model_for_each_object (&dock->model, add_spacer, dock);
dock->launchers = g_list_prepend (dock->launchers, launcher);
dock->num_launchers++;
launcher->object = object;
launcher->dock = dock;
if (GTK_WIDGET_REALIZED (GTK_WIDGET (dock)))
kiba_launcher_create_window (launcher);
if (GTK_WIDGET_MAPPED (GTK_WIDGET (dock)))
gdk_window_show (launcher->window);
kiba_dock_layout (dock);
}
static void
kiba_dock_init_model (KibaDock *dock, int width, int height)
{
Object *object;
model_init (&dock->model);
dock->model.elasticity = 0.9;
dock->model.friction = 150;
dock->model.k = 0.8;
dock->model.constrain_iterations = 1;
model_add_enclosing_rectangle (&dock->model, ICON_SIZE / 2, ICON_SIZE / 2,
width - ICON_SIZE / 2, height - ICON_SIZE / 2);
object = model_add_object (&dock->model, 0, 0, 0, NULL);
dock->anchor = model_add_anchor (&dock->model, object, 0, 0 );
dock->mouse_anchor = model_add_anchor (&dock->model, NULL, 0, 0);
}
static gboolean
kiba_dock_button_press_event (GtkWidget *widget,
GdkEventButton *event)
{
KibaDock *dock = KIBA_DOCK (widget);
KibaLauncher *launcher;
dock->drag_offset_x = event->x;
dock->drag_offset_y = event->y;
dock->drag_distance = 0;
launcher = kiba_dock_get_launcher_for_window (dock, event->window);
if (launcher == NULL)
return FALSE;
dock->dragging_launcher = launcher;
dock->mouse_anchor->x = launcher->object->position.x;
dock->mouse_anchor->y = launcher->object->position.y;
dock->mouse_anchor->object = launcher->object;
return TRUE;
}
static gboolean
kiba_launcher_exec (gpointer data)
{
KibaLauncher *launcher = data;
char **argv;
GError *error = NULL;
/* Bounce the icon. */
launcher->object->previous_position.y += g_random_double_range (5, 10);
/* FIXME: Support drag and drop on launchers with %U and %F */
/* FIXME: Use g_shell_quote or split or whatever it was... */
argv = g_strsplit (launcher->exec, " ", 0);
g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, NULL, &error);
if (error != NULL)
g_error ("failed to launch %s: %s\n", argv[0], error->message);
g_strfreev (argv);
return FALSE;
}
static void
kiba_launcher_queue_draw (KibaLauncher *launcher)
{
GdkRectangle invalid_rect;
invalid_rect.x = 0;
invalid_rect.y = 0;
invalid_rect.width = gdk_pixbuf_get_width (launcher->pixbuf);
invalid_rect.height = gdk_pixbuf_get_height (launcher->pixbuf);
gdk_window_invalidate_rect (launcher->window, &invalid_rect, TRUE);
}
static gboolean
kiba_dock_button_release_event (GtkWidget *widget,
GdkEventButton *event)
{
KibaDock *dock = KIBA_DOCK (widget);
if (dock->dragging_launcher && dock->drag_distance <= 1)
g_idle_add (kiba_launcher_exec, dock->dragging_launcher);
dock->dragging_launcher = NULL;
dock->mouse_anchor->object = NULL;
return TRUE;
}
static gboolean
kiba_dock_motion_notify_event (GtkWidget *widget,
GdkEventMotion *event)
{
KibaDock *dock = KIBA_DOCK (widget);
GdkModifierType state;
int x, y, new_x, new_y, dx, dy;
GdkScreen *screen;
int width, height;
screen = gdk_screen_get_default ();
width = gdk_screen_get_width (screen);
height = gdk_screen_get_height (screen);
gdk_window_get_pointer (gdk_get_default_root_window(), &x, &y, &state);
new_x = x - dock->drag_offset_x + ICON_SIZE / 2;
new_y = y - dock->drag_offset_y + ICON_SIZE / 2;
dx = new_x - dock->mouse_anchor->x;
dy = new_y - dock->mouse_anchor->y;
dock->mouse_anchor->x = CLAMP (new_x, ICON_SIZE / 2, width - ICON_SIZE /2 );
dock->mouse_anchor->y = CLAMP (new_y, ICON_SIZE / 2, height - ICON_SIZE / 2);
dock->drag_distance += sqrt (dx * dx + dy * dy);
return TRUE;
}
static gboolean
kiba_dock_enter_notify_event (GtkWidget *widget,
GdkEventCrossing *event)
{
KibaDock *dock = KIBA_DOCK (widget);
KibaLauncher *launcher;
launcher = kiba_dock_get_launcher_for_window (dock, event->window);
if (launcher == NULL)
return FALSE;
launcher->prelight = TRUE;
kiba_launcher_queue_draw (launcher);
return TRUE;
}
static gboolean
kiba_dock_leave_notify_event (GtkWidget *widget,
GdkEventCrossing *event)
{
KibaDock *dock = KIBA_DOCK (widget);
KibaLauncher *launcher;
launcher = kiba_dock_get_launcher_for_window (dock, event->window);
if (launcher == NULL)
return FALSE;
launcher->prelight = FALSE;
kiba_launcher_queue_draw (launcher);
return TRUE;
}
static void
parse_desktop_file (KibaLauncher *launcher, const char *uri, GError **error)
{
static const char file_uri_prefix[] = "file://";
static const char desktop_file_suffix[] = ".desktop";
static const char desktop_entry_group[] = "Desktop Entry";
static const struct { const char *name; size_t offset; } fields[] = {
{ "Icon", G_STRUCT_OFFSET (KibaLauncher, icon) },
{ "Exec", G_STRUCT_OFFSET (KibaLauncher, exec) },
{ "Name", G_STRUCT_OFFSET (KibaLauncher, name) },
{ "Comment", G_STRUCT_OFFSET (KibaLauncher, comment) },
};
GKeyFile *file;
const char *filename ;
char *s;
int i;
if (!g_str_has_suffix (uri, desktop_file_suffix)) {
g_set_error (error, KIBA_ERROR, 0,
"%s is not a .desktop file", uri);
return;
}
if (g_str_has_prefix (uri, file_uri_prefix))
filename = uri + strlen (file_uri_prefix);
else
filename = uri;
file = g_key_file_new ();
if (!g_key_file_load_from_file (file, filename, 0, error))
return;
for (i = 0; i < G_N_ELEMENTS (fields); i++) {
s = g_key_file_get_value (file, desktop_entry_group,
fields[i].name, error);
if (s == NULL)
return;
G_STRUCT_MEMBER (char *, launcher, fields[i].offset) = s;
}
}
static gboolean
load_icon (KibaLauncher *launcher, GError **error)
{
char *path;
char *suffix;
path = g_strdup_printf (PIXMAP_PATH "/%s", launcher->icon);
launcher->pixbuf = gdk_pixbuf_new_from_file (path, error);
g_free (path);
if (launcher->pixbuf != NULL)
return TRUE;
g_clear_error (error);
launcher->pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default(),
launcher->icon, 48, 0, error);
if (launcher->pixbuf != NULL)
return TRUE;
suffix = strstr (launcher->icon, ".png");
if (suffix == NULL)
return FALSE;
*suffix = '\0';
g_clear_error (error);
launcher->pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default(),
launcher->icon, 48, 0, error);
return launcher->pixbuf != NULL;
}
static void
kiba_launcher_create_window (KibaLauncher *launcher)
{
GdkWindowAttr attributes;
gint attributes_mask;
GdkScreen *screen = gdk_screen_get_default ();
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gdk_screen_get_rgba_visual (screen);
attributes.colormap = gdk_screen_get_rgba_colormap (screen);
attributes.window_type = GDK_WINDOW_TEMP;
attributes.x = launcher->object->position.x;
attributes.y = launcher->object->position.y;
attributes.width = gdk_pixbuf_get_width (launcher->pixbuf);
attributes.height = gdk_pixbuf_get_height (launcher->pixbuf);
attributes.event_mask =
GDK_EXPOSURE_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK |
GDK_ENTER_NOTIFY_MASK |
GDK_LEAVE_NOTIFY_MASK |
GDK_POINTER_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
launcher->window = gdk_window_new (gdk_screen_get_root_window (screen),
&attributes, attributes_mask);
gdk_window_set_user_data (launcher->window, launcher->dock);
}
static KibaLauncher *
kiba_launcher_new (KibaDock *dock, const char *gconf_path)
{
KibaLauncher *launcher;
GError *error = NULL;
char *desktop_file;
char *key;
launcher = g_new0 (KibaLauncher, 1);
key = g_strdup_printf ("%s/file", gconf_path);
desktop_file = gconf_client_get_string (dock->gconf_client, key, &error);
g_free (key);
if (error != NULL) {
printf ("error getting desktop file path for launcher: %s\n",
error->message);
g_error_free (error);
g_free (launcher);
return NULL;
}
parse_desktop_file (launcher, desktop_file, &error);
free (desktop_file);
if (error != NULL) {
printf ("error parsing desktop file: %s\n", error->message);
g_error_free (error);
g_free (launcher);
return NULL;
}
load_icon (launcher, &error);
if (error != NULL) {
printf ("error creating launcher window: %s\n", error->message);
g_error_free (error);
g_free (launcher);
return NULL;
}
return launcher;
}
static void
kiba_dock_drag_data_received(GtkWidget *widget,
GdkDragContext *drag_context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time)
{
KibaDock *dock = KIBA_DOCK (widget);
GError *error = NULL;
char *key;
gtk_drag_finish (drag_context, TRUE, FALSE, time);
g_strstrip ((char *) selection_data->data);
printf ("got desktop file: %s\n", selection_data->data);
key = g_strdup_printf (KIBA_GCONF_PATH "/launchers/%x/file", time);
gconf_client_set_string (dock->gconf_client,
key, (char *) selection_data->data, &error);
if (error != NULL) {
printf ("failed to set gconf key %s: %s\n", key, error->message);
g_error_free (error);
}
if (error == NULL) {
gconf_engine_associate_schema (dock->gconf_client->engine,
key,
"/schemas" KIBA_GCONF_PATH "/launchers/file",
&error);
if (error != NULL) {
printf ("failed to set schema for %s: %s\n", key, error->message);
g_error_free (error);
}
}
g_free (key);
}
static void
set_position_from_string (KibaDock *dock, const char *string)
{
int i;
for (i = 0; i < G_N_ELEMENTS (kiba_dock_layout_data); i++)
{
if (strcmp (kiba_dock_layout_data[i].name, string) == 0)
{
kiba_dock_set_position (dock, i);
return;
}
}
printf ("invalid position\n");
}
static void
options_changed_callback(GConfClient *client,
guint cnxn_id,
GConfEntry *entry,
gpointer user_data)
{
KibaDock *dock;
const char *value;
dock = KIBA_DOCK (user_data);
if (strcmp (entry->key, KIBA_GCONF_PATH "/options/position") == 0)
{
if (entry->value == NULL || entry->value->type != GCONF_VALUE_STRING)
{
printf ("unset or wrong type\n");
}
else
{
value = gconf_value_get_string (entry->value);
set_position_from_string (dock, value);
}
}
}
static void
launchers_changed_callback(GConfClient *client,
guint cnxn_id,
GConfEntry *entry,
gpointer user_data)
{
KibaDock *dock;
KibaLauncher *launcher;
char *path, *last_slash;
GdkScreen *screen;
int width, height;
screen = gdk_screen_get_default ();
width = gdk_screen_get_width (screen);
height = gdk_screen_get_height (screen);
dock = KIBA_DOCK (user_data);
if (!g_str_has_suffix (entry->key, "/file"))
{
printf ("ignoring gconf event for %s\n", entry->key);
return;
}
path = g_strdup (entry->key);
last_slash = strrchr (path, '/');
*last_slash = '\0';
printf ("entry: key = %s, path = %s\n", entry->key, path);
launcher = kiba_launcher_new (dock, path);
g_free (path);
if (launcher == NULL)
return;
kiba_dock_add_launcher (dock, launcher,
g_random_int_range (0, width),
g_random_int_range (0, height));
}
static void
add_launchers (KibaDock *dock, GConfClient *gconf_client)
{
GSList *launchers, *l;
GError *error = NULL;
KibaLauncher *launcher;
GdkScreen *screen;
int width, height;
screen = gdk_screen_get_default ();
width = gdk_screen_get_width (screen);
height = gdk_screen_get_height (screen);
launchers = gconf_client_all_dirs (gconf_client,
KIBA_GCONF_PATH "/launchers",
&error);
if (error != NULL)
{
printf ("error getting launchers: %s\n", error->message);
g_free (error->message);
}
for (l = launchers; l != NULL; l = l->next)
{
char *path = l->data;
launcher = kiba_launcher_new (dock, path);
if (launcher == NULL)
continue;
kiba_dock_add_launcher (dock, launcher,
g_random_int_range (0, width),
g_random_int_range (0, height));
}
}
int main (int argc, char *argv[])
{
GtkWidget *dock;
GConfClient *gconf_client;
GError *error = NULL;
char *position;
gtk_init (&argc, &argv);
gconf_client = gconf_client_get_default ();
dock = kiba_dock_new (gconf_client);
gconf_client_add_dir (gconf_client, KIBA_GCONF_PATH,
GCONF_CLIENT_PRELOAD_NONE, &error);
position = gconf_client_get_string (gconf_client,
KIBA_GCONF_PATH "/options/position",
&error);
set_position_from_string (KIBA_DOCK (dock), position);
g_free (position);
gconf_client_notify_add(gconf_client, KIBA_GCONF_PATH "/options",
options_changed_callback,
dock,
NULL, &error);
gconf_client_notify_add(gconf_client, KIBA_GCONF_PATH "/launchers",
launchers_changed_callback,
dock,
NULL, &error);
add_launchers (KIBA_DOCK (dock), gconf_client);
gtk_widget_show (dock);
gtk_main ();
return 0;
}
|