-
Notifications
You must be signed in to change notification settings - Fork 62
/
CNFGXDriver.c
684 lines (578 loc) · 18.4 KB
/
CNFGXDriver.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//Copyright (c) 2011, 2017, 2018 <>< Charles Lohr - Under the MIT/x11 or NewBSD License you choose.
//portions from
//http://www.xmission.com/~georgeps/documentation/tutorials/Xlib_Beginner.html
//#define HAS_XINERAMA
//#define CNFG_HAS_XSHAPE
//#define FULL_SCREEN_STEAL_FOCUS
#ifndef _CNFGXDRIVER_C
#define _CNFGXDRIVER_C
#include "CNFG.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAS_XINERAMA
#include <X11/extensions/shape.h>
#include <X11/extensions/Xinerama.h>
#endif
#ifdef CNFG_HAS_XSHAPE
#include <X11/extensions/shape.h>
static XGCValues xsval;
static Pixmap xspixmap;
static GC xsgc;
static int taint_shape;
static int prepare_xshape;
static int was_transp;
#endif
#ifdef CNFG_BATCH
void CNFGSetupBatchInternal();
#endif
XWindowAttributes CNFGWinAtt;
XClassHint *CNFGClassHint;
char * wm_res_name = 0;
char * wm_res_class = 0;
Display *CNFGDisplay;
Atom CFNGWMDeleteWindow;
Window CNFGWindow;
int CNFGWindowInvisible;
Pixmap CNFGPixmap;
GC CNFGGC;
GC CNFGWindowGC;
int CNFGDepth;
int CNFGScreen;
Visual * CNFGVisual;
VisualID CNFGVisualID;
#ifdef CNFGOGL
#include <GL/glx.h>
#include <GL/glxext.h>
GLXContext CNFGCtx;
void * CNFGGetExtension( const char * extname ) { return (void*)glXGetProcAddressARB((const GLubyte *) extname); }
GLXFBConfig CNFGGLXFBConfig;
GLXFBConfig* CNFGGLXFBConfigs;
void CNFGGLXSetup( )
{
int attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
None };
int elements = 0;
GLXFBConfig * cfgs = glXChooseFBConfig( CNFGDisplay, CNFGScreen, attribs, &elements );
if( elements == 0 )
{
fprintf( stderr, "Error: could not get valid GLXFBConfig visual.\n" );
exit( -1 );
}
CNFGGLXFBConfigs = cfgs;
CNFGGLXFBConfig = cfgs[0];
XVisualInfo * vis = glXGetVisualFromFBConfig( CNFGDisplay, CNFGGLXFBConfig );
CNFGVisual = vis->visual;
CNFGVisualID = vis->visualid;
CNFGDepth = vis->depth;
CNFGCtx = glXCreateContext( CNFGDisplay, vis, NULL, True );
XFree( vis );
}
#endif
int CNFGX11ForceNoDecoration;
XImage *xi;
XIM CNFGXIM = NULL;
XIC CNFGXIC = NULL;
int g_x_global_key_state;
int g_x_global_shift_key;
void CNFGSetWindowIconData( int w, int h, uint32_t * data )
{
static Atom net_wm_icon;
static Atom cardinal;
if( !net_wm_icon ) net_wm_icon = XInternAtom( CNFGDisplay, "_NET_WM_ICON", False );
if( !cardinal ) cardinal = XInternAtom( CNFGDisplay, "CARDINAL", False );
unsigned long outdata[w*h];
int i;
for( i = 0; i < w*h; i++ )
{
outdata[i+2] = data[i];
}
outdata[0] = w;
outdata[1] = h;
XChangeProperty(CNFGDisplay, CNFGWindow, net_wm_icon, cardinal,
32, PropModeReplace, (const unsigned char*)outdata, 2 + w*h);
}
#ifdef CNFG_HAS_XSHAPE
void CNFGPrepareForTransparency() { prepare_xshape = 1; }
void CNFGDrawToTransparencyMode( int transp )
{
static Pixmap BackupCNFGPixmap;
static GC BackupCNFGGC;
if( was_transp && ! transp )
{
CNFGGC = BackupCNFGGC;
CNFGPixmap = BackupCNFGPixmap;
}
if( !was_transp && transp )
{
BackupCNFGPixmap = CNFGPixmap;
BackupCNFGGC = CNFGGC;
taint_shape = 1;
CNFGGC = xsgc;
CNFGPixmap = xspixmap;
}
was_transp = transp;
}
void CNFGClearTransparencyLevel()
{
taint_shape = 1;
XSetForeground(CNFGDisplay, xsgc, 0);
XFillRectangle(CNFGDisplay, xspixmap, xsgc, 0, 0, CNFGWinAtt.width, CNFGWinAtt.height);
XSetForeground(CNFGDisplay, xsgc, 1);
}
#endif
void CNFGSetMousePosition( int x, int y ) {
XWarpPointer( CNFGDisplay, None, CNFGWindow, 0, 0, 0, 0, x, y );
}
void CNFGConfineMouse( int confined ) {
if (confined) {
XGrabPointer( CNFGDisplay, CNFGWindow, True, 0, GrabModeAsync, GrabModeAsync, CNFGWindow, None, CurrentTime );
} else {
XUngrabPointer( CNFGDisplay, CurrentTime );
}
}
Cursor CNFGCursors[CNFG_CURSOR_LAST] = { None };
void CNFGSetCursor( CNFGCursorShape shape ) {
if (shape == CNFG_CURSOR_ARROW) XUndefineCursor( CNFGDisplay, CNFGWindow );
else XDefineCursor( CNFGDisplay, CNFGWindow, CNFGCursors[shape] );
}
int FullScreen = 0;
void CNFGGetDimensions( short * x, short * y )
{
static int lastx;
static int lasty;
*x = CNFGWinAtt.width;
*y = CNFGWinAtt.height;
if( lastx != *x || lasty != *y )
{
lastx = *x;
lasty = *y;
#ifndef CNFGCONTEXTONLY
CNFGInternalResize( lastx, lasty );
#endif
}
}
void CNFGChangeWindowTitle( const char * WindowName )
{
XSetStandardProperties( CNFGDisplay, CNFGWindow, WindowName, 0, 0, 0, 0, 0 );
}
static void InternalLinkScreenAndGo( const char * WindowName )
{
XFlush(CNFGDisplay);
XGetWindowAttributes( CNFGDisplay, CNFGWindow, &CNFGWinAtt );
if( !wm_res_name ) wm_res_name = strdup( "rawdraw" );
if( !wm_res_class ) wm_res_class = strdup( "rawdraw" );
XGetClassHint( CNFGDisplay, CNFGWindow, CNFGClassHint );
if (!CNFGClassHint) {
CNFGClassHint = XAllocClassHint();
if (CNFGClassHint) {
CNFGClassHint->res_name = wm_res_name;
CNFGClassHint->res_class = wm_res_class;
XSetClassHint( CNFGDisplay, CNFGWindow, CNFGClassHint );
} else {
fprintf( stderr, "Failed to allocate XClassHint!\n" );
}
} else {
fprintf( stderr, "Pre-existing XClassHint\n" );
}
XSelectInput (CNFGDisplay, CNFGWindow, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | PointerMotionMask | FocusChangeMask );
CNFGWindowGC = XCreateGC(CNFGDisplay, CNFGWindow, 0, 0);
if( CNFGX11ForceNoDecoration )
{
Atom window_type = XInternAtom(CNFGDisplay, "_NET_WM_WINDOW_TYPE", False);
long value = XInternAtom(CNFGDisplay, "_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(CNFGDisplay, CNFGWindow, window_type,
XA_ATOM, 32, PropModeReplace, (unsigned char *) &value,1 );
}
CNFGPixmap = XCreatePixmap( CNFGDisplay, CNFGWindow, CNFGWinAtt.width, CNFGWinAtt.height, CNFGWinAtt.depth );
CNFGGC = XCreateGC(CNFGDisplay, CNFGPixmap, 0, 0);
XSetLineAttributes(CNFGDisplay, CNFGGC, 1, LineSolid, CapRound, JoinRound);
CNFGChangeWindowTitle( WindowName );
if( !CNFGWindowInvisible )
XMapWindow(CNFGDisplay, CNFGWindow);
CNFGXIM = XOpenIM(CNFGDisplay, NULL, wm_res_name, wm_res_class);
CNFGXIC = XCreateIC(CNFGXIM, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL);
#ifdef CNFG_HAS_XSHAPE
if( prepare_xshape )
{
xsval.foreground = 1;
xsval.line_width = 1;
xsval.line_style = LineSolid;
xspixmap = XCreatePixmap(CNFGDisplay, CNFGWindow, CNFGWinAtt.width, CNFGWinAtt.height, 1);
xsgc = XCreateGC(CNFGDisplay, xspixmap, 0, &xsval);
XSetLineAttributes(CNFGDisplay, xsgc, 1, LineSolid, CapRound, JoinRound);
}
#endif
}
void CNFGSetupFullscreen( const char * WindowName, int screen_no )
{
#ifdef HAS_XINERAMA
XineramaScreenInfo *screeninfo = NULL;
int screens;
int event_basep, error_basep, a, b;
CNFGDisplay = XOpenDisplay(NULL);
CNFGScreen = XDefaultScreen(CNFGDisplay);
int xpos, ypos;
if (!XShapeQueryExtension(CNFGDisplay, &event_basep, &error_basep)) {
fprintf( stderr, "X-Server does not support shape extension\n" );
exit( 1 );
}
CNFGVisual = DefaultVisual(CNFGDisplay, CNFGScreen);
CNFGVisualID = 0;
CNFGWinAtt.depth = DefaultDepth(CNFGDisplay, CNFGScreen);
#ifdef CNFGOGL
CNFGGLXSetup();
#endif
if (XineramaQueryExtension(CNFGDisplay, &a, &b ) &&
(screeninfo = XineramaQueryScreens(CNFGDisplay, &screens)) &&
XineramaIsActive(CNFGDisplay) && screen_no >= 0 &&
screen_no < screens ) {
CNFGWinAtt.width = screeninfo[screen_no].width;
CNFGWinAtt.height = screeninfo[screen_no].height;
xpos = screeninfo[screen_no].x_org;
ypos = screeninfo[screen_no].y_org;
} else
{
CNFGWinAtt.width = XDisplayWidth(CNFGDisplay, CNFGScreen);
CNFGWinAtt.height = XDisplayHeight(CNFGDisplay, CNFGScreen);
xpos = 0;
ypos = 0;
}
if (screeninfo)
XFree(screeninfo);
XSetWindowAttributes setwinattr;
setwinattr.override_redirect = 1;
setwinattr.save_under = 1;
#ifdef CNFG_HAS_XSHAPE
if (prepare_xshape && !XShapeQueryExtension(CNFGDisplay, &event_basep, &error_basep))
{
fprintf( stderr, "X-Server does not support shape extension" );
exit( 1 );
}
setwinattr.event_mask = 0;
#else
//This code is probably made irrelevant by the XSetEventMask in InternalLinkScreenAndGo, if this code is not found needed by 2019-12-31, please remove.
//setwinattr.event_mask = StructureNotifyMask | SubstructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | ButtonPressMask | PointerMotionMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask |KeyPressMask |KeyReleaseMask | SubstructureNotifyMask | FocusChangeMask;
#endif
setwinattr.border_pixel = 0;
setwinattr.colormap = XCreateColormap( CNFGDisplay, RootWindow(CNFGDisplay, 0), CNFGVisual, AllocNone);
CNFGWindow = XCreateWindow(CNFGDisplay, XRootWindow(CNFGDisplay, CNFGScreen),
xpos, ypos, CNFGWinAtt.width, CNFGWinAtt.height,
0, CNFGWinAtt.depth, InputOutput, CNFGVisual,
CWBorderPixel/* | CWEventMask */ | CWOverrideRedirect | CWSaveUnder | CWColormap,
&setwinattr);
FullScreen = 1;
InternalLinkScreenAndGo( WindowName );
#ifdef CNFGOGL
glXMakeCurrent( CNFGDisplay, CNFGWindow, CNFGCtx );
#endif
#ifdef CNFG_BATCH
#ifndef CNFGCONTEXTONLY
CNFGSetupBatchInternal();
#endif
#endif
#else
CNFGSetup( WindowName, 640, 480 );
#endif
}
void CNFGTearDown()
{
HandleDestroy();
if( xi ) free( xi );
if ( CNFGClassHint ) XFree( CNFGClassHint );
if ( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
if ( CNFGWindowGC ) XFreeGC( CNFGDisplay, CNFGWindowGC );
if ( CNFGDisplay ) XCloseDisplay( CNFGDisplay );
if ( CNFGXIC ) XDestroyIC( CNFGXIC );
if ( CNFGXIM ) XCloseIM( CNFGXIM );
#ifdef CNFGOGL
if ( CNFGGLXFBConfigs ) XFree( CNFGGLXFBConfigs );
CNFGGLXFBConfigs = NULL;
#endif
CNFGDisplay = NULL;
CNFGWindowGC = CNFGGC = NULL;
CNFGClassHint = NULL;
CFNGWMDeleteWindow = None;
}
int CNFGSetupWMClass( const char * WindowName, int w, int h , char * wm_res_name_ , char * wm_res_class_ )
{
wm_res_name = wm_res_name_;
wm_res_class = wm_res_class_;
return CNFGSetup( WindowName, w, h);
}
int CNFGSetup( const char * WindowName, int w, int h )
{
CNFGDisplay = XOpenDisplay(NULL);
if ( !CNFGDisplay ) {
fprintf( stderr, "Could not get an X Display.\n%s",
"Are you in text mode or using SSH without X11-Forwarding?\n" );
exit( 1 );
}
atexit( CNFGTearDown );
CNFGScreen = DefaultScreen(CNFGDisplay);
CNFGDepth = DefaultDepth(CNFGDisplay, CNFGScreen);
CNFGVisual = DefaultVisual(CNFGDisplay, CNFGScreen);
CNFGVisualID = 0;
Window wnd = DefaultRootWindow( CNFGDisplay );
#ifdef CNFGOGL
CNFGGLXSetup( );
#endif
XSetWindowAttributes attr;
attr.background_pixel = 0;
attr.colormap = XCreateColormap( CNFGDisplay, wnd, CNFGVisual, AllocNone);
if( w > 0 && h > 0 )
CNFGWindow = XCreateWindow(CNFGDisplay, wnd, 1, 1, w, h, 0, CNFGDepth, InputOutput, CNFGVisual, CWBackPixel | CWColormap, &attr );
else
{
if( w < 0 ) w = -w;
if( h < 0 ) h = -h;
CNFGWindow = XCreateWindow(CNFGDisplay, wnd, 1, 1, w, h, 0, CNFGDepth, InputOutput, CNFGVisual, CWBackPixel | CWColormap, &attr );
CNFGWindowInvisible = 1;
}
InternalLinkScreenAndGo( WindowName );
//Not sure of the purpose of this code - if it's still commented out after 2019-12-31 and no one knows why, please delete it.
CFNGWMDeleteWindow = XInternAtom( CNFGDisplay, "WM_DELETE_WINDOW", False );
XSetWMProtocols( CNFGDisplay, CNFGWindow, &CFNGWMDeleteWindow, 1 );
// X11 doesn't have a concept of a hidden cursor. Make a blank cursor as a substitute
XColor col = { 0 };
Pixmap blank = XCreateBitmapFromData( CNFGDisplay, CNFGWindow, (char*)(&col), 1, 1 );
CNFGCursors[CNFG_CURSOR_HIDDEN] = XCreatePixmapCursor( CNFGDisplay, blank, blank, &col, &col, 0, 0 );
XFreePixmap( CNFGDisplay, blank );
#ifdef CNFGOGL
glXMakeCurrent( CNFGDisplay, CNFGWindow, CNFGCtx );
#endif
#ifdef CNFG_BATCH
#ifndef CNFGCONTEXTONLY
CNFGSetupBatchInternal();
#endif
#endif
return 0;
}
int CNFGHandleInput()
{
if( !CNFGWindow ) return 0;
static int ButtonsDown;
XEvent report;
int bKeyDirection = 1;
while( XPending( CNFGDisplay ) )
{
XNextEvent( CNFGDisplay, &report );
bKeyDirection = 1;
switch (report.type)
{
case NoExpose:
break;
case Expose:
XGetWindowAttributes( CNFGDisplay, CNFGWindow, &CNFGWinAtt );
if( CNFGPixmap ) XFreePixmap( CNFGDisplay, CNFGPixmap );
CNFGPixmap = XCreatePixmap( CNFGDisplay, CNFGWindow, CNFGWinAtt.width, CNFGWinAtt.height, CNFGWinAtt.depth );
if( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
CNFGGC = XCreateGC(CNFGDisplay, CNFGPixmap, 0, 0);
CNFGLastScancode = 0;
HandleKey( CNFG_X11_EXPOSE, 0 );
break;
case KeyRelease:
{
bKeyDirection = 0;
//Tricky - handle key repeats cleanly.
if( XPending( CNFGDisplay ) )
{
XEvent nev;
XPeekEvent( CNFGDisplay, &nev );
if (nev.type == KeyPress && nev.xkey.time == report.xkey.time && nev.xkey.keycode == report.xkey.keycode )
bKeyDirection = 2;
}
}
case KeyPress:
g_x_global_key_state = report.xkey.state;
g_x_global_shift_key = XLookupKeysym(&report.xkey, 1);
CNFGLastScancode = report.xkey.keycode;
// Chars should ONLY be handled on Key Press
if (report.type == KeyPress) {
char buf[8] = {0};
if (Xutf8LookupString(CNFGXIC, &report.xkey, buf, 8, NULL, NULL)) CNFGLastCharacter = *((int*)buf);
else CNFGLastCharacter = 0;
}
KeySym sym = XLookupKeysym(&report.xkey, 0);
HandleKey( sym, bKeyDirection );
// Don't confuse the program when CNFGLastCharacter is set on KeyRelease.
// Since we shouldn't be using CNFGLastCharacter outside HandleKey anyways, just clear it here.
CNFGLastCharacter = 0;
break;
case ButtonRelease:
bKeyDirection = 0;
case ButtonPress:
HandleButton( report.xbutton.x, report.xbutton.y, report.xbutton.button, bKeyDirection );
ButtonsDown = (ButtonsDown & (~(1<<report.xbutton.button))) | ( bKeyDirection << report.xbutton.button );
//Intentionall fall through -- we want to send a motion in event of a button as well.
case MotionNotify:
HandleMotion( report.xmotion.x, report.xmotion.y, ButtonsDown>>1 );
break;
case FocusIn:
case FocusOut:
CNFGLastScancode = 0;
HandleKey( CNFG_KEY_FOCUS, report.type == FocusIn );
break;
case ClientMessage:
if ( report.xclient.data.l[0] == CFNGWMDeleteWindow )
return 0;
break;
default:
break;
//printf( "Event: %d\n", report.type );
}
}
return 1;
}
#ifdef CNFGOGL
void CNFGSetVSync( int vson )
{
void (*glfn)( int );
void (*glfnXE)( Display *, GLXDrawable, int );
glfn = (void (*)( int ))CNFGGetExtension( "glXSwapIntervalMESA" ); if( glfn ) glfn( vson );
glfn = (void (*)( int ))CNFGGetExtension( "glXSwapIntervalSGI" ); if( glfn ) glfn( vson );
glfnXE = (void (*)( Display *, GLXDrawable, int ))CNFGGetExtension( "glXSwapIntervalEXT" ); if( glfn ) glfnXE( CNFGDisplay, CNFGWindow, vson );
}
#ifdef CNFGRASTERIZER
void CNFGSwapBuffersInternal()
#else
void CNFGSwapBuffers()
#endif
{
if( CNFGWindowInvisible ) return;
#ifndef CNFGRASTERIZER
#ifndef CNFGCONTEXTONLY
CNFGFlushRender();
#endif
#endif
#ifdef CNFG_HAS_XSHAPE
if( taint_shape )
{
XShapeCombineMask(CNFGDisplay, CNFGWindow, ShapeBounding, 0, 0, xspixmap, ShapeSet);
taint_shape = 0;
}
#endif //CNFG_HAS_XSHAPE
glXSwapBuffers( CNFGDisplay, CNFGWindow );
#ifdef FULL_SCREEN_STEAL_FOCUS
if( FullScreen )
XSetInputFocus( CNFGDisplay, CNFGWindow, RevertToParent, CurrentTime );
#endif //FULL_SCREEN_STEAL_FOCUS
}
#else //CNFGOGL
#ifndef CNFGRASTERIZER
void CNFGBlitImage( uint32_t * data, int x, int y, int w, int h )
{
static int lw, lh;
if( lw != w || lh != h || !xi )
{
if( xi ) free( xi );
xi = XCreateImage(CNFGDisplay, CNFGVisual, CNFGDepth, ZPixmap, 0, (char*)data, w, h, 32, w*4 );
lw = w;
lh = h;
}
xi->data = (char*)data;
//Draw image to pixmap (not a screen flip)
XPutImage(CNFGDisplay, CNFGPixmap, CNFGGC, xi, 0, 0, x, y, w, h );
}
#endif
void CNFGUpdateScreenWithBitmap( uint32_t * data, int w, int h )
{
static int lw, lh;
if( lw != w || lh != h || !xi )
{
if( xi ) free( xi );
xi = XCreateImage(CNFGDisplay, CNFGVisual, CNFGDepth, ZPixmap, 0, (char*)data, w, h, 32, w*4 );
lw = w;
lh = h;
}
xi->data = (char*)data;
//Directly write image to screen (effectively a flip)
XPutImage(CNFGDisplay, CNFGWindow, CNFGWindowGC, xi, 0, 0, 0, 0, w, h );
}
#endif //CNFGOGL
#if !defined( CNFGOGL)
#define AGLF(x) x
#else
#define AGLF(x) static inline BACKEND_##x
#endif
#if defined( CNFGRASTERIZER )
#include "CNFGRasterizer.c"
#undef AGLF
#define AGLF(x) static inline BACKEND_##x
#endif
uint32_t AGLF(CNFGColor)( uint32_t RGB )
{
CNFGLastColor = RGB;
unsigned char red = ( RGB >> 24 ) & 0xFF;
unsigned char grn = ( RGB >> 16 ) & 0xFF;
unsigned char blu = ( RGB >> 8 ) & 0xFF;
unsigned long color = (red<<16)|(grn<<8)|(blu);
XSetForeground(CNFGDisplay, CNFGGC, color);
return color;
}
void AGLF(CNFGClearFrame)()
{
XGetWindowAttributes( CNFGDisplay, CNFGWindow, &CNFGWinAtt );
XSetForeground(CNFGDisplay, CNFGGC, CNFGColor(CNFGBGColor) );
XFillRectangle(CNFGDisplay, CNFGPixmap, CNFGGC, 0, 0, CNFGWinAtt.width, CNFGWinAtt.height );
}
void AGLF(CNFGSwapBuffers)()
{
#ifdef CNFG_HAS_XSHAPE
if( taint_shape )
{
XShapeCombineMask(CNFGDisplay, CNFGWindow, ShapeBounding, 0, 0, xspixmap, ShapeSet);
taint_shape = 0;
}
#endif
XCopyArea(CNFGDisplay, CNFGPixmap, CNFGWindow, CNFGWindowGC, 0,0,CNFGWinAtt.width,CNFGWinAtt.height,0,0);
XFlush(CNFGDisplay);
#ifdef FULL_SCREEN_STEAL_FOCUS
if( FullScreen )
XSetInputFocus( CNFGDisplay, CNFGWindow, RevertToParent, CurrentTime );
#endif
}
void AGLF(CNFGTackSegment)( short x1, short y1, short x2, short y2 )
{
if( x1 == x2 && y1 == y2 )
{
//On some targets, zero-length lines will not show up.
//This is tricky - since this will also cause more draw calls for points on systems like GLAMOR.
XDrawPoint( CNFGDisplay, CNFGPixmap, CNFGGC, x2, y2 );
}
else
{
//XXX HACK! See discussion here: https://github.com/cntools/cnping/issues/68
XDrawLine( CNFGDisplay, CNFGPixmap, CNFGGC, x1, y1, x2, y2 );
XDrawLine( CNFGDisplay, CNFGPixmap, CNFGGC, x2, y2, x1, y1 );
}
}
void AGLF(CNFGTackPixel)( short x1, short y1 )
{
XDrawPoint( CNFGDisplay, CNFGPixmap, CNFGGC, x1, y1 );
}
void AGLF(CNFGTackRectangle)( short x1, short y1, short x2, short y2 )
{
XFillRectangle(CNFGDisplay, CNFGPixmap, CNFGGC, x1, y1, x2-x1, y2-y1 );
}
void AGLF(CNFGTackPoly)( RDPoint * points, int verts )
{
XFillPolygon(CNFGDisplay, CNFGPixmap, CNFGGC, (XPoint *)points, verts, Convex, CoordModeOrigin );
}
void AGLF(CNFGInternalResize)( short x, short y ) { }
void AGLF(CNFGSetLineWidth)( short width )
{
XSetLineAttributes(CNFGDisplay, CNFGGC, width, LineSolid, CapRound, JoinRound);
}
#endif // _CNFGXDRIVER_C