Guest User

Untitled

a guest
Apr 25th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. //// TODO :: 3. Clean up
  2. //// 4. Events
  3. //// 5. Create a colormap
  4. //// 6. glx ?
  5.  
  6. #ifdef DEBUG
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/un.h>
  10. #include <sys/select.h>
  11. #include <fcntl.h>
  12.  
  13. #include <sys/syscall.h>
  14. #include <unistd.h>
  15. #include <linux/net.h>
  16.  
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #endif // DEBUG
  20.  
  21. #include <stdint.h>
  22. #include <asm/unistd.h>
  23.  
  24. static int fd;
  25. #ifdef DEBUG
  26. static struct sockaddr_un addr =
  27. #else
  28. struct
  29. {
  30. uint16_t sun_family;
  31. char sun_path[32];
  32. } addr =
  33. #endif
  34. {
  35. 1, // AF_UNIX,
  36. "/tmp/.X11-unix/X0"
  37. };
  38.  
  39. struct
  40. {
  41. uint8_t byte_order;
  42. uint8_t pad0;
  43. uint16_t protocol_major_version;
  44. uint16_t protocol_minor_version;
  45. uint16_t authorization_protocol_name_len;
  46. uint16_t authorization_protocol_data_len;
  47. uint8_t pad1[2];
  48. } setup_request =
  49. {
  50. 0x6c, /* LSB */
  51. 0,
  52. 11, /* protocol major version */
  53. 0, /* protocol minor version */
  54. 0, 0,
  55. { 0, 0 }
  56. };
  57.  
  58. static union
  59. {
  60. uint8_t buffer[4096];
  61.  
  62. struct
  63. {
  64. uint8_t status;
  65. uint8_t pad0[5];
  66. uint16_t length;
  67. } generic_setup_response;
  68.  
  69. struct
  70. {
  71. uint8_t success;
  72. uint8_t pad0;
  73. uint16_t protocol_major_version;
  74. uint16_t protocol_minor_version;
  75. uint16_t length;
  76. uint32_t release_number;
  77. uint32_t resource_id_base;
  78. uint32_t resource_id_mask;
  79. uint32_t motion_buffer_size;
  80. uint16_t vendor_len;
  81. uint16_t maximum_request_length;
  82. uint8_t roots_len;
  83. uint8_t pixmap_formats_len;
  84. uint8_t image_byte_order;
  85. uint8_t bitmap_format_bit_order;
  86. uint8_t bitmap_format_scanline_unit;
  87. uint8_t bitmap_format_scanline_pad;
  88. uint8_t min_keycode;
  89. uint8_t max_keycode;
  90. uint8_t pad1[4];
  91. } setup_response;
  92. } response;
  93.  
  94. struct pixmap_format_t
  95. {
  96. uint8_t depth;
  97. uint8_t bits_per_pixel;
  98. uint8_t scan_line_pad;
  99. uint8_t pad0;
  100. uint32_t pad2;
  101. };
  102.  
  103. struct depth_t
  104. {
  105. uint8_t depth;
  106. uint8_t pad1;
  107. uint16_t visual_count;
  108. uint32_t pad;
  109. };
  110.  
  111. struct visual_type_t
  112. {
  113. uint32_t visual_id;
  114. uint8_t _class;
  115. uint8_t bits_per_rgb;
  116. uint16_t colormap_entries;
  117. uint32_t red_mask;
  118. uint32_t green_mask;
  119. uint32_t blue_mask;
  120. uint32_t pad0;
  121. };
  122.  
  123. struct screen_t
  124. {
  125. uint32_t root;
  126. uint32_t default_colormap;
  127. uint32_t white_pixel;
  128. uint32_t black_pixel;
  129. uint32_t current_input_masks;
  130. uint16_t width_in_pixels;
  131. uint16_t height_in_pixels;
  132. uint16_t width_in_millimeters;
  133. uint16_t height_in_millimeters;
  134. uint16_t min_installed_maps;
  135. uint16_t max_installed_maps;
  136. uint32_t root_visual_id;
  137. uint8_t backing_stores;
  138. uint8_t save_unders;
  139. uint8_t root_depth;
  140. uint8_t allowed_depths_len;
  141. };
  142.  
  143. #define CREATE_WINDOW_REQ 1
  144. #define DESTROY_WINDOW_REQ 4
  145. #define MAP_WINDOW_REQ 8
  146.  
  147. struct general_request_t
  148. {
  149. uint8_t request_type;
  150. uint8_t pad0;
  151. uint16_t length;
  152. uint32_t id;
  153. };
  154.  
  155. struct window_request_t
  156. {
  157. uint8_t request_type;
  158. uint8_t depth;
  159. uint16_t length;
  160. uint32_t window_id;
  161. uint32_t parent_id;
  162. int16_t x;
  163. int16_t y;
  164. uint16_t width;
  165. uint16_t height;
  166. uint16_t border_width;
  167. uint16_t _class;
  168. uint32_t visual_id;
  169. uint32_t value_mask;
  170. };
  171.  
  172. struct error_t
  173. {
  174. uint8_t type;
  175. uint8_t error_code;
  176. uint16_t sequence_number;
  177. uint32_t resource_id;
  178. uint16_t minor_code;
  179. uint8_t major_code;
  180. uint8_t pad1;
  181. uint32_t pad3;
  182. uint32_t pad4;
  183. uint32_t pad5;
  184. uint32_t pad6;
  185. uint32_t pad7;
  186. };
  187.  
  188. unsigned long arg[] =
  189. {
  190. 1, // AF_UNIX
  191. 1, // SOCK_STREAM,
  192. 0
  193. };
  194. #ifdef DEBUG
  195. int main()
  196. #else
  197. int _start()
  198. #endif // DEBUG
  199. {
  200. int ret;
  201.  
  202. __asm__ __volatile__
  203. (
  204. "movl $102,%%eax \n\t" // SYS_socketcall
  205. "movl $1,%%ebx \n\t" // SYS_SOCKET
  206. "int $0x80"
  207. : "=a"(fd) : "c"(arg)
  208. );
  209.  
  210. arg[0] = fd;
  211. arg[1] = (unsigned long int)&addr;
  212. arg[2] = 20 + sizeof(addr.sun_family);
  213. __asm__ __volatile__
  214. (
  215. "movl $102,%%eax \n\t" // SYS_socketcall
  216. "movl $3,%%ebx \n\t" // SYS_CONNECT
  217. "int $0x80"
  218. : "=a"(ret) : "c"(arg)
  219. );
  220.  
  221. __asm__ __volatile__
  222. (
  223. "movl $55, %%eax \n\t" // SYS_fcntl
  224. "movl $0x02, %%ecx \n\t" // F_SETFD
  225. "movl $0x01, %%edx \n\t" // FD_CLOEXEC
  226. "int $0x80"
  227. : : "b"(fd)
  228. );
  229.  
  230. // Set setup request
  231. __asm__ __volatile__
  232. (
  233. "movl $4, %%eax \n\t" // SYS_write
  234. "movl $%c[count], %%edx \n\t" // count
  235. "int $0x80"
  236. : "=a"(ret) : "b"(fd), "c"(&setup_request), [count] "i"(sizeof(setup_request))
  237. );
  238.  
  239. // Read first part of the setup response
  240. __asm__ __volatile__
  241. (
  242. "movl $3, %%eax \n\t" // SYS_read
  243. "movl $%c[count], %%edx \n\t" // count
  244. "int $0x80"
  245. : "=a"(ret) : "b"(fd), "c"(response.buffer), [count] "i"(sizeof(response.generic_setup_response))
  246. );
  247.  
  248. if( response.generic_setup_response.status != 1 )
  249. {
  250. goto end;
  251. }
  252.  
  253. // Read the rest of the setup response
  254. __asm__ __volatile__
  255. (
  256. "movl $3, %%eax \n\t" // SYS_read
  257. "int $0x80"
  258. : "=a"(ret) : "b"(fd), "c"(response.buffer+sizeof(response.generic_setup_response)), "d"(response.generic_setup_response.length*4)
  259. );
  260.  
  261. struct screen_t *scr = (struct screen_t*)(response.buffer + sizeof( response.setup_response ) + response.setup_response.vendor_len + response.setup_response.pixmap_formats_len * sizeof(struct pixmap_format_t));
  262.  
  263. int shift = 0;
  264. int mask = response.setup_response.resource_id_mask;
  265. while(!(mask & 1))
  266. {
  267. ++shift;
  268. mask >>= 1;
  269. }
  270.  
  271. // Send window request
  272. struct window_request_t *win_req = (struct window_request_t*)(response.buffer + sizeof(response.generic_setup_response) + response.generic_setup_response.length*4);
  273. win_req->request_type = CREATE_WINDOW_REQ;
  274. win_req->depth = scr->root_depth;
  275. win_req->window_id = response.setup_response.resource_id_base+(1<<shift);
  276. win_req->parent_id = scr->root;
  277. win_req->x = 0;
  278. win_req->y = 0;
  279. win_req->width = 320;
  280. win_req->height = 240;
  281. win_req->border_width = 0;
  282. win_req->_class = 1; // InputOutput
  283. win_req->visual_id = 0; // CopyFromParent
  284. win_req->value_mask = 0; // None atm
  285. win_req->length = sizeof(struct window_request_t) >> 2;
  286.  
  287. __asm__ __volatile__
  288. (
  289. "movl $4, %%eax \n\t" // SYS_write
  290. "movl $%c[count], %%edx \n\t" // count
  291. "int $0x80"
  292. : "=a"(ret) : "b"(fd), "c"(win_req), [count] "i"(sizeof(struct window_request_t))
  293. );
  294.  
  295. // Map window
  296. struct general_request_t map_window_req;
  297. map_window_req.request_type = MAP_WINDOW_REQ;
  298. map_window_req.pad0 = 0;
  299. map_window_req.length = sizeof(struct general_request_t)>>2;
  300. map_window_req.id = win_req->window_id;
  301.  
  302. __asm__ __volatile__
  303. (
  304. "movl $4, %%eax \n\t" // SYS_write
  305. "movl $%c[count], %%edx \n\t" // count
  306. "int $0x80"
  307. : "=a"(ret) : "b"(fd), "c"(&map_window_req), [count] "i"(sizeof(struct general_request_t ))
  308. );
  309.  
  310. #ifdef DEBUG
  311. // Sleep
  312. sleep(5);
  313. #endif // DEBUG
  314.  
  315. // Destroy window
  316. struct general_request_t destroy_window_req;
  317. destroy_window_req.request_type = DESTROY_WINDOW_REQ;
  318. destroy_window_req.pad0 = 0;
  319. destroy_window_req.length = sizeof(struct general_request_t)>>2;
  320. map_window_req.id = win_req->window_id;
  321. __asm__ __volatile__
  322. (
  323. "movl $4, %%eax \n\t" // SYS_write
  324. "movl $%c[count], %%edx \n\t" // count
  325. "int $0x80"
  326. : "=a"(ret) : "b"(fd), "c"(&destroy_window_req), [count] "i"(sizeof(struct general_request_t ))
  327. );
  328.  
  329. end:
  330. //close(fd);
  331. //syscall( SYS_close, fd );
  332. __asm__ __volatile__
  333. (
  334. "movl $6, %%eax \n\t" // SYS_close
  335. "int $0x80"
  336. : : "b"(fd)
  337. );
  338.  
  339. #ifdef DEBUG
  340. return 0;
  341. #else
  342. __asm__ __volatile__
  343. (
  344. "movl $1, %eax \n\t" // SYS_exit
  345. "xorl %ebx, %ebx \n\t"
  346. "int $0x80"
  347. );
  348. #endif // DEBUG
  349. }
Add Comment
Please, Sign In to add comment