source: libcaca/trunk/python/examples/driver.py @ 4712

Last change on this file since 4712 was 4712, checked in by alxf, 3 years ago
  • Add examples: colors, driver and event.
  • Property svn:executable set to *
File size: 1.8 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# driver        libcaca test drivers program
5# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
6#
7# This file is a Python port of "examples/driver.c"
8# which is:
9# Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
10#               All Rights Reserverd
11#
12# This library is free software. It comes without any warranty, to
13# the extent permitted by applicable law. You can redistribute it
14# and/or modify it under the terms of the Do What The Fuck You Want
15# To Public License, Version 2, as published by Sam Hocevar. See
16# http://sam.zoy.org/wtfpl/COPYING for more details.
17#
18
19import sys
20
21import caca
22from caca.canvas import Canvas, CanvasError
23from caca.display import Display, DisplayError, Event
24
25def main():
26    """ Main function. """
27
28    lst = caca.get_display_driver_list()
29    cur = 0
30
31    try:
32        cv = Canvas(0, 0)
33        dp = Display(cv)
34    except (CanvasError, DisplayError), err:
35        sys.stderr.write("%s\n" % err)
36        sys.exit(127)
37
38    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
39
40    while True:
41        cv.put_str(1, 0, "Available drivers:")
42        cur_driver = dp.get_driver()
43        n = 0
44        for driver, desc in lst:
45            if driver == cur_driver:
46                cv.put_str(2, n + 2, "%s %s (%s)" % ('*', driver, desc))
47            else:
48                cv.put_str(2, n + 2, "%s %s (%s)" % (' ', driver, desc))
49            n += 1
50
51        cv.put_str(2, n + 3, "Switching driver in 5 seconds")
52        dp.refresh()
53
54        if dp.get_event(caca.EVENT_KEY_PRESS, Event(), 5000000):
55            break
56
57        cur += 1
58        if cur < len(lst) and lst[cur][0] == "raw":
59            cur += 1
60        if cur >= len(lst):
61            cur = 0
62
63        dp.set_driver(lst[cur][0])
64
65if __name__ == "__main__":
66    main()
67
Note: See TracBrowser for help on using the repository browser.