summaryrefslogtreecommitdiff
path: root/src/scapx.c
blob: 50edc7f0ebe62c6abf6f7559797d54a364a68f10 (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
// std
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>

// X
#include <X11/keysymdef.h>
#include <X11/keysym.h>
#include <xcb/xcb_keysyms.h>
#include <X11/cursorfont.h>
//#include <xcb/xcb_atom.h>
// #include <xcb/xcb_util.h>
// #include <X11/Xutil.h>
#include <xcb/xcb_cursor.h>

// local
#include "types.h"
#include "scapx.h"
#include "x.h"
#include "config.h"



void dbg_printf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}

int main(int argc, char **argv)
{
	// TODO: parse flags

	Server_context_t *info;

	if (!(info = init_XCB_server()))
		FATAL_ERROR("Couldn't make a connection to the x server. %s", "");

	xcb_drawable_t win = create_win(info);
	xcb_key_symbols_t *ksymbols = xcb_key_symbols_alloc(info->con);
	xcb_generic_event_t *event;
	xcb_cursor_t mptr;

	bool done = 0;
	while (!done && (event = xcb_wait_for_event(info->con))) {
		if (event->response_type == 0 )
			SLOG("Error reported by the x server.\n");

		switch (event->response_type & ~0x80) {
			{ // mouse
			static bool lmbtn = 0;
			static bool rmbtn = 0;
			case XCB_BUTTON_PRESS:
				if (((xcb_button_press_event_t *)event)->detail == M_BTN_1) {
					lmbtn = 1;
					printf("left clicked.\n");
				}

				if (((xcb_button_press_event_t *)event)->detail == M_BTN_2) {
					printf("right clicked.\n");
					rmbtn = 1;
				}
				break;
			case XCB_BUTTON_RELEASE:
				cursor_die(info->con, win, mptr);
				if (((xcb_button_press_event_t *)event)->detail == M_BTN_1) {
					lmbtn = 0;
				}
				if (((xcb_button_press_event_t *)event)->detail == M_BTN_2) {
					rmbtn = 0;
				}
				break;
			case XCB_MOTION_NOTIFY:
				if (lmbtn || rmbtn) {
					mptr = cursor_set(info->con, info->scr, win, XC_cross);
					printf("%s drag.\n", lmbtn ? "left" : "right");
				} else
					printf("moving.\n");
				break;
			}

			case XCB_KEY_PRESS:
				printf("\nKeyPress event received:\n");
				xcb_key_press_event_t *kevent = (xcb_key_press_event_t *)event;

				// NOTE: field is a mask of the buttons held down during the event
				printf("state %#x ", kevent->state);
				printf("keycode %u ", kevent->detail);


				xcb_keysym_t ksym = xcb_key_symbols_get_keysym(ksymbols, kevent->detail, kevent->state);
				const char *sym_str = ksym_to_str(&ksym);

				if (ksym == XK_Escape || ksym == XK_q || ksym == XK_Q)
					die(ksymbols, info, win);


				printf("keysym [%#x %s]\n", ksym, sym_str ? sym_str : "");
				break;
			case XCB_KEY_RELEASE:
				break;
		}

		free(event);
	}

	xcb_free_cursor(info->con, mptr);

	die(ksymbols, info, win);
	return 0;
}