~golfish/netbook-remix-launcher/desktop

« back to all changes in this revision

Viewing changes to src/launcher-wm.c

  • Committer: Neil J. Patel
  • Date: 2008-04-16 11:31:15 UTC
  • Revision ID: njpatel@gmail.com-20080416113115-ztljg0qms79anijd
* Inital import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Canonical Ltd
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Authored by Neil Jagdish Patel <neil.patel@canonical.com>
 
20
 *
 
21
 */
 
22
 
 
23
#include "launcher-wm.h"
 
24
 
 
25
#include "launcher-menu.h"
 
26
 
 
27
G_DEFINE_TYPE (LauncherWm, launcher_wm, G_TYPE_OBJECT);
 
28
 
 
29
#define LAUNCHER_WM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
 
30
  LAUNCHER_TYPE_WM, \
 
31
  LauncherWmPrivate))
 
32
 
 
33
struct _LauncherWmPrivate
 
34
{
 
35
  WnckScreen *screen;
 
36
 
 
37
  GHashTable *table;
 
38
};
 
39
 
 
40
enum
 
41
{
 
42
  SHOW_DECS,
 
43
  HIDE_DECS,
 
44
 
 
45
  LAST_SIGNAL
 
46
};
 
47
static guint _wm_signals[LAST_SIGNAL] = { 0 };
 
48
 
 
49
static void
 
50
on_active_window_changed (WnckScreen *screen, 
 
51
                          WnckWindow *prev_window,
 
52
                          LauncherWm *wm)
 
53
{
 
54
  WnckWindow *window;
 
55
  WnckWindowType type;
 
56
 
 
57
  window = wnck_screen_get_active_window (screen);
 
58
 
 
59
  if (!WNCK_IS_WINDOW (window))
 
60
  {
 
61
    g_signal_emit (wm, _wm_signals[HIDE_DECS], 0);
 
62
    /*g_debug ("ACTIVE: Desktop\n");*/
 
63
    return;
 
64
  }
 
65
  
 
66
  type = wnck_window_get_window_type (window);
 
67
  switch (type)
 
68
  {
 
69
    case WNCK_WINDOW_NORMAL:
 
70
      break;
 
71
    case WNCK_WINDOW_DESKTOP:
 
72
    case WNCK_WINDOW_DOCK:
 
73
      g_signal_emit (wm, _wm_signals[HIDE_DECS], 0);
 
74
      /*g_debug ("ACTIVE: Desktop\n");*/
 
75
      return;
 
76
    default:
 
77
      return;
 
78
   }
 
79
 
 
80
  g_signal_emit (wm, _wm_signals[SHOW_DECS], 0, window);
 
81
  /*g_debug ("ACTIVE: %s\n", wnck_window_get_name (window));*/
 
82
}
 
83
 
 
84
static void
 
85
on_window_opened (WnckScreen *screen, WnckWindow *window, LauncherWm *wm)
 
86
{
 
87
  LauncherWmPrivate *priv;
 
88
  WnckWindowType type;
 
89
  GList *list, *l;
 
90
  gint pid;
 
91
 
 
92
  g_return_if_fail (LAUNCHER_IS_WM (wm));
 
93
  priv = wm->priv;
 
94
 
 
95
  type = wnck_window_get_window_type (window);
 
96
  switch (type)
 
97
  {
 
98
    case WNCK_WINDOW_NORMAL:
 
99
      break;
 
100
    default:
 
101
      return;
 
102
   }
 
103
 
 
104
  pid = wnck_window_get_pid (window);
 
105
  
 
106
  list = launcher_menu_get_applications (launcher_menu_get_default ());
 
107
  for (l = list; l; l = l->next)
 
108
  {
 
109
    if (pid == launcher_menu_application_get_pid (l->data))
 
110
    {
 
111
      launcher_menu_application_set_window (l->data, window);
 
112
 
 
113
      g_hash_table_insert (priv->table, (gpointer)&pid, l->data);
 
114
      return;
 
115
    }
 
116
  }
 
117
}
 
118
 
 
119
static void
 
120
on_window_closed (WnckScreen *screen, WnckWindow *window, LauncherWm *wm)
 
