summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
committerPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
commit2497c1392b81e093f4de2541a98746aebd449a7f (patch)
treedb87e59d4c70c677ddb1899d9161b004f6e454ac
downloadc-files-2497c1392b81e093f4de2541a98746aebd449a7f.tar.gz
Initial commit
-rw-r--r--abuse.c57
-rw-r--r--bind.c43
-rw-r--r--grab.cpp59
-rw-r--r--hex2bin.c28
-rw-r--r--inotify.c92
-rw-r--r--ioctl-w.c33
-rw-r--r--le2bin.c12
-rw-r--r--overflow.c31
-rw-r--r--pipe.c35
-rw-r--r--pipe.cpp56
-rw-r--r--rev.c10
-rw-r--r--rkbd/Makefile9
-rw-r--r--rkbd/rkbd.cpp128
-rw-r--r--rkbd/uinput.c90
-rw-r--r--viewimagefs.cpp31
-rw-r--r--xcbviewfs.c139
-rw-r--r--xlistpixmapformats.c18
17 files changed, 871 insertions, 0 deletions
diff --git a/abuse.c b/abuse.c
new file mode 100644
index 0000000..040fb5e
--- /dev/null
+++ b/abuse.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define host "192.168.2.3"
+#define port 8888
+
+int f(struct in_addr *saddr, const char *data, const size_t data_len) {
+ int sockfd;
+ struct sockaddr_in addr;
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (sockfd < -1) {
+ perror("socket");
+ return 1;
+ }
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ addr.sin_addr = *saddr;
+ if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr))) {
+ perror("connect");
+ return 1;
+ }
+ write(sockfd, data, data_len);
+ close(sockfd);
+ return 0;
+}
+int main(int argc, char **argv) {
+ struct in_addr saddr;
+ const char *data = "GET / HTTP/1.0\r\n\r\n";
+ const size_t data_len = strlen(data);
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s count\n", *argv);
+ return 1;
+ }
+ if (!inet_aton(host, &saddr)) {
+ perror("inet_aton");
+ return 1;
+ }
+ int count = atoi(argv[1]);
+ const char dot = '.';
+ unsigned fails;
+ while (--count >= 0) {
+ if (!(count % 10)) {
+ write(STDOUT_FILENO, &dot, sizeof(dot));
+ }
+ if (f(&saddr, data, data_len)) {
+ if (++fails > 10) break;
+ } else
+ fails /= 2;
+ }
+ putchar('\n');
+ return 0;
+}
diff --git a/bind.c b/bind.c
new file mode 100644
index 0000000..8b9cae3
--- /dev/null
+++ b/bind.c
@@ -0,0 +1,43 @@
+#include <sys/socket.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+static void fail(const char *err) {
+ perror(err);
+ exit(1);
+}
+
+int main(int argc, char **argv) {
+ unsigned short int port;
+ int sockfd;
+ struct sockaddr_in addr;
+ struct in_addr sa;
+
+ if (argc < 2) {
+ printf("Usage: %s port\n", argv[0]);
+ return -1;
+ }
+ port = atoi(argv[1]);
+ printf("Port: %hu\n", port);
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (sockfd < 0) fail("socket");
+
+ memset(&addr, 0, sizeof addr);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ sa.s_addr = htonl(INADDR_LOOPBACK);
+ addr.sin_addr = sa;
+ printf("sockfd = %i\naddr = %p\nsizeof addr = %zi\n", sockfd,
+ (struct sockaddr *)&addr, sizeof addr);
+ if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) < 0) fail("bind");
+
+ if (listen(sockfd, 10) < 0) fail("listen");
+
+ if (close(sockfd) < 0) fail("close");
+ printf("Success!\n");
+ return 0;
+}
diff --git a/grab.cpp b/grab.cpp
new file mode 100644
index 0000000..6071c2f
--- /dev/null
+++ b/grab.cpp
@@ -0,0 +1,59 @@
+/**
+ * Attempt to display picture fullscreen, capturing input (failed at capture kbd keys)
+ * Author: Peter Wu
+ * Date: 2012-09-24
+ */
+#include <QApplication>
+#include <QLabel>
+#include <iostream>
+#include <stdio.h>
+#include <unistd.h>
+
+#define WITH_XGRAB
+//#undef WITH_XGRAB
+#ifdef WITH_XGRAB
+# include <X11/Xlib.h>
+#endif
+
+class Application: public QApplication {
+public:
+ Application(int argc, char **argv): QApplication(argc, argv) {}
+ ~Application(){}
+ bool notify(QObject *object, QEvent *event) {
+ std::cerr << "Event: " << event->type() << std::endl;
+ return object->event(event);
+ }
+};
+
+int main(int argc, char **argv) {
+ Application app(argc, argv);
+ QLabel lbl;
+ //lbl.setCursor(QCursor(Qt::BlankCursor));
+ lbl.resize(300, 200);
+ lbl.setWindowTitle(*argv);
+ lbl.show();
+ lbl.grabKeyboard();
+#ifdef WITH_XGRAB
+ Display *dpy = XOpenDisplay(NULL);
+ if (!dpy) fprintf(stderr, "Could not open DISPLAY\n");
+ else{
+ Window win = DefaultRootWindow(dpy);
+ XGrabKeyboard(dpy, win, False,
+ GrabModeAsync,
+ GrabModeAsync,
+ CurrentTime);
+#if 0
+ XGrabPointer(dpy, win, True,
+ 0,
+ GrabModeAsync,
+ GrabModeAsync,
+ win,
+ None,
+ CurrentTime);
+#endif
+ }
+#endif
+ return app.exec();
+ //std::cerr << "Closing" << std::endl;
+ //return 0;
+}
diff --git a/hex2bin.c b/hex2bin.c
new file mode 100644
index 0000000..78efd60
--- /dev/null
+++ b/hex2bin.c
@@ -0,0 +1,28 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <ctype.h>
+
+int main() {
+ char c;
+ int n = 0;
+ int count = 0;
+ int skipLine = 0;
+ while ((c = getchar()) != EOF) {
+ if (skipLine && c != '\n') continue;
+ if (isxdigit(c)) {
+ ++count;
+ n <<= 4;
+ if (isdigit(c)) {
+ n |= c - '0';
+ } else {
+ n += 10 + tolower(c) - 'a';
+ }
+ }
+ if (count >= 2 || (count && !isxdigit(c))) {
+ putchar(n);
+ n = count = 0;
+ }
+ skipLine = c == '#';
+ }
+ return 0;
+}
diff --git a/inotify.c b/inotify.c
new file mode 100644
index 0000000..d363c77
--- /dev/null
+++ b/inotify.c
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <sys/inotify.h>
+#include <unistd.h>
+#include <malloc.h>
+
+#include <error.h>
+#include <errno.h>
+#define fail(msg) error(1, errno, msg)
+
+#ifndef PATH_MAX
+# define PATH_MAX 4096
+#endif
+
+struct ev {
+ int val;
+ char *name;
+};
+
+#define STRVAL(val) #val
+#define IN(name) {IN_##name, STRVAL(name)}
+
+struct ev events[] = {
+ IN(CLOSE_WRITE),
+ IN(CLOSE_NOWRITE),
+ IN(OPEN),
+ {0, NULL}
+};
+
+int main(int argc, char **argv) {
+ int i, ifd, wfd, mask;
+ int wfds[argc - 1];
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s file_to_watch ...\n", argv[0]);
+ return 1;
+ }
+ for (i=1; i<argc; i++) {
+ printf("file = %s\n", argv[i]);
+ }
+
+ ifd = inotify_init();
+ if (ifd == -1) fail("inotify_init");
+ printf("ifd = %i\n", ifd);
+
+ mask = 0;
+
+ struct ev *event = events;
+ while (event->name != NULL) {
+ mask |= event->val;
+ printf("event IN_%-16s = %8x\n", event->name, event->val);
+ ++event;
+ }
+ for (i=1; i<argc; i++) {
+ wfd = inotify_add_watch(ifd, argv[i], mask);
+ if (wfd == -1) fail("inotify_add_watch");
+ printf("wfd = %i\n", wfd);
+ wfds[i - 1] = wfd;
+ }
+
+ int iev_size = sizeof(struct inotify_event) + PATH_MAX + 1;
+ struct inotify_event *iev = malloc(iev_size);
+ if (!iev) fail("malloc");
+
+ putchar('\n');
+
+ int j = 100;
+ while (--j) {
+ char *file;
+ if (read(ifd, iev, iev_size) == -1) {
+ perror("read");
+ break;
+ }
+ for (i=0; i<argc-1; i++) {
+ if (wfds[i] == iev->wd) {
+ file = argv[i + 1];
+ break;
+ }
+ }
+ printf("wd = %i, mask = %x, name = %s\tfile = %s\n",
+ iev->wd, iev->mask, iev->name,
+ file);
+ }
+
+ free(iev);
+
+ for (i=0; i<argc-1; i++) {
+ wfd = wfds[i];
+ if (inotify_rm_watch(ifd, wfd) == -1) fail("inotify_rm_watch");
+ }
+
+ close(ifd);
+ return 0;
+}
diff --git a/ioctl-w.c b/ioctl-w.c
new file mode 100644
index 0000000..1fc07b8
--- /dev/null
+++ b/ioctl-w.c
@@ -0,0 +1,33 @@
+#include <sys/ioctl.h>
+#include <string.h>
+#include <stdio.h>
+#include "iwlib.h"
+//#include "wireless.22.h"
+//make wireless.h
+
+int main() {
+ int r;
+ int skfd;
+ int request;
+ struct iwreq wrq;
+
+ skfd = iw_sockets_open();
+ if (skfd == -1) {
+ fprintf(stderr, "skfd fail\n");
+ return 1;
+ }
+ request = SIOCGIWAP;
+
+ memset(&wrq, 0, sizeof(wrq));
+ strcpy(wrq.ifr_name, "wlan0");
+
+ r = ioctl(skfd, request, &wrq);
+ printf("ret = %x\n", r);
+ struct sockaddr sa = wrq.u.ap_addr;
+ char *data = sa.sa_data;
+ int i;
+ for (i=0; i<14; i++)
+ printf("%02X ", data[i]);
+ printf("\n");
+ return 0;
+}
diff --git a/le2bin.c b/le2bin.c
new file mode 100644
index 0000000..fa31dd7
--- /dev/null
+++ b/le2bin.c
@@ -0,0 +1,12 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+
+int main(int argc, char **argv) {
+ int i;
+ for (i=1; i<argc; i++) {
+ uint32_t n = strtoul(argv[i], NULL, 16);
+ write(STDOUT_FILENO, &n, 4);
+ }
+ return 0;
+}
diff --git a/overflow.c b/overflow.c
new file mode 100644
index 0000000..1a01984
--- /dev/null
+++ b/overflow.c
@@ -0,0 +1,31 @@
+/**
+ * 27-08-2012 buffer overflow exercise
+ * gcc -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -g overflow.c -o overflow
+ */
+#include <stdio.h>
+#include <stdlib.h>
+
+void g() {
+ static int j = 0;
+ printf("Call %u\n", ++j);
+}
+void bye() {
+ puts("Last call - ciao");
+ exit(42);
+}
+
+void f() {
+ unsigned int i;
+ void * x[0];
+ scanf("%u", &i);
+ puts("From f()");
+ x[++i] = bye;
+ while (i--)
+ x[i] = g;
+ printf("Stuff: %p\n", x);
+}
+
+int main() {
+ f();
+ return 0;
+}
diff --git a/pipe.c b/pipe.c
new file mode 100644
index 0000000..ae114db
--- /dev/null
+++ b/pipe.c
@@ -0,0 +1,35 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#define F(cmd) ( (r=cmd),r<0?(perror(#cmd),exit(1),r):r )
+
+int main() {
+ int r;
+ int pA[2];
+ int f = F(open("mehlog", O_CREAT | O_APPEND | O_WRONLY, 0644));
+ pipe(pA);
+ //pipe(pB);
+ if (!F(fork())) {
+ close(f);
+ dup2(pA[1], STDOUT_FILENO);
+ close(pA[0]);
+ close(pA[1]);
+ F(execlp("cat", "cat", NULL));
+ }
+ close(pA[1]);
+ if (!F(fork())) {
+ dup2(f, STDOUT_FILENO);
+ dup2(pA[0], STDIN_FILENO);
+ close(pA[0]);
+ close(f);
+ F(execlp("rev", "rev", NULL));
+ }
+ close(pA[0]);
+ close(f);
+ wait(NULL);
+ return 0;
+}
diff --git a/pipe.cpp b/pipe.cpp
new file mode 100644
index 0000000..76cd19d
--- /dev/null
+++ b/pipe.cpp
@@ -0,0 +1,56 @@
+#include <iostream>
+#include <unistd.h>
+#include <cstdio>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define BS 10
+
+int main(int argc, char **argv) {
+ int fds[2], fu[2];
+ if (argc <= 1){
+ std::cerr << "Usage: " << argv[0] << " program args..." << std::endl;
+ return 1;
+ }
+ if (pipe(fds) == -1 || pipe(fu) == -1) {
+ perror("pipe");
+ return 1;
+ }
+ pid_t p = fork();
+ if (p == 0) {
+ /*child*/
+ close(fu[1]);
+ close(fds[0]);
+ if (dup2(fu[0], STDIN_FILENO) == -1) perror("dup stdin");
+ if (dup2(fds[1], STDOUT_FILENO) == -1) perror("dup stdout");
+ execvp(argv[1], argv+1);
+ perror("Execvp");
+ _exit(42);
+ } else {
+ char buf[BS];
+ std::string in("meh\n\04");
+ int flags = fcntl(fds[0], F_GETFL);
+ fcntl(fds[0], F_SETFL, flags | O_NONBLOCK);
+ close(fds[1]);close(fu[0]);
+ do {
+ int r = read(fds[0], buf, BS);
+ if (r == -1 && errno == EWOULDBLOCK) ;
+ else if (r == -1) perror("read");
+ else if (r > 0) {
+ std::cout.write(buf, r);
+ }
+ if (write(fu[1], in.c_str(), in.size()) == -1) perror("write");
+ int status = 0;
+ if (waitpid(p, &status, WNOHANG) != 0) {
+ if (WIFEXITED(status) || WTERMSIG(status)) {
+ break;
+ }
+ }
+ } while (1);
+ kill(p, 15);
+ }
+ return 0;
+}
diff --git a/rev.c b/rev.c
new file mode 100644
index 0000000..3bcab18
--- /dev/null
+++ b/rev.c
@@ -0,0 +1,10 @@
+#include <unistd.h>
+
+int main() {
+ char buf[1024];
+ char *eob = buf + sizeof(buf) - 1;
+ char *p = eob;
+ while (read(STDIN_FILENO, p, 1) == 1 && --p >= buf);
+ write(STDOUT_FILENO, p + 1, eob - p);
+ return 0;
+}
diff --git a/rkbd/Makefile b/rkbd/Makefile
new file mode 100644
index 0000000..f87a6c5
--- /dev/null
+++ b/rkbd/Makefile
@@ -0,0 +1,9 @@
+LDFLAGS := $(shell pkg-config --libs QtGui) -lrt
+CPPFLAGS := -Wall $(shell pkg-config --cflags QtGui)
+
+obj := rkbd
+
+$(obj): $(obj).cpp
+ g++ -o $@ $(CPPFLAGS) $< $(LDFLAGS)
+clean:
+ rm $(obj)
diff --git a/rkbd/rkbd.cpp b/rkbd/rkbd.cpp
new file mode 100644
index 0000000..62e6fea
--- /dev/null
+++ b/rkbd/rkbd.cpp
@@ -0,0 +1,128 @@
+#include <QtGui>
+#include <iostream>
+#include <time.h>
+
+int keys[] = {
+ Qt::Key_Z, /* prev wep */
+ Qt::Key_X, /* next wep */
+ Qt::Key_Space, /* shoot */
+ Qt::Key_Up,
+ Qt::Key_Left,
+ Qt::Key_Down,
+ Qt::Key_Right,
+ Qt::Key_P,
+ Qt::Key_Backspace,
+};
+
+std::string key_names[] = {
+ "Z",
+ "X",
+ "Space",
+ "Up",
+ "Left",
+ "Down",
+ "Right",
+ "Pause",
+ "Backspace",
+};
+
+#define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a) )
+
+class Application: public QApplication {
+public:
+ Application(int argc, char **argv): QApplication(argc, argv) {
+ setbuf(stdout, NULL); /* disable buffering */
+ }
+ ~Application(){}
+ bool notify(QObject *object, QEvent *event) {
+ bool is_pressed = event->type() == QEvent::KeyPress;
+ if (is_pressed || event->type() == QEvent::KeyRelease) {
+ if (handle_key(static_cast<QKeyEvent *>(event)->key(), is_pressed))
+ return true;
+ }
+ return object->event(event);
+ }
+private:
+ bool handle_key(int code, bool is_pressed) {
+ for (unsigned int i=0; i<ARRAY_SIZE(keys); ++i) {
+ if (keys[i] == code) {
+ report_key(i + 1, is_pressed);
+ return true;
+ }
+ }
+ if (code == Qt::Key_Shift) {
+ release_key();
+ return true;
+ }
+ if (code == Qt::Key_Escape) {
+ std::cerr << "Escape received - quitting" << std::endl;
+ quit();
+ }
+ return false;
+ }
+ int getMS() {
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
+ perror("clock_gettime");
+ return 0;
+ }
+ return (ts.tv_sec * 1000) + ts.tv_nsec / 1000000;
+ }
+ int last_code;
+ bool last_is_pressed;
+ //int last_time;
+ void release_key() {
+ if (last_code && last_is_pressed) {
+ send_key(last_code, false);
+ last_is_pressed = false;
+ }
+ }
+ bool always_send_key(int key) {
+ if (!key) return false;
+ switch (keys[key - 1]) {
+ case Qt::Key_Up:
+ case Qt::Key_Down:
+ case Qt::Key_Left:
+ case Qt::Key_Right:
+ return false;
+ }
+ return true;
+ }
+ void report_key(int key, bool is_pressed) {
+ //int timestamp = getMS();
+ //bool timed_out = timestamp - last_time >= 300;
+ if (last_code == key && !always_send_key(key)) {
+ if ((is_pressed == last_is_pressed) ||
+ (last_is_pressed && !is_pressed)) {
+ //if (!timed_out) {
+ return;
+ //}
+ }
+ } else {
+ if (!always_send_key(last_code))
+ release_key();
+ }
+ last_code = key;
+ last_is_pressed = is_pressed;
+ //last_time = timestamp;
+
+ send_key(key, is_pressed);
+ }
+ void send_key(int key, bool is_pressed) {
+ printf("%u %u\n", key, is_pressed);
+ if (is_pressed)
+ std::cerr << key_names[key - 1] << " " << (is_pressed ? "pressed" : "released") << std::endl;
+ }
+};
+
+
+int main(int argc, char **argv) {
+ Application app(argc, argv);
+
+ QWidget window;
+ window.resize(250, 150);
+ window.setWindowTitle("Remote keyboard");
+ window.show();
+
+ return app.exec();
+}
diff --git a/rkbd/uinput.c b/rkbd/uinput.c
new file mode 100644
index 0000000..5704679
--- /dev/null
+++ b/rkbd/uinput.c
@@ -0,0 +1,90 @@
+#include <linux/input.h>
+#include <linux/uinput.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h> /* memset */
+#include <stdlib.h> /* EXIT_FAILURE */
+
+int keys[] = {
+ KEY_Q, /* prev */
+ KEY_E, /* next */
+ KEY_Z, /* shoot */
+ KEY_W,
+ KEY_A,
+ KEY_S,
+ KEY_D,
+ KEY_P, /* pause */
+ KEY_BACKSPACE, /* really "backspace" */
+};
+
+#define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a) )
+
+int main() {
+ int i;
+ int fd = open("/dev/uinput", O_WRONLY);
+ if (fd < 0) {
+ perror("open");
+ return 1;
+ }
+ if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) {
+ perror("Cannot set EV_KEY bit");
+ goto err_close;
+ }
+ if (ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0) {
+ perror("Cannot set EV_SYN bit");
+ goto err_close;
+ }
+ for (i = 0; i < ARRAY_SIZE(keys); ++i) {
+ if (ioctl(fd, UI_SET_KEYBIT, keys[i]) < 0) {
+ perror("SET_KEYBIT");
+ goto err_close;
+ }
+ }
+
+ struct uinput_user_dev uidev;
+ memset(&uidev, 0, sizeof(uidev));
+ snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Boxhead");
+ if (write(fd, &uidev, sizeof(uidev)) < 0) {
+ perror("write uinput struct");
+ goto err_close;
+ }
+
+ if (ioctl(fd, UI_DEV_CREATE) < 0) {
+ perror("UI_DEV_CREATE");
+ goto err_close;
+ }
+
+ unsigned int code, is_pressed;
+ printf("Waiting for input...\n");
+ struct input_event ev;
+ memset(&ev, 0, sizeof(ev));
+ while (scanf("%u %u", &code, &is_pressed) == 2) {
+ if (code >= 1 && code <= ARRAY_SIZE(keys)) {
+ ev.type = EV_KEY;
+ ev.code = keys[code - 1];
+ ev.value = !!is_pressed;
+ //printf("code=%u value=%u\n", ev.code, ev.value);
+ if (write(fd, &ev, sizeof(ev)) < 0)
+ perror("write key");
+ /* input_sync() */
+ ev.type = EV_SYN;
+ ev.code = SYN_REPORT;
+ ev.value = 0;
+ if (write(fd, &ev, sizeof(ev)) < 0)
+ perror("write sync");
+ } else {
+ fprintf(stderr, "Unexpected key: %u (%#02x)\n", code, code);
+ break;
+ }
+ }
+ if (ioctl(fd, UI_DEV_DESTROY) < 0)
+ perror("Cannot destroy device");
+ if (close(fd) < 0)
+ perror("close");
+ return 0;
+err_close:
+ if (close(fd) < 0)
+ perror("close");
+ return EXIT_FAILURE;
+}
diff --git a/viewimagefs.cpp b/viewimagefs.cpp
new file mode 100644
index 0000000..ed7b8e8
--- /dev/null
+++ b/viewimagefs.cpp
@@ -0,0 +1,31 @@
+/**
+ * Shows picture arg1 fullscreen
+ * Author: Peter Wu
+ * Date: 2012-09-22
+ */
+#include <QApplication>
+#include <QLabel>
+#include <iostream>
+
+int main (int argc, char **argv) {
+ QApplication app(argc, argv);
+ QStringList args(app.arguments());
+ if (args.size() < 2) {
+ std::cerr << "Usage: " << argv[0] << " filename" << std::endl;
+ return 1;
+ }
+
+ QString filename(args[1]);
+ QPixmap pic(filename);
+ if (!pic) {
+ std::cerr << "Invalid image: " << filename.toStdString() << std::endl;
+ return 1;
+ }
+
+ QLabel lbl;
+ lbl.setCursor(QCursor(Qt::BlankCursor));
+ lbl.setPixmap(pic);
+ lbl.setScaledContents(true);
+ lbl.showFullScreen();
+ return app.exec();
+}
diff --git a/xcbviewfs.c b/xcbviewfs.c
new file mode 100644
index 0000000..79386dd
--- /dev/null
+++ b/xcbviewfs.c
@@ -0,0 +1,139 @@
+/*
+ * Attempt to implement picture viewer XCB with kbd and pointer grabbing, fullscreen
+ *
+ * Author: Peter Wu <lekensteyn@gmail.com>
+ * Date: 2012-09-24
+ * gcc -o xcbviewfs xcbviewfs.c -lxcb-image -lxcb-shm -lxcb
+ */
+
+#include <xcb/xcb.h>
+//#include <X11/Xlib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define die(msg) do { \
+ fprintf(stderr, msg); \
+ exit(EXIT_FAILURE); \
+} while (0)
+
+#define bail(lbl, msg) do { \
+ fprintf(stderr, msg); \
+ goto lbl; \
+} while (0)
+
+static const char empty_cursor_data[32];
+
+int main(int argc, char **argv) {
+ xcb_connection_t *c;
+ xcb_screen_t *screen;
+ xcb_window_t win;
+ xcb_cursor_t cursor;
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s filename\n", *argv);
+ return 1;
+ }
+ c = xcb_connect(NULL, NULL);
+ if (!c) {
+ fprintf(stderr, "Cannot open display\n");
+ return 1;
+ }
+ screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
+ win = xcb_generate_id(c);
+ xcb_create_window(c, 0, win, /* conn, depth, WinID */
+ screen->root, /* parent */
+ 0, 0, /* x y */
+ 100, 100, /* width height */
+ 0, /* border_width */
+ XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
+ screen->root_visual,
+ 0, NULL);
+ xcb_map_window(c, win);
+ xcb_flush(c);
+
+ /* options */
+ int grab_pointer = 0;
+ int hide_cursor = 0;
+ int grab_kbd = 0;
+ int fullscreen = 0;
+ grab_kbd = !(fullscreen = grab_pointer = hide_cursor = 0 );
+
+ if (grab_pointer) {
+ xcb_grab_pointer_cookie_t cookie;
+ xcb_grab_pointer_reply_t *reply;
+ cookie = xcb_grab_pointer(c, 1, screen->root, XCB_NONE,
+ XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
+ XCB_NONE, XCB_NONE,
+ XCB_CURRENT_TIME);
+ if (!(reply = xcb_grab_pointer_reply(c, cookie, NULL))) {
+ bail(disconnect, "No pointer grab reply\n");
+ }
+ if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
+ bail(disconnect, "Grab pointer FAILED\n");
+ }
+ free(reply);
+ }
+ if (hide_cursor) {
+ xcb_pixmap_t cp = xcb_create_pixmap_from_bitmap_data(c, win,
+ empty_cursor_data, 16, 16, 1, 0, 0, 0);
+ xcb_pixmap_t mp = xcb_create_pixmap_from_bitmap_data(c, win,
+ empty_cursor_data, 16, 16, 1, 0, 0, 0);
+
+ cursor = xcb_generate_id(c);
+ xcb_create_cursor(c, cursor, cp, mp, 0, 0, 0, 0xFFFF, 0xFFFF, 0xFFFF, 8, 8);
+ xcb_free_pixmap(c, cp);
+ xcb_free_pixmap(c, mp);
+
+ xcb_change_window_attributes(c, win, XCB_CW_CURSOR, &cursor);
+ xcb_free_cursor(c, cursor);
+ xcb_flush(c);
+ }
+ if (grab_kbd) {
+ xcb_grab_keyboard_cookie_t cookie;
+ xcb_grab_keyboard_reply_t *reply;
+ cookie = xcb_grab_keyboard(c, 1, screen->root, XCB_CURRENT_TIME,
+ XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
+ if (!(reply = xcb_grab_keyboard_reply(c, cookie, NULL))) {
+ bail(ungrab_pointer, "No kbd grab reply\n");
+ }
+ if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
+ bail(ungrab_pointer, "Grab kbd FAILED\n");
+ }
+ free(reply);
+ }
+ if (fullscreen) {
+ xcb_intern_atom_cookie_t cookie;
+ xcb_intern_atom_reply_t *reply;
+ xcb_atom_t property, data;
+
+ cookie = xcb_intern_atom(c, 0, 13, "_NET_WM_STATE");
+ if (!(reply = xcb_intern_atom_reply(c, cookie, 0))) {
+ bail(ungrab_kbd, "Failed to get _NET_WM_STATE atom\n");
+ }
+ property = reply->atom;
+ free(reply);
+
+ cookie = xcb_intern_atom(c, 0, 24, "_NET_WM_STATE_FULLSCREEN");
+ if (!(reply = xcb_intern_atom_reply(c, cookie, 0))) {
+ bail(ungrab_kbd, "Failed to get _NET_WM_STATE_FULLSCREEN atom\n");
+ }
+ data = reply->atom;
+ free(reply);
+
+ xcb_change_property(c, XCB_PROP_MODE_REPLACE, win,
+ property, XCB_ATOM_ATOM, 32, 1, &data);
+ xcb_flush(c);
+ }
+ xcb_generic_event_t *e;
+ while ((e = xcb_wait_for_event(c))) {
+ free(e);
+ }
+ungrab_kbd:
+ xcb_ungrab_keyboard(c, XCB_CURRENT_TIME);
+ungrab_pointer:
+ xcb_ungrab_pointer(c, XCB_CURRENT_TIME);
+disconnect:
+ xcb_disconnect(c);
+ return 0;
+}
diff --git a/xlistpixmapformats.c b/xlistpixmapformats.c
new file mode 100644
index 0000000..93a9a20
--- /dev/null
+++ b/xlistpixmapformats.c
@@ -0,0 +1,18 @@
+#include <X11/Xlib.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+ Display *dpy = XOpenDisplay(NULL);
+ int total = 0;
+ XPixmapFormatValues *fs = XListPixmapFormats(dpy, &total);
+ if (total) {
+ int i;
+ for (i=0; i<total; ++i) {
+ XPixmapFormatValues f = fs[i];
+ printf("depth=%2d bpp=%2d scanline_pad=%d\n",
+ f.depth, f.bits_per_pixel, f.scanline_pad);
+ }
+ }
+ XCloseDisplay(dpy);
+ return 0;
+}