[go: up one dir, main page]

Menu

[02b3bd]: / lib.c  Maximize  Restore  History

Download this file

227 lines (208 with data), 5.9 kB

  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
/*
* Copyright 2010 Johan Veenhuizen
*
* 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 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.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include "wind.h"
// Lock-mask permutations that should be grabbed in addition
// to the modifiers specified to the functions below.
static const unsigned lockmasks[] = {
0,
LockMask, // Caps Lock
Mod2Mask, // Num Lock
Mod5Mask, // Scroll Lock
LockMask | Mod2Mask,
LockMask | Mod5Mask,
Mod2Mask | Mod5Mask,
LockMask | Mod2Mask | Mod5Mask
};
void *xmalloc(size_t size)
{
void *p;
while ((p = malloc(size)) == NULL && size != 0) {
errorf("out of memory");
sleep(1);
}
return p;
}
void *xrealloc(const void *p, size_t size)
{
void *q;
while ((q = realloc((void *)p, size)) == NULL && size != 0) {
errorf("out of memory");
sleep(1);
}
return q;
}
char *xstrdup(const char *s)
{
size_t n = strlen(s) + 1;
return memcpy(xmalloc(n), s, n);
}
/*
* Grabs a key. See manual page of XGrabKey.
*/
void grabkey(int keycode, unsigned modifiers, Window grabwin,
Bool ownerevents, int ptrmode, int keymode)
{
if (modifiers == AnyModifier)
XGrabKey(dpy, keycode, modifiers, grabwin, ownerevents,
ptrmode, keymode);
else
for (int i = 0; i < NELEM(lockmasks); i++)
XGrabKey(dpy, keycode, modifiers | lockmasks[i],
grabwin, ownerevents, ptrmode,
keymode);
}
/*
* Ungrabs a key. See manual page of XUngrabKey.
*/
void ungrabkey(int keycode, unsigned modifiers, Window grabwin)
{
if (modifiers == AnyModifier)
XUngrabKey(dpy, keycode, AnyModifier, grabwin);
else
for (int i = 0; i < NELEM(lockmasks); i++)
XUngrabKey(dpy, keycode, modifiers | lockmasks[i],
grabwin);
}
/*
* Ungrabs a pointer button. See manual page of XGrabButton.
*/
void grabbutton(unsigned button, unsigned modifiers, Window grabwin,
Bool ownerevents, unsigned eventmask, int ptrmode,
int keymode, Window confineto, Cursor cursor)
{
if (modifiers == AnyModifier)
XGrabButton(dpy, button, AnyModifier, grabwin, ownerevents,
eventmask, ptrmode, keymode, confineto,
cursor);
else
for (int i = 0; i < NELEM(lockmasks); i++)
XGrabButton(dpy, button, modifiers | lockmasks[i],
grabwin, ownerevents, eventmask,
ptrmode, keymode, confineto, cursor);
}
/*
* Ungrabs a pointer button. See manual page of XUngrabButton.
*/
void ungrabbutton(unsigned button, unsigned modifiers, Window grabwin)
{
if (modifiers == AnyModifier)
XUngrabButton(dpy, button, modifiers, grabwin);
else
for (int i = 0; i < lockmasks[i]; i++)
XUngrabButton(dpy, button, modifiers | lockmasks[i],
grabwin);
}
/*
* Returns the WM_STATE hint of a client window
*/
long getwmstate(Window w)
{
unsigned long nitems, bytesafter;
unsigned char *prop;
Atom actualtype;
int actualformat;
long state = WithdrawnState;
if (XGetWindowProperty(dpy, w, WM_STATE, 0L, 2L, False, WM_STATE,
&actualtype, &actualformat, &nitems,
&bytesafter, &prop) == Success) {
if (nitems > 0)
state = ((long *)prop)[0];
XFree(prop);
}
return state;
}
/*
* Sets the WM_STATE hint of a client window. The state can
* be one of Normalstate, IconicState, or WithdrawnState.
*/
void setwmstate(Window w, long state)
{
long data[2] = { state, None };
XChangeProperty(dpy, w, WM_STATE, WM_STATE, 32,
PropModeReplace, (unsigned char *)data, 2);
}
/*
* Tests if a client window is mapped.
*/
Bool ismapped(Window w)
{
XWindowAttributes a;
return XGetWindowAttributes(dpy, w, &a) && a.map_state != IsUnmapped;
}
char *decodetextproperty(XTextProperty *p)
{
char *s = NULL;
char **v = NULL;
int n = 0;
XmbTextPropertyToTextList(dpy, p, &v, &n);
if (n > 0)
s = xstrdup(v[0]);
if (v != NULL)
XFreeStringList(v);
return s;
}
void setprop(Window w, Atom prop, Atom type, int fmt, void *ptr, int nelem)
{
XChangeProperty(dpy, w, prop, type, fmt, PropModeReplace, ptr, nelem);
}
void *getprop(Window w, Atom prop, Atom type, int fmt, unsigned long *rcountp)
{
void *ptr = NULL;
unsigned long count = 32;
Atom rtype;
int rfmt;
unsigned long rafter;
for (;;) {
if (XGetWindowProperty(dpy, w, prop, 0L, count,
False, type, &rtype, &rfmt, rcountp,
&rafter, (unsigned char **)&ptr) != Success) {
// Error
return NULL;
} else if (rtype != type || rfmt != fmt) {
// Does not exist (type=None), or wrong type/format
return NULL;
} else if (rafter > 0) {
XFree(ptr);
ptr = NULL;
count *= 2;
} else {
return ptr;
}
}
}
void drawbitmap(Drawable d, GC gc, struct bitmap *b, int x, int y)
{
if (b->pixmap == None)
b->pixmap = XCreateBitmapFromData(dpy, d, (char *)b->bits,
b->width, b->height);
XCopyPlane(dpy, b->pixmap, d, gc, 0, 0, b->width, b->height, x, y, 1);
}
unsigned long getpixel(const char *name)
{
XColor tc, sc;
XAllocNamedColor(dpy, DefaultColormap(dpy, scr), name, &sc, &tc);
return sc.pixel;
}