Newer
Older
/*
* This file contains ONLY the functions that are called from the
* event thread.
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <threadwrap.h>
#include <genwrap.h>
#include <dirwrap.h>
#include "vidmodes.h"
#define BITMAP_CIOLIB_DRIVER
#include "utf8_codepages.h"
/*
* Exported variables
*/
int local_pipe[2]; /* Used for passing local events */
int key_pipe[2]; /* Used for passing keyboard events */
struct x11 x11;
char *copybuf;
pthread_mutex_t copybuf_mutex;
char *pastebuf;
sem_t pastebuf_set;
sem_t pastebuf_used;
sem_t init_complete;
sem_t mode_set;
int x11_window_xpos;
int x11_window_ypos;
int x11_initialized=0;
sem_t event_thread_complete;
int terminate = 0;
Atom copybuf_format;
Atom pastebuf_format;
/*
* Local variables
*/
/* Sets the atom to be used for copy/paste operations */
#define CONSOLE_CLIPBOARD XA_PRIMARY
static Atom WM_DELETE_WINDOW = None;
static Atom _NET_WM_PING = None;
static Cursor curs = None;
static Colormap wincmap;
static Pixmap icn;
static Pixmap icn_mask;
static bool VisualIsRGB8 = false;
static unsigned int depth=0;
static int xfd;
static unsigned long black;
static unsigned long white;
static unsigned long base_pixel;
static int r_shift;
static int g_shift;
static int b_shift;
#ifdef WITH_XRENDER
static XRenderPictFormat *xrender_pf = NULL;
static Pixmap xrender_pm = None;
static Picture xrender_src_pict = None;
static Picture xrender_dst_pict = None;
#endif
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
static WORD Ascii2Scan[] = {
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0x000e, 0x000f, 0xffff, 0xffff, 0xffff, 0x001c, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0x0001, 0xffff, 0xffff, 0xffff, 0xffff,
0x0039, 0x0102, 0x0128, 0x0104, 0x0105, 0x0106, 0x0108, 0x0028,
0x010a, 0x010b, 0x0109, 0x010d, 0x0033, 0x000c, 0x0034, 0x0035,
0x000b, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
0x0009, 0x000a, 0x0127, 0x0027, 0x0133, 0x000d, 0x0134, 0x0135,
0x0103, 0x011e, 0x0130, 0x012e, 0x0120, 0x0112, 0x0121, 0x0122,
0x0123, 0x0117, 0x0124, 0x0125, 0x0126, 0x0132, 0x0131, 0x0118,
0x0119, 0x0110, 0x0113, 0x011f, 0x0114, 0x0116, 0x012f, 0x0111,
0x012d, 0x0115, 0x012c, 0x001a, 0x002b, 0x001b, 0x0107, 0x010c,
0x0029, 0x001e, 0x0030, 0x002e, 0x0020, 0x0012, 0x0021, 0x0022,
0x0023, 0x0017, 0x0024, 0x0025, 0x0026, 0x0032, 0x0031, 0x0018,
0x0019, 0x0010, 0x0013, 0x001f, 0x0014, 0x0016, 0x002f, 0x0011,
0x002d, 0x0015, 0x002c, 0x011a, 0x012b, 0x011b, 0x0129, 0xffff,
};
static struct {
WORD base;
WORD shift;
WORD ctrl;
WORD alt;
} ScanCodes[] = {
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 0 */
{ 0x011b, 0x011b, 0x011b, 0xffff }, /* key 1 - Escape key */
{ 0x0231, 0x0221, 0xffff, 0x7800 }, /* key 2 - '1' */
{ 0x0332, 0x0340, 0x0300, 0x7900 }, /* key 3 - '2' - special handling */
{ 0x0433, 0x0423, 0xffff, 0x7a00 }, /* key 4 - '3' */
{ 0x0534, 0x0524, 0xffff, 0x7b00 }, /* key 5 - '4' */
{ 0x0635, 0x0625, 0xffff, 0x7c00 }, /* key 6 - '5' */
{ 0x0736, 0x075e, 0x071e, 0x7d00 }, /* key 7 - '6' */
{ 0x0837, 0x0826, 0xffff, 0x7e00 }, /* key 8 - '7' */
{ 0x0938, 0x092a, 0xffff, 0x7f00 }, /* key 9 - '8' */
{ 0x0a39, 0x0a28, 0xffff, 0x8000 }, /* key 10 - '9' */
{ 0x0b30, 0x0b29, 0xffff, 0x8100 }, /* key 11 - '0' */
{ 0x0c2d, 0x0c5f, 0x0c1f, 0x8200 }, /* key 12 - '-' */
{ 0x0d3d, 0x0d2b, 0xffff, 0x8300 }, /* key 13 - '=' */
{ 0x0e08, 0x0e08, 0x0e7f, 0xffff }, /* key 14 - backspace */
{ 0x0f09, 0x0f00, 0xffff, 0xffff }, /* key 15 - tab */
{ 0x1071, 0x1051, 0x1011, 0x1000 }, /* key 16 - 'Q' */
{ 0x1177, 0x1157, 0x1117, 0x1100 }, /* key 17 - 'W' */
{ 0x1265, 0x1245, 0x1205, 0x1200 }, /* key 18 - 'E' */
{ 0x1372, 0x1352, 0x1312, 0x1300 }, /* key 19 - 'R' */
{ 0x1474, 0x1454, 0x1414, 0x1400 }, /* key 20 - 'T' */
{ 0x1579, 0x1559, 0x1519, 0x1500 }, /* key 21 - 'Y' */
{ 0x1675, 0x1655, 0x1615, 0x1600 }, /* key 22 - 'U' */
{ 0x1769, 0x1749, 0x1709, 0x1700 }, /* key 23 - 'I' */
{ 0x186f, 0x184f, 0x180f, 0x1800 }, /* key 24 - 'O' */
{ 0x1970, 0x1950, 0x1910, 0x1900 }, /* key 25 - 'P' */
{ 0x1a5b, 0x1a7b, 0x1a1b, 0xffff }, /* key 26 - '[' */
{ 0x1b5d, 0x1b7d, 0x1b1d, 0xffff }, /* key 27 - ']' */
{ 0x1c0d, 0x1c0d, 0x1c0a, 0xffff }, /* key 28 - CR */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 29 - control */
{ 0x1e61, 0x1e41, 0x1e01, 0x1e00 }, /* key 30 - 'A' */
{ 0x1f73, 0x1f53, 0x1f13, 0x1f00 }, /* key 31 - 'S' */
{ 0x2064, 0x2044, 0x2004, 0x2000 }, /* key 32 - 'D' */
{ 0x2166, 0x2146, 0x2106, 0x2100 }, /* key 33 - 'F' */
{ 0x2267, 0x2247, 0x2207, 0x2200 }, /* key 34 - 'G' */
{ 0x2368, 0x2348, 0x2308, 0x2300 }, /* key 35 - 'H' */
{ 0x246a, 0x244a, 0x240a, 0x2400 }, /* key 36 - 'J' */
{ 0x256b, 0x254b, 0x250b, 0x2500 }, /* key 37 - 'K' */
{ 0x266c, 0x264c, 0x260c, 0x2600 }, /* key 38 - 'L' */
{ 0x273b, 0x273a, 0xffff, 0xffff }, /* key 39 - ';' */
{ 0x2827, 0x2822, 0xffff, 0xffff }, /* key 40 - ''' */
{ 0x2960, 0x297e, 0xffff, 0xffff }, /* key 41 - '`' */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 42 - left shift */
{ 0x2b5c, 0x2b7c, 0x2b1c, 0xffff }, /* key 43 - '' */
{ 0x2c7a, 0x2c5a, 0x2c1a, 0x2c00 }, /* key 44 - 'Z' */
{ 0x2d78, 0x2d58, 0x2d18, 0x2d00 }, /* key 45 - 'X' */
{ 0x2e63, 0x2e43, 0x2e03, 0x2e00 }, /* key 46 - 'C' */
{ 0x2f76, 0x2f56, 0x2f16, 0x2f00 }, /* key 47 - 'V' */
{ 0x3062, 0x3042, 0x3002, 0x3000 }, /* key 48 - 'B' */
{ 0x316e, 0x314e, 0x310e, 0x3100 }, /* key 49 - 'N' */
{ 0x326d, 0x324d, 0x320d, 0x3200 }, /* key 50 - 'M' */
{ 0x332c, 0x333c, 0xffff, 0xffff }, /* key 51 - ',' */
{ 0x342e, 0x343e, 0xffff, 0xffff }, /* key 52 - '.' */
{ 0x352f, 0x353f, 0xffff, 0xffff }, /* key 53 - '/' */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 54 - right shift - */
{ 0x372a, 0xffff, 0x3772, 0xffff }, /* key 55 - prt-scr - */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 56 - Alt - */
{ 0x3920, 0x3920, 0x3920, 0x3920 }, /* key 57 - space bar */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 58 - caps-lock - */
{ 0x3b00, 0x5400, 0x5e00, 0x6800 }, /* key 59 - F1 */
{ 0x3c00, 0x5500, 0x5f00, 0x6900 }, /* key 60 - F2 */
{ 0x3d00, 0x5600, 0x6000, 0x6a00 }, /* key 61 - F3 */
{ 0x3e00, 0x5700, 0x6100, 0x6b00 }, /* key 62 - F4 */
{ 0x3f00, 0x5800, 0x6200, 0x6c00 }, /* key 63 - F5 */
{ 0x4000, 0x5900, 0x6300, 0x6d00 }, /* key 64 - F6 */
{ 0x4100, 0x5a00, 0x6400, 0x6e00 }, /* key 65 - F7 */
{ 0x4200, 0x5b00, 0x6500, 0x6f00 }, /* key 66 - F8 */
{ 0x4300, 0x5c00, 0x6600, 0x7000 }, /* key 67 - F9 */
{ 0x4400, 0x5d00, 0x6700, 0x7100 }, /* key 68 - F10 */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 69 - num-lock - */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 70 - scroll-lock - */
{ 0x4700, 0x4737, 0x7700, 0xffff }, /* key 71 - home */
{ 0x4800, 0x4838, 0x8d00, 0x9800 }, /* key 72 - cursor up */
{ 0x4900, 0x4939, 0x8400, 0xffff }, /* key 73 - page up */
{ 0x4a2d, 0x4a2d, 0xffff, 0xffff }, /* key 74 - minus sign */
{ 0x4b00, 0x4b34, 0x7300, 0xffff }, /* key 75 - cursor left */
{ 0xffff, 0x4c35, 0xffff, 0xffff }, /* key 76 - center key */
{ 0x4d00, 0x4d36, 0x7400, 0xffff }, /* key 77 - cursor right */
{ 0x4e2b, 0x4e2b, 0xffff, 0xffff }, /* key 78 - plus sign */
{ 0x4f00, 0x4f31, 0x7500, 0xffff }, /* key 79 - end */
{ 0x5000, 0x5032, 0x9100, 0xa000 }, /* key 80 - cursor down */
{ 0x5100, 0x5133, 0x7600, 0xffff }, /* key 81 - page down */
{ CIO_KEY_IC, CIO_KEY_SHIFT_IC, CIO_KEY_CTRL_IC, CIO_KEY_ALT_IC}, /* key 82 - insert */
{ CIO_KEY_DC, CIO_KEY_SHIFT_DC, CIO_KEY_CTRL_DC, CIO_KEY_ALT_IC}, /* key 83 - delete */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 84 - sys key */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 85 */
{ 0xffff, 0xffff, 0xffff, 0xffff }, /* key 86 */
{ 0x8500, 0x5787, 0x8900, 0x8b00 }, /* key 87 - F11 */
{ 0x8600, 0x5888, 0x8a00, 0x8c00 }, /* key 88 - F12 */
};
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
static bool
x11_get_maxsize(int *w, int *h)
{
int i;
long offset;
int maxw = 0, maxh = 0;
Atom atr;
int afr;
long *ret;
unsigned long nir;
unsigned long bytes_left = 4;
Window root = DefaultRootWindow(dpy);
unsigned int rw, rh;
uint64_t dummy;
unsigned char *prop;
if (dpy == NULL)
return false;
// First, try to get _NET_WORKAREA...
if (x11.workarea == None) {
x11.workarea = x11.XInternAtom(dpy, "_NET_WORKAREA", True);
}
if (x11.workarea != None) {
for (i = 0, offset = 0; bytes_left; i++) {
if (x11.XGetWindowProperty(dpy, root, x11.workarea, offset, 4, False, XA_CARDINAL, &atr, &afr, &nir, &bytes_left, &prop) != Success)
break;
if (atr != XA_CARDINAL)
break;
ret = (long *)prop;
if (nir >= 3) {
if (ret[2] > maxw)
maxw = ret[2];
}
if (nir >= 4) {
if (ret[3] > maxh)
maxh = ret[3];
}
x11.XFree(prop);
}
}
// Couldn't get work area, get size of root window instead. :(
if (maxw == 0 || maxh == 0) {
if (x11.XGetGeometry(dpy, root, (void *)&dummy, (void *)&dummy, (void *)&dummy, &rw, &rh, (void *)&dummy, (void *)&dummy)) {
if (rw > maxw)
maxw = rw;
if (rh > maxh)
maxh = rh;
}
}
if (maxw != 0 && maxh != 0) {
*w = maxw;
*h = maxh;
return true;
}
return false;
}
#ifdef WITH_XRENDER
if (xrender_pf == NULL)
xrender_pf = x11.XRenderFindStandardFormat(dpy, PictStandardRGB24);
if (xrender_pf == NULL)
xrender_pf = x11.XRenderFindVisualFormat(dpy, visual);
if (xrender_pf == NULL)
xrender_pf = x11.XRenderFindVisualFormat(dpy, DefaultVisual(dpy, DefaultScreen(dpy)));
if (xrender_pm != None)
x11.XFreePixmap(dpy, xrender_pm);
xrender_pm = x11.XCreatePixmap(dpy, win, x_cvstat.scrnwidth, x_cvstat.scrnheight, depth);
if (xrender_src_pict != None)
x11.XRenderFreePicture(dpy, xrender_src_pict);
if (xrender_dst_pict != None)
x11.XRenderFreePicture(dpy, xrender_dst_pict);
XRenderPictureAttributes pa;
xrender_src_pict = x11.XRenderCreatePicture(dpy, xrender_pm, xrender_pf, 0, &pa);
xrender_dst_pict = x11.XRenderCreatePicture(dpy, win, xrender_pf, 0, &pa);
x11.XRenderSetPictureFilter(dpy, xrender_src_pict, "best", NULL, 0);
XTransform transform_matrix = {{
{XDoubleToFixed((double)vstat.scrnwidth / vstat.winwidth), XDoubleToFixed(0), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed((double)vstat.scrnheight / vstat.winheight), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(1.0)}
}};
x11.XRenderSetPictureTransform(dpy, xrender_src_pict, &transform_matrix);
#endif
}
static void resize_xim(void)
{
int width, height;
resize_pictures();
if (x_internal_scaling) {
pthread_mutex_lock(&vstatlock);
bitmap_get_scaled_win_size(x_cvstat.scaling, &width, &height, 0, 0);
pthread_mutex_unlock(&vstatlock);
}
else {
width = x_cvstat.scrnwidth;
height = x_cvstat.scrnheight;
}
if (width == xim->width
&& height == xim->height) {
if (last) {
release_buffer(last);
last = NULL;
}
return;
}
#ifdef XDestroyImage
XDestroyImage(xim);
#else
x11.XDestroyImage(xim);
#endif
}
xim = x11.XCreateImage(dpy, visual, depth, ZPixmap, 0, NULL, width, height, 32, 0);
xim->data=(char *)calloc(1, xim->bytes_per_line*xim->height);
/* Swiped from FreeBSD libc */
static int
my_fls(unsigned long mask)
{
int bit;
if (mask == 0)
return (0);
for (bit = 1; mask != 1; bit++)
mask = mask >> 1;
return (bit);
}
/*
* Actually maps (shows) the window
*/
static void map_window()
{
XSizeHints *sh;
sh = x11.XAllocSizeHints();
if (sh == NULL) {
fprintf(stderr, "Could not get XSizeHints structure");
exit(1);
}
if (x11_get_maxsize(&sh->max_width,&sh->max_height)) {
bitmap_get_scaled_win_size(bitmap_double_mult_inside(sh->max_width, sh->max_height), &sh->max_width, &sh->max_height, sh->max_width, sh->max_height);
}
else {
pthread_mutex_lock(&vstatlock);
bitmap_get_scaled_win_size(7.0, &sh->max_width, &sh->max_height, 0, 0);
bitmap_get_scaled_win_size(x_cvstat.scaling, &sh->base_width, &sh->base_height, 0, 0);
sh->width = sh->base_width;
sh->height = sh->base_height;
bitmap_get_scaled_win_size(1.0, &sh->min_width, &sh->min_height, 0, 0);
pthread_mutex_unlock(&vstatlock);
if (x_cvstat.aspect_width != 0 && x_cvstat.aspect_height != 0) {
sh->min_aspect.x = sh->max_aspect.x = x_cvstat.aspect_width;
sh->min_aspect.y = sh->max_aspect.y = x_cvstat.aspect_height;
}
else {
sh->min_aspect.x = sh->max_aspect.x = sh->min_width;
sh->min_aspect.y = sh->max_aspect.y = sh->min_height;
}
sh->flags = PMinSize | PSize | PAspect | PMaxSize | PBaseSize;
x11.XSetWMNormalHints(dpy, win, sh);
pthread_mutex_lock(&vstatlock);
vstat.scaling = x_cvstat.scaling;
pthread_mutex_unlock(&vstatlock);
x11.XMapWindow(dpy, win);
x11.XFree(sh);
return;
}
static void
set_icon(const void *data, size_t width, XWMHints *hints)
{
const uint32_t *idata = data;
Pixmap opm = icn;
Pixmap opmm = icn_mask;
XGCValues gcv = {
.function = GXcopy,
.foreground = black | 0xff000000,
.background = white
};
int x,y,i;
GC igc, imgc;
bool fail = false;
unsigned short tmp;
XColor fg;
unsigned long lasti, lastim;
// This is literally the wost possible way to create a pixmap. :)
/*
* This whole mess was added to get the icon working on ChromeOS...
* as it happens, ChromeOS doesn't actually use the X11 icons at
* all for anything ever. Instead it does some weird hackery in
* the icon theme and pulls the icon files out to the host.
* Leaving this here though since it is marginally "better" aside
* from the insane method to create a Pixmap.
*/
icn = x11.XCreatePixmap(dpy, DefaultRootWindow(dpy), width, width, depth);
igc = x11.XCreateGC(dpy, icn, GCFunction | GCForeground | GCBackground | GCGraphicsExposures, &gcv);
icn_mask = x11.XCreatePixmap(dpy, DefaultRootWindow(dpy), width, width, 1);
imgc = x11.XCreateGC(dpy, icn_mask, GCFunction | GCForeground | GCBackground | GCGraphicsExposures, &gcv);
fail = (!icn) || (!icn_mask);
if (!fail) {
for (x = 0, i = 0; x < width; x++) {
for (y = 0; y < width; y++) {
if (idata[i] & 0xff000000) {
if (VisualIsRGB8)
fg.pixel = idata[i] & 0xffffff;
tmp = (idata[i] & 0xff);
fg.red = tmp << 8 | tmp;
tmp = (idata[i] & 0xff00) >> 8;
fg.green = tmp << 8 | tmp;
tmp = (idata[i] & 0xff0000) >> 16;
fg.blue = tmp << 8 | tmp;
fg.flags = DoRed | DoGreen | DoBlue;
if (x11.XAllocColor(dpy, wincmap, &fg) == 0)
fail = true;
if (lasti != fg.pixel) {
x11.XSetForeground(dpy, igc, fg.pixel);
lasti = fg.pixel;
}
x11.XDrawPoint(dpy, icn, igc, y, x);
if (lastim != white) {
x11.XSetForeground(dpy, imgc, white);
lastim = white;
}
x11.XDrawPoint(dpy, icn_mask, imgc, y, x);
if (lastim != black) {
x11.XSetForeground(dpy, imgc, black);
lastim = black;
}
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
x11.XDrawPoint(dpy, icn_mask, imgc, y, x);
}
i++;
}
if (fail)
break;
}
}
if (!fail) {
if (!hints)
hints = x11.XGetWMHints(dpy, win);
if (!hints)
hints = x11.XAllocWMHints();
if (hints) {
hints->flags |= IconPixmapHint | IconMaskHint;
hints->icon_pixmap = icn;
hints->icon_mask = icn_mask;
if (sethints) {
x11.XSetWMHints(dpy, win, hints);
x11.XFree(hints);
}
if (opm)
x11.XFreePixmap(dpy, opm);
if (opmm)
x11.XFreePixmap(dpy, opmm);
}
}
x11.XFreeGC(dpy, igc);
x11.XFreeGC(dpy, imgc);
}
/* Get a connection to the X server and create the window. */
static int init_window()
{
XGCValues gcv;
XWMHints *wmhints;
x11.XSynchronize(dpy, False);
#ifdef WITH_XRENDER
if (xrender_found && x11.XRenderQueryVersion(dpy, &major, &minor) == 0)
xrender_found = false;
#endif
xfd = ConnectionNumber(dpy);
x11.utf8 = x11.XInternAtom(dpy, "UTF8_STRING", False);
x11.targets = x11.XInternAtom(dpy, "TARGETS", False);
x11.workarea = x11.XInternAtom(dpy, "_NET_WORKAREA", True);
screen = DefaultScreen(dpy);
#ifdef DefaultVisual
visual = DefaultVisual(dpy, screen);
#else
visual = x11.DefaultVisual(dpy, screen);
#endif
#ifdef DefaultDepth
depth = DefaultDepth(dpy, screen);
#else
depth = x11.DefaultDepth(dpy, screen);
#endif
base_pixel = ULONG_MAX;
base_pixel &= ~visual->red_mask;
base_pixel &= ~visual->green_mask;
base_pixel &= ~visual->blue_mask;
r_shift = my_fls(visual->red_mask)-16;
g_shift = my_fls(visual->green_mask)-16;
b_shift = my_fls(visual->blue_mask)-16;
if (visual->red_mask == 0xff0000 && visual->green_mask == 0xff00 && visual->blue_mask == 0xff)
VisualIsRGB8 = true;
/* Allocate black and white */
black=BlackPixel(dpy, DefaultScreen(dpy));
white=WhitePixel(dpy, DefaultScreen(dpy));
/* Create window, but defer setting a size and GC. */
wincmap = x11.XCreateColormap(dpy, DefaultRootWindow(dpy), visual, AllocNone);
wa.background_pixel = black;
wa.border_pixel = black;
x11_get_maxsize(&mw, &mh);
pthread_mutex_lock(&vstatlock);
bitmap_get_scaled_win_size(x_cvstat.scaling, &w, &h, mw, mh);
vstat.winwidth = x_cvstat.winwidth = w;
vstat.winheight = x_cvstat.winheight = h;
vstat.scaling = x_cvstat.scaling;
pthread_mutex_unlock(&vstatlock);
win = x11.XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0,
w, h, 2, depth, InputOutput, visual, CWColormap | CWBorderPixel | CWBackPixel, &wa);
if (classhints) {
classhints->res_name = ciolib_initial_program_name;
classhints->res_class = ciolib_initial_program_class;
wmhints=x11.XAllocWMHints();
if(wmhints) {
wmhints->initial_state=NormalState;
wmhints->input = True;
set_icon(ciolib_initial_icon, ciolib_initial_icon_width, wmhints);
x11.XSetWMProperties(dpy, win, NULL, NULL, 0, 0, NULL, wmhints, classhints);
x11.XFree(wmhints);
Atom pid_atom = x11.XInternAtom(dpy, "_NET_WM_PID", False);
if (pid_atom != None) {
unsigned long pid_l = getpid();
x11.XChangeProperty(dpy, win, pid_atom, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&pid_l, 1);
}
im = x11.XOpenIM(dpy, NULL, "CIOLIB", "CIOLIB");
if (im != NULL) {
ic = x11.XCreateIC(im, XNClientWindow, win, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL);
if (ic)
x11.XSetICFocus(ic);
}
if (classhints)
x11.XFree(classhints);
WM_DELETE_WINDOW = x11.XInternAtom(dpy, "WM_DELETE_WINDOW", False);
_NET_WM_PING = x11.XInternAtom(dpy, "_NET_WM_PING", False);
gcv.foreground = black | 0xff000000;
gcv.background = white;
gc=x11.XCreateGC(dpy, win, GCFunction | GCForeground | GCBackground | GCGraphicsExposures, &gcv);
x11.XSelectInput(dpy, win, KeyReleaseMask | KeyPressMask |
ExposureMask | ButtonPressMask
| ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
x11.XStoreName(dpy, win, "SyncConsole");
Atom protos[2];
int i = 0;
if (WM_DELETE_WINDOW != None)
protos[i++] = WM_DELETE_WINDOW;
if (_NET_WM_PING != None)
protos[i++] = _NET_WM_PING;
if (i)
x11.XSetWMProtocols(dpy, win, protos, i);
/* Resize the window. This function is called after a mode change. */
static void resize_window()
{
int width = x_cvstat.scrnwidth * x_cvstat.scaling;
int height = x_cvstat.scrnheight * x_cvstat.scaling;
bitmap_get_scaled_win_size(x_cvstat.scaling, &width, &height, 0, 0);

Deucе
committed
if (width == vstat.winwidth && height == vstat.winheight) {
x_cvstat.scaling = bitmap_double_mult_inside(width, height);
vstat.scaling = x_cvstat.scaling;
pthread_mutex_unlock(&vstatlock);
resize_xim();
return;
}
x_cvstat.winwidth = vstat.winwidth;
x_cvstat.winheight = vstat.winheight;
x_cvstat.scaling = bitmap_double_mult_inside(width, height);
vstat.scaling = x_cvstat.scaling;
x11.XResizeWindow(dpy, win, width, height);
static void init_mode_internal(int mode)
ow = vstat.winwidth;
oh = vstat.winheight;
x_cvstat = vstat;
vstat.winwidth = ow;
vstat.winheight = oh;
pthread_mutex_unlock(&vstatlock);

Deucе
committed
resize_window();
x_cvstat = vstat;
pthread_mutex_unlock(&vstatlock);
}
static void check_scaling(void)
{
pthread_mutex_lock(&vstatlock);
pthread_mutex_lock(&scalinglock);
pthread_mutex_unlock(&scalinglock);
pthread_mutex_unlock(&vstatlock);
}
else {
pthread_mutex_unlock(&scalinglock);
pthread_mutex_unlock(&vstatlock);
}
}
static int init_mode(int mode)
{
init_mode_internal(mode);
bitmap_drv_request_pixels();
/* If we are running under X, get a connection to the X server and create
an empty window of size (1, 1). It makes a couple of init functions a
lot easier. */
x_internal_scaling = (ciolib_initial_scaling_type == CIOLIB_SCALING_INTERNAL);
if (ciolib_initial_scaling != 0.0)
x_cvstat.scaling = vstat.scaling = ciolib_initial_scaling;
if (x_cvstat.scaling < 1.0 || vstat.scaling < 1.0)
x_cvstat.scaling = vstat.scaling = 1.0;
if(load_vmode(&vstat, ciolib_initial_mode)) {
pthread_mutex_unlock(&vstatlock);
pthread_mutex_unlock(&vstatlock);
if(init_window())
return(-1);
bitmap_drv_init(x11_drawrect, x11_flush);
pthread_mutex_lock(&vstatlock);
bitmap_drv_init_mode(vstat.mode, NULL, NULL, 0, 0);
pthread_mutex_unlock(&vstatlock);
init_mode_internal(x_cvstat.mode);
int x, y, xoff = 0, yoff = 0;
unsigned int r, g, b;
unsigned long pixel;
int cleft;
int cright = -100;
int ctop;
if (x_cvstat.scrnwidth != rect->rect.width || x_cvstat.scrnheight != rect->rect.height || xim == NULL) {
bitmap_get_scaled_win_size(vstat.scaling, &w, &h, vstat.winwidth, vstat.winheight);
pthread_mutex_unlock(&vstatlock);
if (w < rect->rect.width || h < rect->rect.height) {
bitmap_drv_free_rect(rect);
return;
}
if (x_internal_scaling) {
source = do_scale(rect, w, h);
bitmap_drv_free_rect(rect);
if (source == NULL)
return;
cleft = source->w;
ctop = source->h;
source_data = source->data;
dw = source->w;
dh = source->h;
}
else {
cleft = w;
ctop = h;
source_data = rect->data;
dw = rect->rect.width;
dh = rect->rect.height;
}
pthread_mutex_lock(&vstatlock);
w = vstat.winwidth;
h = vstat.winheight;
pthread_mutex_unlock(&vstatlock);
/* TODO: Translate into local colour depth */
if (x < cleft)
cleft = x;
if (x > cright)
cright = x;
if (y < ctop)
ctop = y;
if (y > cbottom)
cbottom = y;
}
else {
if (last_pixel != source_data[idx]) {
last_pixel = source_data[idx];
r = source_data[idx] >> 16 & 0xff;
g = source_data[idx] >> 8 & 0xff;
b = source_data[idx] & 0xff;
r = (r<<8)|r;
g = (g<<8)|g;
b = (b<<8)|b;
pixel = base_pixel;
if (r_shift >= 0)
idx++;
}
if (x_internal_scaling) {
/* This line was changed */
// TODO: Previously this did one update per display line...
if (last && cright >= 0 && (cbottom != y || y == source->h - 1)) {
x11.XPutImage(dpy, win, gc, xim, cleft, ctop
, cleft + xoff, ctop + yoff
, (cright - cleft + 1), (cbottom - ctop + 1));
cleft = source->w;
cright = cbottom = -100;
ctop = source->h;
}
// TODO: We really only need to do this once after changing resolution...
if (xoff > 0 || yoff > 0) {
x11.XFillRectangle(dpy, win, gc, 0, 0, w, yoff);
x11.XFillRectangle(dpy, win, gc, 0, yoff, xoff, yoff + xim->height);
x11.XFillRectangle(dpy, win, gc, xoff+xim->width, yoff, w, yoff + xim->height);
x11.XFillRectangle(dpy, win, gc, 0, yoff + xim->height, w, h);
}
if (x_internal_scaling || xrender_found == false) {
if (last == NULL)
x11.XPutImage(dpy, win, gc, xim, 0, 0, xoff, yoff, source->w, source->h);
else
release_buffer(last);
}
else {
#ifdef WITH_XRENDER
bitmap_drv_free_rect(rect);
if (last != NULL)
release_buffer(last);
x11.XPutImage(dpy, xrender_pm, gc, xim, 0, 0, 0, 0, dw, dh);
x11.XRenderComposite(dpy, PictOpSrc, xrender_src_pict, 0, xrender_dst_pict,
0, 0, 0, 0, 0, 0,
cleft, ctop);
#else
fprintf(stderr, "External scaling enabled without XRender support compiled in!\n");
#endif
}
static void handle_resize_event(int width, int height)
{
vstat.winwidth = width;
vstat.winheight = height;
new_scaling = bitmap_double_mult_inside(width, height);
resize = true;
}
if (new_scaling > 16) {
new_scaling = 16;
pthread_mutex_lock(&scalinglock);
pthread_mutex_unlock(&scalinglock);
x_cvstat.scaling = new_scaling;
resize = true;
}
pthread_mutex_unlock(&vstatlock);
* We only need to resize if the width/height are not even multiples,
* or if the two axis don't scale the same way.
* Otherwise, we can simply resend everything
*/
else
resize_xim();
bitmap_drv_request_pixels();
}
static void expose_rect(int x, int y, int width, int height)
int xoff=0, yoff=0;
if (xim == NULL) {
fprintf(stderr, "Exposing NULL xim!\n");
return;
}
pthread_mutex_lock(&vstatlock);
w = vstat.winwidth;
h = vstat.winheight;
s = vstat.scaling;
pthread_mutex_unlock(&vstatlock);
xoff = (w - xim->width) / 2;
if (xoff < 0)
xoff = 0;
if (yoff < 0)
yoff = 0;
if (xoff > 0 || yoff > 0) {
if (x < xoff || y < yoff || x + width > xoff + xim->width || y + height > yoff + xim->height) {
x11.XFillRectangle(dpy, win, gc, 0, yoff, xoff, yoff + xim->height);
x11.XFillRectangle(dpy, win, gc, xoff+xim->width, yoff, w, yoff + xim->height);
x11.XFillRectangle(dpy, win, gc, 0, yoff + xim->height, w, h);
}
}
if (sx < 0)
sx = 0;
if (sy < 0)
sy = 0;
ex=(x-xoff)+width-1;
ey=(y-yoff)+height-1;
if (ex < 0)
ex = 0;
if (ey < 0)
ey = 0;