Advertisement
Guest User

Code GTKMM and XCM problem

a guest
Sep 4th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. #include <gtkmm.h>
  2. #include <inttypes.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <thread>
  7. #include <xcb/xcb.h>
  8. #include <xcb/xcb_cursor.h>
  9. #include <xcb/xcb_keysyms.h>
  10.  
  11. static xcb_connection_t *dpy;
  12. static xcb_screen_t *scre;
  13. static xcb_drawable_t win;
  14. static uint32_t values[3];
  15.  
  16. class WindowMain : public Gtk::Window {
  17. public:
  18. void On_button_clicked() { std::cout << "test" << std::endl; }
  19.  
  20. void css() {
  21. GtkCssProvider *provider;
  22. GdkDisplay *display;
  23.  
  24. provider = gtk_css_provider_new();
  25. display = gdk_display_get_default();
  26. gtk_style_context_add_provider_for_display(
  27. display, GTK_STYLE_PROVIDER(provider),
  28. GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  29.  
  30. gtk_css_provider_load_from_file(provider, g_file_new_for_path("style.css"));
  31. g_object_unref(provider);
  32. }
  33.  
  34. WindowMain() {
  35. set_decorated(false);
  36. button1.set_label("button1");
  37. button1.signal_clicked().connect(
  38. sigc::mem_fun(*this, &WindowMain::On_button_clicked));
  39. button1.set_margin(0);
  40.  
  41. set_title("Button example");
  42. set_child(button1);
  43.  
  44. css();
  45. }
  46.  
  47. private:
  48. Gtk::Button button1;
  49. };
  50.  
  51. static void handleButtonPress(xcb_generic_event_t *ev) {
  52. xcb_button_press_event_t *e = (xcb_button_press_event_t *)ev;
  53. win = e->child;
  54. values[0] = XCB_STACK_MODE_ABOVE;
  55. xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  56. values[2] = ((1 == e->detail) ? 1 : ((win != 0) ? 3 : 0));
  57. xcb_grab_pointer(dpy, 0, scre->root,
  58. XCB_EVENT_MASK_BUTTON_RELEASE |
  59. XCB_EVENT_MASK_BUTTON_MOTION |
  60. XCB_EVENT_MASK_POINTER_MOTION_HINT,
  61. XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, scre->root,
  62. XCB_NONE, XCB_CURRENT_TIME);
  63. }
  64.  
  65. static void handleMotionNotify(xcb_generic_event_t *ev) {
  66. xcb_query_pointer_cookie_t coord = xcb_query_pointer(dpy, scre->root);
  67. xcb_query_pointer_reply_t *poin = xcb_query_pointer_reply(dpy, coord, 0);
  68. if ((values[2] == (uint32_t)(1)) && (win != 0)) {
  69. xcb_get_geometry_cookie_t geom_now = xcb_get_geometry(dpy, win);
  70. xcb_get_geometry_reply_t *geom =
  71. xcb_get_geometry_reply(dpy, geom_now, NULL);
  72. uint16_t geom_x = geom->width + (2);
  73. uint16_t geom_y = geom->height + (2);
  74. values[0] = ((poin->root_x + geom_x) > scre->width_in_pixels)
  75. ? (scre->width_in_pixels - geom_x)
  76. : poin->root_x;
  77. values[1] = ((poin->root_y + geom_y) > scre->height_in_pixels)
  78. ? (scre->height_in_pixels - geom_y)
  79. : poin->root_y;
  80. xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
  81. values);
  82. } else if ((values[2] == (uint32_t)(3)) && (win != 0)) {
  83. xcb_get_geometry_cookie_t geom_now = xcb_get_geometry(dpy, win);
  84. xcb_get_geometry_reply_t *geom =
  85. xcb_get_geometry_reply(dpy, geom_now, NULL);
  86. if (!((poin->root_x <= geom->x) || (poin->root_y <= geom->y))) {
  87. values[0] = poin->root_x - geom->x;
  88. values[1] = poin->root_y - geom->y;
  89. if ((values[0] >= (uint32_t)(10)) && (values[1] >= (uint32_t)(10))) {
  90. xcb_configure_window(dpy, win,
  91. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  92. values);
  93. }
  94. }
  95. } else {
  96. }
  97. }
  98.  
  99. void setup(void) {
  100. values[0] =
  101. XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
  102. XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;
  103. xcb_change_window_attributes_checked(dpy, scre->root, XCB_CW_EVENT_MASK,
  104. values);
  105. xcb_ungrab_key(dpy, XCB_GRAB_ANY, scre->root, XCB_MOD_MASK_ANY);
  106. xcb_flush(dpy);
  107. xcb_grab_button(dpy, 0, scre->root,
  108. XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
  109. XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, scre->root,
  110. XCB_NONE, 1, XCB_MOD_MASK_4);
  111. xcb_grab_button(dpy, 0, scre->root,
  112. XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
  113. XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, scre->root,
  114. XCB_NONE, 3, XCB_MOD_MASK_4);
  115. xcb_flush(dpy);
  116. }
  117.  
  118. void xThread(int argc, char *argv[]) {
  119. dpy = xcb_connect(NULL, 0);
  120. scre = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
  121. setup();
  122.  
  123. xcb_window_t window = xcb_generate_id(dpy);
  124.  
  125. uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
  126. uint32_t values[2] = {
  127. scre->white_pixel,
  128. XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS |
  129. XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION |
  130. XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
  131. XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE};
  132.  
  133. xcb_create_window(dpy, 0, /* depth */
  134. window, scre->root, /* parent window */
  135. 150, 150, /* x, y */
  136. 150, 150, /* width, height */
  137. 10, /* border_width */
  138. XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
  139. scre->root_visual, /* visual */
  140. mask, values); /* masks */
  141.  
  142. xcb_map_window(dpy, window);
  143.  
  144. xcb_cursor_context_t *pcursorctx;
  145. xcb_cursor_context_new(dpy, scre, &pcursorctx);
  146. xcb_cursor_t cursor = xcb_cursor_load_cursor(pcursorctx, "left_ptr");
  147. xcb_change_window_attributes(dpy, scre->root, XCB_CW_CURSOR, &cursor);
  148.  
  149. xcb_flush(dpy);
  150.  
  151. xcb_generic_event_t *event;
  152. while ((event = xcb_wait_for_event(dpy))) {
  153. switch (event->response_type) {
  154. case XCB_BUTTON_PRESS: {
  155. handleButtonPress(event);
  156. break;
  157. }
  158. case XCB_MOTION_NOTIFY: {
  159. handleMotionNotify(event);
  160. break;
  161. }
  162. }
  163.  
  164. free(event);
  165. }
  166. }
  167.  
  168. void gtkThread(int argc, char *argv[]) {
  169. auto application = Gtk::Application::create("hmm");
  170. application->make_window_and_run<WindowMain>(argc, argv);
  171. }
  172.  
  173. int main(int argc, char *argv[]) {
  174. std::thread threadTest(gtkThread, argc, argv);
  175. threadTest.join();
  176.  
  177. std::thread threadTest2(xThread, argc, argv);
  178. threadTest2.join();
  179.  
  180. return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement