summaryrefslogtreecommitdiff
path: root/transSet.c
blob: 8c45486a8cc9565841f8e7d15bcb29be5310cf65 (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
/* Copyright © 2003-2004 Matthew Hawn
 * Copyright © 2003-2004 Andreas Kohn
 * Copyright © 2003-2004 Roman Divacky
 * Copyright © 2003-2004 Keith Packard
 * Copyright © 2005-2007 Daniel Forchheimer
 * Copyright © 2011-2012 Arnaud Fontaine
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define VERSIONSTR "6"

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "dsimple.h"
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>

static Window target_win;

/* needed by dsimple.c */

void
Usage (void)
{
    fputs ("usage: transset [-options ...] [opacity]\n"
           "options:\n"
           "    -h, --help           display this message\n"
           "    -t, --toggle         force toggle of opacity\n"
           "    -c, --click          select by clicking on window  (default)\n"
           "    -p, --point          select the window currently under the cursor\n"
           "    -a, --actual         select the actual window\n"
           "    -n, --name NAME      select by name, NAME is matched as regular expression\n"
           "    --no-regex           don't use regular expression for matching name\n"
           "    -i, --id             select by window id\n"
           "        --inc            increase by the given opacity\n"
           "        --dec            decrease by given opacity\n"
           "    -m, --min OPACITY    minimum possible opacity  (default = 0)\n"
           "    -x, --max OPACITY    maximum possible opacity  (default = 1)\n"
           "    -v, --verbose        print some debug info\n"
           "    -V, --version        print version number\n",
        stderr);

    exit (1);
}

#define OPAQUE	0xffffffff
#define OPACITY	"_NET_WM_WINDOW_OPACITY"

/* returns the highest parent of child that is not the root-window */
static Window
Get_Top_Window (Display *disp, Window child) {
    Window parent;
    Window root;
    Window *child_list;
    unsigned int num_children;

    if (!XQueryTree (disp, child, &root, &parent, &child_list,
                     &num_children))
        Fatal_Error ("Can't query window tree.");

    XFree (child_list);
    if (parent == root)
        return child;

    while (parent != root) {
        child = parent;
        if (!XQueryTree (disp, child, &root, &parent, &child_list,
                         &num_children))
            Fatal_Error ("Can't query window tree.");
        XFree (child_list);
    }
    return child;
}

static Window
Get_Actual_Window (Display *disp)
{
    int i;
    Window w;

    XGetInputFocus (disp, &w, &i);
    return Get_Top_Window (disp, w);
}

typedef enum {
    SELECT_METHOD_CLICK = 0,
    SELECT_METHOD_WINDOW_UNDER_CURSOR = 1,
    SELECT_METHOD_WINDOW_ID = 2,
    SELECT_METHOD_WINDOW_NAME = 3,
    SELECT_METHOD_FOCUSED_WINDOW = 4
} select_method_t;

int
main (int argc, char **argv)
{
    Bool gotd = False;
    double d = 0.75;
    unsigned int opacity;
    unsigned int current_opacity;
    select_method_t select_method = SELECT_METHOD_CLICK;
    Bool flag_toggle = False;
    Bool flag_increase = False;
    Bool flag_decrease = False;
    Bool flag_verbose = False;
    Bool flag_no_regex = False;
    int o;
    double min = 0.0, max = 1.0;
    char *idstr = NULL, *namestr = NULL;
    char *windowname = NULL;

    int options_index = 0;
    static struct option long_options[] = {
        {"toggle", 0, NULL, 't'},
        {"help", 0, NULL, 'h'},
        {"point", 0, NULL, 'p'},
        {"actual", 0, NULL, 'a'},
        {"click", 0, NULL, 'c'},
        {"id", 1, NULL, 'i'},
        {"name", 1, NULL, 'n'},
        {"inc", 0, NULL, '1'},
        {"dec", 0, NULL, '2'},
        {"min", 1, NULL, 'm'},
        {"max", 1, NULL, 'x'},
        {"no-regex", 0, NULL, '4'},
        {"version", 0, NULL, 'V'},
        {"verbose", 0, NULL, 'v'},
        {0, 0, 0, 0}
    };
    unsigned char *data;

    Atom actual;
    int format;
    unsigned long n, left;

    /* wonderful utility */
    Setup_Display_And_Screen (&argc, argv);

    /* parse arguments */
    while ((o = getopt_long (argc, argv, "thapci:n:vVm:x:123",
                             long_options, &options_index)) != -1) {
        switch (o) {
        case 't':
            flag_toggle = True;
            break;
        case 'h':
            Usage ();
            break;
        case 'c':
            select_method = SELECT_METHOD_CLICK;
            break;
        case 'p':
            select_method = SELECT_METHOD_WINDOW_UNDER_CURSOR;
            break;
        case 'i':
            idstr = optarg;
            select_method = SELECT_METHOD_WINDOW_ID;
            break;
        case 'n':
            namestr = optarg;
            select_method = SELECT_METHOD_WINDOW_NAME;
            break;
        case 'a':
            select_method = SELECT_METHOD_FOCUSED_WINDOW;
            break;
        case '1':
            flag_increase = True;
            break;
        case '2':
            flag_decrease = True;
            break;
        case 'v':
            flag_verbose = True;
            break;
        case '4':
            flag_no_regex = True;
            break;
        case 'm':
            min = atof (optarg);
            break;
        case 'x':
            max = atof (optarg);
            break;
        case 'V':
            fprintf (stderr, "%s\nversion: %s\nreleased: %s\n",
                     PACKAGE_STRING, VERSIONSTR, RELEASE_DATE);
            exit (1);
            break;
        default:
            Usage ();
        }
    }

    if (optind < argc) {
        d = atof (argv[optind]);
        gotd = True;
    }

    /* select the window to make transparent */
    switch (select_method) {
    case SELECT_METHOD_WINDOW_UNDER_CURSOR:
        /* don't wait for click */
        if (flag_verbose)
            printf ("Selecting window by click\n");
        target_win = Get_Window_Under_Cursor (dpy);
        break;

    case SELECT_METHOD_WINDOW_ID:
        /* pretty much ripped from dsimple.c */
        if (flag_verbose)
            printf ("Selecting window by id\n");
        sscanf (idstr, "0x%lx", &target_win);
        if (!target_win)
            sscanf (idstr, "%ld", &target_win);
        if (!target_win) {
            fprintf (stderr, "Invalid window id format: %s.\n", idstr);
            XCloseDisplay (dpy);
            return 1;
        }
        if (flag_verbose)
            printf ("Selected 0x%x, trying to get top parent ... ",
                    (unsigned int) target_win);
        target_win = Get_Top_Window (dpy, target_win);
        if (flag_verbose)
            printf ("found 0x%x\n", (unsigned int) target_win);

        break;

    case SELECT_METHOD_WINDOW_NAME:
        /* select by name, pretty much ripped from dsimple.c */
        if (flag_verbose)
            printf ("Selecting window by name\n");

        if (flag_no_regex)
            target_win = Window_With_Name (dpy, RootWindow (dpy, screen),
                                           namestr);
        else
            target_win = Window_With_Name_Regex (dpy, RootWindow (dpy, screen),
                                                 namestr);

        if (!target_win) {
            fprintf (stderr, "No window matching %s exists!\n", namestr);
            XCloseDisplay (dpy);
            return 1;
        }
        /* store the matched window name*/
        XFetchName (dpy, target_win, &windowname);

        if (flag_verbose)
            printf ("Selected 0x%x, trying to get top parent ... ",
                    (unsigned int) target_win);

        target_win = Get_Top_Window (dpy, target_win);
        if (flag_verbose)
            printf ("found 0x%x\n", (unsigned int) target_win);

        break;

    case SELECT_METHOD_FOCUSED_WINDOW:
        target_win = Get_Actual_Window (dpy);
        break;

    default:
        /* grab mouse and return window that is next clicked */
        target_win = Select_Window (dpy);
        break;
    }

    /* get property */
    if ((XGetWindowProperty (dpy, target_win, XInternAtom (dpy, OPACITY, False),
                             0L, 1L, False, XA_CARDINAL, &actual, &format, &n,
                             &left, &data) == Success)
        && (data != None)) {
        memcpy (&current_opacity, data, sizeof (unsigned int));
        XFree (data);
        if (flag_verbose)
            printf ("Found transparency: %g\n",
                    (double) current_opacity / OPAQUE);
    } else
        current_opacity = OPAQUE;

    if (flag_increase)
        d = (double) current_opacity / OPAQUE + d;
    else if (flag_decrease)
        d = (double) current_opacity / OPAQUE - d;

    /* check min and max */
    if (d < min)
        d = min;
    if (d > max)
        d = max;

    opacity = (unsigned int) (d * OPAQUE);

    /* for user-compatibility with transset */
    if (!gotd)
        flag_toggle = True;

    /* toggle */
    if (flag_toggle)
        if (current_opacity != OPAQUE)
            opacity = OPAQUE;

    if (opacity == OPAQUE)
  	XDeleteProperty (dpy, target_win, XInternAtom (dpy, OPACITY, False));
    /* set it */
    else
        XChangeProperty (dpy, target_win, XInternAtom (dpy, OPACITY, False),
                         XA_CARDINAL, 32, PropModeReplace,
                         (unsigned char *) &opacity, 1L);
    XSync (dpy, False);

    printf ("Set Property to %g", (double) opacity / OPAQUE);
    if (windowname)
        printf (" on \n%s\n", windowname);
    else
        printf ("\n");

    if (flag_verbose)
        printf ("Property set on: 0x%x\n", (unsigned int) target_win);

    XCloseDisplay (dpy);
    return 0;
}