Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //// TODO :: 3. Clean up
- //// 4. Events
- //// 5. Create a colormap
- //// 6. glx ?
- #ifdef DEBUG
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <sys/un.h>
- #include <sys/select.h>
- #include <fcntl.h>
- #include <sys/syscall.h>
- #include <unistd.h>
- #include <linux/net.h>
- #include <errno.h>
- #include <stdio.h>
- #endif // DEBUG
- #include <stdint.h>
- #include <asm/unistd.h>
- static int fd;
- #ifdef DEBUG
- static struct sockaddr_un addr =
- #else
- struct
- {
- uint16_t sun_family;
- char sun_path[32];
- } addr =
- #endif
- {
- 1, // AF_UNIX,
- "/tmp/.X11-unix/X0"
- };
- struct
- {
- uint8_t byte_order;
- uint8_t pad0;
- uint16_t protocol_major_version;
- uint16_t protocol_minor_version;
- uint16_t authorization_protocol_name_len;
- uint16_t authorization_protocol_data_len;
- uint8_t pad1[2];
- } setup_request =
- {
- 0x6c, /* LSB */
- 0,
- 11, /* protocol major version */
- 0, /* protocol minor version */
- 0, 0,
- { 0, 0 }
- };
- static union
- {
- uint8_t buffer[4096];
- struct
- {
- uint8_t status;
- uint8_t pad0[5];
- uint16_t length;
- } generic_setup_response;
- struct
- {
- uint8_t success;
- uint8_t pad0;
- uint16_t protocol_major_version;
- uint16_t protocol_minor_version;
- uint16_t length;
- uint32_t release_number;
- uint32_t resource_id_base;
- uint32_t resource_id_mask;
- uint32_t motion_buffer_size;
- uint16_t vendor_len;
- uint16_t maximum_request_length;
- uint8_t roots_len;
- uint8_t pixmap_formats_len;
- uint8_t image_byte_order;
- uint8_t bitmap_format_bit_order;
- uint8_t bitmap_format_scanline_unit;
- uint8_t bitmap_format_scanline_pad;
- uint8_t min_keycode;
- uint8_t max_keycode;
- uint8_t pad1[4];
- } setup_response;
- } response;
- struct pixmap_format_t
- {
- uint8_t depth;
- uint8_t bits_per_pixel;
- uint8_t scan_line_pad;
- uint8_t pad0;
- uint32_t pad2;
- };
- struct depth_t
- {
- uint8_t depth;
- uint8_t pad1;
- uint16_t visual_count;
- uint32_t pad;
- };
- struct visual_type_t
- {
- uint32_t visual_id;
- uint8_t _class;
- uint8_t bits_per_rgb;
- uint16_t colormap_entries;
- uint32_t red_mask;
- uint32_t green_mask;
- uint32_t blue_mask;
- uint32_t pad0;
- };
- struct screen_t
- {
- uint32_t root;
- uint32_t default_colormap;
- uint32_t white_pixel;
- uint32_t black_pixel;
- uint32_t current_input_masks;
- uint16_t width_in_pixels;
- uint16_t height_in_pixels;
- uint16_t width_in_millimeters;
- uint16_t height_in_millimeters;
- uint16_t min_installed_maps;
- uint16_t max_installed_maps;
- uint32_t root_visual_id;
- uint8_t backing_stores;
- uint8_t save_unders;
- uint8_t root_depth;
- uint8_t allowed_depths_len;
- };
- #define CREATE_WINDOW_REQ 1
- #define DESTROY_WINDOW_REQ 4
- #define MAP_WINDOW_REQ 8
- struct general_request_t
- {
- uint8_t request_type;
- uint8_t pad0;
- uint16_t length;
- uint32_t id;
- };
- struct window_request_t
- {
- uint8_t request_type;
- uint8_t depth;
- uint16_t length;
- uint32_t window_id;
- uint32_t parent_id;
- int16_t x;
- int16_t y;
- uint16_t width;
- uint16_t height;
- uint16_t border_width;
- uint16_t _class;
- uint32_t visual_id;
- uint32_t value_mask;
- };
- struct error_t
- {
- uint8_t type;
- uint8_t error_code;
- uint16_t sequence_number;
- uint32_t resource_id;
- uint16_t minor_code;
- uint8_t major_code;
- uint8_t pad1;
- uint32_t pad3;
- uint32_t pad4;
- uint32_t pad5;
- uint32_t pad6;
- uint32_t pad7;
- };
- unsigned long arg[] =
- {
- 1, // AF_UNIX
- 1, // SOCK_STREAM,
- 0
- };
- #ifdef DEBUG
- int main()
- #else
- int _start()
- #endif // DEBUG
- {
- int ret;
- __asm__ __volatile__
- (
- "movl $102,%%eax \n\t" // SYS_socketcall
- "movl $1,%%ebx \n\t" // SYS_SOCKET
- "int $0x80"
- : "=a"(fd) : "c"(arg)
- );
- arg[0] = fd;
- arg[1] = (unsigned long int)&addr;
- arg[2] = 20 + sizeof(addr.sun_family);
- __asm__ __volatile__
- (
- "movl $102,%%eax \n\t" // SYS_socketcall
- "movl $3,%%ebx \n\t" // SYS_CONNECT
- "int $0x80"
- : "=a"(ret) : "c"(arg)
- );
- __asm__ __volatile__
- (
- "movl $55, %%eax \n\t" // SYS_fcntl
- "movl $0x02, %%ecx \n\t" // F_SETFD
- "movl $0x01, %%edx \n\t" // FD_CLOEXEC
- "int $0x80"
- : : "b"(fd)
- );
- // Set setup request
- __asm__ __volatile__
- (
- "movl $4, %%eax \n\t" // SYS_write
- "movl $%c[count], %%edx \n\t" // count
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(&setup_request), [count] "i"(sizeof(setup_request))
- );
- // Read first part of the setup response
- __asm__ __volatile__
- (
- "movl $3, %%eax \n\t" // SYS_read
- "movl $%c[count], %%edx \n\t" // count
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(response.buffer), [count] "i"(sizeof(response.generic_setup_response))
- );
- if( response.generic_setup_response.status != 1 )
- {
- goto end;
- }
- // Read the rest of the setup response
- __asm__ __volatile__
- (
- "movl $3, %%eax \n\t" // SYS_read
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(response.buffer+sizeof(response.generic_setup_response)), "d"(response.generic_setup_response.length*4)
- );
- 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));
- int shift = 0;
- int mask = response.setup_response.resource_id_mask;
- while(!(mask & 1))
- {
- ++shift;
- mask >>= 1;
- }
- // Send window request
- struct window_request_t *win_req = (struct window_request_t*)(response.buffer + sizeof(response.generic_setup_response) + response.generic_setup_response.length*4);
- win_req->request_type = CREATE_WINDOW_REQ;
- win_req->depth = scr->root_depth;
- win_req->window_id = response.setup_response.resource_id_base+(1<<shift);
- win_req->parent_id = scr->root;
- win_req->x = 0;
- win_req->y = 0;
- win_req->width = 320;
- win_req->height = 240;
- win_req->border_width = 0;
- win_req->_class = 1; // InputOutput
- win_req->visual_id = 0; // CopyFromParent
- win_req->value_mask = 0; // None atm
- win_req->length = sizeof(struct window_request_t) >> 2;
- __asm__ __volatile__
- (
- "movl $4, %%eax \n\t" // SYS_write
- "movl $%c[count], %%edx \n\t" // count
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(win_req), [count] "i"(sizeof(struct window_request_t))
- );
- // Map window
- struct general_request_t map_window_req;
- map_window_req.request_type = MAP_WINDOW_REQ;
- map_window_req.pad0 = 0;
- map_window_req.length = sizeof(struct general_request_t)>>2;
- map_window_req.id = win_req->window_id;
- __asm__ __volatile__
- (
- "movl $4, %%eax \n\t" // SYS_write
- "movl $%c[count], %%edx \n\t" // count
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(&map_window_req), [count] "i"(sizeof(struct general_request_t ))
- );
- #ifdef DEBUG
- // Sleep
- sleep(5);
- #endif // DEBUG
- // Destroy window
- struct general_request_t destroy_window_req;
- destroy_window_req.request_type = DESTROY_WINDOW_REQ;
- destroy_window_req.pad0 = 0;
- destroy_window_req.length = sizeof(struct general_request_t)>>2;
- map_window_req.id = win_req->window_id;
- __asm__ __volatile__
- (
- "movl $4, %%eax \n\t" // SYS_write
- "movl $%c[count], %%edx \n\t" // count
- "int $0x80"
- : "=a"(ret) : "b"(fd), "c"(&destroy_window_req), [count] "i"(sizeof(struct general_request_t ))
- );
- end:
- //close(fd);
- //syscall( SYS_close, fd );
- __asm__ __volatile__
- (
- "movl $6, %%eax \n\t" // SYS_close
- "int $0x80"
- : : "b"(fd)
- );
- #ifdef DEBUG
- return 0;
- #else
- __asm__ __volatile__
- (
- "movl $1, %eax \n\t" // SYS_exit
- "xorl %ebx, %ebx \n\t"
- "int $0x80"
- );
- #endif // DEBUG
- }
Add Comment
Please, Sign In to add comment