File: x11-common.c

package info (click to toggle)
ocp 1%3A0.1.21-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,344 kB
  • ctags: 7,862
  • sloc: ansic: 91,449; cpp: 9,729; sh: 3,119; makefile: 2,482
file content (135 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download | duplicates (2)
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
/* OpenCP Module Player
 * copyright (c) '94-'10 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
 *
 * X11 common stuff
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * revision history: (please note changes here)
 *  -ss050429   Stian Skjelstad <stian@nixia.no>
 *    -first release
 */

#define _CONSOLE_DRIVER
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include "types.h"
#include "poutput.h"
#include "x11-common.h"

static uint16_t red[256]=  {0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa, 0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff};
static uint16_t green[256]={0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa, 0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff};
static uint16_t blue[256]= {0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff};

uint32_t x11_palette32[256];
uint16_t x11_palette16[256];
uint16_t x11_palette15[256];

Display *mDisplay = NULL;
int mLocalDisplay=0;
int mScreen;
int plDepth=8;

void x11_gupdatepal(unsigned char color, unsigned char _red, unsigned char _green, unsigned char _blue)
{
	red[color]=_red<<10;
	green[color]=_green<<10;
	blue[color]=_blue<<10;
}

void x11_gflushpal(void)
{
	int i, r, g, b;
	if (plDepth==8)
	{
		Colormap cmap=0;

		cmap=XCreateColormap(mDisplay, mScreen, XDefaultVisual(mDisplay, mScreen), AllocAll);
		for (i = 0; i < 256; i++)
		{
			XColor xcol;

			xcol.pixel = i;
			xcol.red = red[i];
			xcol.green = green[i];
			xcol.blue = blue[i];
			xcol.flags = DoBlue | DoGreen | DoRed;
			XStoreColor(mDisplay, cmap, &xcol);
		}
		XInstallColormap(mDisplay, cmap);
		XFreeColormap(mDisplay, cmap);
		return;
	}
	for (i=0;i<256;i++)
	{
		r=red[i]>>8;
		g=green[i]>>8;
		b=blue[i]>>8;
		x11_palette32[i]=(r<<16)+(g<<8)+b;
		r>>=3;
		g>>=2;
		b>>=3;
		x11_palette16[i]=(r<<11)+(g<<5)+b;
		g>>=1;
		x11_palette15[i]=(r<<10)+(g<<5)+b;
	}
}

static int inited=0;

int x11_connect(void)
{
	char *dispName;

	if (inited++)
		return !mDisplay;

	dispName = XDisplayName(NULL);

	if ( (mDisplay = XOpenDisplay(dispName)) == NULL )
	{
		fprintf(stderr, "[x11] can't connect to X server %s\n", XDisplayName(NULL));
		return -1;
	}
	fprintf(stderr, "[x11] X is online\n");

	if (strncmp(dispName, "unix:", 5) == 0)
		dispName += 4;
	else if (strncmp(dispName, "localhost:", 10) == 0)
		dispName += 9;
	if (*dispName == ':' && atoi(dispName + 1) < 10)
		mLocalDisplay = 1;
	else
		mLocalDisplay = 0;

	mScreen=DefaultScreen(mDisplay);

	return 0;
}

void x11_disconnect(void)
{
	if (!inited)
		return;
	if (!(--inited))
	{
		XCloseDisplay(mDisplay);
		mDisplay=NULL;
	}
}