This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkpie.c
300 lines (233 loc) · 5.94 KB
/
kpie.c
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
/**
* kpie.c - Devils Pie-like window manipulation with Lua.
*
* Copyright (c) 2014 by Steve Kemp. All rights reserved.
*
**
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 dated June, 1991, or (at your
* option) any later version.
*
* On Debian GNU/Linux systems, the complete text of version 2 of the GNU
* General Public License can be found in `/usr/share/common-licenses/GPL-2'
*
**
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <getopt.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#define WNCK_I_KNOW_THIS_IS_UNSTABLE 1
#include <libwnck/libwnck.h>
#include "bindings.h"
/**
* The window we're currently manipulating.
*
* A callback from libwnck which is invoked every time a window is created.
*
* The callback updates _this_ pointer to point to the newly-created window,
* which all the Lua primitives then use.
*
* NOTE: This is safe because the Lua invocation is single-threaded.
*
*/
WnckWindow *g_window;
/**
* This function is called when a new window is created.
*
* It has two jobs:
*
* 1. Save the window pointer to our global g_window variable.
*
* 2. Invoke the lua callback.
*
* The prototype is here to avoid the warning from clang, which is
* a surprise.
*
*/
void on_window_opened(WnckScreen * screen, WnckWindow * window, gpointer data);
void on_window_opened(WnckScreen * screen, WnckWindow * window, gpointer data)
{
(void)(screen);
(void)(data);
/**
* Update the global "current window" to point to the current
* window which has just been created.
*/
g_window = window;
/**
* Load/Invoke the lua callback.
*/
invoke_lua();
}
/**
* Entry point to our code.
*/
int main(int argc, char **argv)
{
/**
* The Lua configuration file we parse/use.
*/
gchar *config_file;
/**
* Setup our default configuration file.
*/
if (getenv("HOME") != NULL)
config_file = g_strdup_printf("%s/.kpie.lua", getenv("HOME"));
else
config_file = "";
/**
* Command-line flags;
*/
int debug = 0;
int single = 0;
/**
* Parse our options.
*/
while (1)
{
static struct option long_options[] =
{
{"debug", no_argument, 0, 'd'},
{"single", no_argument, 0, 's'},
{"config", required_argument, 0, 'c'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
char c;
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long(argc, argv, "dsc:v", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'd':
debug = TRUE;
break;
case 'c':
g_free(config_file);
config_file = g_strdup(optarg);
break;
case 's':
single = TRUE;
break;
case 'v':
printf("kpie - %s", VERSION);
#ifdef LUA_VERSION
printf(" (built against " LUA_VERSION ")");
#endif
printf("\n");
return (0);
case '?':
/* getopt_long already printed an error message. */
return (1);
}
}
/**
* If a configuration file was specified use a different one.
*/
int index;
for (index = optind; index < argc; index++)
{
g_free(config_file);
config_file = g_strdup(argv[index]);
}
/**
* Show details if we should.
*/
if (debug)
printf("Loading configuration file: %s\n", config_file);
if (debug && single)
printf("Single run\n");
/**
* Initialize Lua.
*/
init_lua(debug, config_file);
/**
* Initialize GDK
*/
gdk_init(&argc, &argv);
/**
* Get a new loop and the screen.
*/
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
/**
* If we're running just the once, thanks to "--single", then do that.
* once.
*/
if (single)
{
/**
* Force a sync.
*/
wnck_screen_force_update(wnck_screen_get_default());
/**
* Count each screen.
*/
int i;
int count = gdk_display_get_n_screens(gdk_display_get_default());
/**
* For each screen ..
*/
for (i = 0; i < count; i++)
{
WnckScreen *screen = wnck_screen_get(i);
/**
* Get all windows.
*/
GList *window_l;
for (window_l = wnck_screen_get_windows(screen);
window_l != NULL; window_l = window_l->next)
{
/**
* Update the global pointer to the current window.
*/
WnckWindow *w = WNCK_WINDOW(window_l->data);
/**
* Invoke our callback.
*/
on_window_opened(screen, w, NULL);
}
}
}
else
{
/**
* Count the screens on the system.
*/
int i;
int count = gdk_display_get_n_screens(gdk_display_get_default());
/**
* For each screen.
*/
for (i = 0; i < count; i++)
{
/**
* Ensure our callback is invoked when new windows are opened.
*/
WnckScreen *screen = wnck_screen_get(i);
g_signal_connect(screen, "window-opened", G_CALLBACK(on_window_opened), NULL);
}
/**
* Launch - NOTE: Never returns.
*/
g_main_loop_run(loop);
}
/**
* Never reached unless --single was used.
*/
/**
* Cleanup
*/
g_main_loop_unref(loop);
g_free(config_file);
close_lua();
return 0;
}