#include #include #include "window.h" /* According to POSIX.1-2001 */ #include static void repaint (window_t *window) { pixman_color_t color = { 0xffff, 0xabcd, 0x7777, 0xaaaa }; pixman_color_t bg = { 0x0, 0x0, 0x0, 0xffff }; pixman_color_t blue = { 0xaaaa, 0xaaaa, 0xffff, 0xffff }; pixman_point_fixed_t p1 = { 0, 0 }; pixman_point_fixed_t p2 = { 0, 0 }; pixman_gradient_stop_t stops[3] = { { pixman_int_to_fixed (0), { 0x0000, 0x0000, 0x4444, 0xeeee } }, { pixman_double_to_fixed (0.5), { 0xeeee, 0xeeee, 0x8888, 0xeeee } }, { pixman_int_to_fixed (1), { 0x6666, 0x2222, 0x2222, 0xeeee } } }; pixman_image_t *image; int w, h; #if 0 color.red = rand(); color.blue = rand(); #endif w = ws_window_get_width (window); h = ws_window_get_height (window); image = pixman_image_create_solid_fill (&bg); ws_window_copy_from_image (window, image, 0, 0, 0, 0, w, h); pixman_image_unref (image); p2.x = w << 16; p2.y = h << 16; image = pixman_image_create_linear_gradient (&p1, &p2, stops, 3); ws_window_copy_from_image (window, image, 0, 0, 0, 0, w, h); pixman_image_unref (image); image = pixman_image_create_solid_fill (&color); ws_window_copy_from_image (window, image, 0, 0, 0, 0, 20, h); ws_window_copy_from_image (window, image, 0, 0, w - 20, 0, 20, h); ws_window_copy_from_image (window, image, 0, 0, 0, 0, w, 20); ws_window_copy_from_image (window, image, 0, 0, 0, h - 20, w, 20); pixman_image_unref (image); image = pixman_image_create_solid_fill (&bg); ws_window_copy_from_image (window, image, 0, 0, 0, 0, 1, h); ws_window_copy_from_image (window, image, 0, 0, w - 1, 0, 1, h); ws_window_copy_from_image (window, image, 0, 0, 0, 0, w, 1); ws_window_copy_from_image (window, image, 0, 0, 0, h - 1, w, 1); pixman_image_unref (image); ws_window_finish (&window, 1); } int need_repaint = 0; int dragging = 0; int drag_x = -1; int drag_y = -1; int drag_w = -1; int drag_h = -1; int drag_pos_x = -1; int drag_pos_y = -1; static void on_event (window_t *window, ws_event_t *event, void *data) { int w = ws_window_get_width (window); int h = ws_window_get_height (window); int x = ws_window_get_x (window); int y = ws_window_get_y (window); switch (event->common.type) { case WS_CONFIGURE: #if 0 printf ("configure %d %d %d %d\n", x, y, w, h); #endif if (event->configure.configure_type & WS_WINDOW_SIZE_CHANGED) need_repaint = 1; break; case WS_BUTTON_DOWN: if (!dragging) { if (event->button.x > w - 20 && event->button.y > h - 20) { dragging = 1; } else if (event->button.y < 20) { dragging = 2; } if (dragging) { drag_x = event->button.root_x; drag_y = event->button.root_y; drag_w = w; drag_h = h; drag_pos_x = x; drag_pos_y = y; } } break; case WS_BUTTON_UP: if (dragging == 1) { int dx = event->button.root_x - drag_x; int dy = event->button.root_y - drag_y; ws_window_resize (window, drag_w + dx, drag_h + dy); need_repaint = 1; } else if (dragging == 2) { int dx = event->button.root_x - drag_x; int dy = event->button.root_y - drag_y; #if 0 printf ("move to %d %d\n", drag_pos_x + dx, drag_pos_y + dy); #endif ws_window_move (window, drag_pos_x + dx, drag_pos_y + dy); } if (dragging) dragging = 0; break; case WS_MOTION: if (dragging == 1) { int dx = event->motion.root_x - drag_x; int dy = event->motion.root_y - drag_y; ws_window_resize (window, drag_w + dx, drag_h + dy); need_repaint = 1; } else if (dragging == 2) { int dx = event->motion.root_x - drag_x; int dy = event->motion.root_y - drag_y; #if 0 printf ("move to %d %d\n", drag_pos_x + dx, drag_pos_y + dy); #endif ws_window_move (window, drag_pos_x + dx, drag_pos_y + dy); } } } int main () { ws_t *ws = ws_open(); window_t *window; pixman_image_t *image; pixman_color_t color = { 0xffff, 0xabcd, 0x7777, 0x7777 }; fd_set set; int fd; if (!ws) { printf ("could not open display\n"); return 0; } fd = ws_get_fd (ws); window = ws_create_window (ws, 300, 200, 200, 200); ws_window_set_callback (window, on_event, ws); ws_window_show (window); repaint (window); while (1) { if (!ws_pending (ws)) { struct timeval tv; FD_ZERO (&set); FD_SET (fd, &set); tv.tv_usec = 0; tv.tv_sec = 5; select (fd + 1, &set, NULL, NULL, NULL); } ws_process (ws); if (need_repaint) { repaint (window); need_repaint = 0; } } return 0; }