Remove old main() functions
[tilda-gobject.git] / tomboyutil.c
1
2
3 #include <gdk/gdk.h>
4 #include <gdk/gdkwindow.h>
5 #include <gdk/gdkx.h>
6 #include <gtk/gtk.h>
7 #include <X11/Xlib.h>
8 #include <X11/Xatom.h>
9
10 #include "tomboykeybinder.h"
11 #include "tomboyutil.h"
12
13 /* Uncomment the next line to print a debug trace. */
14 /* #define DEBUG */
15
16 #ifdef DEBUG
17 #  define TRACE(x) x
18 #else
19 #  define TRACE(x) do {} while (FALSE);
20 #endif
21
22 gint
23 tomboy_window_get_workspace (GtkWindow *window)
24 {
25         GdkWindow *gdkwin = GTK_WIDGET (window)->window;
26         GdkAtom wm_desktop = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
27         GdkAtom out_type;
28         gint out_format, out_length;
29         gulong *out_val;
30         int workspace;
31
32         if (!gdk_property_get (gdkwin,
33                                wm_desktop,
34                                _GDK_MAKE_ATOM (XA_CARDINAL),
35                                0, G_MAXLONG,
36                                FALSE,
37                                &out_type,
38                                &out_format,
39                                &out_length,
40                                (guchar **) &out_val))
41                 return -1;
42
43         workspace = *out_val;
44         g_free (out_val);
45
46         return workspace;
47 }
48
49 void
50 tomboy_window_move_to_current_workspace (GtkWindow *window)
51 {
52         GdkWindow *gdkwin = GTK_WIDGET (window)->window;
53         GdkWindow *rootwin = 
54                 gdk_screen_get_root_window (gdk_drawable_get_screen (gdkwin));
55
56         GdkAtom current_desktop = 
57                 gdk_atom_intern ("_NET_CURRENT_DESKTOP", FALSE);
58         GdkAtom wm_desktop = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
59         GdkAtom out_type;
60         gint out_format, out_length;
61         gulong *out_val;
62         int workspace;
63         XEvent xev;
64
65         if (!gdk_property_get (rootwin,
66                                current_desktop,
67                                _GDK_MAKE_ATOM (XA_CARDINAL),
68                                0, G_MAXLONG,
69                                FALSE,
70                                &out_type,
71                                &out_format,
72                                &out_length,
73                                (guchar **) &out_val))
74                 return;
75
76         workspace = *out_val;
77         g_free (out_val);
78
79         TRACE (g_print ("Setting _NET_WM_DESKTOP to: %d\n", workspace));
80
81         xev.xclient.type = ClientMessage;
82         xev.xclient.serial = 0;
83         xev.xclient.send_event = True;
84         xev.xclient.display = GDK_WINDOW_XDISPLAY (gdkwin);
85         xev.xclient.window = GDK_WINDOW_XWINDOW (gdkwin);
86         xev.xclient.message_type = 
87                 gdk_x11_atom_to_xatom_for_display(
88                         gdk_drawable_get_display (gdkwin),
89                         wm_desktop);
90         xev.xclient.format = 32;
91         xev.xclient.data.l[0] = workspace;
92         xev.xclient.data.l[1] = 0;
93         xev.xclient.data.l[2] = 0;
94
95         XSendEvent (GDK_WINDOW_XDISPLAY (rootwin),
96                     GDK_WINDOW_XWINDOW (rootwin),
97                     False,
98                     SubstructureRedirectMask | SubstructureNotifyMask,
99                     &xev);
100 }
101
102 static void
103 tomboy_window_override_user_time (GtkWindow *window)
104 {
105         guint32 ev_time = gtk_get_current_event_time();
106
107         if (ev_time == 0) {
108                 /* 
109                  * FIXME: Global keypresses use an event filter on the root
110                  * window, which processes events before GDK sees them.
111                  */
112                 ev_time = tomboy_keybinder_get_current_event_time ();
113         }
114         if (ev_time == 0) {
115                 gint ev_mask = gtk_widget_get_events (GTK_WIDGET(window));
116                 if (!(ev_mask & GDK_PROPERTY_CHANGE_MASK)) {
117                         gtk_widget_add_events (GTK_WIDGET (window),
118                                                GDK_PROPERTY_CHANGE_MASK);
119                 }
120
121                 /* 
122                  * NOTE: Last resort for D-BUS or other non-interactive
123                  *       openings.  Causes roundtrip to server.  Lame. 
124                  */
125                 ev_time = gdk_x11_get_server_time (GTK_WIDGET(window)->window);
126         }
127
128         TRACE (g_print("Setting _NET_WM_USER_TIME to: %d\n", ev_time));
129         gdk_x11_window_set_user_time (GTK_WIDGET(window)->window, ev_time);
130 }
131
132 void
133 tomboy_window_present_hardcore (GtkWindow *window)
134 {
135         if (!GTK_WIDGET_REALIZED (window))
136                 gtk_widget_realize (GTK_WIDGET (window));
137         else if (GTK_WIDGET_VISIBLE (window))
138                 tomboy_window_move_to_current_workspace (window);
139
140         tomboy_window_override_user_time (window);
141
142         gtk_window_present (window);
143 }
144