Re: Rattling skeletons of X



\~o/~///annabee wrote:
//Okay, this isn't the "Xkeleton" Wannabee requested. Merely the shadow
of
a ghost of a skeleton.`\\

Not bad. I can read it. Saved.

Okay. That was kinda the point... to provide something you can comprehend and work with, if and when you decide to get into Linux/X programming...


What were the problems with my bitmap?

Ahh, well, haven't really figured out how to do it. I guess maybe it's "XPutImage" I want... Takes parameters including a pointer to a "glob of bytes" which includes a pointer to "data"... To be honest, I haven't spent much time with it.


This is as far as I've gotten. It's the classic "hello world" - not much different from the one Chuck posted - you can substitute the "XDrawRectangle(s)", lines, points, polypoints, arcs... "XDrawImageString", which I use, draws foreground and background pixels of the text, "XDrawString" draws just foreground pixels... like that...

But to draw to a "buffer" and "blit" it to the screen... that's going to take some more study and/or experimentation... Can't be too tough, but I haven't "got" it yet. Betov's right - progress in "Xasm" is very slow (unless there are projects I don't know about, which is quite possible). My excuse is I'm a lazy mofo - I don't know what other people's excuse is :)

A note about this command-line... We're using "C libraries" without running any C startup code. This strikes me as a "fragile" thing to do. I haven't had any trouble with it, but it seems "dangerous". I thought I'd discovered a problem - this thing was segfaulting on me. Soon traced it down to "wrong parameters to XFreeGC" (right where it says "exit gracefully" :) But I thought that it really shouldn't segfault - it should return "BADMATCH" (I think)... in "errno"... But wait a minute - where's "errno"??? I tried adding a "global errno" to my program... no complaints about it, but it still segfaulted. Tried replacing "global _start" with "global main" and linking via gcc. Still segfaulted. So there may be things that we want that C startup code for, but this isn't one of them, apparently. Back to the "ld" command line.

More as it comes...

Best,
Frank

; rudimentary "hello world" for Xwindows

; nasm -f elf -O999 x1.asm
; add the "-g" switch for debugging info - good luck!
;
; sorry 'bout this one...
; ld -s -o x1 x1.o -I/lib/ld-linux.so.2 -L/usr/X11R6/lib -lX11
;
; if you want it small:
; strip -R .comment x1
;
; after all that, we still have to tell ld where the fun begins:

global _start

; inform Nasm that ld knows where to find this stuff (we hope!)

extern XOpenDisplay
extern XDefaultRootWindow
extern XCreateSimpleWindow
extern XNextEvent
extern XDestroyWindow
extern XCloseDisplay
extern XMapRaised
extern XSelectInput
extern XStoreName
extern XCreateGC
extern XFreeGC
extern XDrawImageString
extern XSetForeground
extern XSetBackground


; we'll be needing lots more of these...

KeyPressMask equ 1     ; mask for XSelectInput
KeyPress     equ 2     ; the "type" of event (offset 0 in "glob of bytes")
ExposeMask   equ 1 << 15
ExposeEvent  equ 12


event_size equ 20h ; ??? max size of several different structs ; oops, I mean "globs of bytes" :), in dwords ; I *think* the biggest one is 15 dwords(???)

;-------------------------------------------
; a rudimentary "RosAsm compatibility" macro.
; we need a space after this for it to work!
; Note that this depends on undocumented Nasm syntax!!!

%idefine D$ dword &
%idefine W$ word &     ; unused, currently
%idefine B$ byte &     ;
;-------------------------------------------

;------------------------------------------
section .data
    StringOpenFailed db "Can't Open X display!", 10
    StringOpenFailed_len equ $ - StringOpenFailed

    caption db 'No Caption', 0

    string db ' Merry X, World! '
    string_len equ $ - string
;-----------------------------------------

;-----------------------------------------
section .bss

    Display resd 1
    Window resd 1
    GC resd 1

; this is a place for XNextEvent to put a "glob of bytes".
    event resd event_size
;-------------------------------------

;-------------------------------------
section .text

_start:    ; let the ritual begin.

;    nop   ; parking place for gdb

; open a connection to the server.
; pushing 0 uses the "DISPLAY" environment variable
; or defaults to local machine.

    push 0
    call XOpenDisplay
    add esp, 4

    or eax, eax
    je OpenFailed

    mov D$ Display, eax

    push D$ Display
    call XDefaultRootWindow
    add esp, 4
    ; error?

    push 0              ; background colour
    push 0              ; border colour
    push 0              ; border width
    push 300            ; height
    push 400            ; width
    push 50             ; top co-ord
    push 50             ; left co-ord
    push eax            ; XDefaultRootWindow, from above
    push D$ Display     ; display handle
    call XCreateSimpleWindow
    add esp, 36         ; C clean-up parameters stuff

    or eax, eax
    je CreateFailed

    mov D$ Window, eax

; this is one Windows doesn't do. if we don't specify
; what events (messages) we want to receive, we don't get any.

    push KeyPressMask | ExposeMask
                           ; "or" this with other events to recv!
                           ; don't forget to handle 'em!
    push D$ Window
    push D$ Display
    call XSelectInput
    add esp, 12
    ; error?

; spec said "No Caption", so that's what we do :)

    push caption
    push D$ Window
    push D$ Display
    call XStoreName
    add esp, 12
    ; error?


; make our window visible.

    push D$ Window
    push D$ Display
    call XMapRaised
    add esp, 8
    ; error?


push 0 ; values? push 0 ; valuemask? push D$ Window push D$ Display call XCreateGC add esp, 16 ;error? mov D$ GC, eax

; Mmmm, looks like 16-bit color, 5-6-5, on my machine.
; Bet we can't count on it!

;    push 65535               ; white
    push 1111100000000000b   ; red
    push D$ GC
    push D$ Display
    call XSetForeground
    add esp, 12
    ; error?

;    push 0                   ; black
    push 0000011111100000b   ; green
    push D$ GC
    push D$ Display
    call XSetBackground
    add esp, 12
    ; error?

;    call drawscreen
; (no use)

Eventloop:
    push event
    push D$ Display
    call XNextEvent
    add esp, 8
    ; error?

    cmp D$ event, ExposeEvent
    jnz not_expose
    call drawscreen
    jmp Eventloop
not_expose:

    cmp D$ event, KeyPress
    jne Eventloop

; exit gracefully if key pressed

    push D$ GC
    push D$ Display
    call XFreeGC
    add esp, 8

    push D$ Window
    push D$ Display
    call XDestroyWindow
    add esp, 8

CreateFailed:
    push D$ Display
    call XCloseDisplay
    add esp, 4
    jmp Terminate

OpenFailed:
    mov eax, 4                      ; __NR_write
    mov ebx, 2                      ; stderr
    mov ecx, StringOpenFailed       ; buffer
    mov edx, StringOpenFailed_len   ; count
    int 80h

Terminate:
    mov eax, 1     ; function (sys_exit)
    xor ebx, ebx   ; exit code
    int 80h        ; make Linux system call
;-------------------------------------------

;-------------------------------------------
drawscreen:
    push string_len
    push string
    push 140            ; y pos
    push 155            ; x pos
    push D$ GC
    push D$ Window
    push D$ Display
    call XDrawImageString
    add esp, 28
    ; error?
    ret
;---------------------------------------------
.



Relevant Pages

  • Re: I pee in the open
    ... extern pclose ... push mod ... add esp, 8 ... mov, eax ...
    (alt.lang.asm)
  • I pee in the open
    ... extern pclose ... push mod ... add esp, 8 ... mov, eax ...
    (alt.lang.asm)
  • Rattling skeletons of X
    ... extern XDefaultRootWindow ... StringOpenFailed db "Can't Open X display!", ... add esp, byte 4 ... push D$ Display ...
    (alt.lang.asm)
  • Re: Linux / Windows GUI application with assembly
    ... extern XDefaultRootWindow ... StringOpenFailed db "Can't Open X display!", ... add esp, 4 ... push D$ Display ...
    (alt.lang.asm)
  • Re: Crit: Ivni, plus noodling
    ... Tilde and her husband Thorn. ... Engineering Library" display and it looked a little boring; ... Things have been quiet this week -- mostly visitors rather than students ...
    (rec.arts.sf.composition)