5 # define EXPORT __declspec(dllexport)
11 #include <X11/Xutil.h>
12 #include <X11/Xatom.h>
18 #include <cairo-xlib.h>
23 typedef intptr_t slate_int_t
;
26 union float_int_union
{
37 char *msg
= "Hello, World!";
40 /* open connection with the server */
41 d
= XOpenDisplay(NULL
);
43 fprintf(stderr
, "Cannot open display\n");
50 w
= XCreateSimpleWindow(d
, RootWindow(d
, s
), 10, 10, 100, 100, 1,
51 BlackPixel(d
, s
), WhitePixel(d
, s
));
53 /* select kind of events we are interested in */
54 XSelectInput(d
, w
, ExposureMask
| KeyPressMask
);
56 /* map (show) the window */
62 /* draw or redraw the window */
63 if (e
.type
== Expose
) {
64 XFillRectangle(d
, w
, DefaultGC(d
, s
), 20, 20, 10, 10);
65 XDrawString(d
, w
, DefaultGC(d
, s
), 50, 50, msg
, strlen(msg
));
67 /* exit on key press */
68 if (e
.type
== KeyPress
)
72 /* close connection to server */
79 EXPORT Display
* x_open_display(char* name
) {
81 d
= XOpenDisplay(name
);
82 printf("Opening X Display '%s' ==> %p\n", name
, (void*)d
);
87 EXPORT
void x_close_display(Display
* d
) {
93 char* globalSelection
= 0;
94 int globalSelectionSize
= 0;
96 EXPORT
void slate_clipboard_update(Display
* d
, Window w
) {
97 Window selectionOwner
;
100 unsigned long len
, bytes_left
, size
;
102 selectionOwner
= XGetSelectionOwner (d
, XA_PRIMARY
);
104 if (selectionOwner
== None
) return;
105 if (selectionOwner
== w
) return; /*already on our globalSelection*/
107 XConvertSelection(d
, XA_PRIMARY
, XA_STRING
, None
, selectionOwner
, CurrentTime
);
109 XGetWindowProperty(d
, selectionOwner
, XA_STRING
, 0, 0, 0, /*don't grab any*/
110 AnyPropertyType
, &type
, &format
, &len
, &bytes_left
, &data
);
111 printf ("type:%i len:%i format:%i byte_left:%i\n", (int)type
, (int)len
, (int)format
, (int)bytes_left
);
112 if (bytes_left
> 0) {
114 result
= XGetWindowProperty (d
, selectionOwner
, XA_STRING
, 0, size
, 0,
115 AnyPropertyType
, &type
, &format
, &len
, &bytes_left
, &data
);
116 if (result
== Success
) {
117 free(globalSelection
);
118 globalSelection
= malloc(size
);
119 if (globalSelection
== NULL
) {
120 globalSelectionSize
= 0;
124 globalSelectionSize
= size
;
125 memcpy(globalSelection
, data
, size
);
127 else printf ("FAIL\n");
133 EXPORT
int slate_clipboard_size(Display
* d
, Window w
) {
134 return globalSelectionSize
;
137 EXPORT
void slate_clipboard_copy(Display
* d
, Window w
, char* bytes
, int size
) {
138 free(globalSelection
);
139 globalSelection
= malloc(size
);
140 if (globalSelection
== NULL
) {
141 globalSelectionSize
= 0;
144 globalSelectionSize
= size
;
145 memcpy(globalSelection
, bytes
, size
);
146 XSetSelectionOwner(d
, XA_PRIMARY
, w
, CurrentTime
);
150 EXPORT
void slate_clipboard_paste(Display
* d
, Window w
, char* bytes
, int size
) {
151 if (globalSelection
== NULL
) return;
152 memcpy(bytes
, globalSelection
, (size
< globalSelectionSize
) ? size
: globalSelectionSize
);
155 EXPORT Window
x_create_window(Display
* d
, int width
, int height
) {
158 s
= DefaultScreen(d
);
161 w
= XCreateSimpleWindow(d
, RootWindow(d
, s
), 10, 10, width
, height
, 1,
162 BlackPixel(d
, s
), WhitePixel(d
, s
));
164 /* select kind of events we are interested in */
165 XSelectInput(d
, w
, ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
166 | ButtonReleaseMask
| PointerMotionMask
| KeymapStateMask
| StructureNotifyMask
);
168 /* map (show) the window */
177 EXPORT
void x_next_event(Display
* d
) {
178 /*we should probably fix this and make it per window...
179 also... should we through out extra mouse movements for speed?*/
183 XNextEvent(d
, &globalEvent
);
184 if (globalEvent
.type
!= SelectionRequest
) break;
186 printf ("prop:%i tar:%i sel:%i\n",
187 (int)globalEvent
.xselectionrequest
.property
,
188 (int)globalEvent
.xselectionrequest
.target
,
189 (int)globalEvent
.xselectionrequest
.selection
);
190 if (globalEvent
.xselectionrequest
.target
== XA_STRING
)
193 globalEvent
.xselectionrequest
.requestor
,
194 globalEvent
.xselectionrequest
.property
,
198 (unsigned char*) globalSelection
,
199 globalSelectionSize
);
200 reply
.xselection
.property
=globalEvent
.xselectionrequest
.property
;
204 printf ("No String %i\n",
205 (int)globalEvent
.xselectionrequest
.target
);
206 reply
.xselection
.property
= None
;
208 reply
.xselection
.type
= SelectionNotify
;
209 reply
.xselection
.display
= globalEvent
.xselectionrequest
.display
;
210 reply
.xselection
.requestor
= globalEvent
.xselectionrequest
.requestor
;
211 reply
.xselection
.selection
=globalEvent
.xselectionrequest
.selection
;
212 reply
.xselection
.target
= globalEvent
.xselectionrequest
.target
;
213 reply
.xselection
.time
= globalEvent
.xselectionrequest
.time
;
214 XSendEvent(d
, globalEvent
.xselectionrequest
.requestor
,0,0,&reply
);
221 EXPORT
int x_event_type() {
223 return globalEvent
.type
;
227 EXPORT
int x_event_keyboard_key(Display
* display
) {
228 //KeySym XKeycodeToKeysym(Display *display, KeyCode keycode, int index);
230 KeySym ks
, ksHigh
, ksLow
;
232 XComposeStatus composeStatus
;
234 kc
= globalEvent
.xkey
.keycode
;
235 if (0 != XLookupString(&globalEvent
.xkey
, buf
, 1, &ks
, &composeStatus
)) {
239 ks
= XKeycodeToKeysym(display
, kc
, 0);
243 XConvertCase(ks
, &ksLow
, &ksHigh
);
244 if (globalEvent
.xkey
.state
& 3) { /*shift or capslock?*/
252 EXPORT
int x_event_keyboard_modifiers() {
254 return globalEvent
.xkey
.state
;
258 EXPORT
int x_event_pointer_x() {
260 return globalEvent
.xbutton
.x
;
264 EXPORT
int x_event_pointer_y() {
266 return globalEvent
.xbutton
.y
;
270 EXPORT
int x_event_pointer_state() {
272 return globalEvent
.xbutton
.state
;
276 EXPORT
int x_event_pointer_button() {
278 return globalEvent
.xbutton
.button
;
282 EXPORT
int x_event_resize_width() {
284 return globalEvent
.xresizerequest
.width
;
287 EXPORT
int x_event_resize_height() {
289 return globalEvent
.xresizerequest
.height
;
293 EXPORT
int x_event_configure_width() {
295 return globalEvent
.xconfigure
.width
;
298 EXPORT
int x_event_configure_height() {
300 return globalEvent
.xconfigure
.height
;
304 EXPORT cairo_t
* slate_cairo_create_context(cairo_surface_t
*surface
)
306 return cairo_create(surface
);
309 EXPORT
void slate_cairo_destroy_context(cairo_t
*context
)
311 cairo_destroy(context
);
315 EXPORT
void slate_cairo_paint(cairo_t
* c
) {
322 EXPORT
void slate_cairo_resize(cairo_surface_t
* surface
, int width
, int height
) {
324 cairo_xlib_surface_set_size(surface
, width
, height
);
329 EXPORT cairo_surface_t
* slate_cairo_create_surface(Display
* d
, Window w
, int width
, int height
) {
330 return cairo_xlib_surface_create(d
, w
, DefaultVisual(d
, 0), width
, height
);
334 EXPORT
void slate_cairo_destroy_surface(cairo_surface_t
*surface
) {
335 cairo_surface_destroy(surface
);
340 /******************************************************************************
342 ******************************************************************************/
344 EXPORT
void clip(cairo_t
*context
)
349 EXPORT
void clip_preserve(cairo_t
*context
)
351 cairo_clip_preserve(context
);
355 EXPORT
void fill(cairo_t
*context
)
360 EXPORT
void fill_preserve(cairo_t
*context
)
362 cairo_fill_preserve(context
);
365 EXPORT
void restore(cairo_t
*context
)
367 cairo_restore(context
);
370 EXPORT
void save(cairo_t
*context
)
375 EXPORT slate_int_t
get_line_width(cairo_t
*context
)
377 union float_int_union u
;
378 u
.f
= (float)cairo_get_line_width(context
);
382 EXPORT
void set_line_width(cairo_t
*context
, slate_int_t width
)
384 union float_int_union u
;
386 cairo_set_line_width(context
, u
.f
);
389 EXPORT
void set_source_rgb(cairo_t
*context
, slate_int_t r
, slate_int_t g
, slate_int_t b
)
391 union float_int_union ru
,bu
,gu
;
396 cairo_set_source_rgb(context
, ru
.f
, gu
.f
, bu
.f
);
399 EXPORT
void set_source_rgba(cairo_t
*context
, slate_int_t r
, slate_int_t g
, slate_int_t b
, slate_int_t a
)
401 union float_int_union ru
,bu
,gu
,au
;
406 cairo_set_source_rgba(context
, ru
.f
, gu
.f
, bu
.f
, au
.f
);
409 EXPORT
void stroke(cairo_t
*context
)
411 cairo_stroke(context
);
414 EXPORT
void stroke_preserve(cairo_t
*context
)
416 cairo_stroke_preserve(context
);
419 /******************************************************************************
421 ******************************************************************************/
423 EXPORT
void close_path(cairo_t
*context
)
425 cairo_close_path(context
);
428 EXPORT
void line_to(cairo_t
*context
, slate_int_t x
, slate_int_t y
)
430 union float_int_union xu
, yu
;
433 cairo_line_to(context
, xu
.f
, yu
.f
);
436 EXPORT
void move_to(cairo_t
*context
, slate_int_t x
, slate_int_t y
)
438 union float_int_union xu
, yu
;
441 cairo_move_to(context
, xu
.f
, yu
.f
);
444 EXPORT
void new_path(cairo_t
*context
)
446 cairo_new_path(context
);
449 EXPORT
void rectangle(cairo_t
*context
, slate_int_t x
, slate_int_t y
, slate_int_t w
, slate_int_t h
)
451 union float_int_union xu
, yu
, wu
, hu
;
456 cairo_rectangle(context
, xu
.f
, yu
.f
, wu
.f
, hu
.f
);
459 /******************************************************************************
461 ******************************************************************************/
463 EXPORT
void translate(cairo_t
*context
, slate_int_t x
, slate_int_t y
)
465 union float_int_union xu
, yu
;
468 cairo_translate(context
, xu
.f
, yu
.f
);
471 EXPORT
void scale(cairo_t
*context
, slate_int_t x
, slate_int_t y
)
473 union float_int_union xu
, yu
;
476 cairo_scale(context
, xu
.f
, yu
.f
);
479 /******************************************************************************
481 ******************************************************************************/
483 EXPORT
void select_font_face(cairo_t
*context
, const char *family
, slate_int_t italic
, slate_int_t bold
)
485 cairo_select_font_face(
488 italic
? CAIRO_FONT_SLANT_ITALIC
: CAIRO_FONT_SLANT_NORMAL
,
489 bold
? CAIRO_FONT_WEIGHT_BOLD
: CAIRO_FONT_WEIGHT_NORMAL
);
492 EXPORT
void set_font_size(cairo_t
*context
, slate_int_t size
)
494 union float_int_union su
;
496 cairo_set_font_size(context
, su
.f
);
499 EXPORT
void show_text(cairo_t
*context
, const char *text
)
501 if(!*text
) // Cairo has insufficient checking
503 cairo_show_text(context
, text
);
512 ==========================================================
513 cairo_status_t cairo_status (cairo_t *cr);
514 cairo_surface_t* cairo_get_target (cairo_t *cr);
515 void cairo_set_source (cairo_t *cr,
516 cairo_pattern_t *source);
517 void cairo_set_source_surface (cairo_t *cr,
518 cairo_surface_t *surface,
521 cairo_pattern_t* cairo_get_source (cairo_t *cr);
522 void cairo_set_antialias (cairo_t *cr,
523 cairo_antialias_t antialias);
524 cairo_antialias_t cairo_get_antialias (cairo_t *cr);
525 void cairo_set_dash (cairo_t *cr,
529 void cairo_set_fill_rule (cairo_t *cr,
530 cairo_fill_rule_t fill_rule);
531 cairo_fill_rule_t cairo_get_fill_rule (cairo_t *cr);
532 void cairo_set_line_cap (cairo_t *cr,
533 cairo_line_cap_t line_cap);
534 cairo_line_cap_t cairo_get_line_cap (cairo_t *cr);
535 void cairo_set_line_join (cairo_t *cr,
536 cairo_line_join_t line_join);
537 cairo_line_join_t cairo_get_line_join (cairo_t *cr);
538 void cairo_set_miter_limit (cairo_t *cr,
540 double cairo_get_miter_limit (cairo_t *cr);
541 void cairo_set_operator (cairo_t *cr,
542 cairo_operator_t op);
543 cairo_operator_t cairo_get_operator (cairo_t *cr);
544 void cairo_set_tolerance (cairo_t *cr,
546 double cairo_get_tolerance (cairo_t *cr);
547 void cairo_reset_clip (cairo_t *cr);
548 void cairo_fill_extents (cairo_t *cr,
553 cairo_bool_t cairo_in_fill (cairo_t *cr,
556 void cairo_mask (cairo_t *cr,
557 cairo_pattern_t *pattern);
558 void cairo_mask_surface (cairo_t *cr,
559 cairo_surface_t *surface,
562 void cairo_paint (cairo_t *cr);
563 void cairo_paint_with_alpha (cairo_t *cr,
565 void cairo_stroke_extents (cairo_t *cr,
570 cairo_bool_t cairo_in_stroke (cairo_t *cr,
573 void cairo_copy_page (cairo_t *cr);
574 void cairo_show_page (cairo_t *cr);
577 ==========================================================
579 union cairo_path_data_t;
580 enum cairo_path_data_type_t;
581 cairo_path_t* cairo_copy_path (cairo_t *cr);
582 cairo_path_t* cairo_copy_path_flat (cairo_t *cr);
583 void cairo_path_destroy (cairo_path_t *path);
584 void cairo_append_path (cairo_t *cr,
586 void cairo_get_current_point (cairo_t *cr,
589 void cairo_new_path (cairo_t *cr);
590 void cairo_close_path (cairo_t *cr);
591 void cairo_arc (cairo_t *cr,
597 void cairo_arc_negative (cairo_t *cr,
603 void cairo_curve_to (cairo_t *cr,
610 void cairo_line_to (cairo_t *cr,
613 void cairo_move_to (cairo_t *cr,
616 void cairo_rectangle (cairo_t *cr,
621 void cairo_glyph_path (cairo_t *cr,
622 cairo_glyph_t *glyphs,
624 void cairo_text_path (cairo_t *cr,
626 void cairo_rel_curve_to (cairo_t *cr,
633 void cairo_rel_line_to (cairo_t *cr,
636 void cairo_rel_move_to (cairo_t *cr,
641 ==========================================================
642 typedef cairo_pattern_t;
643 void cairo_pattern_add_color_stop_rgb
644 (cairo_pattern_t *pattern,
649 void cairo_pattern_add_color_stop_rgba
650 (cairo_pattern_t *pattern,
656 cairo_pattern_t* cairo_pattern_create_rgb (double red,
659 cairo_pattern_t* cairo_pattern_create_rgba (double red,
663 cairo_pattern_t* cairo_pattern_create_for_surface
664 (cairo_surface_t *surface);
665 cairo_pattern_t* cairo_pattern_create_linear
670 cairo_pattern_t* cairo_pattern_create_radial
677 void cairo_pattern_destroy (cairo_pattern_t *pattern);
678 cairo_pattern_t* cairo_pattern_reference (cairo_pattern_t *pattern);
679 cairo_status_t cairo_pattern_status (cairo_pattern_t *pattern);
681 void cairo_pattern_set_extend (cairo_pattern_t *pattern,
682 cairo_extend_t extend);
683 cairo_extend_t cairo_pattern_get_extend (cairo_pattern_t *pattern);
685 void cairo_pattern_set_filter (cairo_pattern_t *pattern,
686 cairo_filter_t filter);
687 cairo_filter_t cairo_pattern_get_filter (cairo_pattern_t *pattern);
688 void cairo_pattern_set_matrix (cairo_pattern_t *pattern,
689 const cairo_matrix_t *matrix);
690 void cairo_pattern_get_matrix (cairo_pattern_t *pattern,
691 cairo_matrix_t *matrix);
694 ==========================================================
695 void cairo_translate (cairo_t *cr,
698 void cairo_rotate (cairo_t *cr,
700 void cairo_transform (cairo_t *cr,
701 const cairo_matrix_t *matrix);
702 void cairo_set_matrix (cairo_t *cr,
703 const cairo_matrix_t *matrix);
704 void cairo_get_matrix (cairo_t *cr,
705 cairo_matrix_t *matrix);
706 void cairo_identity_matrix (cairo_t *cr);
707 void cairo_user_to_device (cairo_t *cr,
710 void cairo_user_to_device_distance (cairo_t *cr,
713 void cairo_device_to_user (cairo_t *cr,
716 void cairo_device_to_user_distance (cairo_t *cr,
721 ==========================================================
723 enum cairo_font_slant_t;
724 enum cairo_font_weight_t;
725 void cairo_set_font_matrix (cairo_t *cr,
726 const cairo_matrix_t *matrix);
727 void cairo_get_font_matrix (cairo_t *cr,
728 cairo_matrix_t *matrix);
729 void cairo_set_font_options (cairo_t *cr,
730 const cairo_font_options_t *options);
731 void cairo_get_font_options (cairo_t *cr,
732 cairo_font_options_t *options);
733 void cairo_show_glyphs (cairo_t *cr,
734 cairo_glyph_t *glyphs,
736 cairo_font_face_t* cairo_get_font_face (cairo_t *cr);
737 void cairo_font_extents (cairo_t *cr,
738 cairo_font_extents_t *extents);
739 void cairo_set_font_face (cairo_t *cr,
740 cairo_font_face_t *font_face);
741 void cairo_text_extents (cairo_t *cr,
743 cairo_text_extents_t *extents);
744 void cairo_glyph_extents (cairo_t *cr,
745 cairo_glyph_t *glyphs,
747 cairo_text_extents_t *extents);
749 ==========================================================