aboutsummaryrefslogtreecommitdiff
path: root/setupx.c
blob: 4f4f5834ffb53067f93134f6d5a6d4bf10cd62e7 (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
/* Copyright 1998 DJ Delorie <dj@delorie.com>
   Distributed under the terms of the GNU GPL
   http://www.delorie.com/store/hcalc/
   Revisions copyright 2007,  
   Theodore Kilgore <kilgota@auburn.edu>
	More revisions copyright 2023, B. Watson <urchlay@slackware.uk>
*/
#include "hcalc.h"
#include <stdlib.h>

int widths[3] = { 125, 250, 500 };
int heights[3] = { 147, 294, 588 };

int winsize = 2;

Display *display = 0;
int screen = 0;
Visual *visual = 0;
Colormap cmap = 0;
Window window = 0;
Window rootwin = 0;
GC gc = 0;
Atom wm_protocols_atom = 0;
Atom delete_atom = 0;
Atom paste_atom = 0;
XEvent event;

int bit_on, bit_off;

void setup_x(void) {
	char *name = "hcalc";

	if(winsize == 2)
		scale_factor = 4;
	else
		scale_factor = winsize + 1;

	XSizeHints size_hints;
	XTextProperty xtp;
	XSetWindowAttributes attributes;
	XColor color;

	display = XOpenDisplay(0);
	screen = XDefaultScreen(display);
	cmap = XDefaultColormap(display, screen);
	visual = XDefaultVisual(display, screen);
	rootwin = XDefaultRootWindow(display);
	gc = XCreateGC(display, rootwin, 0, 0);

	wm_protocols_atom = XInternAtom(display, "WM_PROTOCOLS", 0);
	delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", 0);
	paste_atom = XInternAtom(display, "PASTE_DATA", 0);

	size_hints.flags = PSize | PMinSize | PMaxSize;
	size_hints.width = size_hints.min_width = size_hints.max_width =
		widths[winsize];
	size_hints.height = size_hints.min_height = size_hints.max_height =
		heights[winsize];
	size_hints.x = 0;
	size_hints.y = 0;

	window = XCreateWindow(display, rootwin, size_hints.x, size_hints.y, size_hints.width, size_hints.height, 0, CopyFromParent,	/* depth */
						   InputOutput, CopyFromParent,	/* visual */
						   0, 0);

	XSetWMNormalHints(display, window, &size_hints);

	XStringListToTextProperty(&name, 1, &xtp);
	XSetWMName(display, window, &xtp);
	XFree(xtp.value);

	XSetWMProtocols(display, window, &delete_atom, 1);

	attributes.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
	XChangeWindowAttributes(display, window, CWEventMask, &attributes);

	XMapWindow(display, window);
	XFlush(display);

	color.red = 0x6666;
	color.green = 0xffff;
	color.blue = 0x6666;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(display, cmap, &color);
	bit_on = color.pixel;

	color.red = 0x3333;
	color.green = 0xcccc;
	color.blue = 0x3333;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(display, cmap, &color);
	bit_off = color.pixel;
}

void process_input() {
	char c;
	KeySym keysym;
	XTextProperty tprop;

	show_value();

	while(1) {
		XNextEvent(display, &event);
		if(event.xany.window == window) {
			switch (event.type) {
				case Expose:
					if(event.xexpose.count == 0)
						redraw();
					break;
				case KeyPress:
					if(XLookupString(&event.xkey, &c, 1, &keysym, 0) == 1) {
						key(c);
					} else {
						switch (keysym) {
							case XK_F1:
								key('D');
								break;
							case XK_F2:
								key('H');
								break;
							case XK_F3:
								key('O');
								break;
							case XK_F4:
								key('B');
								break;
							case XK_Insert:
								if(event.xkey.state & ShiftMask)
									paste();
								break;
						}
					}
					break;
				case ButtonPress:
					button(event.xbutton.button, event.xbutton.x,
						   event.xbutton.y);
					break;
				case SelectionRequest:
					send_current_display();
					break;
				case SelectionNotify:
					if(event.xselection.property == paste_atom) {
						XGetTextProperty(display, window, &tprop,
										 paste_atom);
						complete_paste(tprop.value, tprop.nitems);
					}
					break;
			}
			if(event.type == ClientMessage
			   && event.xclient.data.l[0] == delete_atom) {
				/* printf( "Shutting down now!!!\n"); */
				break;
			}
		}
	}
	XCloseDisplay(display);
}