121
{
 
122
  LauncherWmPrivate *priv;
 
123
  LauncherMenuApplication *app = NULL;
 
124
  WnckWindowType type;
 
125
  gint pid;
 
126
 
 
127
  g_return_if_fail (LAUNCHER_IS_WM (wm));
 
128
  priv = wm->priv;
 
129
 
 
130
  type = wnck_window_get_window_type (window);
 
131
  switch (type)
 
132
  {
 
133
    case WNCK_WINDOW_NORMAL:
 
134
      break;
 
135
    default:
 
136
      return;
 
137
   }
 
138
 
 
139
  pid = wnck_window_get_pid (window);
 
140
  
 
141
  app = g_hash_table_lookup (priv->table, (gconstpointer)&pid);
 
142
 
 
143
  if (app)
 
144
  {
 
145
    launcher_menu_application_set_pid (app, -1);
 
146
    launcher_menu_application_set_window (app, NULL);
 
147
    g_hash_table_remove (priv->table, (gconstpointer)&pid);
 
148
  }
 
149
  
 
150
}
 
151
 
 
152
/*
 
153
 * Destroy the hashtable when the menu changes, as all its keys are invalid
 
154
 */
 
155
static void
 
156
on_menu_changed (LauncherMenu *menu, LauncherWm *wm)
 
157
{
 
158
  g_return_if_fail (LAUNCHER_IS_WM (wm));
 
159
 
 
160
  g_hash_table_unref (wm->priv->table);
 
161
  wm->priv->table = NULL;
 
162
 
163
 
 
164
/* GObject stuff */
 
165
static void
 
166
launcher_wm_class_init (LauncherWmClass *klass)
 
167
{
 
168
  GObjectClass        *obj_class = G_OBJECT_CLASS (klass);
 
169
 
 
170
        _wm_signals[SHOW_DECS] =
 
171
                g_signal_new ("show-windec",
 
172
                              G_OBJECT_CLASS_TYPE (obj_class),
 
173
                              G_SIGNAL_RUN_LAST,
 
174
                              G_STRUCT_OFFSET (LauncherWmClass, show_windec),
 
175
                              NULL, NULL,
 
176
                              g_cclosure_marshal_VOID__OBJECT, 
 
177
                              G_TYPE_NONE,
 
178
                              1, WNCK_TYPE_WINDOW);
 
179
 
 
180
  _wm_signals[HIDE_DECS] =
 
181
                g_signal_new ("hide-windec",
 
182
                              G_OBJECT_CLASS_TYPE (obj_class),
 
183
                              G_SIGNAL_RUN_LAST,
 
184
                              G_STRUCT_OFFSET (LauncherWmClass, hide_windec),
 
185
                              NULL, NULL,
 
186
                              g_cclosure_marshal_VOID__VOID, 
 
187
                              G_TYPE_NONE, 0);  
 
188
 
 
189
  g_type_class_add_private (obj_class, sizeof (LauncherWmPrivate));
 
190
}
 
191
 
 
192
static void
 
193
launcher_wm_init (LauncherWm *wm)
 
194
{
 
195
  LauncherWmPrivate *priv;
 
196
        
 
197
  priv = wm->priv = LAUNCHER_WM_GET_PRIVATE (wm);
 
198
 
 
199
  priv->screen = wnck_screen_get_default ();
 
200
 
 
201
  priv->table = g_hash_table_new (g_int_hash, g_direct_equal);
 
202
 
 
203
  g_signal_connect (priv->screen, "active-window-changed",
 
204
                    G_CALLBACK (on_active_window_changed), (gpointer)wm);
 
205
  g_signal_connect (priv->screen, "window-opened",
 
206
                    G_CALLBACK (on_window_opened), (gpointer)wm);
 
207
  g_signal_connect (priv->screen, "window-closed",
 
208
                    G_CALLBACK (on_window_closed), (gpointer)wm);
 
209
 
 
210
  g_signal_connect (launcher_menu_get_default (), "menu-changed",
 
211
                    G_CALLBACK (on_menu_changed), wm);
 
212
}
 
213
 
 
214
LauncherWm *
 
215
launcher_wm_get_default (void)
 
216
 
 
217
{
 
218
  static LauncherWm *wm = NULL;
 
219
 
 
220
  if (!wm)
 
221
    wm = g_object_new (LAUNCHER_TYPE_WM, 
 
222
                       NULL);
 
223
 
 
224
  return wm;
 
225
}